Skip to content

Commit

Permalink
improve scaleway SDK to return created SG rule
Browse files Browse the repository at this point in the history
closes #25
  • Loading branch information
nicolai86 committed Dec 2, 2017
1 parent beea3df commit 8faa391
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 46 deletions.
26 changes: 4 additions & 22 deletions scaleway/resource_security_group_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,15 @@ func resourceScalewaySecurityGroupRuleCreate(d *schema.ResourceData, m interface
mu.Lock()
defer mu.Unlock()

req := api.ScalewayNewSecurityGroupRule{
req := api.NewSecurityGroupRule{
Action: d.Get("action").(string),
Direction: d.Get("direction").(string),
IPRange: d.Get("ip_range").(string),
Protocol: d.Get("protocol").(string),
DestPortFrom: d.Get("port").(int),
}

err := scaleway.PostSecurityGroupRule(d.Get("security_group").(string), req)
rule, err := scaleway.PostSecurityGroupRule(d.Get("security_group").(string), req)
if err != nil {
if serr, ok := err.(api.ScalewayAPIError); ok {
log.Printf("[DEBUG] Error creating Security Group Rule: %q\n", serr.APIMessage)
Expand All @@ -93,25 +93,7 @@ func resourceScalewaySecurityGroupRuleCreate(d *schema.ResourceData, m interface
return err
}

resp, err := scaleway.GetSecurityGroupRules(d.Get("security_group").(string))
if err != nil {
return err
}

matches := func(rule api.ScalewaySecurityGroupRule) bool {
return rule.Action == req.Action &&
rule.Direction == req.Direction &&
rule.IPRange == req.IPRange &&
rule.Protocol == req.Protocol &&
rule.DestPortFrom == req.DestPortFrom
}

for _, rule := range resp.Rules {
if matches(rule) {
d.SetId(rule.ID)
break
}
}
d.SetId(rule.ID)

if d.Id() == "" {
return fmt.Errorf("Failed to find created security group rule")
Expand Down Expand Up @@ -152,7 +134,7 @@ func resourceScalewaySecurityGroupRuleUpdate(d *schema.ResourceData, m interface
mu.Lock()
defer mu.Unlock()

var req = api.ScalewayNewSecurityGroupRule{
var req = api.NewSecurityGroupRule{
Action: d.Get("action").(string),
Direction: d.Get("direction").(string),
IPRange: d.Get("ip_range").(string),
Expand Down
43 changes: 43 additions & 0 deletions scaleway/resource_security_group_rule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,26 @@ func TestAccScalewaySecurityGroupRule_Basic(t *testing.T) {
})
}

func TestAccScalewaySecurityGroupRule_Count(t *testing.T) {
var group api.ScalewaySecurityGroups

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckScalewaySecurityGroupRuleDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccCheckScalewaySecurityGroupRuleVariablesConfig,
Check: resource.ComposeTestCheckFunc(
testAccCheckScalewaySecurityGroupsExists("scaleway_security_group.base", &group),
testAccCheckScalewaySecurityGroupRuleExists("scaleway_security_group_rule.rule.0", &group),
testAccCheckScalewaySecurityGroupRuleExists("scaleway_security_group_rule.rule.1", &group),
),
},
},
})
}

func testAccCheckScalewaySecurityGroupsExists(n string, group *api.ScalewaySecurityGroups) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
Expand Down Expand Up @@ -179,3 +199,26 @@ resource "scaleway_security_group_rule" "https" {
port = 443
}
`

var testAccCheckScalewaySecurityGroupRuleVariablesConfig = `
variable "trusted_ips" {
type = "list"
default = ["1.1.1.1", "2.2.2.2/31"]
}
resource "scaleway_security_group" "base" {
name = "sg"
description = "sg rules"
}
resource "scaleway_security_group_rule" "rule" {
count = "${length(var.trusted_ips)}"
security_group = "${scaleway_security_group.base.id}"
action = "accept"
direction = "inbound"
ip_range = "${element(var.trusted_ips, count.index)}"
protocol = "TCP"
port = "22"
}`
51 changes: 30 additions & 21 deletions vendor/github.com/nicolai86/scaleway-sdk/api/security_group_rule.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions vendor/vendor.json
Original file line number Diff line number Diff line change
Expand Up @@ -552,10 +552,10 @@
"revisionTime": "2016-10-03T17:45:16Z"
},
{
"checksumSHA1": "ROp7ZtwHdxYKpmwycOxbOOXNnzo=",
"checksumSHA1": "3QoIoQcuvO0CDnsgsLYa78XYT84=",
"path": "github.com/nicolai86/scaleway-sdk/api",
"revision": "33df10cad9fff60467a3dcb992d5340c2b9a8046",
"revisionTime": "2017-09-17T18:57:50Z"
"revision": "93cc5757856b2ff303e6bd21c998e0525f9708a1",
"revisionTime": "2017-12-02T17:25:18Z"
},
{
"checksumSHA1": "u5s2PZ7fzCOqQX7bVPf9IJ+qNLQ=",
Expand Down

0 comments on commit 8faa391

Please sign in to comment.