Skip to content

Commit

Permalink
remove suffix
Browse files Browse the repository at this point in the history
  • Loading branch information
loicbourgois committed Feb 6, 2020
1 parent 602aeac commit 8561273
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 30 deletions.
28 changes: 5 additions & 23 deletions scaleway/resource_instance_security_group_rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package scaleway

import (
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"strings"
)

func resourceScalewayInstanceSecurityGroupRules() *schema.Resource {
Expand Down Expand Up @@ -46,21 +45,20 @@ func customImporterState(d *schema.ResourceData, m interface{}) ([]*schema.Resou
importZID := d.Id()

d.Set("security_group_id", importZID)
d.SetId(securityGroupRulesZIDFromSecurityGroupZID(importZID))
d.SetId(importZID)

return []*schema.ResourceData{d}, nil
}

func resourceScalewayInstanceSecurityGroupRulesCreate(d *schema.ResourceData, m interface{}) error {
d.SetId(securityGroupRulesZIDFromSecurityGroupZID(d.Get("security_group_id").(string)))
d.SetId(d.Get("security_group_id").(string))

// We call update instead of read as it will take care of creating rules.
return resourceScalewayInstanceSecurityGroupRulesUpdate(d, m)
}

func resourceScalewayInstanceSecurityGroupRulesRead(d *schema.ResourceData, m interface{}) error {
securityGroupRulesZID := d.Id()
securityGroupZID := securityGroupZIDFromSecurityGroupRulesZID(securityGroupRulesZID)
securityGroupZID := d.Id()

instanceApi, zone, securityGroupID, err := instanceAPIWithZoneAndID(m, securityGroupZID)
if err != nil {
Expand All @@ -81,7 +79,7 @@ func resourceScalewayInstanceSecurityGroupRulesRead(d *schema.ResourceData, m in
}

func resourceScalewayInstanceSecurityGroupRulesUpdate(d *schema.ResourceData, m interface{}) error {
securityGroupZID := securityGroupZIDFromSecurityGroupRulesZID(d.Id())
securityGroupZID := d.Id()
instanceApi, zone, securityGroupID, err := instanceAPIWithZoneAndID(m, securityGroupZID)
if err != nil {
return err
Expand All @@ -96,7 +94,7 @@ func resourceScalewayInstanceSecurityGroupRulesUpdate(d *schema.ResourceData, m
}

func resourceScalewayInstanceSecurityGroupRulesDelete(d *schema.ResourceData, m interface{}) error {
securityGroupZID := securityGroupZIDFromSecurityGroupRulesZID(d.Id())
securityGroupZID := d.Id()
instanceApi, zone, securityGroupID, err := instanceAPIWithZoneAndID(m, securityGroupZID)
if err != nil {
return err
Expand All @@ -112,19 +110,3 @@ func resourceScalewayInstanceSecurityGroupRulesDelete(d *schema.ResourceData, m

return nil
}

// securityGroupRulesZIDFromSecurityGroupZID returns the
//
// SecurityGroupRules.ID should be the based on SecurityGroup.ID.
// This is necessary to support Terraform import feature. -> false -> why is this necessary ?
// If we want to support multiple SGs(SecurityGroup) for 1 SGRS(SecurityGroupRules),
// we could always use the first SecurityGroup's ID,
// because from the API,
// the data for a single SGRS is duplicated for all SGs using the same SGRS.
func securityGroupRulesZIDFromSecurityGroupZID(SecurityGroupZID string) string {
return securityGroupZIDFromSecurityGroupRulesZID(SecurityGroupZID) + "-sgrs-id"
}

func securityGroupZIDFromSecurityGroupRulesZID(zid string) string {
return strings.Replace(zid, "-sgrs-id", "", 1)
}
7 changes: 0 additions & 7 deletions scaleway/resource_instance_security_group_rules_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,13 +140,6 @@ func TestAccScalewayInstanceSecurityGroupRules(t *testing.T) {
resource.TestCheckResourceAttrPair("scaleway_instance_security_group.sg01", "id", "scaleway_instance_security_group_rules.sgrs01", "security_group_id"),
),
},
{
Config: `resource scaleway_instance_security_group sg01 {
}`,
Check: resource.ComposeTestCheckFunc(
testAccCheckScalewayInstanceSecurityGroupExists("scaleway_instance_security_group.sg01"),
),
},
},
})
}

0 comments on commit 8561273

Please sign in to comment.