-
Notifications
You must be signed in to change notification settings - Fork 9.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
aws_iam_account_alias
resource (#12648)
- Loading branch information
Showing
8 changed files
with
304 additions
and
0 deletions.
There are no files selected for viewing
31 changes: 31 additions & 0 deletions
31
builtin/providers/aws/import_aws_iam_account_alias_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package aws | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform/helper/acctest" | ||
"github.com/hashicorp/terraform/helper/resource" | ||
) | ||
|
||
func TestAccAWSIAMAccountAlias_importBasic(t *testing.T) { | ||
resourceName := "aws_iam_account_alias.test" | ||
|
||
rstring := acctest.RandString(5) | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testAccCheckAWSIAMAccountAliasDestroy, | ||
Steps: []resource.TestStep{ | ||
resource.TestStep{ | ||
Config: testAccAWSIAMAccountAliasConfig(rstring), | ||
}, | ||
|
||
resource.TestStep{ | ||
ResourceName: resourceName, | ||
ImportState: true, | ||
ImportStateVerify: true, | ||
}, | ||
}, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package aws | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/service/iam" | ||
"github.com/hashicorp/terraform/helper/schema" | ||
) | ||
|
||
func resourceAwsIamAccountAlias() *schema.Resource { | ||
return &schema.Resource{ | ||
Create: resourceAwsIamAccountAliasCreate, | ||
Read: resourceAwsIamAccountAliasRead, | ||
Delete: resourceAwsIamAccountAliasDelete, | ||
|
||
Importer: &schema.ResourceImporter{ | ||
State: schema.ImportStatePassthrough, | ||
}, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"account_alias": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
ValidateFunc: validateAccountAlias, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceAwsIamAccountAliasCreate(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*AWSClient).iamconn | ||
|
||
account_alias := d.Get("account_alias").(string) | ||
|
||
params := &iam.CreateAccountAliasInput{ | ||
AccountAlias: aws.String(account_alias), | ||
} | ||
|
||
_, err := conn.CreateAccountAlias(params) | ||
|
||
if err != nil { | ||
return fmt.Errorf("Error creating account alias with name %s", account_alias) | ||
} | ||
|
||
d.SetId(account_alias) | ||
|
||
return nil | ||
} | ||
|
||
func resourceAwsIamAccountAliasRead(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*AWSClient).iamconn | ||
|
||
params := &iam.ListAccountAliasesInput{} | ||
|
||
resp, err := conn.ListAccountAliases(params) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
if resp == nil || len(resp.AccountAliases) == 0 { | ||
d.SetId("") | ||
return nil | ||
} | ||
|
||
account_alias := aws.StringValue(resp.AccountAliases[0]) | ||
|
||
d.SetId(account_alias) | ||
d.Set("account_alias", account_alias) | ||
|
||
return nil | ||
} | ||
|
||
func resourceAwsIamAccountAliasDelete(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*AWSClient).iamconn | ||
|
||
account_alias := d.Get("account_alias").(string) | ||
|
||
params := &iam.DeleteAccountAliasInput{ | ||
AccountAlias: aws.String(account_alias), | ||
} | ||
|
||
_, err := conn.DeleteAccountAlias(params) | ||
|
||
if err != nil { | ||
return fmt.Errorf("Error deleting account alias with name %s", account_alias) | ||
} | ||
|
||
d.SetId("") | ||
|
||
return nil | ||
} |
91 changes: 91 additions & 0 deletions
91
builtin/providers/aws/resource_aws_iam_account_alias_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package aws | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/service/iam" | ||
"github.com/hashicorp/terraform/helper/acctest" | ||
"github.com/hashicorp/terraform/helper/resource" | ||
"github.com/hashicorp/terraform/terraform" | ||
) | ||
|
||
func TestAccAWSIAMAccountAlias_basic(t *testing.T) { | ||
var account_alias string | ||
|
||
rstring := acctest.RandString(5) | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testAccCheckAWSIAMAccountAliasDestroy, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccAWSIAMAccountAliasConfig(rstring), | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckAWSIAMAccountAliasExists("aws_iam_account_alias.test", &account_alias), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccCheckAWSIAMAccountAliasDestroy(s *terraform.State) error { | ||
conn := testAccProvider.Meta().(*AWSClient).iamconn | ||
|
||
for _, rs := range s.RootModule().Resources { | ||
if rs.Type != "aws_iam_account_alias" { | ||
continue | ||
} | ||
|
||
params := &iam.ListAccountAliasesInput{} | ||
|
||
resp, err := conn.ListAccountAliases(params) | ||
|
||
if err != nil || resp == nil { | ||
return nil | ||
} | ||
|
||
if len(resp.AccountAliases) > 0 { | ||
return fmt.Errorf("Bad: Account alias still exists: %q", rs.Primary.ID) | ||
} | ||
} | ||
|
||
return nil | ||
|
||
} | ||
|
||
func testAccCheckAWSIAMAccountAliasExists(n string, a *string) resource.TestCheckFunc { | ||
return func(s *terraform.State) error { | ||
rs, ok := s.RootModule().Resources[n] | ||
if !ok { | ||
return fmt.Errorf("Not found: %s", n) | ||
} | ||
|
||
conn := testAccProvider.Meta().(*AWSClient).iamconn | ||
params := &iam.ListAccountAliasesInput{} | ||
|
||
resp, err := conn.ListAccountAliases(params) | ||
|
||
if err != nil || resp == nil { | ||
return nil | ||
} | ||
|
||
if len(resp.AccountAliases) == 0 { | ||
return fmt.Errorf("Bad: Account alias %q does not exist", rs.Primary.ID) | ||
} | ||
|
||
*a = aws.StringValue(resp.AccountAliases[0]) | ||
|
||
return nil | ||
} | ||
} | ||
|
||
func testAccAWSIAMAccountAliasConfig(rstring string) string { | ||
return fmt.Sprintf(` | ||
resource "aws_iam_account_alias" "test" { | ||
account_alias = "terraform-%s-alias" | ||
} | ||
`, rstring) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
website/source/docs/providers/aws/r/iam_account_alias.html.markdown
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
--- | ||
layout: "aws" | ||
page_title: "AWS: aws_iam_account_alias" | ||
sidebar_current: "docs-aws-resource-iam-account-alias" | ||
description: |- | ||
Manages the account alias for the AWS Account. | ||
--- | ||
|
||
# aws\_iam\_account\_alias | ||
|
||
-> **Note:** There is only a single account alias per AWS account. | ||
|
||
Manages the account alias for the AWS Account. | ||
|
||
## Example Usage | ||
|
||
``` | ||
resource "aws_iam_account_alias" "alias" { | ||
account_alias = "my-account-alias" | ||
} | ||
``` | ||
|
||
## Argument Reference | ||
|
||
The following arguments are supported: | ||
|
||
* `account_alias` - (Required) The account alias | ||
|
||
## Import | ||
|
||
The current Account Alias can be imported using the `account_alias`, e.g. | ||
|
||
``` | ||
$ terraform import aws_iam_account_alias.alias my-account-alias | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters