Skip to content

Commit

Permalink
providers/aws: security group import imports rules
Browse files Browse the repository at this point in the history
  • Loading branch information
mitchellh committed May 11, 2016
1 parent b728e55 commit 6bdab07
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 28 deletions.
17 changes: 10 additions & 7 deletions builtin/providers/aws/import_aws_security_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,17 @@ func resourceAwsSecurityGroupImportState(
for ruleType, perms := range permMap {
for _, perm := range perms {
// Construct the rule. We do this by populating the absolute
// minimum necessary for Refresh on the rule to work.
// minimum necessary for Refresh on the rule to work. This
// happens to be a lot of fields since they're almost all needed
// for de-dupping.
id := ipPermissionIDHash(sgId, ruleType, perm)
data := ruleResource.Data(nil)
data.SetId(id)
data.SetType("aws_security_group_rule")
data.Set("security_group_id", sgId)
data.Set("type", ruleType)
results = append(results, data)
d := ruleResource.Data(nil)
d.SetId(id)
d.SetType("aws_security_group_rule")
d.Set("security_group_id", sgId)
d.Set("type", ruleType)
setFromIPPerm(d, sg, perm)
results = append(results, d)
}
}

Expand Down
47 changes: 27 additions & 20 deletions builtin/providers/aws/resource_aws_security_group_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,27 +239,8 @@ func resourceAwsSecurityGroupRuleRead(d *schema.ResourceData, meta interface{})

log.Printf("[DEBUG] Found rule for Security Group Rule (%s): %s", d.Id(), rule)

d.Set("from_port", rule.FromPort)
d.Set("to_port", rule.ToPort)
d.Set("protocol", rule.IpProtocol)
d.Set("type", ruleType)

var cb []string
for _, c := range p.IpRanges {
cb = append(cb, *c.CidrIp)
}

d.Set("cidr_blocks", cb)

if len(p.UserIdGroupPairs) > 0 {
s := p.UserIdGroupPairs[0]
if isVPC {
d.Set("source_security_group_id", *s.GroupId)
} else {
d.Set("source_security_group_id", *s.GroupName)
}
}

setFromIPPerm(d, sg, rule)
return nil
}

Expand Down Expand Up @@ -515,3 +496,29 @@ func expandIPPerm(d *schema.ResourceData, sg *ec2.SecurityGroup) (*ec2.IpPermiss

return &perm, nil
}

func setFromIPPerm(d *schema.ResourceData, sg *ec2.SecurityGroup, rule *ec2.IpPermission) error {
isVPC := sg.VpcId != nil && *sg.VpcId != ""

d.Set("from_port", rule.FromPort)
d.Set("to_port", rule.ToPort)
d.Set("protocol", rule.IpProtocol)

var cb []string
for _, c := range rule.IpRanges {
cb = append(cb, *c.CidrIp)
}

d.Set("cidr_blocks", cb)

if len(rule.UserIdGroupPairs) > 0 {
s := rule.UserIdGroupPairs[0]
if isVPC {
d.Set("source_security_group_id", *s.GroupId)
} else {
d.Set("source_security_group_id", *s.GroupName)
}
}

return nil
}
1 change: 0 additions & 1 deletion terraform/eval_refresh.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ func (n *EvalRefresh) Eval(ctx EvalContext) (interface{}, error) {
return nil, err
}

log.Printf("STATE: %#v", state)
if n.Output != nil {
*n.Output = state
}
Expand Down

0 comments on commit 6bdab07

Please sign in to comment.