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