Skip to content

Commit

Permalink
Add 'aws_iam_account_alias' data source. (#10804)
Browse files Browse the repository at this point in the history
  • Loading branch information
ewbankkit authored and stack72 committed Dec 18, 2016
1 parent 717131f commit ca18312
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 2 deletions.
47 changes: 47 additions & 0 deletions builtin/providers/aws/data_source_aws_iam_account_alias.go
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 builtin/providers/aws/data_source_aws_iam_account_alias_test.go
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" { }
`
3 changes: 2 additions & 1 deletion builtin/providers/aws/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,14 +157,15 @@ func Provider() terraform.ResourceProvider {
"aws_ecs_container_definition": dataSourceAwsEcsContainerDefinition(),
"aws_eip": dataSourceAwsEip(),
"aws_elb_service_account": dataSourceAwsElbServiceAccount(),
"aws_route53_zone": dataSourceAwsRoute53Zone(),
"aws_iam_account_alias": dataSourceAwsIamAccountAlias(),
"aws_iam_policy_document": dataSourceAwsIamPolicyDocument(),
"aws_iam_server_certificate": dataSourceAwsIAMServerCertificate(),
"aws_ip_ranges": dataSourceAwsIPRanges(),
"aws_prefix_list": dataSourceAwsPrefixList(),
"aws_redshift_service_account": dataSourceAwsRedshiftServiceAccount(),
"aws_region": dataSourceAwsRegion(),
"aws_route_table": dataSourceAwsRouteTable(),
"aws_route53_zone": dataSourceAwsRoute53Zone(),
"aws_s3_bucket_object": dataSourceAwsS3BucketObject(),
"aws_subnet": dataSourceAwsSubnet(),
"aws_security_group": dataSourceAwsSecurityGroup(),
Expand Down
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.
7 changes: 6 additions & 1 deletion website/source/layouts/aws.erb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
<li<%= sidebar_current("docs-aws-datasource-alb") %>>
<a href="/docs/providers/aws/d/alb.html">aws_alb</a>
</li>

<li<%= sidebar_current("docs-aws-datasource-ami") %>>
<a href="/docs/providers/aws/d/ami.html">aws_ami</a>
</li>
Expand Down Expand Up @@ -51,6 +50,12 @@
<li<%= sidebar_current("docs-aws-datasource-elb-service-account") %>>
<a href="/docs/providers/aws/d/elb_service_account.html">aws_elb_service_account</a>
</li>
<li<%= sidebar_current("docs-aws-datasource-hosted-zone") %>>
<a href="/docs/providers/aws/d/hosted_zone.html">aws_hosted_zone</a>
</li>
<li<%= sidebar_current("docs-aws-datasource-iam-account-alias") %>>
<a href="/docs/providers/aws/d/iam_account_alias.html">aws_iam_account_alias</a>
</li>
<li<%= sidebar_current("docs-aws-datasource-iam-policy-document") %>>
<a href="/docs/providers/aws/d/iam_policy_document.html">aws_iam_policy_document</a>
</li>
Expand Down

0 comments on commit ca18312

Please sign in to comment.