2015-06-08 18:17:15 +02:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
2015-06-08 21:20:39 +02:00
|
|
|
"fmt"
|
2015-06-08 18:17:15 +02:00
|
|
|
|
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
2015-06-08 21:20:39 +02:00
|
|
|
"github.com/aws/aws-sdk-go/aws/awserr"
|
2015-06-08 18:17:15 +02:00
|
|
|
"github.com/aws/aws-sdk-go/service/iam"
|
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
|
|
|
)
|
|
|
|
|
|
|
|
func resourceAwsIamGroupMembership() *schema.Resource {
|
|
|
|
return &schema.Resource{
|
|
|
|
Create: resourceAwsIamGroupMembershipCreate,
|
|
|
|
Read: resourceAwsIamGroupMembershipRead,
|
2015-06-08 22:01:23 +02:00
|
|
|
Update: resourceAwsIamGroupMembershipUpdate,
|
2015-06-08 18:17:15 +02:00
|
|
|
Delete: resourceAwsIamGroupMembershipDelete,
|
|
|
|
|
|
|
|
Schema: map[string]*schema.Schema{
|
2015-06-08 21:20:39 +02:00
|
|
|
"name": &schema.Schema{
|
2015-06-08 18:17:15 +02:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
2015-06-08 21:20:39 +02:00
|
|
|
|
|
|
|
"users": &schema.Schema{
|
|
|
|
Type: schema.TypeSet,
|
|
|
|
Required: true,
|
|
|
|
Elem: &schema.Schema{Type: schema.TypeString},
|
|
|
|
Set: schema.HashString,
|
|
|
|
},
|
|
|
|
|
|
|
|
"group": &schema.Schema{
|
2015-06-08 18:17:15 +02:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceAwsIamGroupMembershipCreate(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
conn := meta.(*AWSClient).iamconn
|
|
|
|
|
2015-06-08 21:20:39 +02:00
|
|
|
group := d.Get("group").(string)
|
2015-06-08 22:01:23 +02:00
|
|
|
userList := expandStringList(d.Get("users").(*schema.Set).List())
|
2015-06-08 18:17:15 +02:00
|
|
|
|
2015-06-08 22:01:23 +02:00
|
|
|
if err := addUsersToGroup(conn, userList, group); err != nil {
|
|
|
|
return err
|
2015-06-08 18:17:15 +02:00
|
|
|
}
|
|
|
|
|
2015-06-08 21:20:39 +02:00
|
|
|
d.SetId(d.Get("name").(string))
|
2015-06-08 18:17:15 +02:00
|
|
|
return resourceAwsIamGroupMembershipRead(d, meta)
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceAwsIamGroupMembershipRead(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
conn := meta.(*AWSClient).iamconn
|
2015-06-08 22:01:23 +02:00
|
|
|
group := d.Get("group").(string)
|
2016-05-17 22:23:05 +02:00
|
|
|
|
|
|
|
var ul []string
|
|
|
|
var marker *string
|
|
|
|
for {
|
|
|
|
resp, err := conn.GetGroup(&iam.GetGroupInput{
|
|
|
|
GroupName: aws.String(group),
|
|
|
|
Marker: marker,
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
if awsErr, ok := err.(awserr.Error); ok {
|
|
|
|
// aws specific error
|
|
|
|
if awsErr.Code() == "NoSuchEntity" {
|
|
|
|
// group not found
|
|
|
|
d.SetId("")
|
|
|
|
return nil
|
|
|
|
}
|
2015-06-08 22:01:23 +02:00
|
|
|
}
|
2016-05-17 22:23:05 +02:00
|
|
|
return err
|
2015-06-08 21:20:39 +02:00
|
|
|
}
|
2015-06-08 18:17:15 +02:00
|
|
|
|
2016-05-17 22:23:05 +02:00
|
|
|
for _, u := range resp.Users {
|
|
|
|
ul = append(ul, *u.UserName)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !*resp.IsTruncated {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
marker = resp.Marker
|
2015-06-08 18:17:15 +02:00
|
|
|
}
|
|
|
|
|
2015-06-08 21:20:39 +02:00
|
|
|
if err := d.Set("users", ul); err != nil {
|
2015-06-08 22:01:23 +02:00
|
|
|
return fmt.Errorf("[WARN] Error setting user list from IAM Group Membership (%s), error: %s", group, err)
|
2015-06-08 18:17:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-06-08 22:01:23 +02:00
|
|
|
func resourceAwsIamGroupMembershipUpdate(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
conn := meta.(*AWSClient).iamconn
|
|
|
|
|
|
|
|
if d.HasChange("users") {
|
|
|
|
group := d.Get("group").(string)
|
|
|
|
|
|
|
|
o, n := d.GetChange("users")
|
|
|
|
if o == nil {
|
|
|
|
o = new(schema.Set)
|
|
|
|
}
|
|
|
|
if n == nil {
|
|
|
|
n = new(schema.Set)
|
|
|
|
}
|
|
|
|
|
|
|
|
os := o.(*schema.Set)
|
|
|
|
ns := n.(*schema.Set)
|
|
|
|
remove := expandStringList(os.Difference(ns).List())
|
|
|
|
add := expandStringList(ns.Difference(os).List())
|
|
|
|
|
|
|
|
if err := removeUsersFromGroup(conn, remove, group); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := addUsersToGroup(conn, add, group); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return resourceAwsIamGroupMembershipRead(d, meta)
|
|
|
|
}
|
|
|
|
|
2015-06-08 18:17:15 +02:00
|
|
|
func resourceAwsIamGroupMembershipDelete(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
conn := meta.(*AWSClient).iamconn
|
2015-06-08 21:20:39 +02:00
|
|
|
userList := expandStringList(d.Get("users").(*schema.Set).List())
|
|
|
|
group := d.Get("group").(string)
|
2015-06-08 18:17:15 +02:00
|
|
|
|
2015-06-08 22:01:23 +02:00
|
|
|
if err := removeUsersFromGroup(conn, userList, group); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func removeUsersFromGroup(conn *iam.IAM, users []*string, group string) error {
|
|
|
|
for _, u := range users {
|
2015-06-08 21:20:39 +02:00
|
|
|
_, err := conn.RemoveUserFromGroup(&iam.RemoveUserFromGroupInput{
|
|
|
|
UserName: u,
|
|
|
|
GroupName: aws.String(group),
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
2015-09-13 09:37:45 +02:00
|
|
|
if iamerr, ok := err.(awserr.Error); ok && iamerr.Code() == "NoSuchEntity" {
|
|
|
|
return nil
|
|
|
|
}
|
2015-06-08 21:20:39 +02:00
|
|
|
return err
|
|
|
|
}
|
2015-06-08 18:17:15 +02:00
|
|
|
}
|
2015-06-08 22:01:23 +02:00
|
|
|
return nil
|
|
|
|
}
|
2015-06-08 18:17:15 +02:00
|
|
|
|
2015-06-08 22:01:23 +02:00
|
|
|
func addUsersToGroup(conn *iam.IAM, users []*string, group string) error {
|
|
|
|
for _, u := range users {
|
|
|
|
_, err := conn.AddUserToGroup(&iam.AddUserToGroupInput{
|
|
|
|
UserName: u,
|
|
|
|
GroupName: aws.String(group),
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2015-06-08 18:17:15 +02:00
|
|
|
return nil
|
|
|
|
}
|