From f5267dfa4477f14975b124710cbc696796950857 Mon Sep 17 00:00:00 2001 From: Patrick Gray Date: Tue, 15 Sep 2015 01:00:22 -0400 Subject: [PATCH 1/4] add support for group name and path changes with group update function --- .../providers/aws/resource_aws_iam_group.go | 35 +++++++++++++++---- .../aws/resource_aws_iam_group_test.go | 13 +++++++ 2 files changed, 42 insertions(+), 6 deletions(-) diff --git a/builtin/providers/aws/resource_aws_iam_group.go b/builtin/providers/aws/resource_aws_iam_group.go index 45defaaaf9cb..c34137663f8d 100644 --- a/builtin/providers/aws/resource_aws_iam_group.go +++ b/builtin/providers/aws/resource_aws_iam_group.go @@ -14,8 +14,7 @@ func resourceAwsIamGroup() *schema.Resource { return &schema.Resource{ Create: resourceAwsIamGroupCreate, Read: resourceAwsIamGroupRead, - // TODO - //Update: resourceAwsIamGroupUpdate, + Update: resourceAwsIamGroupUpdate, Delete: resourceAwsIamGroupDelete, Schema: map[string]*schema.Schema{ @@ -30,13 +29,11 @@ func resourceAwsIamGroup() *schema.Resource { "name": &schema.Schema{ Type: schema.TypeString, Required: true, - ForceNew: true, }, "path": &schema.Schema{ Type: schema.TypeString, Optional: true, Default: "/", - ForceNew: true, }, }, } @@ -45,9 +42,10 @@ func resourceAwsIamGroup() *schema.Resource { func resourceAwsIamGroupCreate(d *schema.ResourceData, meta interface{}) error { iamconn := meta.(*AWSClient).iamconn name := d.Get("name").(string) + path := d.Get("path").(string) request := &iam.CreateGroupInput{ - Path: aws.String(d.Get("path").(string)), + Path: aws.String(path), GroupName: aws.String(name), } @@ -60,9 +58,10 @@ func resourceAwsIamGroupCreate(d *schema.ResourceData, meta interface{}) error { func resourceAwsIamGroupRead(d *schema.ResourceData, meta interface{}) error { iamconn := meta.(*AWSClient).iamconn + name := d.Get("name").(string) request := &iam.GetGroupInput{ - GroupName: aws.String(d.Id()), + GroupName: aws.String(name), } getResp, err := iamconn.GetGroup(request) @@ -93,6 +92,30 @@ func resourceAwsIamGroupReadResult(d *schema.ResourceData, group *iam.Group) err return nil } +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 { + if iamerr, ok := err.(awserr.Error); ok && iamerr.Code() == "NoSuchEntity" { + d.SetId("") + return nil + } + return fmt.Errorf("Error updating IAM Group %s: %s", d.Id(), err) + } + return resourceAwsIamGroupRead(d, meta) + } + return nil +} + func resourceAwsIamGroupDelete(d *schema.ResourceData, meta interface{}) error { iamconn := meta.(*AWSClient).iamconn diff --git a/builtin/providers/aws/resource_aws_iam_group_test.go b/builtin/providers/aws/resource_aws_iam_group_test.go index 67a72733af38..c36a938b64ed 100644 --- a/builtin/providers/aws/resource_aws_iam_group_test.go +++ b/builtin/providers/aws/resource_aws_iam_group_test.go @@ -26,6 +26,13 @@ func TestAccAWSIAMGroup_basic(t *testing.T) { testAccCheckAWSGroupAttributes(&conf), ), }, + resource.TestStep{ + Config: testAccAWSGroupConfig2, + Check: resource.ComposeTestCheckFunc( + testAccCheckAWSGroupExists("aws_iam_group.group", &conf), + testAccCheckAWSGroupAttributes(&conf), + ), + }, }, }) } @@ -105,3 +112,9 @@ resource "aws_iam_group" "group" { path = "/" } ` +const testAccAWSGroupConfig2 = ` +resource "aws_iam_group" "group" { + name = "test-group2" + path = "/funnypath/" +} +` From 9ab559645868071fc2149b6087e7b34c4abafd31 Mon Sep 17 00:00:00 2001 From: Patrick Gray Date: Sun, 4 Oct 2015 21:28:28 -0400 Subject: [PATCH 2/4] will not swallow error on aws iam group update if we get NoSuchEntity error --- builtin/providers/aws/resource_aws_iam_group.go | 4 ---- 1 file changed, 4 deletions(-) diff --git a/builtin/providers/aws/resource_aws_iam_group.go b/builtin/providers/aws/resource_aws_iam_group.go index c34137663f8d..73a91b52e767 100644 --- a/builtin/providers/aws/resource_aws_iam_group.go +++ b/builtin/providers/aws/resource_aws_iam_group.go @@ -105,10 +105,6 @@ func resourceAwsIamGroupUpdate(d *schema.ResourceData, meta interface{}) error { } _, err := iamconn.UpdateGroup(request) if err != nil { - if iamerr, ok := err.(awserr.Error); ok && iamerr.Code() == "NoSuchEntity" { - d.SetId("") - return nil - } return fmt.Errorf("Error updating IAM Group %s: %s", d.Id(), err) } return resourceAwsIamGroupRead(d, meta) From 6a2d3eaa6b59e3350456ce40cbc39a1b5fb549e7 Mon Sep 17 00:00:00 2001 From: Patrick Gray Date: Sat, 31 Oct 2015 10:52:12 -0400 Subject: [PATCH 3/4] remove unneeded print function --- builtin/providers/aws/resource_aws_iam_group.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/builtin/providers/aws/resource_aws_iam_group.go b/builtin/providers/aws/resource_aws_iam_group.go index 73a91b52e767..8e29388235af 100644 --- a/builtin/providers/aws/resource_aws_iam_group.go +++ b/builtin/providers/aws/resource_aws_iam_group.go @@ -97,7 +97,7 @@ func resourceAwsIamGroupUpdate(d *schema.ResourceData, meta interface{}) error { 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)), From 8fcc13adbc2f72b4051299822692e41199c917b0 Mon Sep 17 00:00:00 2001 From: Patrick Gray Date: Sat, 31 Oct 2015 12:04:54 -0400 Subject: [PATCH 4/4] update aws_iam_group tests to check proper paths and name --- .../providers/aws/resource_aws_iam_group_test.go | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/builtin/providers/aws/resource_aws_iam_group_test.go b/builtin/providers/aws/resource_aws_iam_group_test.go index c36a938b64ed..977889bd655b 100644 --- a/builtin/providers/aws/resource_aws_iam_group_test.go +++ b/builtin/providers/aws/resource_aws_iam_group_test.go @@ -23,14 +23,14 @@ func TestAccAWSIAMGroup_basic(t *testing.T) { Config: testAccAWSGroupConfig, Check: resource.ComposeTestCheckFunc( testAccCheckAWSGroupExists("aws_iam_group.group", &conf), - testAccCheckAWSGroupAttributes(&conf), + testAccCheckAWSGroupAttributes(&conf, "test-group", "/"), ), }, resource.TestStep{ Config: testAccAWSGroupConfig2, Check: resource.ComposeTestCheckFunc( testAccCheckAWSGroupExists("aws_iam_group.group", &conf), - testAccCheckAWSGroupAttributes(&conf), + testAccCheckAWSGroupAttributes(&conf, "test-group2", "/funnypath/"), ), }, }, @@ -92,14 +92,14 @@ func testAccCheckAWSGroupExists(n string, res *iam.GetGroupOutput) resource.Test } } -func testAccCheckAWSGroupAttributes(group *iam.GetGroupOutput) resource.TestCheckFunc { +func testAccCheckAWSGroupAttributes(group *iam.GetGroupOutput, name string, path string) resource.TestCheckFunc { return func(s *terraform.State) error { - if *group.Group.GroupName != "test-group" { - return fmt.Errorf("Bad name: %s", *group.Group.GroupName) + if *group.Group.GroupName != name { + return fmt.Errorf("Bad name: %s when %s was expected", *group.Group.GroupName, name) } - if *group.Group.Path != "/" { - return fmt.Errorf("Bad path: %s", *group.Group.Path) + if *group.Group.Path != path { + return fmt.Errorf("Bad path: %s when %s was expected", *group.Group.Path, path) } return nil