Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 35 additions & 2 deletions docs/resources/vpc_gateway_network.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,17 +62,49 @@ resource scaleway_vpc_gateway_network main {
}
```

### Create a gateway network with IPAM config

```hcl
resource scaleway_vpc vpc01 {
name = "my vpc"
}

resource scaleway_vpc_private_network pn01 {
name = "pn_test_network"
ipv4_subnet {
subnet = "172.16.64.0/22"
}
vpc_id = scaleway_vpc.vpc01.id
}

resource scaleway_vpc_public_gateway pg01 {
name = "foobar"
type = "VPC-GW-S"
}

resource scaleway_vpc_gateway_network main {
gateway_id = scaleway_vpc_public_gateway.pg01.id
private_network_id = scaleway_vpc_private_network.pn01.id
enable_masquerade = true
ipam_config {
push_default_route = true
}
}
```

## Arguments Reference

The following arguments are supported:

- `gateway_id` - (Required) The ID of the public gateway.
- `private_network_id` - (Required) The ID of the private network.
- `dhcp_id` - (Required) The ID of the public gateway DHCP config. Only one of `dhcp_id` and `static_address` should be specified.
- `dhcp_id` - (Required) The ID of the public gateway DHCP config. Only one of `dhcp_id`, `static_address` and `ipam_config` should be specified.
- `enable_masquerade` - (Defaults to true) Enable masquerade on this network
- `enable_dhcp` - (Defaults to true) Enable DHCP config on this network. It requires DHCP id.
- `cleanup_dhcp` - (Defaults to false) Remove DHCP config on this network on destroy. It requires DHCP id.
- `static_address` - Enable DHCP config on this network. Only one of `dhcp_id` and `static_address` should be specified.
- `static_address` - Enable DHCP config on this network. Only one of `dhcp_id`, `static_address` and `ipam_config` should be specified.
- `ipam_config` - Auto-configure the Gateway Network using Scaleway's IPAM (IP address management service).
- `push_default_route` - Defines whether the default route is enabled on that Gateway Network. Only one of `dhcp_id`, `static_address` and `ipam_config` should be specified.
- `zone` - (Defaults to [provider](../index.md#zone) `zone`) The [zone](../guides/regions_and_zones.md#zones) in which the gateway network should be created.

## Attributes Reference
Expand All @@ -86,6 +118,7 @@ In addition to all above arguments, the following attributes are exported:
- `mac_address` - The mac address of the creation of the gateway network.
- `created_at` - The date and time of the creation of the gateway network.
- `updated_at` - The date and time of the last update of the gateway network.
- `status` - The status of the Public Gateway's connection to the Private Network.

## Import

Expand Down
10 changes: 10 additions & 0 deletions scaleway/helpers_vpcgw.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,13 @@ func retryUpdateGatewayReverseDNS(ctx context.Context, api *vpcgw.API, req *vpcg
}
}
}

func expandIpamConfig(raw interface{}) *vpcgw.IpamConfig {
if raw == nil || len(raw.([]interface{})) != 1 {
return nil
}
rawMap := raw.([]interface{})[0].(map[string]interface{})
return &vpcgw.IpamConfig{
PushDefaultRoute: rawMap["push_default_route"].(bool),
}
}
61 changes: 47 additions & 14 deletions scaleway/resource_vpc_gateway_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func resourceScalewayVPCGatewayNetwork() *schema.Resource {
Optional: true,
ValidateFunc: validationUUIDorUUIDWithLocality(),
Description: "The ID of the public gateway DHCP config",
ConflictsWith: []string{"static_address"},
ConflictsWith: []string{"static_address", "ipam_config"},
},
"enable_masquerade": {
Type: schema.TypeBool,
Expand All @@ -72,8 +72,24 @@ func resourceScalewayVPCGatewayNetwork() *schema.Resource {
Type: schema.TypeString,
Description: "The static IP address in CIDR on this network",
Optional: true,
Computed: true,
ValidateFunc: validation.IsCIDR,
ConflictsWith: []string{"dhcp_id"},
ConflictsWith: []string{"dhcp_id", "ipam_config"},
},
"ipam_config": {
Type: schema.TypeList,
Optional: true,
Description: "Auto-configure the Gateway Network using Scaleway's IPAM (IP address management service)",
ConflictsWith: []string{"dhcp_id", "static_address"},
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"push_default_route": {
Type: schema.TypeBool,
Optional: true,
Description: "Defines whether the default route is enabled on that Gateway Network",
},
},
},
},
// Computed elements
"mac_address": {
Expand All @@ -91,6 +107,11 @@ func resourceScalewayVPCGatewayNetwork() *schema.Resource {
Computed: true,
Description: "The date and time of the last update of the gateway network",
},
"status": {
Type: schema.TypeString,
Computed: true,
Description: "The status of the Public Gateway's connection to the Private Network",
},
"zone": zoneSchema(),
},
CustomizeDiff: customizeDiffLocalityCheck("gateway_id", "private_network_id", "dhcp_id"),
Expand All @@ -116,6 +137,7 @@ func resourceScalewayVPCGatewayNetworkCreate(ctx context.Context, d *schema.Reso
PrivateNetworkID: expandRegionalID(d.Get("private_network_id").(string)).ID,
EnableMasquerade: *expandBoolPtr(d.Get("enable_masquerade")),
EnableDHCP: expandBoolPtr(d.Get("enable_dhcp")),
IpamConfig: expandIpamConfig(d.Get("ipam_config")),
}
staticAddress, staticAddressExist := d.GetOk("static_address")
if staticAddressExist {
Expand Down Expand Up @@ -219,6 +241,7 @@ func resourceScalewayVPCGatewayNetworkRead(ctx context.Context, d *schema.Resour
_ = d.Set("created_at", gatewayNetwork.CreatedAt.Format(time.RFC3339))
_ = d.Set("updated_at", gatewayNetwork.UpdatedAt.Format(time.RFC3339))
_ = d.Set("zone", zone.String())
_ = d.Set("status", gatewayNetwork.Status.String())

return nil
}
Expand All @@ -234,15 +257,25 @@ func resourceScalewayVPCGatewayNetworkUpdate(ctx context.Context, d *schema.Reso
return diag.FromErr(err)
}

if d.HasChanges("enable_masquerade", "dhcp_id", "enable_dhcp", "static_address") {
updateRequest := &vpcgw.UpdateGatewayNetworkRequest{
GatewayNetworkID: ID,
Zone: zone,
}

if d.HasChange("enable_masquerade") {
updateRequest.EnableMasquerade = expandBoolPtr(d.Get("enable_masquerade"))
}
if d.HasChange("enable_dhcp") {
updateRequest.EnableDHCP = expandBoolPtr(d.Get("enable_dhcp"))
}
if d.HasChange("dhcp_id") {
dhcpID := expandZonedID(d.Get("dhcp_id").(string)).ID
updateRequest := &vpcgw.UpdateGatewayNetworkRequest{
GatewayNetworkID: ID,
Zone: zone,
EnableMasquerade: expandBoolPtr(d.Get("enable_masquerade")),
EnableDHCP: expandBoolPtr(d.Get("enable_dhcp")),
DHCPID: &dhcpID,
}
updateRequest.DHCPID = &dhcpID
}
if d.HasChange("ipam_config") {
updateRequest.IpamConfig = expandIpamConfig(d.Get("ipam_config"))
}
if d.HasChange("static_address") {
staticAddress, staticAddressExist := d.GetOk("static_address")
if staticAddressExist {
address, err := expandIPNet(staticAddress.(string))
Expand All @@ -251,11 +284,11 @@ func resourceScalewayVPCGatewayNetworkUpdate(ctx context.Context, d *schema.Reso
}
updateRequest.Address = &address
}
}

_, err = vpcgwAPI.UpdateGatewayNetwork(updateRequest, scw.WithContext(ctx))
if err != nil {
return diag.FromErr(err)
}
_, err = vpcgwAPI.UpdateGatewayNetwork(updateRequest, scw.WithContext(ctx))
if err != nil {
return diag.FromErr(err)
}

_, err = waitForVPCGatewayNetwork(ctx, vpcgwAPI, zone, ID, d.Timeout(schema.TimeoutUpdate))
Expand Down
55 changes: 55 additions & 0 deletions scaleway/resource_vpc_gateway_network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,61 @@ func TestAccScalewayVPCGatewayNetwork_WithoutDHCP(t *testing.T) {
})
}

func TestAccScalewayVPCGatewayNetwork_WithIPAMConfig(t *testing.T) {
tt := NewTestTools(t)
defer tt.Cleanup()
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: tt.ProviderFactories,
CheckDestroy: testAccCheckScalewayVPCGatewayNetworkDestroy(tt),
Steps: []resource.TestStep{
{
Config: `
resource scaleway_vpc vpc01 {
name = "my vpc"
}

resource scaleway_vpc_private_network pn01 {
name = "pn_test_network"
ipv4_subnet {
subnet = "172.16.64.0/22"
}
vpc_id = scaleway_vpc.vpc01.id
}

resource scaleway_vpc_public_gateway pg01 {
name = "foobar"
type = "VPC-GW-S"
}

resource scaleway_vpc_gateway_network main {
gateway_id = scaleway_vpc_public_gateway.pg01.id
private_network_id = scaleway_vpc_private_network.pn01.id
enable_masquerade = true
ipam_config {
push_default_route = true
}
}
`,
Check: resource.ComposeTestCheckFunc(
testAccCheckScalewayVPCGatewayNetworkExists(tt, "scaleway_vpc_gateway_network.main"),
resource.TestCheckResourceAttrSet("scaleway_vpc_gateway_network.main", "gateway_id"),
resource.TestCheckResourceAttrSet("scaleway_vpc_gateway_network.main", "private_network_id"),
resource.TestCheckResourceAttrSet("scaleway_vpc_gateway_network.main", "mac_address"),
resource.TestCheckResourceAttrSet("scaleway_vpc_gateway_network.main", "created_at"),
resource.TestCheckResourceAttrSet("scaleway_vpc_gateway_network.main", "updated_at"),
resource.TestCheckResourceAttrSet("scaleway_vpc_gateway_network.main", "status"),
resource.TestCheckResourceAttrSet("scaleway_vpc_gateway_network.main", "zone"),
resource.TestCheckResourceAttrSet("scaleway_vpc_gateway_network.main", "static_address"),
resource.TestCheckResourceAttr("scaleway_vpc_gateway_network.main", "ipam_config.0.push_default_route", "true"),
resource.TestCheckResourceAttr("scaleway_vpc_gateway_network.main", "enable_dhcp", "true"),
resource.TestCheckResourceAttr("scaleway_vpc_gateway_network.main", "enable_masquerade", "true"),
),
},
},
})
}

func testAccCheckScalewayVPCGatewayNetworkExists(tt *TestTools, n string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
Expand Down
Loading