Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(instance): add support for tags in ip #1130

Merged
merged 4 commits into from
Mar 8, 2022
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
1 change: 1 addition & 0 deletions docs/resources/instance_ip.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ In addition to all above arguments, the following attributes are exported:
- `address` - The IP address.
- `reverse` - The reverse dns attached to this IP
- `organization_id` - The organization ID the IP is associated with.
- `tags` - The tags associated with the IP.

## Import

Expand Down
33 changes: 33 additions & 0 deletions scaleway/resource_instance_ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ func resourceScalewayInstanceIP() *schema.Resource {
return &schema.Resource{
CreateContext: resourceScalewayInstanceIPCreate,
ReadContext: resourceScalewayInstanceIPRead,
UpdateContext: resourceScalewayInstanceIPUpdate,
DeleteContext: resourceScalewayInstanceIPDelete,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
Expand All @@ -37,6 +38,14 @@ func resourceScalewayInstanceIP() *schema.Resource {
Computed: true,
Description: "The server associated with this IP",
},
"tags": {
Type: schema.TypeList,
Elem: &schema.Schema{
Type: schema.TypeString,
},
Optional: true,
Description: "The tags associated with the ip",
},
"zone": zoneSchema(),
"organization_id": organizationIDSchema(),
"project_id": projectIDSchema(),
Expand All @@ -53,6 +62,7 @@ func resourceScalewayInstanceIPCreate(ctx context.Context, d *schema.ResourceDat
res, err := instanceAPI.CreateIP(&instance.CreateIPRequest{
Zone: zone,
Project: expandStringPtr(d.Get("project_id")),
Tags: expandStrings(d.Get("tags")),
}, scw.WithContext(ctx))
if err != nil {
return diag.FromErr(err)
Expand All @@ -62,6 +72,28 @@ func resourceScalewayInstanceIPCreate(ctx context.Context, d *schema.ResourceDat
return resourceScalewayInstanceIPRead(ctx, d, meta)
}

func resourceScalewayInstanceIPUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
instanceAPI, zone, ID, err := instanceAPIWithZoneAndID(meta, d.Id())
if err != nil {
return diag.FromErr(err)
}
req := &instance.UpdateIPRequest{
IP: ID,
Zone: zone,
}

if d.HasChange("tags") {
req.Tags = scw.StringsPtr(expandStrings(d.Get("tags")))
}

_, err = instanceAPI.UpdateIP(req, scw.WithContext(ctx))
if err != nil {
return diag.FromErr(err)
}

return resourceScalewayInstanceIPRead(ctx, d, meta)
}

func resourceScalewayInstanceIPRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
instanceAPI, zone, ID, err := instanceAPIWithZoneAndID(meta, d.Id())
if err != nil {
Expand All @@ -87,6 +119,7 @@ func resourceScalewayInstanceIPRead(ctx context.Context, d *schema.ResourceData,
_ = d.Set("organization_id", res.IP.Organization)
_ = d.Set("project_id", res.IP.Project)
_ = d.Set("reverse", res.IP.Reverse)
_ = d.Set("tags", res.IP.Tags)

if res.IP.Server != nil {
_ = d.Set("server_id", newZonedIDString(res.IP.Zone, res.IP.Server.ID))
Expand Down
42 changes: 42 additions & 0 deletions scaleway/resource_instance_ip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,48 @@ func TestAccScalewayInstanceIP_WithZone(t *testing.T) {
})
}

func TestAccScalewayInstanceIP_Tags(t *testing.T) {
tt := NewTestTools(t)
defer tt.Cleanup()
resource.ParallelTest(t, resource.TestCase{
ProviderFactories: tt.ProviderFactories,
CheckDestroy: testAccCheckScalewayInstanceIPDestroy(tt),
Steps: []resource.TestStep{
{
Config: `
resource "scaleway_instance_ip" "main" {}
`,
Check: resource.ComposeTestCheckFunc(
testAccCheckScalewayInstanceIPExists(tt, "scaleway_instance_ip.main"),
resource.TestCheckNoResourceAttr("scaleway_instance_ip.main", "tags"),
),
},
{
Config: `
resource "scaleway_instance_ip" "main" {
tags = ["foo", "bar"]
}
`,
Check: resource.ComposeTestCheckFunc(
testAccCheckScalewayInstanceIPExists(tt, "scaleway_instance_ip.main"),
resource.TestCheckResourceAttr("scaleway_instance_ip.main", "tags.0", "foo"),
resource.TestCheckResourceAttr("scaleway_instance_ip.main", "tags.1", "bar"),
),
},
{
Config: `
resource "scaleway_instance_ip" "main" {
}
`,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckNoResourceAttr("scaleway_instance_ip.main", "tags"),
testAccCheckScalewayInstanceIPExists(tt, "scaleway_instance_ip.main"),
),
},
},
})
}

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