Skip to content

Commit

Permalink
Update interface of resource_aws_iot_policy_attachment
Browse files Browse the repository at this point in the history
  • Loading branch information
jhedev committed Feb 25, 2017
1 parent a65702e commit a143ea2
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 27 deletions.
46 changes: 23 additions & 23 deletions builtin/providers/aws/resource_aws_iot_policy_attachment.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,27 @@ func resourceAwsIotPolicyAttachment() *schema.Resource {
Type: schema.TypeString,
Required: true,
},
"principal": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"policies": &schema.Schema{
"principals": &schema.Schema{
Type: schema.TypeSet,
Required: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"policy": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
},
}
}

func resourceAwsIotPolicyAttachmentCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).iotconn

for _, p := range d.Get("policies").(*schema.Set).List() {
for _, p := range d.Get("principals").(*schema.Set).List() {
_, err := conn.AttachPrincipalPolicy(&iot.AttachPrincipalPolicyInput{
Principal: aws.String(d.Get("principal").(string)),
Principal: aws.String(d.Get("policy").(string)),
PolicyName: aws.String(p.(string)),
})

Expand All @@ -50,40 +50,40 @@ func resourceAwsIotPolicyAttachmentCreate(d *schema.ResourceData, meta interface
}

d.SetId(d.Get("name").(string))
d.Set("principal", d.Get("principal").(string))
d.Set("policies", d.Get("policies").(*schema.Set).List())
d.Set("policy", d.Get("policy").(string))
d.Set("principals", d.Get("principals").(*schema.Set).List())
return nil
}

func resourceAwsIotPolicyAttachmentRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).iotconn

out, err := conn.ListPrincipalPolicies(&iot.ListPrincipalPoliciesInput{
Principal: aws.String(d.Get("principal").(string)),
out, err := conn.ListPolicyPrincipals(&iot.ListPolicyPrincipalsInput{
PolicyName: aws.String(d.Get("policy").(string)),
})

if err != nil {
log.Printf("[ERROR] %s", err)
return err
}

policies := make([]string, len(out.Policies))
for i, p := range out.Policies {
policies[i] = *p.PolicyName
principals := make([]string, len(out.Principals))
for i, p := range out.Principals {
principals[i] = *p
}

d.SetId(d.Get("name").(string))
d.Set("principal", d.Get("principal").(string))
d.Set("policies", policies)
d.Set("policy", d.Get("policy").(string))
d.Set("principals", principals)

return nil
}

func resourceAwsIotPolicyAttachmentUpdate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).iotconn

if d.HasChange("policies") {
err := updatePolicies(conn, d)
if d.HasChange("principals") {
err := updatePrincipalsPolicy(conn, d)
if err != nil {
log.Printf("[ERROR] %v", err)
return err
Expand All @@ -93,8 +93,8 @@ func resourceAwsIotPolicyAttachmentUpdate(d *schema.ResourceData, meta interface
return resourceAwsIotPolicyAttachmentRead(d, meta)
}

func updatePolicies(conn *iot.IoT, d *schema.ResourceData) error {
o, n := d.GetChange("policies")
func updatePrincipalsPolicy(conn *iot.IoT, d *schema.ResourceData) error {
o, n := d.GetChange("principals")
if o == nil {
o = new(schema.Set)
}
Expand Down Expand Up @@ -135,11 +135,11 @@ func updatePolicies(conn *iot.IoT, d *schema.ResourceData) error {
func resourceAwsIotPolicyAttachmentDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).iotconn

for _, p := range d.Get("policies").(*schema.Set).List() {
for _, p := range d.Get("principals").(*schema.Set).List() {
log.Printf("[INFO] %+v", p)
_, err := conn.DetachPrincipalPolicy(&iot.DetachPrincipalPolicyInput{
Principal: aws.String(d.Get("principal").(string)),
PolicyName: aws.String(p.(string)),
Principal: aws.String(p.(string)),
PolicyName: aws.String(d.Get("policy").(string)),
})

if err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ EOF
resource "aws_iot_policy_attachment" "cert_policies" {
name = "cert_policies"
principal = "${aws_iot_certificate.cert.arn}"
policies = ["${aws_iot_policy.pubsub.name}"]
principals = ["${aws_iot_certificate.cert.arn}"]
policy = "${aws_iot_policy.pubsub.name}"
}
`
4 changes: 2 additions & 2 deletions builtin/providers/aws/resource_aws_iot_thing.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ func resourceAwsIotThingUpdate(d *schema.ResourceData, meta interface{}) error {
}

if d.HasChange("principals") {
err := updatePrincipals(conn, d)
err := updatePrincipalsThing(conn, d)
if err != nil {
log.Printf("[ERROR] %v", err)
return err
Expand All @@ -152,7 +152,7 @@ func resourceAwsIotThingUpdate(d *schema.ResourceData, meta interface{}) error {
return resourceAwsIotThingRead(d, meta)
}

func updatePrincipals(conn *iot.IoT, d *schema.ResourceData) error {
func updatePrincipalsThing(conn *iot.IoT, d *schema.ResourceData) error {
o, n := d.GetChange("principals")
if o == nil {
o = new(schema.Set)
Expand Down

0 comments on commit a143ea2

Please sign in to comment.