From 5ad8d418f277a0d881cc50ff216c9ab6b74f500e Mon Sep 17 00:00:00 2001 From: Jack Pearkes Date: Tue, 8 Jul 2014 20:24:50 -0400 Subject: [PATCH] providers/aws: sg flattening of refresh --- .../aws/resource_aws_security_group.go | 25 +++++++----------- builtin/providers/aws/structure.go | 26 +++++++++++++++++++ 2 files changed, 35 insertions(+), 16 deletions(-) diff --git a/builtin/providers/aws/resource_aws_security_group.go b/builtin/providers/aws/resource_aws_security_group.go index a98644f08..5d83690eb 100644 --- a/builtin/providers/aws/resource_aws_security_group.go +++ b/builtin/providers/aws/resource_aws_security_group.go @@ -38,7 +38,6 @@ func resource_aws_security_group_create( log.Printf("[INFO] Security Group ID: %s", rs.ID) ingressRules := []ec2.IPPerm{} - egressRules := []ec2.IPPerm{} // Expand the "ingress" array to goamz compat []ec2.IPPerm v, ok := flatmap.Expand(rs.Attributes, "ingress").([]interface{}) @@ -46,19 +45,6 @@ func resource_aws_security_group_create( ingressRules = expandIPPerms(v) } - // Expand the "egress" array to goamz compat []ec2.IPPerm - v, ok = flatmap.Expand(rs.Attributes, "egress").([]interface{}) - if ok { - egressRules = expandIPPerms(v) - } - - if len(egressRules) > 0 { - _, err = ec2conn.AuthorizeSecurityGroupEgress(group, egressRules) - if err != nil { - return rs, fmt.Errorf("Error authorizing security group egress rules: %s", err) - } - } - if len(ingressRules) > 0 { _, err = ec2conn.AuthorizeSecurityGroup(group, ingressRules) if err != nil { @@ -133,7 +119,6 @@ func resource_aws_security_group_diff( "description": diff.AttrTypeCreate, "vpc_id": diff.AttrTypeUpdate, "ingress": diff.AttrTypeUpdate, - "egress": diff.AttrTypeUpdate, }, ComputedAttrs: []string{ @@ -148,11 +133,19 @@ func resource_aws_security_group_update_state( s *terraform.ResourceState, sg *ec2.SecurityGroupInfo) (*terraform.ResourceState, error) { - s.Attributes["description"] = sg.Description + s.Attributes["description"] = sg.Descriptifon s.Attributes["name"] = sg.Name s.Attributes["vpc_id"] = sg.VpcId s.Attributes["owner_id"] = sg.OwnerId + // Flatten our sg values + toFlatten := make(map[string]interface{}) + toFlatten["ingress"] = flattenIPPerms(sg.IPPerms) + + for k, v := range flatmap.Flatten(toFlatten) { + s.Attributes[k] = v + } + return s, nil } diff --git a/builtin/providers/aws/structure.go b/builtin/providers/aws/structure.go index ac6ff9c55..1eede5d79 100644 --- a/builtin/providers/aws/structure.go +++ b/builtin/providers/aws/structure.go @@ -66,6 +66,32 @@ func expandIPPerms(configured []interface{}) []ec2.IPPerm { return perms } +// Flattens an array of ipPerms into a list of primitives that +// flatmap.Flatten() can handle +func flattenIPPerms(list []ec2.IPPerm) []map[string]interface{} { + result := make([]map[string]interface{}, 0, len(list)) + + for _, perm := range list { + n := make(map[string]interface{}) + n["from_port"] = perm.FromPort + n["protocol"] = perm.Protocol + n["to_port"] = perm.ToPort + n["cidr_blocks"] = perm.SourceIPs + n["security_groups"] = flattenSecurityGroups(perm.SourceGroups) + } + + return result +} + +// Flattens an array of SecurityGroups into a []string +func flattenSecurityGroups(list []ec2.UserSecurityGroup) []string { + result := make([]string, 0, len(list)) + for _, g := range list { + result = append(result, g.Id) + } + return result +} + // Takes the result of flatmap.Expand for an array of strings // and returns a []string func expandStringList(configured []interface{}) []string {