2015-02-06 16:34:24 +01:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2015-06-03 20:36:57 +02:00
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
|
|
|
"github.com/aws/aws-sdk-go/aws/awserr"
|
|
|
|
"github.com/aws/aws-sdk-go/service/iam"
|
2015-02-06 16:34:24 +01:00
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
|
|
|
)
|
|
|
|
|
|
|
|
func resourceAwsIamGroup() *schema.Resource {
|
|
|
|
return &schema.Resource{
|
|
|
|
Create: resourceAwsIamGroupCreate,
|
|
|
|
Read: resourceAwsIamGroupRead,
|
2015-09-15 07:00:22 +02:00
|
|
|
Update: resourceAwsIamGroupUpdate,
|
2015-02-06 16:34:24 +01:00
|
|
|
Delete: resourceAwsIamGroupDelete,
|
|
|
|
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"arn": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
"unique_id": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
"name": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
"path": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
Default: "/",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceAwsIamGroupCreate(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
iamconn := meta.(*AWSClient).iamconn
|
|
|
|
name := d.Get("name").(string)
|
2015-09-15 07:00:22 +02:00
|
|
|
path := d.Get("path").(string)
|
2015-02-06 16:34:24 +01:00
|
|
|
|
|
|
|
request := &iam.CreateGroupInput{
|
2015-09-15 07:00:22 +02:00
|
|
|
Path: aws.String(path),
|
2015-02-06 16:34:24 +01:00
|
|
|
GroupName: aws.String(name),
|
|
|
|
}
|
|
|
|
|
|
|
|
createResp, err := iamconn.CreateGroup(request)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error creating IAM Group %s: %s", name, err)
|
|
|
|
}
|
|
|
|
return resourceAwsIamGroupReadResult(d, createResp.Group)
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceAwsIamGroupRead(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
iamconn := meta.(*AWSClient).iamconn
|
2015-09-15 07:00:22 +02:00
|
|
|
name := d.Get("name").(string)
|
2015-02-06 16:34:24 +01:00
|
|
|
|
|
|
|
request := &iam.GetGroupInput{
|
2015-09-15 07:00:22 +02:00
|
|
|
GroupName: aws.String(name),
|
2015-02-06 16:34:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
getResp, err := iamconn.GetGroup(request)
|
|
|
|
if err != nil {
|
2015-05-20 13:21:23 +02:00
|
|
|
if iamerr, ok := err.(awserr.Error); ok && iamerr.Code() == "NoSuchEntity" {
|
2015-02-06 16:34:24 +01:00
|
|
|
d.SetId("")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return fmt.Errorf("Error reading IAM Group %s: %s", d.Id(), err)
|
|
|
|
}
|
|
|
|
return resourceAwsIamGroupReadResult(d, getResp.Group)
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceAwsIamGroupReadResult(d *schema.ResourceData, group *iam.Group) error {
|
|
|
|
d.SetId(*group.GroupName)
|
|
|
|
if err := d.Set("name", group.GroupName); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-08-17 20:27:16 +02:00
|
|
|
if err := d.Set("arn", group.Arn); err != nil {
|
2015-02-06 16:34:24 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := d.Set("path", group.Path); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-08-17 20:27:16 +02:00
|
|
|
if err := d.Set("unique_id", group.GroupId); err != nil {
|
2015-02-06 16:34:24 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-09-15 07:00:22 +02:00
|
|
|
func resourceAwsIamGroupUpdate(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
if d.HasChange("name") || d.HasChange("path") {
|
|
|
|
iamconn := meta.(*AWSClient).iamconn
|
|
|
|
on, nn := d.GetChange("name")
|
|
|
|
op, np := d.GetChange("path")
|
|
|
|
fmt.Println(on, nn, op, np)
|
|
|
|
request := &iam.UpdateGroupInput{
|
|
|
|
GroupName: aws.String(on.(string)),
|
|
|
|
NewGroupName: aws.String(nn.(string)),
|
|
|
|
NewPath: aws.String(np.(string)),
|
|
|
|
}
|
|
|
|
_, err := iamconn.UpdateGroup(request)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error updating IAM Group %s: %s", d.Id(), err)
|
|
|
|
}
|
|
|
|
return resourceAwsIamGroupRead(d, meta)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-02-06 16:34:24 +01:00
|
|
|
func resourceAwsIamGroupDelete(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
iamconn := meta.(*AWSClient).iamconn
|
|
|
|
|
|
|
|
request := &iam.DeleteGroupInput{
|
|
|
|
GroupName: aws.String(d.Id()),
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := iamconn.DeleteGroup(request); err != nil {
|
|
|
|
return fmt.Errorf("Error deleting IAM Group %s: %s", d.Id(), err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|