-
Notifications
You must be signed in to change notification settings - Fork 9.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2376 from hashicorp/fix-security-group-rule-hashi…
…ng-with-source-sg Consider security groups with source security groups when hashing
- Loading branch information
Showing
4 changed files
with
279 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
builtin/providers/aws/resource_aws_security_group_rule_migrate.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
package aws | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"strconv" | ||
"strings" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/service/ec2" | ||
"github.com/hashicorp/terraform/terraform" | ||
) | ||
|
||
func resourceAwsSecurityGroupRuleMigrateState( | ||
v int, is *terraform.InstanceState, meta interface{}) (*terraform.InstanceState, error) { | ||
switch v { | ||
case 0: | ||
log.Println("[INFO] Found AWS Security Group State v0; migrating to v1") | ||
return migrateSGRuleStateV0toV1(is) | ||
default: | ||
return is, fmt.Errorf("Unexpected schema version: %d", v) | ||
} | ||
|
||
return is, nil | ||
} | ||
|
||
func migrateSGRuleStateV0toV1(is *terraform.InstanceState) (*terraform.InstanceState, error) { | ||
if is.Empty() { | ||
log.Println("[DEBUG] Empty InstanceState; nothing to migrate.") | ||
return is, nil | ||
} | ||
|
||
perm, err := migrateExpandIPPerm(is.Attributes) | ||
|
||
if err != nil { | ||
return nil, fmt.Errorf("[WARN] Error making new IP Permission in Security Group migration") | ||
} | ||
|
||
log.Printf("[DEBUG] Attributes before migration: %#v", is.Attributes) | ||
newID := ipPermissionIDHash(is.Attributes["type"], perm) | ||
is.Attributes["id"] = newID | ||
is.ID = newID | ||
log.Printf("[DEBUG] Attributes after migration: %#v, new id: %s", is.Attributes, newID) | ||
return is, nil | ||
} | ||
|
||
func migrateExpandIPPerm(attrs map[string]string) (*ec2.IPPermission, error) { | ||
var perm ec2.IPPermission | ||
tp, err := strconv.Atoi(attrs["to_port"]) | ||
if err != nil { | ||
return nil, fmt.Errorf("Error converting to_port in Security Group migration") | ||
} | ||
|
||
fp, err := strconv.Atoi(attrs["from_port"]) | ||
if err != nil { | ||
return nil, fmt.Errorf("Error converting from_port in Security Group migration") | ||
} | ||
|
||
perm.ToPort = aws.Long(int64(tp)) | ||
perm.FromPort = aws.Long(int64(fp)) | ||
perm.IPProtocol = aws.String(attrs["protocol"]) | ||
|
||
groups := make(map[string]bool) | ||
if attrs["self"] == "true" { | ||
groups[attrs["security_group_id"]] = true | ||
} | ||
|
||
if attrs["source_security_group_id"] != "" { | ||
groups[attrs["source_security_group_id"]] = true | ||
} | ||
|
||
if len(groups) > 0 { | ||
perm.UserIDGroupPairs = make([]*ec2.UserIDGroupPair, len(groups)) | ||
// build string list of group name/ids | ||
var gl []string | ||
for k, _ := range groups { | ||
gl = append(gl, k) | ||
} | ||
|
||
for i, name := range gl { | ||
perm.UserIDGroupPairs[i] = &ec2.UserIDGroupPair{ | ||
GroupID: aws.String(name), | ||
} | ||
} | ||
} | ||
|
||
var cb []string | ||
for k, v := range attrs { | ||
if k != "cidr_blocks.#" && strings.HasPrefix(k, "cidr_blocks") { | ||
cb = append(cb, v) | ||
} | ||
} | ||
if len(cb) > 0 { | ||
perm.IPRanges = make([]*ec2.IPRange, len(cb)) | ||
for i, v := range cb { | ||
perm.IPRanges[i] = &ec2.IPRange{CIDRIP: aws.String(v)} | ||
} | ||
} | ||
|
||
return &perm, nil | ||
} |
67 changes: 67 additions & 0 deletions
67
builtin/providers/aws/resource_aws_security_group_rule_migrate_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package aws | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform/terraform" | ||
) | ||
|
||
func TestAWSSecurityGroupRuleMigrateState(t *testing.T) { | ||
cases := map[string]struct { | ||
StateVersion int | ||
ID string | ||
Attributes map[string]string | ||
Expected string | ||
Meta interface{} | ||
}{ | ||
"v0_1": { | ||
StateVersion: 0, | ||
ID: "sg-4235098228", | ||
Attributes: map[string]string{ | ||
"self": "false", | ||
"to_port": "0", | ||
"security_group_id": "sg-13877277", | ||
"cidr_blocks.#": "0", | ||
"type": "ingress", | ||
"protocol": "-1", | ||
"from_port": "0", | ||
"source_security_group_id": "sg-11877275", | ||
}, | ||
Expected: "sg-3766347571", | ||
}, | ||
"v0_2": { | ||
StateVersion: 0, | ||
ID: "sg-1021609891", | ||
Attributes: map[string]string{ | ||
"security_group_id": "sg-0981746d", | ||
"from_port": "0", | ||
"to_port": "0", | ||
"type": "ingress", | ||
"self": "false", | ||
"protocol": "-1", | ||
"cidr_blocks.0": "172.16.1.0/24", | ||
"cidr_blocks.1": "172.16.2.0/24", | ||
"cidr_blocks.2": "172.16.3.0/24", | ||
"cidr_blocks.3": "172.16.4.0/24", | ||
"cidr_blocks.#": "4"}, | ||
Expected: "sg-4100229787", | ||
}, | ||
} | ||
|
||
for tn, tc := range cases { | ||
is := &terraform.InstanceState{ | ||
ID: tc.ID, | ||
Attributes: tc.Attributes, | ||
} | ||
is, err := resourceAwsSecurityGroupRuleMigrateState( | ||
tc.StateVersion, is, tc.Meta) | ||
|
||
if err != nil { | ||
t.Fatalf("bad: %s, err: %#v", tn, err) | ||
} | ||
|
||
if is.ID != tc.Expected { | ||
t.Fatalf("bad sg rule id: %s\n\n expected: %s", is.ID, tc.Expected) | ||
} | ||
} | ||
} |
Oops, something went wrong.