From f12ab3120b86f0b9cb9b9402e8cf090d442ade0e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julian=20T=C3=B6lle?= Date: Mon, 12 Jun 2023 14:36:57 +0200 Subject: [PATCH] feat: deprecation info for server types --- internal/deprecation/deprecation.go | 39 +++++++++++++++++++++++++++++ internal/server/resource.go | 13 +++++++++- internal/servertype/data_source.go | 8 ++++-- website/docs/d/server_type.html.md | 3 +++ 4 files changed, 60 insertions(+), 3 deletions(-) create mode 100644 internal/deprecation/deprecation.go diff --git a/internal/deprecation/deprecation.go b/internal/deprecation/deprecation.go new file mode 100644 index 00000000..03dfc580 --- /dev/null +++ b/internal/deprecation/deprecation.go @@ -0,0 +1,39 @@ +package deprecation + +import ( + "time" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + "github.com/hetznercloud/hcloud-go/hcloud" +) + +func AddToSchema(s map[string]*schema.Schema) map[string]*schema.Schema { + s["is_deprecated"] = &schema.Schema{ + Type: schema.TypeBool, + Computed: true, + } + s["deprecation_announced"] = &schema.Schema{ + Type: schema.TypeString, + Computed: true, + Optional: true, + } + s["unavailable_after"] = &schema.Schema{ + Type: schema.TypeString, + Computed: true, + Optional: true, + } + + return s +} + +func SetData(d *schema.ResourceData, r hcloud.Deprecatable) { + if !r.IsDeprecated() { + d.Set("is_deprecated", false) + d.Set("deprecation_announced", nil) + d.Set("unavailable_after", nil) + } else { + d.Set("is_deprecated", true) + d.Set("deprecation_announced", r.DeprecationAnnounced().Format(time.RFC3339)) + d.Set("unavailable_after", r.UnavailableAfter().Format(time.RFC3339)) + } +} diff --git a/internal/server/resource.go b/internal/server/resource.go index f161bfb7..44ab47f7 100644 --- a/internal/server/resource.go +++ b/internal/server/resource.go @@ -279,6 +279,14 @@ func resourceServerCreate(ctx context.Context, d *schema.ResourceData, m interfa return diag.Errorf("server type %s not found", d.Get("server_type")) } + if serverType.IsDeprecated() { + if time.Now().Before(serverType.UnavailableAfter()) { + tflog.Warn(ctx, 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("2006-01-02"))) + } else { + return diag.Errorf("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) + } + } + imageNameOrID := d.Get("image").(string) image, _, err := c.Image.GetForArchitecture(ctx, imageNameOrID, serverType.Architecture) if err != nil { @@ -1080,7 +1088,10 @@ func setServerSchema(d *schema.ResourceData, s *hcloud.Server) { case "id": d.SetId(strconv.Itoa(val.(int))) default: - d.Set(key, val) + err := d.Set(key, val) + if err != nil { + log.Fatal(err) + } } } } diff --git a/internal/servertype/data_source.go b/internal/servertype/data_source.go index 3a04cd56..eafccf2d 100644 --- a/internal/servertype/data_source.go +++ b/internal/servertype/data_source.go @@ -7,6 +7,8 @@ import ( "strconv" "strings" + "github.com/hetznercloud/terraform-provider-hcloud/internal/deprecation" + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" "github.com/hetznercloud/hcloud-go/hcloud" @@ -25,7 +27,7 @@ const ( // getCommonDataSchema returns a new common schema used by all server type data sources. func getCommonDataSchema() map[string]*schema.Schema { - return map[string]*schema.Schema{ + return deprecation.AddToSchema(map[string]*schema.Schema{ "id": { Type: schema.TypeInt, Optional: true, @@ -68,7 +70,7 @@ func getCommonDataSchema() map[string]*schema.Schema { Type: schema.TypeInt, Computed: true, }, - } + }) } // DataSource creates a new Terraform schema for the hcloud_server_type data @@ -152,6 +154,8 @@ func setServerTypeSchema(d *schema.ResourceData, t *hcloud.ServerType) { d.Set(key, val) } } + + deprecation.SetData(d, t) } func getServerTypeAttributes(t *hcloud.ServerType) map[string]interface{} { diff --git a/website/docs/d/server_type.html.md b/website/docs/d/server_type.html.md index b09517b0..ce4a3b04 100644 --- a/website/docs/d/server_type.html.md +++ b/website/docs/d/server_type.html.md @@ -31,3 +31,6 @@ data "hcloud_server_type" "ds_2" { - `disk` - (int) Disk size a Server of this type will have in GB. - `architecture` - (string) Architecture of the server_type. - `included_traffic` - (int) Free traffic per month in bytes. +- `is_deprecated` - (bool) Deprecation status of server type. +- `deprecation_announced` (Optional, string) Date when the deprecation of the server type was announced. Only set when the server type is deprecated. +- `unavailable_after` (Optional, string) Date when the server type will not be available for new servers. Only set when the server type is deprecated.