github.com/mohanarpit/terraform@v0.6.16-0.20160909104007-291f29853544/builtin/providers/aws/resource_aws_iam_group.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  
    10  	"github.com/hashicorp/terraform/helper/schema"
    11  )
    12  
    13  func resourceAwsIamGroup() *schema.Resource {
    14  	return &schema.Resource{
    15  		Create: resourceAwsIamGroupCreate,
    16  		Read:   resourceAwsIamGroupRead,
    17  		Update: resourceAwsIamGroupUpdate,
    18  		Delete: resourceAwsIamGroupDelete,
    19  		Importer: &schema.ResourceImporter{
    20  			State: schema.ImportStatePassthrough,
    21  		},
    22  
    23  		Schema: map[string]*schema.Schema{
    24  			"arn": &schema.Schema{
    25  				Type:     schema.TypeString,
    26  				Computed: true,
    27  			},
    28  			"unique_id": &schema.Schema{
    29  				Type:     schema.TypeString,
    30  				Computed: true,
    31  			},
    32  			"name": &schema.Schema{
    33  				Type:     schema.TypeString,
    34  				Required: true,
    35  			},
    36  			"path": &schema.Schema{
    37  				Type:     schema.TypeString,
    38  				Optional: true,
    39  				Default:  "/",
    40  			},
    41  		},
    42  	}
    43  }
    44  
    45  func resourceAwsIamGroupCreate(d *schema.ResourceData, meta interface{}) error {
    46  	iamconn := meta.(*AWSClient).iamconn
    47  	name := d.Get("name").(string)
    48  	path := d.Get("path").(string)
    49  
    50  	request := &iam.CreateGroupInput{
    51  		Path:      aws.String(path),
    52  		GroupName: aws.String(name),
    53  	}
    54  
    55  	createResp, err := iamconn.CreateGroup(request)
    56  	if err != nil {
    57  		return fmt.Errorf("Error creating IAM Group %s: %s", name, err)
    58  	}
    59  	d.SetId(*createResp.Group.GroupName)
    60  
    61  	return resourceAwsIamGroupReadResult(d, createResp.Group)
    62  }
    63  
    64  func resourceAwsIamGroupRead(d *schema.ResourceData, meta interface{}) error {
    65  	iamconn := meta.(*AWSClient).iamconn
    66  
    67  	request := &iam.GetGroupInput{
    68  		GroupName: aws.String(d.Id()),
    69  	}
    70  
    71  	getResp, err := iamconn.GetGroup(request)
    72  	if err != nil {
    73  		if iamerr, ok := err.(awserr.Error); ok && iamerr.Code() == "NoSuchEntity" {
    74  			d.SetId("")
    75  			return nil
    76  		}
    77  		return fmt.Errorf("Error reading IAM Group %s: %s", d.Id(), err)
    78  	}
    79  	return resourceAwsIamGroupReadResult(d, getResp.Group)
    80  }
    81  
    82  func resourceAwsIamGroupReadResult(d *schema.ResourceData, group *iam.Group) error {
    83  	if err := d.Set("name", group.GroupName); err != nil {
    84  		return err
    85  	}
    86  	if err := d.Set("arn", group.Arn); err != nil {
    87  		return err
    88  	}
    89  	if err := d.Set("path", group.Path); err != nil {
    90  		return err
    91  	}
    92  	if err := d.Set("unique_id", group.GroupId); err != nil {
    93  		return err
    94  	}
    95  	return nil
    96  }
    97  
    98  func resourceAwsIamGroupUpdate(d *schema.ResourceData, meta interface{}) error {
    99  	if d.HasChange("name") || d.HasChange("path") {
   100  		iamconn := meta.(*AWSClient).iamconn
   101  		on, nn := d.GetChange("name")
   102  		_, np := d.GetChange("path")
   103  
   104  		request := &iam.UpdateGroupInput{
   105  			GroupName:    aws.String(on.(string)),
   106  			NewGroupName: aws.String(nn.(string)),
   107  			NewPath:      aws.String(np.(string)),
   108  		}
   109  		_, err := iamconn.UpdateGroup(request)
   110  		if err != nil {
   111  			return fmt.Errorf("Error updating IAM Group %s: %s", d.Id(), err)
   112  		}
   113  		return resourceAwsIamGroupRead(d, meta)
   114  	}
   115  	return nil
   116  }
   117  
   118  func resourceAwsIamGroupDelete(d *schema.ResourceData, meta interface{}) error {
   119  	iamconn := meta.(*AWSClient).iamconn
   120  
   121  	request := &iam.DeleteGroupInput{
   122  		GroupName: aws.String(d.Id()),
   123  	}
   124  
   125  	if _, err := iamconn.DeleteGroup(request); err != nil {
   126  		return fmt.Errorf("Error deleting IAM Group %s: %s", d.Id(), err)
   127  	}
   128  	return nil
   129  }