Skip to content

Commit

Permalink
feat: show server-type deprecation warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
apricote committed Jun 13, 2023
1 parent 5e481e8 commit 946c7b2
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 10 deletions.
4 changes: 4 additions & 0 deletions internal/cmd/server/change_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ var ChangeTypeCommand = base.Cmd{
return fmt.Errorf("server type not found: %s", serverTypeIDOrName)
}

if serverType.IsDeprecated() {
fmt.Print(warningDeprecatedServerType(serverType))
}

keepDisk, _ := cmd.Flags().GetBool("keep-disk")
opts := hcloud.ServerChangeTypeOpts{
ServerType: serverType,
Expand Down
4 changes: 4 additions & 0 deletions internal/cmd/server/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,10 @@ func createOptsFromFlags(
return
}

if serverType.IsDeprecated() {
fmt.Print(warningDeprecatedServerType(serverType))
}

// Select correct image based on server type architecture
image, _, err := client.Image().GetForArchitecture(ctx, imageIDorName, serverType.Architecture)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions internal/cmd/server/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ var describeCmd = base.DescribeCmd{
fmt.Printf(" Memory:\t%v GB\n", server.ServerType.Memory)
fmt.Printf(" Disk:\t\t%d GB\n", server.PrimaryDiskSize)
fmt.Printf(" Storage Type:\t%s\n", server.ServerType.StorageType)
fmt.Printf(util.PrefixLines(util.DescribeDeprecation(server.ServerType), " "))

fmt.Printf("Public Net:\n")
fmt.Printf(" IPv4:\n")
Expand Down
20 changes: 20 additions & 0 deletions internal/cmd/server/texts.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package server

import (
"fmt"
"time"

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

func warningDeprecatedServerType(serverType *hcloud.ServerType) string {
if !serverType.IsDeprecated() {
return ""
}

if time.Now().After(serverType.UnavailableAfter()) {
return fmt.Sprintf("Attention: The server plan %q is deprecated and can no longer be ordered. Existing servers of that plan will continue to work as before and no action is required on your part. It is possible to migrate this server to another server plan by using the \"hcloud server change-type\" command.\n\n", serverType.Name)
}

return fmt.Sprintf("Attention: The server plan %q is deprecated and will no longer be available for order as of %s. Existing servers of that plan will continue to work as before and no action is required on your part. It is possible to migrate this server to another server plan by using the \"hcloud server change-type\" command.\n\n", serverType.Name, serverType.UnavailableAfter().Format(time.DateOnly))
}
1 change: 1 addition & 0 deletions internal/cmd/servertype/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ var describeCmd = base.DescribeCmd{
fmt.Printf("Disk:\t\t\t%d GB\n", serverType.Disk)
fmt.Printf("Storage Type:\t\t%s\n", serverType.StorageType)
fmt.Printf("Included Traffic:\t%d TB\n", serverType.IncludedTraffic/util.Tebibyte)
fmt.Printf(util.DescribeDeprecation(serverType))

fmt.Printf("Pricings per Location:\n")
for _, price := range serverType.Pricings {
Expand Down
19 changes: 19 additions & 0 deletions internal/cmd/util/deprecation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package util

import (
"fmt"
"github.com/dustin/go-humanize"
"github.com/hetznercloud/hcloud-go/hcloud"
)

func DescribeDeprecation(resource hcloud.Deprecatable) string {
if !resource.IsDeprecated() {
return ""
}

info := "Deprecation:\n"
info += fmt.Sprintf(" Announced:\t\t%s (%s)\n", Datetime(resource.DeprecationAnnounced()), humanize.Time(resource.DeprecationAnnounced()))
info += fmt.Sprintf(" Unavailable After:\t%s (%s)\n", Datetime(resource.UnavailableAfter()), humanize.Time(resource.UnavailableAfter()))

return info
}
46 changes: 36 additions & 10 deletions internal/cmd/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,17 @@ func LabelsToString(labels map[string]string) string {
return strings.Join(labelsString, ", ")
}

// PrefixLines will prefix all individual lines in the text with the passed prefix.
func PrefixLines(text, prefix string) string {
var lines []string

for _, line := range strings.Split(text, "\n") {
lines = append(lines, prefix+line)
}

return strings.Join(lines, "\n")
}

func DescribeFormat(object interface{}, format string) error {
if !strings.HasSuffix(format, "\n") {
format = format + "\n"
Expand Down Expand Up @@ -181,16 +192,17 @@ func DatacenterToSchema(datacenter hcloud.Datacenter) schema.Datacenter {

func ServerTypeToSchema(serverType hcloud.ServerType) schema.ServerType {
serverTypeSchema := schema.ServerType{
ID: serverType.ID,
Name: serverType.Name,
Description: serverType.Description,
Cores: serverType.Cores,
Memory: serverType.Memory,
Disk: serverType.Disk,
StorageType: string(serverType.StorageType),
CPUType: string(serverType.CPUType),
Architecture: string(serverType.Architecture),
IncludedTraffic: serverType.IncludedTraffic,
ID: serverType.ID,
Name: serverType.Name,
Description: serverType.Description,
Cores: serverType.Cores,
Memory: serverType.Memory,
Disk: serverType.Disk,
StorageType: string(serverType.StorageType),
CPUType: string(serverType.CPUType),
Architecture: string(serverType.Architecture),
IncludedTraffic: serverType.IncludedTraffic,
DeprecatableResource: DeprecatableResourceToSchema(serverType.DeprecatableResource),
}
for _, pricing := range serverType.Pricings {
serverTypeSchema.Prices = append(serverTypeSchema.Prices, schema.PricingServerTypePrice{
Expand Down Expand Up @@ -293,6 +305,20 @@ func PlacementGroupToSchema(placementGroup hcloud.PlacementGroup) schema.Placeme
}
}

func DeprecatableResourceToSchema(deprecatableResource hcloud.DeprecatableResource) schema.DeprecatableResource {
var deprecation *schema.DeprecationInfo

if deprecatableResource.IsDeprecated() {
deprecation = &schema.DeprecationInfo{
Announced: deprecatableResource.Deprecation.Announced,
UnavailableAfter: deprecatableResource.Deprecation.UnavailableAfter,
}
}

return schema.DeprecatableResource{
Deprecation: deprecation}
}

// ValidateRequiredFlags ensures that flags has values for all flags with
// the passed names.
//
Expand Down

0 comments on commit 946c7b2

Please sign in to comment.