Skip to content

Commit b999c6c

Browse files
committed
fix(dhcp): boolean attributes update and test
1 parent a6071dc commit b999c6c

3 files changed

+241
-67
lines changed

scaleway/resource_vpc_public_gateway_dhcp.go

+18-32
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func resourceScalewayVPCPublicGatewayDHCP() *schema.Resource {
5454
"enable_dynamic": {
5555
Type: schema.TypeBool,
5656
Optional: true,
57-
Default: true,
57+
Computed: true,
5858
Description: "Whether to enable dynamic pooling of IPs. By turning the dynamic pool off, only pre-existing DHCP reservations will be handed out. Defaults to true.",
5959
},
6060
"valid_lifetime": {
@@ -79,7 +79,7 @@ func resourceScalewayVPCPublicGatewayDHCP() *schema.Resource {
7979
Type: schema.TypeBool,
8080
Optional: true,
8181
Computed: true,
82-
Description: "Whether the gateway should push a default route to DHCP clients or only hand out IPs. Defaults to true",
82+
Description: "Whether the gateway should push a default route to DHCP clients or only hand out IPs. Defaults to true.",
8383
},
8484
"push_dns_server": {
8585
Type: schema.TypeBool,
@@ -93,15 +93,15 @@ func resourceScalewayVPCPublicGatewayDHCP() *schema.Resource {
9393
Elem: &schema.Schema{
9494
Type: schema.TypeString,
9595
},
96-
Description: "Override the DNS server list pushed to DHCP clients, instead of the gateway itself",
96+
Description: "Override the DNS server list pushed to DHCP clients, instead of the gateway itself.",
9797
},
9898
"dns_search": {
9999
Type: schema.TypeList,
100100
Optional: true,
101101
Elem: &schema.Schema{
102102
Type: schema.TypeString,
103103
},
104-
Description: "Additional DNS search paths",
104+
Description: "Additional DNS search paths.",
105105
},
106106
"dns_local_name": {
107107
Type: schema.TypeString,
@@ -113,12 +113,12 @@ func resourceScalewayVPCPublicGatewayDHCP() *schema.Resource {
113113
"created_at": {
114114
Type: schema.TypeString,
115115
Computed: true,
116-
Description: "The date and time of the creation of the public gateway",
116+
Description: "The date and time of the creation of the public gateway.",
117117
},
118118
"updated_at": {
119119
Type: schema.TypeString,
120120
Computed: true,
121-
Description: "The date and time of the last update of the public gateway",
121+
Description: "The date and time of the last update of the public gateway.",
122122
},
123123
},
124124
}
@@ -169,23 +169,23 @@ func resourceScalewayVPCPublicGatewayDHCPCreate(ctx context.Context, d *schema.R
169169
}
170170

171171
if renewTimer, ok := d.GetOk("renew_timer"); ok {
172-
req.RenewTimer = &scw.Duration{Seconds: renewTimer.(int64)}
172+
req.RenewTimer = &scw.Duration{Seconds: int64(renewTimer.(int))}
173173
}
174174

175175
if validLifetime, ok := d.GetOk("valid_lifetime"); ok {
176-
req.ValidLifetime = &scw.Duration{Seconds: validLifetime.(int64)}
176+
req.ValidLifetime = &scw.Duration{Seconds: int64(validLifetime.(int))}
177177
}
178178

179179
if rebindTimer, ok := d.GetOk("rebind_timer"); ok {
180-
req.RebindTimer = &scw.Duration{Seconds: rebindTimer.(int64)}
180+
req.RebindTimer = &scw.Duration{Seconds: int64(rebindTimer.(int))}
181181
}
182182

183183
if poolLow, ok := d.GetOk("pool_low"); ok {
184184
req.PoolLow = scw.IPPtr(net.ParseIP(poolLow.(string)))
185185
}
186186

187187
if poolHigh, ok := d.GetOk("pool_high"); ok {
188-
req.PoolLow = scw.IPPtr(net.ParseIP(poolHigh.(string)))
188+
req.PoolHigh = scw.IPPtr(net.ParseIP(poolHigh.(string)))
189189
}
190190

191191
res, err := vpcgwAPI.CreateDHCP(req, scw.WithContext(ctx))
@@ -244,7 +244,7 @@ func resourceScalewayVPCPublicGatewayDHCPUpdate(ctx context.Context, d *schema.R
244244
return diag.FromErr(err)
245245
}
246246

247-
if d.HasChangesExcept("subnet", "address", "pool_low", "pool_high",
247+
if d.HasChanges("subnet", "address", "pool_low", "pool_high",
248248
"enable_dynamic", "push_default_route", "push_dns_server", "dns_servers_override",
249249
"dns_search", "dns_local_name", "renew_timer", "valid_lifetime", "rebind_timer") {
250250
req := &vpcgw.UpdateDHCPRequest{
@@ -264,25 +264,11 @@ func resourceScalewayVPCPublicGatewayDHCPUpdate(ctx context.Context, d *schema.R
264264
req.Address = scw.IPPtr(net.ParseIP(address.(string)))
265265
}
266266

267-
if poolLow, ok := d.GetOk("pool_low"); ok {
268-
req.PoolLow = scw.IPPtr(net.ParseIP(poolLow.(string)))
269-
}
270-
271-
if poolHigh, ok := d.GetOk("pool_low"); ok {
272-
req.PoolHigh = scw.IPPtr(net.ParseIP(poolHigh.(string)))
273-
}
274-
275-
if pushDefaultRoute, ok := d.GetOk("push_default_route"); ok {
276-
req.PushDefaultRoute = expandBoolPtr(pushDefaultRoute)
277-
}
267+
req.PushDNSServer = expandBoolPtr(d.Get("push_dns_server"))
278268

279-
if pushDNServer, ok := d.GetOk("push_dns_server"); ok {
280-
req.PushDNSServer = expandBoolPtr(pushDNServer)
281-
}
269+
req.EnableDynamic = expandBoolPtr(d.Get("enable_dynamic"))
282270

283-
if enableDynamic, ok := d.GetOk("enable_dynamic"); ok {
284-
req.EnableDynamic = expandBoolPtr(enableDynamic)
285-
}
271+
req.PushDefaultRoute = expandBoolPtr(d.Get("push_default_route"))
286272

287273
if dnsServerOverride, ok := d.GetOk("dns_servers_override"); ok {
288274
req.DNSServersOverride = expandStringsPtr(dnsServerOverride)
@@ -297,23 +283,23 @@ func resourceScalewayVPCPublicGatewayDHCPUpdate(ctx context.Context, d *schema.R
297283
}
298284

299285
if renewTimer, ok := d.GetOk("renew_timer"); ok {
300-
req.RenewTimer = &scw.Duration{Seconds: renewTimer.(int64)}
286+
req.RenewTimer = &scw.Duration{Seconds: int64(renewTimer.(int))}
301287
}
302288

303289
if validLifetime, ok := d.GetOk("valid_lifetime"); ok {
304-
req.ValidLifetime = &scw.Duration{Seconds: validLifetime.(int64)}
290+
req.ValidLifetime = &scw.Duration{Seconds: int64(validLifetime.(int))}
305291
}
306292

307293
if rebindTimer, ok := d.GetOk("rebind_timer"); ok {
308-
req.RebindTimer = &scw.Duration{Seconds: rebindTimer.(int64)}
294+
req.RebindTimer = &scw.Duration{Seconds: int64(rebindTimer.(int))}
309295
}
310296

311297
if poolLow, ok := d.GetOk("pool_low"); ok {
312298
req.PoolLow = scw.IPPtr(net.ParseIP(poolLow.(string)))
313299
}
314300

315301
if poolHigh, ok := d.GetOk("pool_high"); ok {
316-
req.PoolLow = scw.IPPtr(net.ParseIP(poolHigh.(string)))
302+
req.PoolHigh = scw.IPPtr(net.ParseIP(poolHigh.(string)))
317303
}
318304

319305
_, err = vpcgwAPI.UpdateDHCP(req, scw.WithContext(ctx))

scaleway/resource_vpc_public_gateway_dhcp_test.go

+33-12
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,38 @@ func TestAccScalewayVPCPublicGatewayDHCP_Basic(t *testing.T) {
4444
resource.TestCheckResourceAttrSet("scaleway_vpc_public_gateway_dhcp.main", "organization_id"),
4545
),
4646
},
47+
{
48+
Config: `
49+
resource scaleway_vpc_public_gateway_dhcp main {
50+
subnet = "192.168.1.0/24"
51+
valid_lifetime = 3000
52+
renew_timer = 2000
53+
rebind_timer = 2060
54+
push_default_route = false
55+
push_dns_server = false
56+
enable_dynamic = false
57+
}
58+
`,
59+
Check: resource.ComposeTestCheckFunc(
60+
testAccCheckScalewayVPCPublicGatewayDHCPExists(tt, "scaleway_vpc_public_gateway_dhcp.main"),
61+
resource.TestCheckResourceAttr("scaleway_vpc_public_gateway_dhcp.main", "subnet", "192.168.1.0/24"),
62+
resource.TestCheckResourceAttr("scaleway_vpc_public_gateway_dhcp.main", "push_default_route", "false"),
63+
resource.TestCheckResourceAttr("scaleway_vpc_public_gateway_dhcp.main", "push_dns_server", "false"),
64+
resource.TestCheckResourceAttr("scaleway_vpc_public_gateway_dhcp.main", "enable_dynamic", "false"),
65+
resource.TestCheckResourceAttr("scaleway_vpc_public_gateway_dhcp.main", "valid_lifetime", "3000"),
66+
resource.TestCheckResourceAttr("scaleway_vpc_public_gateway_dhcp.main", "renew_timer", "2000"),
67+
resource.TestCheckResourceAttr("scaleway_vpc_public_gateway_dhcp.main", "rebind_timer", "2060"),
68+
resource.TestCheckResourceAttr("scaleway_vpc_public_gateway_dhcp.main", "dns_server_override.#", "0"),
69+
resource.TestCheckResourceAttr("scaleway_vpc_public_gateway_dhcp.main", "dns_search.#", "0"),
70+
resource.TestCheckResourceAttrSet("scaleway_vpc_public_gateway_dhcp.main", "dns_local_name"),
71+
resource.TestCheckResourceAttrSet("scaleway_vpc_public_gateway_dhcp.main", "pool_low"),
72+
resource.TestCheckResourceAttrSet("scaleway_vpc_public_gateway_dhcp.main", "pool_high"),
73+
resource.TestCheckResourceAttrSet("scaleway_vpc_public_gateway_dhcp.main", "created_at"),
74+
resource.TestCheckResourceAttrSet("scaleway_vpc_public_gateway_dhcp.main", "updated_at"),
75+
resource.TestCheckResourceAttrSet("scaleway_vpc_public_gateway_dhcp.main", "zone"),
76+
resource.TestCheckResourceAttrSet("scaleway_vpc_public_gateway_dhcp.main", "organization_id"),
77+
),
78+
},
4779
},
4880
})
4981
}
@@ -60,7 +92,7 @@ func testAccCheckScalewayVPCPublicGatewayDHCPExists(tt *TestTools, n string) res
6092
return err
6193
}
6294

63-
dhcp, err := vpcgwAPI.GetDHCP(&vpcgw.GetDHCPRequest{
95+
_, err = vpcgwAPI.GetDHCP(&vpcgw.GetDHCPRequest{
6496
DHCPID: ID,
6597
Zone: zone,
6698
})
@@ -69,17 +101,6 @@ func testAccCheckScalewayVPCPublicGatewayDHCPExists(tt *TestTools, n string) res
69101
return err
70102
}
71103

72-
// Test default values
73-
if !dhcp.EnableDynamic {
74-
return fmt.Errorf("enable_dynamic is false, should default to true")
75-
}
76-
if !dhcp.PushDefaultRoute {
77-
return fmt.Errorf("push_default_route is false, should default to true")
78-
}
79-
if !dhcp.PushDNSServer {
80-
return fmt.Errorf("push_dns_server is false, should default to true")
81-
}
82-
83104
return nil
84105
}
85106
}

0 commit comments

Comments
 (0)