-
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' data source. (#10804)
- Loading branch information
Showing
5 changed files
with
131 additions
and
2 deletions.
There are no files selected for viewing
47 changes: 47 additions & 0 deletions
47
builtin/providers/aws/data_source_aws_iam_account_alias.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,47 @@ | ||
package aws | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"time" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/service/iam" | ||
"github.com/hashicorp/terraform/helper/schema" | ||
) | ||
|
||
func dataSourceAwsIamAccountAlias() *schema.Resource { | ||
return &schema.Resource{ | ||
Read: dataSourceAwsIamAccountAliasRead, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"account_alias": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func dataSourceAwsIamAccountAliasRead(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*AWSClient).iamconn | ||
|
||
log.Printf("[DEBUG] Reading IAM Account Aliases.") | ||
d.SetId(time.Now().UTC().String()) | ||
|
||
req := &iam.ListAccountAliasesInput{} | ||
resp, err := conn.ListAccountAliases(req) | ||
if err != nil { | ||
return err | ||
} | ||
// 'AccountAliases': [] if there is no alias. | ||
if resp == nil || len(resp.AccountAliases) == 0 { | ||
return fmt.Errorf("no IAM account alias found") | ||
} | ||
|
||
alias := aws.StringValue(resp.AccountAliases[0]) | ||
log.Printf("[DEBUG] Setting AWS IAM Account Alias to %s.", alias) | ||
d.Set("account_alias", alias) | ||
|
||
return nil | ||
} |
43 changes: 43 additions & 0 deletions
43
builtin/providers/aws/data_source_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,43 @@ | ||
package aws | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform/helper/resource" | ||
"github.com/hashicorp/terraform/terraform" | ||
) | ||
|
||
func TestAccAWSIamAccountAlias_basic(t *testing.T) { | ||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccCheckAwsIamAccountAliasConfig_basic, | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckAwsIamAccountAlias("data.aws_iam_account_alias.current"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccCheckAwsIamAccountAlias(n string) resource.TestCheckFunc { | ||
return func(s *terraform.State) error { | ||
rs, ok := s.RootModule().Resources[n] | ||
if !ok { | ||
return fmt.Errorf("Can't find Account Alias resource: %s", n) | ||
} | ||
|
||
if rs.Primary.Attributes["account_alias"] == "" { | ||
return fmt.Errorf("Missing Account Alias") | ||
} | ||
|
||
return nil | ||
} | ||
} | ||
|
||
const testAccCheckAwsIamAccountAliasConfig_basic = ` | ||
data "aws_iam_account_alias" "current" { } | ||
` |
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
33 changes: 33 additions & 0 deletions
33
website/source/docs/providers/aws/d/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,33 @@ | ||
--- | ||
layout: "aws" | ||
page_title: "AWS: aws_iam_account_alias" | ||
sidebar_current: "docs-aws-datasource-iam-account-alias" | ||
description: |- | ||
Provides the account alias for the AWS account associated with the provider | ||
connection to AWS. | ||
--- | ||
|
||
# aws\_iam\_account\_alias | ||
|
||
The IAM Account Alias data source allows access to the account alias | ||
for the effective account in which Terraform is working. | ||
|
||
## Example Usage | ||
|
||
``` | ||
data "aws_iam_account_alias" "current" { } | ||
output "account_id" { | ||
value = "${data.aws_iam_account_alias.current.account_alias}" | ||
} | ||
``` | ||
|
||
## Argument Reference | ||
|
||
There are no arguments available for this data source. | ||
|
||
## Attributes Reference | ||
|
||
The following attributes are exported: | ||
|
||
* `account_alias` - The alias associated with the AWS account. |
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