2015-02-06 16:34:24 +01:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2016-10-25 14:18:41 +02:00
|
|
|
"regexp"
|
2015-02-06 16:34:24 +01:00
|
|
|
|
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,
|
2016-06-23 04:10:31 +02:00
|
|
|
Importer: &schema.ResourceImporter{
|
|
|
|
State: schema.ImportStatePassthrough,
|
|
|
|
},
|
2015-02-06 16:34:24 +01:00
|
|
|
|
|
|
|
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{
|
2016-10-25 14:18:41 +02:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ValidateFunc: validateAwsIamGroupName,
|
2015-02-06 16:34:24 +01:00
|
|
|
},
|
|
|
|
"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)
|
|
|
|
}
|
2016-06-23 04:10:31 +02:00
|
|
|
d.SetId(*createResp.Group.GroupName)
|
|
|
|
|
2015-02-06 16:34:24 +01:00
|
|
|
return resourceAwsIamGroupReadResult(d, createResp.Group)
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceAwsIamGroupRead(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
iamconn := meta.(*AWSClient).iamconn
|
|
|
|
|
|
|
|
request := &iam.GetGroupInput{
|
2016-06-23 04:10:31 +02:00
|
|
|
GroupName: aws.String(d.Id()),
|
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 {
|
|
|
|
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")
|
2015-11-18 14:08:45 +01:00
|
|
|
_, np := d.GetChange("path")
|
2015-10-31 15:52:12 +01:00
|
|
|
|
2015-09-15 07:00:22 +02:00
|
|
|
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
|
|
|
|
}
|
2016-10-25 14:18:41 +02:00
|
|
|
|
|
|
|
func validateAwsIamGroupName(v interface{}, k string) (ws []string, errors []error) {
|
|
|
|
value := v.(string)
|
|
|
|
if !regexp.MustCompile(`^[0-9A-Za-z=,.@-]+$`).MatchString(value) {
|
|
|
|
errors = append(errors, fmt.Errorf(
|
|
|
|
"only alphanumeric characters, hyphens, commas, periods, @ symbols and equals signs allowed in %q: %q",
|
|
|
|
k, value))
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|