Skip to content

Commit 0d31176

Browse files
committed
feat(instance): add support for tags in ip (scaleway#1130)
1 parent d1efe0c commit 0d31176

File tree

4 files changed

+653
-0
lines changed

4 files changed

+653
-0
lines changed

docs/resources/instance_ip.md

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ In addition to all above arguments, the following attributes are exported:
2929
- `address` - The IP address.
3030
- `reverse` - The reverse dns attached to this IP
3131
- `organization_id` - The organization ID the IP is associated with.
32+
- `tags` - The tags associated with the IP.
3233

3334
## Import
3435

scaleway/resource_instance_ip.go

+33
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ func resourceScalewayInstanceIP() *schema.Resource {
1313
return &schema.Resource{
1414
CreateContext: resourceScalewayInstanceIPCreate,
1515
ReadContext: resourceScalewayInstanceIPRead,
16+
UpdateContext: resourceScalewayInstanceIPUpdate,
1617
DeleteContext: resourceScalewayInstanceIPDelete,
1718
Importer: &schema.ResourceImporter{
1819
StateContext: schema.ImportStatePassthroughContext,
@@ -37,6 +38,14 @@ func resourceScalewayInstanceIP() *schema.Resource {
3738
Computed: true,
3839
Description: "The server associated with this IP",
3940
},
41+
"tags": {
42+
Type: schema.TypeList,
43+
Elem: &schema.Schema{
44+
Type: schema.TypeString,
45+
},
46+
Optional: true,
47+
Description: "The tags associated with the ip",
48+
},
4049
"zone": zoneSchema(),
4150
"organization_id": organizationIDSchema(),
4251
"project_id": projectIDSchema(),
@@ -53,6 +62,7 @@ func resourceScalewayInstanceIPCreate(ctx context.Context, d *schema.ResourceDat
5362
res, err := instanceAPI.CreateIP(&instance.CreateIPRequest{
5463
Zone: zone,
5564
Project: expandStringPtr(d.Get("project_id")),
65+
Tags: expandStrings(d.Get("tags")),
5666
}, scw.WithContext(ctx))
5767
if err != nil {
5868
return diag.FromErr(err)
@@ -62,6 +72,28 @@ func resourceScalewayInstanceIPCreate(ctx context.Context, d *schema.ResourceDat
6272
return resourceScalewayInstanceIPRead(ctx, d, meta)
6373
}
6474

75+
func resourceScalewayInstanceIPUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
76+
instanceAPI, zone, ID, err := instanceAPIWithZoneAndID(meta, d.Id())
77+
if err != nil {
78+
return diag.FromErr(err)
79+
}
80+
req := &instance.UpdateIPRequest{
81+
IP: ID,
82+
Zone: zone,
83+
}
84+
85+
if d.HasChange("tags") {
86+
req.Tags = scw.StringsPtr(expandStrings(d.Get("tags")))
87+
}
88+
89+
_, err = instanceAPI.UpdateIP(req, scw.WithContext(ctx))
90+
if err != nil {
91+
return diag.FromErr(err)
92+
}
93+
94+
return resourceScalewayInstanceIPRead(ctx, d, meta)
95+
}
96+
6597
func resourceScalewayInstanceIPRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
6698
instanceAPI, zone, ID, err := instanceAPIWithZoneAndID(meta, d.Id())
6799
if err != nil {
@@ -87,6 +119,7 @@ func resourceScalewayInstanceIPRead(ctx context.Context, d *schema.ResourceData,
87119
_ = d.Set("organization_id", res.IP.Organization)
88120
_ = d.Set("project_id", res.IP.Project)
89121
_ = d.Set("reverse", res.IP.Reverse)
122+
_ = d.Set("tags", res.IP.Tags)
90123

91124
if res.IP.Server != nil {
92125
_ = d.Set("server_id", newZonedIDString(res.IP.Zone, res.IP.Server.ID))

scaleway/resource_instance_ip_test.go

+42
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,48 @@ func TestAccScalewayInstanceIP_WithZone(t *testing.T) {
9393
})
9494
}
9595

96+
func TestAccScalewayInstanceIP_Tags(t *testing.T) {
97+
tt := NewTestTools(t)
98+
defer tt.Cleanup()
99+
resource.ParallelTest(t, resource.TestCase{
100+
ProviderFactories: tt.ProviderFactories,
101+
CheckDestroy: testAccCheckScalewayInstanceIPDestroy(tt),
102+
Steps: []resource.TestStep{
103+
{
104+
Config: `
105+
resource "scaleway_instance_ip" "main" {}
106+
`,
107+
Check: resource.ComposeTestCheckFunc(
108+
testAccCheckScalewayInstanceIPExists(tt, "scaleway_instance_ip.main"),
109+
resource.TestCheckNoResourceAttr("scaleway_instance_ip.main", "tags"),
110+
),
111+
},
112+
{
113+
Config: `
114+
resource "scaleway_instance_ip" "main" {
115+
tags = ["foo", "bar"]
116+
}
117+
`,
118+
Check: resource.ComposeTestCheckFunc(
119+
testAccCheckScalewayInstanceIPExists(tt, "scaleway_instance_ip.main"),
120+
resource.TestCheckResourceAttr("scaleway_instance_ip.main", "tags.0", "foo"),
121+
resource.TestCheckResourceAttr("scaleway_instance_ip.main", "tags.1", "bar"),
122+
),
123+
},
124+
{
125+
Config: `
126+
resource "scaleway_instance_ip" "main" {
127+
}
128+
`,
129+
Check: resource.ComposeTestCheckFunc(
130+
resource.TestCheckNoResourceAttr("scaleway_instance_ip.main", "tags"),
131+
testAccCheckScalewayInstanceIPExists(tt, "scaleway_instance_ip.main"),
132+
),
133+
},
134+
},
135+
})
136+
}
137+
96138
func testAccCheckScalewayInstanceIPExists(tt *TestTools, name string) resource.TestCheckFunc {
97139
return func(s *terraform.State) error {
98140
rs, ok := s.RootModule().Resources[name]

0 commit comments

Comments
 (0)