github.com/mohanarpit/terraform@v0.6.16-0.20160909104007-291f29853544/builtin/providers/aws/resource_aws_iam_group_membership.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/aws/aws-sdk-go/aws"
     7  	"github.com/aws/aws-sdk-go/aws/awserr"
     8  	"github.com/aws/aws-sdk-go/service/iam"
     9  	"github.com/hashicorp/terraform/helper/schema"
    10  )
    11  
    12  func resourceAwsIamGroupMembership() *schema.Resource {
    13  	return &schema.Resource{
    14  		Create: resourceAwsIamGroupMembershipCreate,
    15  		Read:   resourceAwsIamGroupMembershipRead,
    16  		Update: resourceAwsIamGroupMembershipUpdate,
    17  		Delete: resourceAwsIamGroupMembershipDelete,
    18  
    19  		Schema: map[string]*schema.Schema{
    20  			"name": &schema.Schema{
    21  				Type:     schema.TypeString,
    22  				Required: true,
    23  				ForceNew: true,
    24  			},
    25  
    26  			"users": &schema.Schema{
    27  				Type:     schema.TypeSet,
    28  				Required: true,
    29  				Elem:     &schema.Schema{Type: schema.TypeString},
    30  				Set:      schema.HashString,
    31  			},
    32  
    33  			"group": &schema.Schema{
    34  				Type:     schema.TypeString,
    35  				Required: true,
    36  				ForceNew: true,
    37  			},
    38  		},
    39  	}
    40  }
    41  
    42  func resourceAwsIamGroupMembershipCreate(d *schema.ResourceData, meta interface{}) error {
    43  	conn := meta.(*AWSClient).iamconn
    44  
    45  	group := d.Get("group").(string)
    46  	userList := expandStringList(d.Get("users").(*schema.Set).List())
    47  
    48  	if err := addUsersToGroup(conn, userList, group); err != nil {
    49  		return err
    50  	}
    51  
    52  	d.SetId(d.Get("name").(string))
    53  	return resourceAwsIamGroupMembershipRead(d, meta)
    54  }
    55  
    56  func resourceAwsIamGroupMembershipRead(d *schema.ResourceData, meta interface{}) error {
    57  	conn := meta.(*AWSClient).iamconn
    58  	group := d.Get("group").(string)
    59  
    60  	var ul []string
    61  	var marker *string
    62  	for {
    63  		resp, err := conn.GetGroup(&iam.GetGroupInput{
    64  			GroupName: aws.String(group),
    65  			Marker:    marker,
    66  		})
    67  
    68  		if err != nil {
    69  			if awsErr, ok := err.(awserr.Error); ok {
    70  				// aws specific error
    71  				if awsErr.Code() == "NoSuchEntity" {
    72  					// group not found
    73  					d.SetId("")
    74  					return nil
    75  				}
    76  			}
    77  			return err
    78  		}
    79  
    80  		for _, u := range resp.Users {
    81  			ul = append(ul, *u.UserName)
    82  		}
    83  
    84  		if !*resp.IsTruncated {
    85  			break
    86  		}
    87  		marker = resp.Marker
    88  	}
    89  
    90  	if err := d.Set("users", ul); err != nil {
    91  		return fmt.Errorf("[WARN] Error setting user list from IAM Group Membership (%s), error: %s", group, err)
    92  	}
    93  
    94  	return nil
    95  }
    96  
    97  func resourceAwsIamGroupMembershipUpdate(d *schema.ResourceData, meta interface{}) error {
    98  	conn := meta.(*AWSClient).iamconn
    99  
   100  	if d.HasChange("users") {
   101  		group := d.Get("group").(string)
   102  
   103  		o, n := d.GetChange("users")
   104  		if o == nil {
   105  			o = new(schema.Set)
   106  		}
   107  		if n == nil {
   108  			n = new(schema.Set)
   109  		}
   110  
   111  		os := o.(*schema.Set)
   112  		ns := n.(*schema.Set)
   113  		remove := expandStringList(os.Difference(ns).List())
   114  		add := expandStringList(ns.Difference(os).List())
   115  
   116  		if err := removeUsersFromGroup(conn, remove, group); err != nil {
   117  			return err
   118  		}
   119  
   120  		if err := addUsersToGroup(conn, add, group); err != nil {
   121  			return err
   122  		}
   123  	}
   124  
   125  	return resourceAwsIamGroupMembershipRead(d, meta)
   126  }
   127  
   128  func resourceAwsIamGroupMembershipDelete(d *schema.ResourceData, meta interface{}) error {
   129  	conn := meta.(*AWSClient).iamconn
   130  	userList := expandStringList(d.Get("users").(*schema.Set).List())
   131  	group := d.Get("group").(string)
   132  
   133  	if err := removeUsersFromGroup(conn, userList, group); err != nil {
   134  		return err
   135  	}
   136  
   137  	return nil
   138  }
   139  
   140  func removeUsersFromGroup(conn *iam.IAM, users []*string, group string) error {
   141  	for _, u := range users {
   142  		_, err := conn.RemoveUserFromGroup(&iam.RemoveUserFromGroupInput{
   143  			UserName:  u,
   144  			GroupName: aws.String(group),
   145  		})
   146  
   147  		if err != nil {
   148  			if iamerr, ok := err.(awserr.Error); ok && iamerr.Code() == "NoSuchEntity" {
   149  				return nil
   150  			}
   151  			return err
   152  		}
   153  	}
   154  	return nil
   155  }
   156  
   157  func addUsersToGroup(conn *iam.IAM, users []*string, group string) error {
   158  	for _, u := range users {
   159  		_, err := conn.AddUserToGroup(&iam.AddUserToGroupInput{
   160  			UserName:  u,
   161  			GroupName: aws.String(group),
   162  		})
   163  
   164  		if err != nil {
   165  			return err
   166  		}
   167  	}
   168  	return nil
   169  }