Skip to content

Commit 9eb0cc4

Browse files
authored
feat(primary-ip): make auto_delete attribute optional (#1429)
The `auto_delete` attribute is currently required, but we recommend always setting its value to `false`: Setting `auto_delete` to `false` is recommended, because if a server assigned to the managed ip is getting deleted, it will also delete the primary IP which will break the terraform state. This change makes the `auto_delete` attribute optional, and defaults to false. Simplifying user's configurations and using a recommended default. https://docs.hetzner.cloud/reference/cloud#tag/primary-ips/create_primary_ip
1 parent 1c22d4d commit 9eb0cc4

5 files changed

Lines changed: 34 additions & 36 deletions

File tree

docs/resources/primary_ip.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,14 @@ resource "hcloud_server" "main" {
6666

6767
### Required
6868

69-
- `auto_delete` (Boolean) Whether auto delete is enabled. Setting `auto_delete` to `false` is recommended, because if a server assigned to the managed ip is getting deleted, it will also delete the primary IP which will break the terraform state.
7069
- `name` (String) Name of the Primary IP.
7170
- `type` (String) Type of the Primary IP (`ipv4` or `ipv6`).
7271

7372
### Optional
7473

7574
- `assignee_id` (Number) ID of the resource the Primary IP should be assigned to.
7675
- `assignee_type` (String) Type of the resource the Primary IP should be assigned to.
76+
- `auto_delete` (Boolean) Whether auto delete is enabled. Setting `auto_delete` to `true` is not recommended, because if a server assigned to the managed ip is deleted, it will also delete the primary IP which will break the terraform state.
7777
- `datacenter` (String, Deprecated) Name of the Datacenter for the Primary IP. See the [Hetzner Docs](https://docs.hetzner.com/cloud/general/locations/#what-datacenters-are-there) for more details about datacenters.
7878
- `delete_protection` (Boolean) Whether delete protection is enabled.
7979
- `labels` (Map of String) User-defined [labels](https://docs.hetzner.cloud/reference/cloud#labels) (key-value pairs) for the resource.

internal/primaryip/resource.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,10 @@ communicating with the API.
123123
Computed: true,
124124
},
125125
"auto_delete": schema.BoolAttribute{
126-
MarkdownDescription: "Whether auto delete is enabled. Setting `auto_delete` to `false` is recommended, because if a server assigned to the managed ip is getting deleted, it will also delete the primary IP which will break the terraform state.",
127-
Required: true,
126+
MarkdownDescription: "Whether auto delete is enabled. Setting `auto_delete` to `true` is not recommended, because if a server assigned to the managed ip is deleted, it will also delete the primary IP which will break the terraform state.",
127+
Optional: true,
128+
Computed: true,
129+
Default: booldefault.StaticBool(false),
128130
},
129131
"labels": resourceutil.LabelsSchema(),
130132
"delete_protection": schema.BoolAttribute{
@@ -198,8 +200,10 @@ func (r *Resource) Create(ctx context.Context, req resource.CreateRequest, resp
198200
opts := hcloud.PrimaryIPCreateOpts{
199201
Name: data.Name.ValueString(),
200202
Type: hcloud.PrimaryIPType(data.Type.ValueString()),
203+
}
201204

202-
AutoDelete: data.AutoDelete.ValueBoolPointer(),
205+
if !data.AutoDelete.IsUnknown() && !data.AutoDelete.IsNull() {
206+
opts.AutoDelete = data.AutoDelete.ValueBoolPointer()
203207
}
204208

205209
resp.Diagnostics.Append(hcloudutil.TerraformLabelsToHCloud(ctx, data.Labels, &opts.Labels)...)

internal/primaryip/resource_test.go

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ func TestAccPrimaryIPResource(t *testing.T) {
2929
Name: "primary-ip",
3030
Type: "ipv6",
3131
Location: teste2e.TestLocationName,
32-
AutoDelete: false,
3332
DeleteProtection: true,
3433
Labels: map[string]string{"key": "value"},
3534
}
@@ -38,7 +37,7 @@ func TestAccPrimaryIPResource(t *testing.T) {
3837
res3 := testtemplate.DeepCopy(t, res1)
3938
res3.Name = res1.Name + "-changed"
4039
res3.Labels = map[string]string{"key": "changed"}
41-
res3.AutoDelete = true
40+
res3.AutoDelete = hcloud.Ptr(true)
4241
res3.DeleteProtection = false
4342

4443
resource.ParallelTest(t, resource.TestCase{
@@ -144,26 +143,23 @@ func TestAccPrimaryIPResource_WithServer(t *testing.T) {
144143

145144
// Step 1
146145
res1A := &primaryip.RData{
147-
Name: "a",
148-
Type: "ipv4",
149-
Location: teste2e.TestLocationName,
150-
AutoDelete: false,
146+
Name: "a",
147+
Type: "ipv4",
148+
Location: teste2e.TestLocationName,
151149
}
152150
res1A.SetRName("a")
153151

154152
res1B := &primaryip.RData{
155-
Name: "b",
156-
Type: "ipv6",
157-
Location: teste2e.TestLocationName,
158-
AutoDelete: false,
153+
Name: "b",
154+
Type: "ipv6",
155+
Location: teste2e.TestLocationName,
159156
}
160157
res1B.SetRName("b")
161158

162159
res1C := &primaryip.RData{
163-
Name: "c",
164-
Type: "ipv4",
165-
Location: teste2e.TestLocationName,
166-
AutoDelete: false,
160+
Name: "c",
161+
Type: "ipv4",
162+
Location: teste2e.TestLocationName,
167163
}
168164
res1C.SetRName("c")
169165

@@ -340,7 +336,7 @@ func TestAccPrimaryIPResource_Reassign(t *testing.T) {
340336
Type: "ipv4",
341337
AssigneeID: res1ServerA.TFID() + ".id",
342338
AssigneeType: "server",
343-
AutoDelete: false,
339+
AutoDelete: hcloud.Ptr(false),
344340
}
345341
res2.SetRName("main")
346342

internal/primaryip/testing.go

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ type RData struct {
7979
AssigneeType string
8080
AssigneeID string
8181
Labels map[string]string
82-
AutoDelete bool
82+
AutoDelete *bool
8383
DeleteProtection bool
8484

8585
Raw string
@@ -103,34 +103,30 @@ func NewBlueprint(t *testing.T) *Blueprint {
103103
b := &Blueprint{}
104104

105105
b.PrimaryIPv4A = &RData{
106-
Name: "a-ipv4",
107-
Type: "ipv4",
108-
Location: teste2e.TestLocationName,
109-
AutoDelete: false,
106+
Name: "a-ipv4",
107+
Type: "ipv4",
108+
Location: teste2e.TestLocationName,
110109
}
111110
b.PrimaryIPv4A.SetRName("primary_ipv4_a")
112111

113112
b.PrimaryIPv4B = &RData{
114-
Name: "b-ipv4",
115-
Type: "ipv4",
116-
Location: teste2e.TestLocationName,
117-
AutoDelete: false,
113+
Name: "b-ipv4",
114+
Type: "ipv4",
115+
Location: teste2e.TestLocationName,
118116
}
119117
b.PrimaryIPv4B.SetRName("primary_ipv4_b")
120118

121119
b.PrimaryIPv6C = &RData{
122-
Name: "c-ipv6",
123-
Type: "ipv6",
124-
Location: teste2e.TestLocationName,
125-
AutoDelete: false,
120+
Name: "c-ipv6",
121+
Type: "ipv6",
122+
Location: teste2e.TestLocationName,
126123
}
127124
b.PrimaryIPv6C.SetRName("primary_ipv6_c")
128125

129126
b.PrimaryIPv6D = &RData{
130-
Name: "d-ipv6",
131-
Type: "ipv6",
132-
Location: teste2e.TestLocationName,
133-
AutoDelete: false,
127+
Name: "d-ipv6",
128+
Type: "ipv6",
129+
Location: teste2e.TestLocationName,
134130
}
135131
b.PrimaryIPv6D.SetRName("primary_ipv6_d")
136132

internal/testdata/r/hcloud_primary_ip.tf.tmpl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ resource "hcloud_primary_ip" "{{ .RName }}" {
2121
labels = {{ .Labels | toPrettyJson }}
2222
{{- end }}
2323

24+
{{- if ne .AutoDelete nil }}
2425
auto_delete = {{ .AutoDelete }}
26+
{{- end }}
2527
{{- if .DeleteProtection }}
2628
delete_protection = {{ .DeleteProtection }}
2729
{{ end }}

0 commit comments

Comments
 (0)