Skip to content

Commit

Permalink
feat: deprecation info for server types (#691)
Browse files Browse the repository at this point in the history
  • Loading branch information
apricote committed Jun 13, 2023
1 parent 9261f37 commit 9e6a22c
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 3 deletions.
39 changes: 39 additions & 0 deletions 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))
}
}
13 changes: 12 additions & 1 deletion internal/server/resource.go
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
}
}
}
Expand Down
8 changes: 6 additions & 2 deletions internal/servertype/data_source.go
Expand Up @@ -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"
Expand All @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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{} {
Expand Down
3 changes: 3 additions & 0 deletions website/docs/d/server_type.html.md
Expand Up @@ -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.

0 comments on commit 9e6a22c

Please sign in to comment.