providers/aws: sg flattening of refresh

This commit is contained in:
Jack Pearkes 2014-07-08 20:24:50 -04:00
parent eda3cb009c
commit 5ad8d418f2
2 changed files with 35 additions and 16 deletions

View File

@ -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
}

View File

@ -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 {