Skip to content

Commit 2865bf2

Browse files
authored
feat(vpcgw): add support for vpc public gateway (scaleway#862)
1 parent 996420c commit 2865bf2

10 files changed

+1886
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
---
2+
page_title: "Scaleway: scaleway_vpc_public_gateway"
3+
description: |-
4+
Get information about Scaleway VPC Public Gateways.
5+
---
6+
7+
# scaleway_vpc_public_gateway
8+
9+
Gets information about a public gateway.
10+
11+
## Example Usage
12+
13+
```hcl
14+
resource "scaleway_vpc_public_gateway" "main" {
15+
name = "demo"
16+
type = "VPC-GW-S"
17+
}
18+
19+
data "scaleway_vpc_public_gateway" "pg_test_by_name" {
20+
name = "${scaleway_vpc_public_gateway.main.name}"
21+
}
22+
23+
data "scaleway_vpc_public_gateway" "pg_test_by_id" {
24+
public_gateway_id = "${scaleway_vpc_public_gateway.main.id}"
25+
}
26+
```
27+
28+
## Argument Reference
29+
30+
* `name` - (Required) Exact name of the public gateway.
31+
32+
## Attributes Reference
33+
34+
`id` is set to the ID of the found public gateway. Addition attributes are
35+
exported.

docs/resources/vpc_public_gateway.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
---
2+
page_title: "Scaleway: scaleway_vpc_public_gateway"
3+
description: |-
4+
Manages Scaleway VPC Public Gateways.
5+
---
6+
7+
# scaleway_vpc_public_gateway
8+
9+
Creates and manages Scaleway VPC Public Gateway.
10+
For more information, see [the documentation](https://developers.scaleway.com/en/products/vpc-gw/api/).
11+
12+
## Example
13+
14+
```hcl
15+
resource "scaleway_vpc_public_gateway" "main" {
16+
name = "public_gateway_demo"
17+
type = "VPC-GW-S"
18+
tags = ["demo", "terraform"]
19+
}
20+
```
21+
22+
## Arguments Reference
23+
24+
The following arguments are supported:
25+
26+
- `type` - (Required) The gateway type.
27+
- `name` - (Optional) The name of the public gateway. If not provided it will be randomly generated.
28+
- `tags` - (Optional) The tags associated with the public gateway.
29+
- `zone` - (Defaults to [provider](../index.md#zone) `zone`) The [zone](../guides/regions_and_zones.md#zones) in which the public gateway should be created.
30+
- `project_id` - (Defaults to [provider](../index.md#project_id) `project_id`) The ID of the project the public gateway is associated with.
31+
- `upstream_dns_servers` - (Optional) override the gateway's default recursive DNS servers, if DNS features are enabled.
32+
- `ip_id` - (Optional) attach an existing flexible IP to the gateway
33+
34+
## Attributes Reference
35+
36+
In addition to all above arguments, the following attributes are exported:
37+
38+
- `id` - The ID of the public gateway.
39+
- `organization_id` - The organization ID the public gateway is associated with.
40+
- `created_at` - The date and time of the creation of the public gateway.
41+
- `updated_at` - The date and time of the last update of the public gateway.
42+
43+
## Import
44+
45+
Public gateway can be imported using the `{zone}/{id}`, e.g.
46+
47+
```bash
48+
$ terraform import scaleway_vpc_public_gateway.main fr-par-1/11111111-1111-1111-1111-111111111111
49+
```
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package scaleway
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
9+
vpcgw "github.com/scaleway/scaleway-sdk-go/api/vpcgw/v1beta1"
10+
"github.com/scaleway/scaleway-sdk-go/scw"
11+
)
12+
13+
func dataSourceScalewayVPCPublicGateway() *schema.Resource {
14+
// Generate datasource schema from resource
15+
dsSchema := datasourceSchemaFromResourceSchema(resourceScalewayVPCPublicGateway().Schema)
16+
17+
// Set 'Optional' schema elements
18+
addOptionalFieldsToSchema(dsSchema, "name")
19+
20+
dsSchema["name"].ConflictsWith = []string{"public_gateway_id"}
21+
dsSchema["public_gateway_id"] = &schema.Schema{
22+
Type: schema.TypeString,
23+
Optional: true,
24+
Description: "The ID of the public gateway",
25+
ValidateFunc: validationUUIDorUUIDWithLocality(),
26+
ConflictsWith: []string{"name"},
27+
}
28+
29+
return &schema.Resource{
30+
Schema: dsSchema,
31+
ReadContext: dataSourceScalewayVPCPublicGatewayRead,
32+
}
33+
}
34+
35+
func dataSourceScalewayVPCPublicGatewayRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
36+
vpcgwAPI, zone, err := vpcgwAPIWithZone(d, meta)
37+
if err != nil {
38+
return diag.FromErr(err)
39+
}
40+
41+
publicGatewayID, ok := d.GetOk("public_gateway_id")
42+
if !ok {
43+
res, err := vpcgwAPI.ListGateways(
44+
&vpcgw.ListGatewaysRequest{
45+
Name: expandStringPtr(d.Get("name").(string)),
46+
Zone: zone,
47+
}, scw.WithContext(ctx))
48+
if err != nil {
49+
return diag.FromErr(err)
50+
}
51+
if res.TotalCount == 0 {
52+
return diag.FromErr(
53+
fmt.Errorf(
54+
"no public gateway found with the name %s",
55+
d.Get("name"),
56+
),
57+
)
58+
}
59+
if res.TotalCount > 1 {
60+
return diag.FromErr(
61+
fmt.Errorf(
62+
"%d public gateways found with the name %s",
63+
res.TotalCount,
64+
d.Get("name"),
65+
),
66+
)
67+
}
68+
publicGatewayID = res.Gateways[0].ID
69+
}
70+
71+
zonedID := datasourceNewZonedID(publicGatewayID, zone)
72+
d.SetId(zonedID)
73+
_ = d.Set("public_gateway_id", zonedID)
74+
return resourceScalewayVPCPublicGatewayRead(ctx, d, meta)
75+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package scaleway
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
8+
)
9+
10+
func TestAccScalewayDataSourceVPCPublicGateway_Basic(t *testing.T) {
11+
tt := NewTestTools(t)
12+
defer tt.Cleanup()
13+
pgName := "TestAccScalewayDataSourceVPCPublicGateway_Basic"
14+
resource.ParallelTest(t, resource.TestCase{
15+
PreCheck: func() { testAccPreCheck(t) },
16+
ProviderFactories: tt.ProviderFactories,
17+
CheckDestroy: testAccCheckScalewayVPCPublicGatewayDestroy(tt),
18+
Steps: []resource.TestStep{
19+
{
20+
Config: fmt.Sprintf(`
21+
resource "scaleway_vpc_public_gateway" "main" {
22+
name = "%s"
23+
type = "VPC-GW-S"
24+
}`, pgName),
25+
},
26+
{
27+
Config: fmt.Sprintf(`
28+
resource "scaleway_vpc_public_gateway" "main" {
29+
name = "%s"
30+
type = "VPC-GW-S"
31+
}
32+
33+
data "scaleway_vpc_public_gateway" "pg_test_by_name" {
34+
name = "${scaleway_vpc_public_gateway.main.name}"
35+
}
36+
37+
data "scaleway_vpc_public_gateway" "pg_test_by_id" {
38+
public_gateway_id = "${scaleway_vpc_public_gateway.main.id}"
39+
}
40+
`, pgName),
41+
Check: resource.ComposeTestCheckFunc(
42+
testAccCheckScalewayVPCPublicGatewayExists(tt, "scaleway_vpc_public_gateway.main"),
43+
resource.TestCheckResourceAttrPair(
44+
"data.scaleway_vpc_public_gateway.pg_test_by_name", "name",
45+
"scaleway_vpc_public_gateway.main", "name"),
46+
resource.TestCheckResourceAttrPair(
47+
"data.scaleway_vpc_public_gateway.pg_test_by_id", "public_gateway_id",
48+
"scaleway_vpc_public_gateway.main", "id"),
49+
),
50+
},
51+
},
52+
})
53+
}

scaleway/helpers_vpcgw.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package scaleway
2+
3+
import (
4+
"time"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
7+
vpcgw "github.com/scaleway/scaleway-sdk-go/api/vpcgw/v1beta1"
8+
"github.com/scaleway/scaleway-sdk-go/scw"
9+
)
10+
11+
const (
12+
gatewayWaitForTimeout = 10 * time.Minute
13+
defaultVPCGatewayTimeout = 10 * time.Minute
14+
)
15+
16+
// vpcgwAPIWithZone returns a new VPC API and the zone for a Create request
17+
func vpcgwAPIWithZone(d *schema.ResourceData, m interface{}) (*vpcgw.API, scw.Zone, error) {
18+
meta := m.(*Meta)
19+
vpcgwAPI := vpcgw.NewAPI(meta.scwClient)
20+
21+
zone, err := extractZone(d, meta)
22+
if err != nil {
23+
return nil, "", err
24+
}
25+
return vpcgwAPI, zone, err
26+
}
27+
28+
// vpcgwAPIWithZoneAndID
29+
func vpcgwAPIWithZoneAndID(m interface{}, id string) (*vpcgw.API, scw.Zone, string, error) {
30+
meta := m.(*Meta)
31+
vpcgwAPI := vpcgw.NewAPI(meta.scwClient)
32+
33+
zone, ID, err := parseZonedID(id)
34+
if err != nil {
35+
return nil, "", "", err
36+
}
37+
return vpcgwAPI, zone, ID, err
38+
}

scaleway/provider.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ func Provider(config *ProviderConfig) plugin.ProviderFunc {
8585
"scaleway_rdb_privilege": resourceScalewayRdbPrivilege(),
8686
"scaleway_rdb_user": resourceScalewayRdbUser(),
8787
"scaleway_object_bucket": resourceScalewayObjectBucket(),
88+
"scaleway_vpc_public_gateway": resourceScalewayVPCPublicGateway(),
8889
"scaleway_vpc_private_network": resourceScalewayVPCPrivateNetwork(),
8990
},
9091

@@ -105,6 +106,7 @@ func Provider(config *ProviderConfig) plugin.ProviderFunc {
105106
"scaleway_marketplace_image": dataSourceScalewayMarketplaceImage(),
106107
"scaleway_registry_namespace": dataSourceScalewayRegistryNamespace(),
107108
"scaleway_registry_image": dataSourceScalewayRegistryImage(),
109+
"scaleway_vpc_public_gateway": dataSourceScalewayVPCPublicGateway(),
108110
"scaleway_vpc_private_network": dataSourceScalewayVPCPrivateNetwork(),
109111
},
110112
}

0 commit comments

Comments
 (0)