Skip to content

Commit f46c282

Browse files
committed
fix(dhcp): default values
1 parent 29c6b6f commit f46c282

5 files changed

+92
-50
lines changed

docs/resources/vpc_public_gateway_dhcp.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ description: |-
77
# scaleway_vpc_public_gateway_dhcp
88

99
Creates and manages Scaleway VPC Public Gateway DHCP.
10-
For more information, see [the documentation](https://developers.scaleway.com/en/products/vpc-gw/api/v1).
10+
For more information, see [the documentation](https://developers.scaleway.com/en/products/vpc-gw/api/v1/#dhcp-c05544).
1111

1212
## Example
1313

@@ -29,7 +29,7 @@ The following arguments are supported:
2929
- `pool_high` - (Optional) High IP (excluded) of the dynamic address pool. Defaults to the last address of the subnet.
3030
- `enable_dynamic` - (Optional) 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`.
3131
- `valid_lifetime` - (Optional) For how long, in seconds, will DHCP entries will be valid. Defaults to 1h (3600s).
32-
- `renew_timer` - (Optional) After how long, in seconds, a renew will be attempted. Must be 30s lower than `rebind_timer`. Defaults to 50m (3000s).
32+
- `renew_timer` - (Optional) After how long, in seconds, a renewal will be attempted. Must be 30s lower than `rebind_timer`. Defaults to 50m (3000s).
3333
- `rebind_timer` - (Optional) After how long, in seconds, a DHCP client will query for a new lease if previous renews fail. Must be 30s lower than `valid_lifetime`. Defaults to 51m (3060s).
3434
- `push_default_route` - (Optional) Whether the gateway should push a default route to DHCP clients or only hand out IPs. Defaults to `true`.
3535
- `push_dns_server` - (Optional) Whether the gateway should push custom DNS servers to clients. This allows for instance hostname -> IP resolution. Defaults to `true`.

scaleway/helpers.go

+8
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,14 @@ func flattenSliceStringPtr(s []*string) interface{} {
451451
return res
452452
}
453453

454+
func flattenSliceString(s []string) interface{} {
455+
res := make([]interface{}, 0, len(s))
456+
for _, strPtr := range s {
457+
res = append(res, strPtr)
458+
}
459+
return res
460+
}
461+
454462
func flattenSliceIDs(certificates []string, zone scw.Zone) interface{} {
455463
res := []interface{}(nil)
456464
for _, certificateID := range certificates {

scaleway/resource_vpc_public_gateway_dhcp.go

+36-18
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
99
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1010
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
11-
vpcgw "github.com/scaleway/scaleway-sdk-go/api/vpcgw/v1"
11+
"github.com/scaleway/scaleway-sdk-go/api/vpcgw/v1"
1212
"github.com/scaleway/scaleway-sdk-go/scw"
1313
)
1414

@@ -35,7 +35,7 @@ func resourceScalewayVPCPublicGatewayDHCP() *schema.Resource {
3535
Type: schema.TypeString,
3636
Optional: true,
3737
Computed: true,
38-
Description: "Address: address of the DHCP server. This will be the gateway's address in the private network. Defaults to the first address of the subnet",
38+
Description: "The address of the DHCP server. This will be the gateway's address in the private network. Defaults to the first address of the subnet",
3939
},
4040
"pool_low": {
4141
Type: schema.TypeString,
@@ -78,13 +78,13 @@ func resourceScalewayVPCPublicGatewayDHCP() *schema.Resource {
7878
"push_default_route": {
7979
Type: schema.TypeBool,
8080
Optional: true,
81-
Default: true,
81+
Computed: true,
8282
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,
8686
Optional: true,
87-
Default: true,
87+
Computed: true,
8888
Description: "Whether the gateway should push custom DNS servers to clients. This allows for instance hostname -> IP resolution. Defaults to true.",
8989
},
9090
"dns_server_override": {
@@ -135,15 +135,33 @@ func resourceScalewayVPCPublicGatewayDHCPCreate(ctx context.Context, d *schema.R
135135
return diag.FromErr(err)
136136
}
137137
req := &vpcgw.CreateDHCPRequest{
138-
Zone: zone,
139-
ProjectID: d.Get("project_id").(string),
140-
Subnet: subnet,
141-
EnableDynamic: expandBoolPtr(d.Get("enable_dynamic")),
142-
PushDefaultRoute: expandBoolPtr(d.Get("push_default_route")),
143-
PushDNSServer: expandBoolPtr(d.Get("push_dns_servers")),
144-
DNSServersOverride: expandStringsPtr(d.Get("dns_servers_override")),
145-
DNSSearch: expandStringsPtr(d.Get("dns_search")),
146-
DNSLocalName: expandStringPtr(d.Get("dns_local_name")),
138+
Zone: zone,
139+
ProjectID: d.Get("project_id").(string),
140+
Subnet: subnet,
141+
}
142+
143+
if pushDefaultRoute, ok := d.GetOk("push_default_route"); ok {
144+
req.PushDefaultRoute = expandBoolPtr(pushDefaultRoute)
145+
}
146+
147+
if pushDNServer, ok := d.GetOk("push_default_route"); ok {
148+
req.PushDNSServer = expandBoolPtr(pushDNServer)
149+
}
150+
151+
if enableDynamic, ok := d.GetOk("enable_dynamic"); ok {
152+
req.EnableDynamic = expandBoolPtr(enableDynamic)
153+
}
154+
155+
if dnsServerOverride, ok := d.GetOk("dns_servers_override"); ok {
156+
req.DNSServersOverride = expandStringsPtr(dnsServerOverride)
157+
}
158+
159+
if dnsSearch, ok := d.GetOk("dns_search"); ok {
160+
req.DNSSearch = expandStringsPtr(dnsSearch)
161+
}
162+
163+
if dsnLocalName, ok := d.GetOk("dns_local_name"); ok {
164+
req.DNSLocalName = expandStringPtr(dsnLocalName)
147165
}
148166

149167
if address, ok := d.GetOk("address"); ok {
@@ -201,8 +219,8 @@ func resourceScalewayVPCPublicGatewayDHCPRead(ctx context.Context, d *schema.Res
201219
_ = d.Set("address", dhcp.Address.String())
202220
_ = d.Set("created_at", dhcp.CreatedAt.Format(time.RFC3339))
203221
_ = d.Set("dns_local_name", dhcp.DNSLocalName)
204-
_ = d.Set("dns_search", dhcp.DNSSearch)
205-
_ = d.Set("dns_server_override", dhcp.DNSServersOverride)
222+
_ = d.Set("dns_search", flattenSliceString(dhcp.DNSSearch))
223+
_ = d.Set("dns_server_override", flattenSliceString(dhcp.DNSServersOverride))
206224
_ = d.Set("enable_dynamic", dhcp.EnableDynamic)
207225
_ = d.Set("organization_id", dhcp.OrganizationID)
208226
_ = d.Set("pool_high", dhcp.PoolLow.String())
@@ -226,14 +244,14 @@ func resourceScalewayVPCPublicGatewayDHCPUpdate(ctx context.Context, d *schema.R
226244
return diag.FromErr(err)
227245
}
228246

229-
if d.HasChangesExcept("enable_dynamic", "push_default_route", "push_dns_servers", "dns_servers_override",
247+
if d.HasChangesExcept("enable_dynamic", "push_default_route", "push_dns_server", "dns_servers_override",
230248
"dns_search", "dns_local_name") {
231249
req := &vpcgw.UpdateDHCPRequest{
232250
DHCPID: ID,
233251
Zone: zone,
234252
EnableDynamic: expandBoolPtr(d.Get("enable_dynamic")),
235253
PushDefaultRoute: expandBoolPtr(d.Get("push_default_route")),
236-
PushDNSServer: expandBoolPtr(d.Get("push_dns_servers")),
254+
PushDNSServer: expandBoolPtr(d.Get("push_dns_server")),
237255
DNSServersOverride: expandStringsPtr(d.Get("dns_servers_override")),
238256
DNSSearch: expandStringsPtr(d.Get("dns_search")),
239257
DNSLocalName: expandStringPtr(d.Get("dns_local_name")),
@@ -245,7 +263,7 @@ func resourceScalewayVPCPublicGatewayDHCPUpdate(ctx context.Context, d *schema.R
245263
}
246264
}
247265

248-
return resourceScalewayVPCPublicGatewayRead(ctx, d, meta)
266+
return resourceScalewayVPCPublicGatewayDHCPRead(ctx, d, meta)
249267
}
250268

251269
func resourceScalewayVPCPublicGatewayDHCPDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {

scaleway/resource_vpc_public_gateway_dhcp_test.go

+17-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,22 @@ func TestAccScalewayVPCPublicGatewayDHCP_Basic(t *testing.T) {
2626
`,
2727
Check: resource.ComposeTestCheckFunc(
2828
testAccCheckScalewayVPCPublicGatewayDHCPExists(tt, "scaleway_vpc_public_gateway_dhcp.main"),
29+
resource.TestCheckResourceAttr("scaleway_vpc_public_gateway_dhcp.main", "subnet", "192.168.1.0/24"),
30+
resource.TestCheckResourceAttr("scaleway_vpc_public_gateway_dhcp.main", "enable_dynamic", "true"),
31+
resource.TestCheckResourceAttr("scaleway_vpc_public_gateway_dhcp.main", "valid_lifetime", "3600"),
32+
resource.TestCheckResourceAttr("scaleway_vpc_public_gateway_dhcp.main", "renew_timer", "3000"),
33+
resource.TestCheckResourceAttr("scaleway_vpc_public_gateway_dhcp.main", "rebind_timer", "3060"),
34+
resource.TestCheckResourceAttr("scaleway_vpc_public_gateway_dhcp.main", "push_default_route", "true"),
35+
resource.TestCheckResourceAttr("scaleway_vpc_public_gateway_dhcp.main", "push_dns_server", "true"),
36+
resource.TestCheckResourceAttr("scaleway_vpc_public_gateway_dhcp.main", "dns_server_override.#", "0"),
37+
resource.TestCheckResourceAttr("scaleway_vpc_public_gateway_dhcp.main", "dns_search.#", "0"),
38+
resource.TestCheckResourceAttrSet("scaleway_vpc_public_gateway_dhcp.main", "dns_local_name"),
39+
resource.TestCheckResourceAttrSet("scaleway_vpc_public_gateway_dhcp.main", "pool_low"),
40+
resource.TestCheckResourceAttrSet("scaleway_vpc_public_gateway_dhcp.main", "pool_high"),
41+
resource.TestCheckResourceAttrSet("scaleway_vpc_public_gateway_dhcp.main", "created_at"),
42+
resource.TestCheckResourceAttrSet("scaleway_vpc_public_gateway_dhcp.main", "updated_at"),
43+
resource.TestCheckResourceAttrSet("scaleway_vpc_public_gateway_dhcp.main", "zone"),
44+
resource.TestCheckResourceAttrSet("scaleway_vpc_public_gateway_dhcp.main", "organization_id"),
2945
),
3046
},
3147
},
@@ -49,7 +65,7 @@ func testAccCheckScalewayVPCPublicGatewayDHCPExists(tt *TestTools, n string) res
4965
Zone: zone,
5066
})
5167

52-
if err != nil {
68+
if err != nil {
5369
return err
5470
}
5571

0 commit comments

Comments
 (0)