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 for security groups #1124

Merged
merged 2 commits into from
Mar 7, 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
2 changes: 2 additions & 0 deletions docs/resources/instance_security_group.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,8 @@ The `inbound_rule` and `outbound_rule` block supports:

- `ip_range`- (Optional) The ip range (e.g `192.168.1.0/24`) this rule applies to. If no `ip` nor `ip_range` are specified, rule will apply to all ip. Only one of `ip` and `ip_range` should be specified.

- `tags`- (Optional) The tags of the security group.

## Attributes Reference

In addition to all above arguments, the following attributes are exported:
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ require (
github.com/hashicorp/go-cty v1.4.1-0.20200414143053-d3edf31b6320
github.com/hashicorp/go-retryablehttp v0.7.0
github.com/hashicorp/terraform-plugin-sdk/v2 v2.10.1
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.9.0.20220221102637-810e5a35c572
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.9.0.20220304124138-3f3535da1025
github.com/stretchr/testify v1.7.0
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1

Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -307,8 +307,8 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
github.com/posener/complete v1.1.1/go.mod h1:em0nMJCgc9GFtwrmVmEMR/ZL6WyhyjMBndrE9hABlRI=
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.9.0.20220221102637-810e5a35c572 h1:/HLfjGzJDzgfUCO6lFf8rQIBUbPEDclXuyjtRw778vQ=
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.9.0.20220221102637-810e5a35c572/go.mod h1:fCa7OJZ/9DRTnOKmxvT6pn+LPWUptQAmHF/SBJUGEcg=
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.9.0.20220304124138-3f3535da1025 h1:k79FedA5EvuHwgTeNDhDSIJE/JrSY1eotHg0K7lcGjg=
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.9.0.20220304124138-3f3535da1025/go.mod h1:fCa7OJZ/9DRTnOKmxvT6pn+LPWUptQAmHF/SBJUGEcg=
github.com/sebdah/goldie v1.0.0/go.mod h1:jXP4hmWywNEwZzhMuv2ccnqTSFpuq8iyQhtQdkkZBH4=
github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo=
github.com/sergi/go-diff v1.1.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM=
Expand Down
14 changes: 14 additions & 0 deletions scaleway/resource_instance_security_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,14 @@ func resourceScalewayInstanceSecurityGroup() *schema.Resource {
Optional: true,
Default: true,
},
"tags": {
Type: schema.TypeList,
Elem: &schema.Schema{
Type: schema.TypeString,
},
Optional: true,
Description: "The tags associated with the security group",
},
"zone": zoneSchema(),
"organization_id": organizationIDSchema(),
"project_id": projectIDSchema(),
Expand All @@ -110,6 +118,7 @@ func resourceScalewayInstanceSecurityGroupCreate(ctx context.Context, d *schema.
InboundDefaultPolicy: instance.SecurityGroupPolicy(d.Get("inbound_default_policy").(string)),
OutboundDefaultPolicy: instance.SecurityGroupPolicy(d.Get("outbound_default_policy").(string)),
EnableDefaultSecurity: expandBoolPtr(d.Get("enable_default_security")),
Tags: expandStrings(d.Get("tags")),
}, scw.WithContext(ctx))
if err != nil {
return diag.FromErr(err)
Expand Down Expand Up @@ -151,6 +160,7 @@ func resourceScalewayInstanceSecurityGroupRead(ctx context.Context, d *schema.Re
_ = d.Set("inbound_default_policy", res.SecurityGroup.InboundDefaultPolicy.String())
_ = d.Set("outbound_default_policy", res.SecurityGroup.OutboundDefaultPolicy.String())
_ = d.Set("enable_default_security", res.SecurityGroup.EnableDefaultSecurity)
_ = d.Set("tags", res.SecurityGroup.Tags)

if !d.Get("external_rules").(bool) {
inboundRules, outboundRules, err := getSecurityGroupRules(ctx, instanceAPI, zone, ID, d)
Expand Down Expand Up @@ -259,6 +269,10 @@ func resourceScalewayInstanceSecurityGroupUpdate(ctx context.Context, d *schema.
updateReq.EnableDefaultSecurity = expandBoolPtr(d.Get("enable_default_security"))
}

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

// Only update name if one is provided in the state
if d.Get("name") != nil && d.Get("name").(string) != "" {
updateReq.Name = expandStringPtr(d.Get("name"))
Expand Down
43 changes: 43 additions & 0 deletions scaleway/resource_instance_security_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,49 @@ func TestAccScalewayInstanceSecurityGroup_WithPortRange(t *testing.T) {
})
}

func TestAccScalewayInstanceSecurityGroup_Tags(t *testing.T) {
tt := NewTestTools(t)
defer tt.Cleanup()
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: tt.ProviderFactories,
CheckDestroy: testAccCheckScalewayInstanceSecurityGroupDestroy(tt),
Steps: []resource.TestStep{
{
Config: `
resource "scaleway_instance_security_group" "main" {
tags = [ "foo", "bar" ]
}
`,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("scaleway_instance_security_group.main", "tags.0", "foo"),
resource.TestCheckResourceAttr("scaleway_instance_security_group.main", "tags.1", "bar"),
),
},
{
Config: `
resource "scaleway_instance_security_group" "main" {
tags = [ "foo", "buzz" ]
}
`,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("scaleway_instance_security_group.main", "tags.0", "foo"),
resource.TestCheckResourceAttr("scaleway_instance_security_group.main", "tags.1", "buzz"),
),
},
{
Config: `
resource "scaleway_instance_security_group" "main" {
}
`,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckNoResourceAttr("scaleway_instance_security_group.main", "tags"),
),
},
},
})
}

func testAccCheckScalewayInstanceSecurityGroupRuleMatch(tt *TestTools, name string, index int, expected *instance.SecurityGroupRule) resource.TestCheckFunc {
return testAccCheckScalewayInstanceSecurityGroupRuleIs(tt, name, expected.Direction, index, func(actual *instance.SecurityGroupRule) error {
if ok, _ := securityGroupRuleEquals(expected, actual); !ok {
Expand Down
Loading