Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions hcloud/exp/deprecationutil/image.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package deprecationutil

import (
"fmt"
"time"

"github.com/hetznercloud/hcloud-go/v2/hcloud"
)

// ImageMessage return a deprecation message when the given Image is
// deprecated and whether the given Image is unavailable.
//
// Experimental: `exp` package is experimental, breaking changes may occur within minor releases.
func ImageMessage(image *hcloud.Image) (string, bool) {
if image.IsDeprecated() {
// Images are unavailable 3 months after the announcement
unavailableAfter := image.Deprecated.AddDate(0, 3, 0)

if time.Now().After(unavailableAfter) {
return fmt.Sprintf(
"Image %q is unavailable and can no longer be ordered.",
image.Name,
), true
}
return fmt.Sprintf(
"Image %q is deprecated and will no longer be available for order as of %s.",
image.Name,
unavailableAfter.Format(time.DateOnly),
), false
}

return "", false
}
41 changes: 41 additions & 0 deletions hcloud/exp/deprecationutil/image_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package deprecationutil

import (
"fmt"
"testing"
"time"

"github.com/stretchr/testify/assert"

"github.com/hetznercloud/hcloud-go/v2/hcloud"
)

func TestImageMessage(t *testing.T) {
t.Run("not deprecated", func(t *testing.T) {
o := &hcloud.Image{Name: "debian-13"}

message, isUnavailable := ImageMessage(o)
assert.Equal(t, "", message)
assert.False(t, isUnavailable)
})

t.Run("deprecated", func(t *testing.T) {
deprecated := time.Now().UTC().AddDate(0, 0, -1)

o := &hcloud.Image{Name: "debian-13", Deprecated: deprecated}

message, isUnavailable := ImageMessage(o)
assert.Equal(t, fmt.Sprintf(`Image "debian-13" is deprecated and will no longer be available for order as of %s.`, deprecated.AddDate(0, 3, 0).Format(time.DateOnly)), message)
assert.False(t, isUnavailable)
})

t.Run("unavailable", func(t *testing.T) {
deprecated := time.Now().UTC().AddDate(0, -3, -1)

o := &hcloud.Image{Name: "debian-13", Deprecated: deprecated}

message, isUnavailable := ImageMessage(o)
assert.Equal(t, `Image "debian-13" is unavailable and can no longer be ordered.`, message)
assert.True(t, isUnavailable)
})
}
4 changes: 2 additions & 2 deletions hcloud/exp/deprecationutil/server_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ import (
"github.com/hetznercloud/hcloud-go/v2/hcloud/exp/kit/sliceutil"
)

// ServerTypeWarning return a warning message when the given Server Type is
// ServerTypeMessage return a deprecation message when the given Server Type is
// deprecated and whether the given Server Type is unavailable.
//
// Experimental: `exp` package is experimental, breaking changes may occur within minor releases.
func ServerTypeWarning(serverType *hcloud.ServerType, locationName string) (string, bool) {
func ServerTypeMessage(serverType *hcloud.ServerType, locationName string) (string, bool) {
if serverType.IsDeprecated() {
if time.Now().After(serverType.UnavailableAfter()) {
return fmt.Sprintf(
Expand Down
80 changes: 39 additions & 41 deletions hcloud/exp/deprecationutil/server_type_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,9 @@ import (
"github.com/hetznercloud/hcloud-go/v2/hcloud"
)

func TestServerTypeWarning(t *testing.T) {
day := 24 * time.Hour

past := time.Now().Add(-14 * day).UTC()
future := time.Now().Add(14 * day).UTC()
func TestServerTypeMessage(t *testing.T) {
past := time.Now().UTC().AddDate(0, 0, -14)
future := time.Now().UTC().AddDate(0, 0, 14)

Deprecated := hcloud.DeprecatableResource{Deprecation: &hcloud.DeprecationInfo{
Announced: past,
Expand All @@ -28,9 +26,9 @@ func TestServerTypeWarning(t *testing.T) {
t.Run("not deprecated", func(t *testing.T) {
o := &hcloud.ServerType{Name: "cx22"}

warn, warnIsError := ServerTypeWarning(o, "")
assert.Equal(t, "", warn)
assert.False(t, warnIsError)
message, isUnavailable := ServerTypeMessage(o, "")
assert.Equal(t, "", message)
assert.False(t, isUnavailable)
})

t.Run("deprecated backward compatible", func(t *testing.T) {
Expand All @@ -39,9 +37,9 @@ func TestServerTypeWarning(t *testing.T) {
DeprecatableResource: Deprecated,
}

warn, warnIsError := ServerTypeWarning(o, "")
assert.Equal(t, fmt.Sprintf(`Server Type "cx22" is deprecated in all locations and will no longer be available for order as of %s.`, future.Format(time.DateOnly)), warn)
assert.False(t, warnIsError)
message, isUnavailable := ServerTypeMessage(o, "")
assert.Equal(t, fmt.Sprintf(`Server Type "cx22" is deprecated in all locations and will no longer be available for order as of %s.`, future.Format(time.DateOnly)), message)
assert.False(t, isUnavailable)
})

t.Run("unavailable backward compatible", func(t *testing.T) {
Expand All @@ -50,9 +48,9 @@ func TestServerTypeWarning(t *testing.T) {
DeprecatableResource: Unavailable,
}

warn, warnIsError := ServerTypeWarning(o, "")
assert.Equal(t, `Server Type "cx22" is unavailable in all locations and can no longer be ordered.`, warn)
assert.True(t, warnIsError)
message, isUnavailable := ServerTypeMessage(o, "")
assert.Equal(t, `Server Type "cx22" is unavailable in all locations and can no longer be ordered.`, message)
assert.True(t, isUnavailable)
})

t.Run("not deprecated in any locations with given location", func(t *testing.T) {
Expand All @@ -68,9 +66,9 @@ func TestServerTypeWarning(t *testing.T) {
},
}

warn, warnIsError := ServerTypeWarning(o, "fsn1")
assert.Equal(t, ``, warn)
assert.False(t, warnIsError)
message, isUnavailable := ServerTypeMessage(o, "fsn1")
assert.Equal(t, ``, message)
assert.False(t, isUnavailable)
})

t.Run("deprecated in some locations with given location", func(t *testing.T) {
Expand All @@ -87,9 +85,9 @@ func TestServerTypeWarning(t *testing.T) {
},
}

warn, warnIsError := ServerTypeWarning(o, "fsn1")
assert.Equal(t, fmt.Sprintf(`Server Type "cx22" is deprecated in "fsn1" and will no longer be available for order as of %s.`, future.Format(time.DateOnly)), warn)
assert.False(t, warnIsError)
message, isUnavailable := ServerTypeMessage(o, "fsn1")
assert.Equal(t, fmt.Sprintf(`Server Type "cx22" is deprecated in "fsn1" and will no longer be available for order as of %s.`, future.Format(time.DateOnly)), message)
assert.False(t, isUnavailable)
})

t.Run("unavailable in some locations with given location", func(t *testing.T) {
Expand All @@ -106,9 +104,9 @@ func TestServerTypeWarning(t *testing.T) {
},
}

warn, warnIsError := ServerTypeWarning(o, "fsn1")
assert.Equal(t, `Server Type "cx22" is unavailable in "fsn1" and can no longer be ordered.`, warn)
assert.True(t, warnIsError)
deprecation, isUnavailable := ServerTypeMessage(o, "fsn1")
assert.Equal(t, `Server Type "cx22" is unavailable in "fsn1" and can no longer be ordered.`, deprecation)
assert.True(t, isUnavailable)
})

t.Run("deprecated in all locations with given location", func(t *testing.T) {
Expand All @@ -126,9 +124,9 @@ func TestServerTypeWarning(t *testing.T) {
},
}

warn, warnIsError := ServerTypeWarning(o, "fsn1")
assert.Equal(t, fmt.Sprintf(`Server Type "cx22" is deprecated in "fsn1" and will no longer be available for order as of %s.`, future.Format(time.DateOnly)), warn)
assert.False(t, warnIsError)
message, isUnavailable := ServerTypeMessage(o, "fsn1")
assert.Equal(t, fmt.Sprintf(`Server Type "cx22" is deprecated in "fsn1" and will no longer be available for order as of %s.`, future.Format(time.DateOnly)), message)
assert.False(t, isUnavailable)
})

t.Run("deprecated in all locations with given unknown location", func(t *testing.T) {
Expand All @@ -146,9 +144,9 @@ func TestServerTypeWarning(t *testing.T) {
},
}

warn, warnIsError := ServerTypeWarning(o, "hel1")
assert.Equal(t, "", warn)
assert.False(t, warnIsError)
message, isUnavailable := ServerTypeMessage(o, "hel1")
assert.Equal(t, "", message)
assert.False(t, isUnavailable)
})

t.Run("deprecated in some locations", func(t *testing.T) {
Expand All @@ -165,9 +163,9 @@ func TestServerTypeWarning(t *testing.T) {
},
}

warn, warnIsError := ServerTypeWarning(o, "")
assert.Equal(t, "", warn)
assert.False(t, warnIsError)
message, isUnavailable := ServerTypeMessage(o, "")
assert.Equal(t, "", message)
assert.False(t, isUnavailable)
})

t.Run("deprecated in all locations", func(t *testing.T) {
Expand All @@ -185,9 +183,9 @@ func TestServerTypeWarning(t *testing.T) {
},
}

warn, warnIsError := ServerTypeWarning(o, "")
assert.Equal(t, `Server Type "cx22" is deprecated in all locations (fsn1,nbg1) and will no longer be available for order.`, warn)
assert.False(t, warnIsError)
message, isUnavailable := ServerTypeMessage(o, "")
assert.Equal(t, `Server Type "cx22" is deprecated in all locations (fsn1,nbg1) and will no longer be available for order.`, message)
assert.False(t, isUnavailable)
})

t.Run("deprecated in all locations and unavailable in some locations", func(t *testing.T) {
Expand All @@ -205,9 +203,9 @@ func TestServerTypeWarning(t *testing.T) {
},
}

warn, warnIsError := ServerTypeWarning(o, "")
assert.Equal(t, `Server Type "cx22" is deprecated in all locations (fsn1,nbg1) and can no longer be ordered some locations (nbg1).`, warn)
assert.False(t, warnIsError)
message, isUnavailable := ServerTypeMessage(o, "")
assert.Equal(t, `Server Type "cx22" is deprecated in all locations (fsn1,nbg1) and can no longer be ordered some locations (nbg1).`, message)
assert.False(t, isUnavailable)
})

t.Run("unavailable in all locations", func(t *testing.T) {
Expand All @@ -225,8 +223,8 @@ func TestServerTypeWarning(t *testing.T) {
},
}

warn, warnIsError := ServerTypeWarning(o, "")
assert.Equal(t, `Server Type "cx22" is unavailable in all locations (fsn1,nbg1) and can no longer be ordered.`, warn)
assert.True(t, warnIsError)
message, isUnavailable := ServerTypeMessage(o, "")
assert.Equal(t, `Server Type "cx22" is unavailable in all locations (fsn1,nbg1) and can no longer be ordered.`, message)
assert.True(t, isUnavailable)
})
}
Loading