-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(servertype): implement new Deprecation api field
We recently added a new Field to the ServerType API response that indicates if and when the server type was deprecated. The same structure will also be used for other resources in the future, replacing the current "deprecated" fields.
- Loading branch information
Showing
9 changed files
with
211 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package hcloud | ||
|
||
import "time" | ||
|
||
// Deprecatable is a shared interface implemented by all Resources that have a defined deprecation workflow. | ||
type Deprecatable interface { | ||
// IsDeprecated returns true if the resource is marked as deprecated. | ||
IsDeprecated() bool | ||
|
||
// UnavailableAfter returns the time that the deprecated resource will be removed from the API. | ||
// This only returns a valid value if [Deprecatable.IsDeprecated] returned true. | ||
UnavailableAfter() time.Time | ||
|
||
// DeprecationAnnounced returns the time that the deprecation of this resource was announced. | ||
// This only returns a valid value if [Deprecatable.IsDeprecated] returned true. | ||
DeprecationAnnounced() time.Time | ||
} | ||
|
||
// DeprecationInfo contains the information published when a resource is actually deprecated. | ||
type DeprecationInfo struct { | ||
Announced time.Time | ||
UnavailableAfter time.Time | ||
} | ||
|
||
// DeprecatableResource implements the [Deprecatable] interface and can be embedded in structs for Resources that can be | ||
// deprecated. | ||
type DeprecatableResource struct { | ||
Deprecation *DeprecationInfo | ||
} | ||
|
||
// IsDeprecated returns true if the resource is marked as deprecated. | ||
func (d DeprecatableResource) IsDeprecated() bool { | ||
return d.Deprecation != nil | ||
} | ||
|
||
// UnavailableAfter returns the time that the deprecated resource will be removed from the API. | ||
// This only returns a valid value if [Deprecatable.IsDeprecated] returned true. | ||
func (d DeprecatableResource) UnavailableAfter() time.Time { | ||
if !d.IsDeprecated() { | ||
// Return "null" time if resource is not deprecated | ||
return time.Unix(0, 0) | ||
} | ||
|
||
return d.Deprecation.UnavailableAfter | ||
} | ||
|
||
// DeprecationAnnounced returns the time that the deprecation of this resource was announced. | ||
// This only returns a valid value if [Deprecatable.IsDeprecated] returned true. | ||
func (d DeprecatableResource) DeprecationAnnounced() time.Time { | ||
if !d.IsDeprecated() { | ||
// Return "null" time if resource is not deprecated | ||
return time.Unix(0, 0) | ||
} | ||
|
||
return d.Deprecation.Announced | ||
} | ||
|
||
// Make sure that all expected Resources actually implement the interface. | ||
var _ Deprecatable = ServerType{} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package hcloud | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
type TestDeprecatableResource struct { | ||
DeprecatableResource | ||
} | ||
|
||
// Interface is implemented | ||
var _ Deprecatable = TestDeprecatableResource{} | ||
|
||
func TestDeprecatableResource_IsDeprecated(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
resource TestDeprecatableResource | ||
want bool | ||
}{ | ||
{name: "nil returns false", resource: TestDeprecatableResource{DeprecatableResource: DeprecatableResource{Deprecation: nil}}, want: false}, | ||
{name: "struct returns true", resource: TestDeprecatableResource{DeprecatableResource: DeprecatableResource{Deprecation: &DeprecationInfo{}}}, want: true}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
assert.Equalf(t, tt.want, tt.resource.IsDeprecated(), "IsDeprecated()") | ||
}) | ||
} | ||
} | ||
|
||
func TestDeprecatableResource_DeprecationAnnounced(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
resource TestDeprecatableResource | ||
want time.Time | ||
}{ | ||
{ | ||
name: "nil returns default time", | ||
resource: TestDeprecatableResource{DeprecatableResource: DeprecatableResource{Deprecation: nil}}, | ||
want: time.Unix(0, 0)}, | ||
{ | ||
name: "actual value is returned", | ||
resource: TestDeprecatableResource{DeprecatableResource: DeprecatableResource{Deprecation: &DeprecationInfo{Announced: mustParseTime(t, apiTimestampFormat, "2023-06-01T00:00:00+00:00")}}}, | ||
want: mustParseTime(t, apiTimestampFormat, "2023-06-01T00:00:00+00:00")}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
assert.Equalf(t, tt.want, tt.resource.DeprecationAnnounced(), "DeprecationAnnounced()") | ||
}) | ||
} | ||
} | ||
|
||
func TestDeprecatableResource_UnavailableAfter(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
resource TestDeprecatableResource | ||
want time.Time | ||
}{ | ||
{ | ||
name: "nil returns default time", | ||
resource: TestDeprecatableResource{DeprecatableResource: DeprecatableResource{Deprecation: nil}}, | ||
want: time.Unix(0, 0)}, | ||
{ | ||
name: "actual value is returned", | ||
resource: TestDeprecatableResource{DeprecatableResource: DeprecatableResource{Deprecation: &DeprecationInfo{UnavailableAfter: mustParseTime(t, apiTimestampFormat, "2023-06-01T00:00:00+00:00")}}}, | ||
want: mustParseTime(t, apiTimestampFormat, "2023-06-01T00:00:00+00:00")}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
assert.Equalf(t, tt.want, tt.resource.UnavailableAfter(), "UnavailableAfter()") | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package schema | ||
|
||
import "time" | ||
|
||
type DeprecationInfo struct { | ||
Announced time.Time `json:"announced"` | ||
UnavailableAfter time.Time `json:"unavailable_after"` | ||
} | ||
|
||
type DeprecatableResource struct { | ||
Deprecation *DeprecationInfo `json:"deprecation"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters