Skip to content

Commit 27b090c

Browse files
remyleonescaleway-bot
authored andcommitted
feat(vpcgw): add support for vpc public gateway ip (scaleway#863)
1 parent b2974a8 commit 27b090c

13 files changed

+1857
-116
lines changed
+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
---
2+
page_title: "Scaleway: scaleway_vpc_public_gateway_ip"
3+
description: |-
4+
Get information about Scaleway VPC Public Gateway IPs.
5+
---
6+
7+
# scaleway_vpc_public_gateway_ip
8+
9+
Gets information about a public gateway IP.
10+
11+
## Example Usage
12+
13+
```hcl
14+
resource "scaleway_vpc_public_gateway_ip" "main" {
15+
}
16+
17+
data "scaleway_vpc_public_gateway_ip" "ip_by_id" {
18+
ip_id = "${scaleway_vpc_public_gateway_ip.main.id}"
19+
}
20+
```
21+
22+
## Argument Reference
23+
24+
## Attributes Reference
25+
26+
`id` is set to the ID of the found public gateway ip. Addition attributes are
27+
exported.
+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
---
2+
page_title: "Scaleway: scaleway_vpc_public_gateway_ip"
3+
description: |-
4+
Manages Scaleway VPC Public Gateways IP.
5+
---
6+
7+
# scaleway_vpc_public_gateway_ip
8+
9+
Creates and manages Scaleway VPC Public Gateway IP.
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_ip" "main" {
16+
reverse = "example.com"
17+
tags = ["demo", "terraform"]
18+
}
19+
```
20+
21+
## Arguments Reference
22+
23+
The following arguments are supported:
24+
25+
- `reverse` - (Optional) The reverse domain name for the IP address
26+
- `tags` - (Optional) The tags associated with the public gateway IP.
27+
- `zone` - (Defaults to [provider](../index.md#zone) `zone`) The [zone](../guides/regions_and_zones.md#zones) in which the public gateway ip should be created.
28+
- `project_id` - (Defaults to [provider](../index.md#project_id) `project_id`) The ID of the project the public gateway ip is associated with.
29+
30+
## Attributes Reference
31+
32+
In addition to all above arguments, the following attributes are exported:
33+
34+
- `id` - The ID of the public gateway ip.
35+
- `address` - The IP address itself.
36+
- `organization_id` - The organization ID the public gateway ip is associated with.
37+
- `created_at` - The date and time of the creation of the public gateway ip.
38+
- `updated_at` - The date and time of the last update of the public gateway ip.
39+
40+
## Import
41+
42+
Public gateway can be imported using the `{zone}/{id}`, e.g.
43+
44+
```bash
45+
$ terraform import scaleway_vpc_public_gateway_ip.main fr-par-1/11111111-1111-1111-1111-111111111111
46+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package scaleway
2+
3+
import (
4+
"context"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
8+
)
9+
10+
func dataSourceScalewayVPCPublicGatewayIP() *schema.Resource {
11+
// Generate datasource schema from resource
12+
dsSchema := datasourceSchemaFromResourceSchema(resourceScalewayVPCPublicGatewayIP().Schema)
13+
14+
dsSchema["ip_id"] = &schema.Schema{
15+
Type: schema.TypeString,
16+
Optional: true,
17+
Description: "The ID of the IP",
18+
ValidateFunc: validationUUIDorUUIDWithLocality(),
19+
}
20+
21+
return &schema.Resource{
22+
Schema: dsSchema,
23+
ReadContext: dataSourceScalewayVPCPublicGatewayIPRead,
24+
}
25+
}
26+
27+
func dataSourceScalewayVPCPublicGatewayIPRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
28+
_, zone, err := vpcgwAPIWithZone(d, meta)
29+
if err != nil {
30+
return diag.FromErr(err)
31+
}
32+
33+
ipID, _ := d.GetOk("ip_id")
34+
35+
zonedID := datasourceNewZonedID(ipID, zone)
36+
d.SetId(zonedID)
37+
_ = d.Set("ip_id", zonedID)
38+
return resourceScalewayVPCPublicGatewayIPRead(ctx, d, meta)
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package scaleway
2+
3+
import (
4+
"testing"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
7+
)
8+
9+
func TestAccScalewayDataSourceVPCPublicGatewayIP_Basic(t *testing.T) {
10+
tt := NewTestTools(t)
11+
defer tt.Cleanup()
12+
13+
resource.ParallelTest(t, resource.TestCase{
14+
PreCheck: func() { testAccPreCheck(t) },
15+
ProviderFactories: tt.ProviderFactories,
16+
CheckDestroy: testAccCheckScalewayVPCPublicGatewayIPDestroy(tt),
17+
Steps: []resource.TestStep{
18+
{
19+
Config: `
20+
resource "scaleway_vpc_public_gateway_ip" "main" {
21+
}`,
22+
},
23+
{
24+
Config: `
25+
resource "scaleway_vpc_public_gateway_ip" "main" {
26+
}
27+
28+
data "scaleway_vpc_public_gateway_ip" "ip_by_id" {
29+
ip_id = "${scaleway_vpc_public_gateway_ip.main.id}"
30+
}
31+
`,
32+
Check: resource.ComposeTestCheckFunc(
33+
testAccCheckScalewayVPCPublicGatewayIPExists(tt, "scaleway_vpc_public_gateway_ip.main"),
34+
resource.TestCheckResourceAttrPair(
35+
"data.scaleway_vpc_public_gateway_ip.ip_by_id", "ip_id",
36+
"scaleway_vpc_public_gateway_ip.main", "id"),
37+
),
38+
},
39+
},
40+
})
41+
}

scaleway/provider.go

+2
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ func Provider(config *ProviderConfig) plugin.ProviderFunc {
8787
"scaleway_object_bucket": resourceScalewayObjectBucket(),
8888
"scaleway_vpc_public_gateway": resourceScalewayVPCPublicGateway(),
8989
"scaleway_vpc_public_gateway_pat_rule": resourceScalewayVPCPublicGatewayPATRule(),
90+
"scaleway_vpc_public_gateway_ip": resourceScalewayVPCPublicGatewayIP(),
9091
"scaleway_vpc_private_network": resourceScalewayVPCPrivateNetwork(),
9192
},
9293

@@ -108,6 +109,7 @@ func Provider(config *ProviderConfig) plugin.ProviderFunc {
108109
"scaleway_registry_namespace": dataSourceScalewayRegistryNamespace(),
109110
"scaleway_registry_image": dataSourceScalewayRegistryImage(),
110111
"scaleway_vpc_public_gateway": dataSourceScalewayVPCPublicGateway(),
112+
"scaleway_vpc_public_gateway_ip": dataSourceScalewayVPCPublicGatewayIP(),
111113
"scaleway_vpc_private_network": dataSourceScalewayVPCPrivateNetwork(),
112114
},
113115
}

scaleway/resource_vpc_public_gateway.go

+10-6
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,11 @@ func resourceScalewayVPCPublicGateway() *schema.Resource {
4242
},
4343
},
4444
"ip_id": {
45-
Type: schema.TypeString,
46-
Optional: true,
47-
Computed: true,
48-
Description: "attach an existing IP to the gateway",
45+
Type: schema.TypeString,
46+
Computed: true,
47+
Optional: true,
48+
Description: "attach an existing IP to the gateway",
49+
DiffSuppressFunc: diffSuppressFuncLocality,
4950
},
5051
"tags": {
5152
Type: schema.TypeList,
@@ -84,11 +85,14 @@ func resourceScalewayVPCPublicGatewayCreate(ctx context.Context, d *schema.Resou
8485
Type: d.Get("type").(string),
8586
Tags: expandStrings(d.Get("tags")),
8687
UpstreamDNSServers: expandStrings(d.Get("upstream_dns_servers")),
87-
IPID: expandStringPtr(d.Get("ip_id").(string)),
8888
ProjectID: d.Get("project_id").(string),
8989
Zone: zone,
9090
}
9191

92+
if ipID, ok := d.GetOk("ip_id"); ok {
93+
req.IPID = expandStringPtr(expandZonedID(ipID).ID)
94+
}
95+
9296
res, err := vpcgwAPI.CreateGateway(req, scw.WithContext(ctx))
9397
if err != nil {
9498
return diag.FromErr(err)
@@ -136,7 +140,7 @@ func resourceScalewayVPCPublicGatewayRead(ctx context.Context, d *schema.Resourc
136140
_ = d.Set("zone", zone)
137141
_ = d.Set("tags", gateway.Tags)
138142
_ = d.Set("upstream_dns_servers", gateway.UpstreamDNSServers)
139-
_ = d.Set("ip_id", gateway.IP.ID)
143+
_ = d.Set("ip_id", newZonedID(zone, gateway.IP.ID).String())
140144

141145
return nil
142146
}
+150
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
package scaleway
2+
3+
import (
4+
"context"
5+
"time"
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 resourceScalewayVPCPublicGatewayIP() *schema.Resource {
14+
return &schema.Resource{
15+
CreateContext: resourceScalewayVPCPublicGatewayIPCreate,
16+
ReadContext: resourceScalewayVPCPublicGatewayIPRead,
17+
UpdateContext: resourceScalewayVPCPublicGatewayIPUpdate,
18+
DeleteContext: resourceScalewayVPCPublicGatewayIPDelete,
19+
Importer: &schema.ResourceImporter{
20+
StateContext: schema.ImportStatePassthroughContext,
21+
},
22+
SchemaVersion: 0,
23+
Schema: map[string]*schema.Schema{
24+
"address": {
25+
Type: schema.TypeString,
26+
Description: "the IP itself",
27+
Computed: true,
28+
},
29+
"tags": {
30+
Type: schema.TypeList,
31+
Optional: true,
32+
Description: "The tags associated with public gateway IP",
33+
Elem: &schema.Schema{
34+
Type: schema.TypeString,
35+
},
36+
},
37+
"reverse": {
38+
Type: schema.TypeString,
39+
Optional: true,
40+
Description: "reverse domain name for the IP address",
41+
},
42+
"project_id": projectIDSchema(),
43+
"zone": zoneSchema(),
44+
// Computed elements
45+
"organization_id": organizationIDSchema(),
46+
"created_at": {
47+
Type: schema.TypeString,
48+
Computed: true,
49+
Description: "The date and time of the creation of the public gateway IP",
50+
},
51+
"updated_at": {
52+
Type: schema.TypeString,
53+
Computed: true,
54+
Description: "The date and time of the last update of the public gateway IP",
55+
},
56+
},
57+
}
58+
}
59+
60+
func resourceScalewayVPCPublicGatewayIPCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
61+
vpcgwAPI, zone, err := vpcgwAPIWithZone(d, meta)
62+
if err != nil {
63+
return diag.FromErr(err)
64+
}
65+
66+
req := &vpcgw.CreateIPRequest{
67+
Tags: expandStrings(d.Get("tags")),
68+
ProjectID: d.Get("project_id").(string),
69+
Zone: zone,
70+
}
71+
72+
res, err := vpcgwAPI.CreateIP(req, scw.WithContext(ctx))
73+
if err != nil {
74+
return diag.FromErr(err)
75+
}
76+
77+
d.SetId(newZonedIDString(zone, res.ID))
78+
79+
return resourceScalewayVPCPublicGatewayIPRead(ctx, d, meta)
80+
}
81+
82+
func resourceScalewayVPCPublicGatewayIPRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
83+
vpcgwAPI, zone, ID, err := vpcgwAPIWithZoneAndID(meta, d.Id())
84+
if err != nil {
85+
return diag.FromErr(err)
86+
}
87+
88+
ip, err := vpcgwAPI.GetIP(&vpcgw.GetIPRequest{
89+
IPID: ID,
90+
Zone: zone,
91+
}, scw.WithContext(ctx))
92+
if err != nil {
93+
if is404Error(err) {
94+
d.SetId("")
95+
return nil
96+
}
97+
return diag.FromErr(err)
98+
}
99+
100+
_ = d.Set("organization_id", ip.OrganizationID)
101+
_ = d.Set("project_id", ip.ProjectID)
102+
_ = d.Set("created_at", ip.CreatedAt.Format(time.RFC3339))
103+
_ = d.Set("updated_at", ip.UpdatedAt.Format(time.RFC3339))
104+
_ = d.Set("zone", zone)
105+
_ = d.Set("tags", ip.Tags)
106+
_ = d.Set("reverse", ip.Reverse)
107+
108+
return nil
109+
}
110+
111+
func resourceScalewayVPCPublicGatewayIPUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
112+
vpcgwAPI, zone, ID, err := vpcgwAPIWithZoneAndID(meta, d.Id())
113+
if err != nil {
114+
return diag.FromErr(err)
115+
}
116+
117+
if d.HasChanges("tags", "reverse") {
118+
updateRequest := &vpcgw.UpdateIPRequest{
119+
IPID: ID,
120+
Zone: zone,
121+
Tags: scw.StringsPtr(expandStrings(d.Get("tags"))),
122+
Reverse: expandStringPtr(d.Get("reverse").(string)),
123+
}
124+
125+
_, err = vpcgwAPI.UpdateIP(updateRequest, scw.WithContext(ctx))
126+
if err != nil {
127+
return diag.FromErr(err)
128+
}
129+
}
130+
131+
return resourceScalewayVPCPublicGatewayIPRead(ctx, d, meta)
132+
}
133+
134+
func resourceScalewayVPCPublicGatewayIPDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
135+
vpcgwAPI, zone, ID, err := vpcgwAPIWithZoneAndID(meta, d.Id())
136+
if err != nil {
137+
return diag.FromErr(err)
138+
}
139+
140+
err = vpcgwAPI.DeleteIP(&vpcgw.DeleteIPRequest{
141+
IPID: ID,
142+
Zone: zone,
143+
}, scw.WithContext(ctx))
144+
145+
if err != nil && !is404Error(err) {
146+
return diag.FromErr(err)
147+
}
148+
149+
return nil
150+
}

0 commit comments

Comments
 (0)