Skip to content

Commit a13745e

Browse files
authored
Add support of using deprecated images (#549)
Fixes #527 Signed-off-by: Lukas Kämmerling <lukas.kaemmerling@hetzner-cloud.de>
1 parent 4636a60 commit a13745e

File tree

6 files changed

+56
-24
lines changed

6 files changed

+56
-24
lines changed

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module github.com/hetznercloud/terraform-provider-hcloud
33
require (
44
github.com/hashicorp/go-cty v1.4.1-0.20200414143053-d3edf31b6320
55
github.com/hashicorp/go-multierror v1.1.1
6+
github.com/hashicorp/terraform-plugin-log v0.3.0
67
github.com/hashicorp/terraform-plugin-sdk/v2 v2.14.0
78
github.com/hetznercloud/hcloud-go v1.35.1
89
github.com/stretchr/testify v1.7.0
@@ -33,7 +34,6 @@ require (
3334
github.com/hashicorp/terraform-exec v0.16.1 // indirect
3435
github.com/hashicorp/terraform-json v0.13.0 // indirect
3536
github.com/hashicorp/terraform-plugin-go v0.9.0 // indirect
36-
github.com/hashicorp/terraform-plugin-log v0.3.0 // indirect
3737
github.com/hashicorp/terraform-registry-address v0.0.0-20210412075316-9b2996cce896 // indirect
3838
github.com/hashicorp/terraform-svchost v0.0.0-20200729002733-f050f53b9734 // indirect
3939
github.com/hashicorp/yamux v0.0.0-20210707203944-259a57b3608c // indirect

internal/e2etests/server/resource_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func TestServerResource_Basic(t *testing.T) {
6767
ImportState: true,
6868
ImportStateVerify: true,
6969
ImportStateVerifyIgnore: []string{
70-
"ssh_keys", "user_data", "keep_disk", "ignore_remote_firewall_ids",
70+
"ssh_keys", "user_data", "keep_disk", "ignore_remote_firewall_ids", "allow_deprecated_images",
7171
},
7272
},
7373
{

internal/server/resource.go

+28-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import (
1111
"strings"
1212
"time"
1313

14+
"github.com/hashicorp/terraform-plugin-log/tflog"
15+
1416
"github.com/hashicorp/go-cty/cty"
1517
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
1618
"github.com/hetznercloud/terraform-provider-hcloud/internal/primaryip"
@@ -97,6 +99,11 @@ func Resource() *schema.Resource {
9799
Optional: true,
98100
Default: false,
99101
},
102+
"allow_deprecated_images": {
103+
Type: schema.TypeBool,
104+
Optional: true,
105+
Default: false,
106+
},
100107
"backup_window": {
101108
Type: schema.TypeString,
102109
Deprecated: "You should remove this property from your terraform configuration.",
@@ -262,10 +269,30 @@ func resourceServerCreate(ctx context.Context, d *schema.ResourceData, m interfa
262269
c := m.(*hcloud.Client)
263270

264271
var err error
265-
image, _, err := c.Image.Get(ctx, d.Get("image").(string))
272+
var image *hcloud.Image
273+
images, err := c.Image.AllWithOpts(ctx, hcloud.ImageListOpts{Name: d.Get("image").(string), IncludeDeprecated: true})
266274
if err != nil {
267275
return hcclient.ErrorToDiag(err)
268276
}
277+
switch len(images) {
278+
case 1:
279+
image = images[0]
280+
case 0:
281+
image, _, err = c.Image.Get(ctx, d.Get("image").(string))
282+
if err != nil {
283+
return hcclient.ErrorToDiag(err)
284+
}
285+
default:
286+
return diag.Errorf("more than one Image found for name %s", d.Get("image").(string))
287+
}
288+
289+
if image.IsDeprecated() {
290+
if d.Get("allow_deprecated_images").(bool) {
291+
tflog.Warn(ctx, fmt.Sprintf("image %s is deprecated. It will continue to be available until %s", image.Name, image.Deprecated.AddDate(0, 3, 0).Format("2006-01-02")))
292+
} else {
293+
return diag.Errorf("image %s is deprecated. It will continue to be available until %s. If you want to use it, specify the allow_deprecated_images option.", image.Name, image.Deprecated.AddDate(0, 3, 0).Format("2006-01-02"))
294+
}
295+
}
269296
opts := hcloud.ServerCreateOpts{
270297
Name: d.Get("name").(string),
271298
ServerType: &hcloud.ServerType{

internal/server/testing.go

+20-19
Original file line numberDiff line numberDiff line change
@@ -89,25 +89,26 @@ func (d *DDataList) TFID() string {
8989
type RData struct {
9090
testtemplate.DataCommon
9191

92-
Name string
93-
Type string
94-
Image string
95-
LocationName string
96-
Datacenter string
97-
PublicNet map[string]interface{}
98-
SSHKeys []string
99-
KeepDisk bool
100-
Rescue bool
101-
Backups bool
102-
ISO string
103-
Labels map[string]string
104-
UserData string
105-
Network RDataInlineNetwork
106-
FirewallIDs []string
107-
DependsOn []string
108-
PlacementGroupID string
109-
DeleteProtection bool
110-
RebuildProtection bool
92+
Name string
93+
Type string
94+
Image string
95+
LocationName string
96+
Datacenter string
97+
PublicNet map[string]interface{}
98+
SSHKeys []string
99+
KeepDisk bool
100+
Rescue bool
101+
Backups bool
102+
ISO string
103+
Labels map[string]string
104+
UserData string
105+
Network RDataInlineNetwork
106+
FirewallIDs []string
107+
DependsOn []string
108+
PlacementGroupID string
109+
DeleteProtection bool
110+
RebuildProtection bool
111+
AllowDeprecatedImages bool
111112
}
112113

113114
// RDataInlineNetwork defines the information required to attach a server

internal/testdata/r/hcloud_server.tf.tmpl

+3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ resource "hcloud_server" "{{ .RName }}" {
1919
{{- if .KeepDisk }}
2020
keep_disk = {{ .KeepDisk }}
2121
{{ end }}
22+
{{- if .AllowDeprecatedImages }}
23+
allow_deprecated_images = {{ .AllowDeprecatedImages }}
24+
{{ end }}
2225
{{- if .ISO }}
2326
iso = {{ .ISO }}
2427
{{ end }}

website/docs/r/server.html.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -150,15 +150,16 @@ The following arguments are supported:
150150
- `labels` - (Optional, map) User-defined labels (key-value pairs) should be created with.
151151
- `backups` - (Optional, boolean) Enable or disable backups.
152152
- `firewall_ids` - (Optional, list) Firewall IDs the server should be attached to on creation.
153-
- `ignore_remote_firewall_ids` - (Optional, boolean) Ingores any updates
153+
- `ignore_remote_firewall_ids` - (Optional, boolean) Ignores any updates
154154
to the `firewall_ids` argument which were received from the server.
155155
This should not be used in normal cases. See the documentation of the
156-
`hcloud_firewall_attachment` resouce for a reason to use this
156+
`hcloud_firewall_attachment` resource for a reason to use this
157157
argument.
158158
- `network` - (Optional) Network the server should be attached to on creation. (Can be specified multiple times)
159159
- `placement_group_id` - (Optional, string) Placement Group ID the server added to on creation.
160160
- `delete_protection` - (Optional, boolean) Enable or disable delete protection (Needs to be the same as `rebuild_protection`).
161161
- `rebuild_protection` - (Optional, boolean) Enable or disable rebuild protection (Needs to be the same as `delete_protection`).
162+
- `allow_deprecated_images` - (Optional, boolean) Enable the use of deprecated images (default: false). **Note** Deprecated images will be removed after three months. Using them is then no longer possible.
162163

163164
`network` support the following fields:
164165
- `network_id` - (Required, int) ID of the network

0 commit comments

Comments
 (0)