Skip to content

Commit 90bdaef

Browse files
authored
Merge pull request hashicorp#8206 from hashicorp/f-aws-account-id
provider/aws: Add aws_account_id data source
2 parents 4b93f63 + 3e14f56 commit 90bdaef

5 files changed

Lines changed: 128 additions & 0 deletions

File tree

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package aws
2+
3+
import (
4+
"fmt"
5+
"log"
6+
"time"
7+
8+
"github.com/hashicorp/terraform/helper/schema"
9+
)
10+
11+
func dataSourceAwsCallerIdentity() *schema.Resource {
12+
return &schema.Resource{
13+
Read: dataSourceAwsCallerIdentityRead,
14+
15+
Schema: map[string]*schema.Schema{
16+
"account_id": {
17+
Type: schema.TypeString,
18+
Computed: true,
19+
},
20+
},
21+
}
22+
}
23+
24+
func dataSourceAwsCallerIdentityRead(d *schema.ResourceData, meta interface{}) error {
25+
client := meta.(*AWSClient)
26+
27+
log.Printf("[DEBUG] Reading Caller Identity.")
28+
d.SetId(time.Now().UTC().String())
29+
30+
if client.accountid == "" {
31+
log.Println("[DEBUG] No Account ID available, failing")
32+
return fmt.Errorf("No AWS Account ID is available to the provider. Please ensure that\n" +
33+
"skip_requesting_account_id is not set on the AWS provider.")
34+
}
35+
36+
log.Printf("[DEBUG] Setting AWS Account ID to %s.", client.accountid)
37+
d.Set("account_id", meta.(*AWSClient).accountid)
38+
39+
return nil
40+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package aws
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/hashicorp/terraform/helper/resource"
8+
"github.com/hashicorp/terraform/terraform"
9+
)
10+
11+
func TestAccAWSCallerIdentity_basic(t *testing.T) {
12+
resource.Test(t, resource.TestCase{
13+
PreCheck: func() { testAccPreCheck(t) },
14+
Providers: testAccProviders,
15+
Steps: []resource.TestStep{
16+
{
17+
Config: testAccCheckAwsCallerIdentityConfig_basic,
18+
Check: resource.ComposeTestCheckFunc(
19+
testAccCheckAwsCallerIdentityAccountId("data.aws_caller_identity.current"),
20+
),
21+
},
22+
},
23+
})
24+
}
25+
26+
func testAccCheckAwsCallerIdentityAccountId(n string) resource.TestCheckFunc {
27+
return func(s *terraform.State) error {
28+
rs, ok := s.RootModule().Resources[n]
29+
if !ok {
30+
return fmt.Errorf("Can't find AccountID resource: %s", n)
31+
}
32+
33+
if rs.Primary.ID == "" {
34+
return fmt.Errorf("Account Id resource ID not set.")
35+
}
36+
37+
expected := testAccProvider.Meta().(*AWSClient).accountid
38+
if rs.Primary.Attributes["account_id"] != expected {
39+
return fmt.Errorf("Incorrect Account ID: expected %q, got %q", expected, rs.Primary.ID)
40+
}
41+
42+
return nil
43+
}
44+
}
45+
46+
const testAccCheckAwsCallerIdentityConfig_basic = `
47+
data "aws_caller_identity" "current" { }
48+
`

builtin/providers/aws/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ func Provider() terraform.ResourceProvider {
142142
DataSourcesMap: map[string]*schema.Resource{
143143
"aws_ami": dataSourceAwsAmi(),
144144
"aws_availability_zones": dataSourceAwsAvailabilityZones(),
145+
"aws_caller_identity": dataSourceAwsCallerIdentity(),
145146
"aws_iam_policy_document": dataSourceAwsIamPolicyDocument(),
146147
"aws_ip_ranges": dataSourceAwsIPRanges(),
147148
"aws_s3_bucket_object": dataSourceAwsS3BucketObject(),
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
---
2+
layout: "aws"
3+
page_title: "AWS: aws_caller_identity"
4+
sidebar_current: "docs-aws-datasource-caller-identity"
5+
description: |-
6+
Get information about the identity of the caller for the provider
7+
connection to AWS.
8+
---
9+
10+
# aws\_caller\_identity
11+
12+
Use this data source to get the access to the effective Account ID in
13+
which Terraform is working.
14+
15+
~> **NOTE on `aws_caller_identity`:** - an Account ID is only available
16+
if `skip_requesting_account_id` is not set on the AWS provider. In such
17+
cases, the data source will return an error.
18+
19+
## Example Usage
20+
21+
```
22+
data "aws_caller_identity" "current" { }
23+
24+
output "account_id" {
25+
value = "${data.aws_caller_identity.current.account_id}"
26+
}
27+
```
28+
29+
## Argument Reference
30+
31+
There are no arguments available for this data source.
32+
33+
## Attributes Reference
34+
35+
`account_id` is set to the ID of the AWS account.

website/source/layouts/aws.erb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,16 @@
1313
<li<%= sidebar_current(/^docs-aws-datasource/) %>>
1414
<a href="#">Data Sources</a>
1515
<ul class="nav nav-visible">
16+
1617
<li<%= sidebar_current("docs-aws-datasource-ami") %>>
1718
<a href="/docs/providers/aws/d/ami.html">aws_ami</a>
1819
</li>
1920
<li<%= sidebar_current("docs-aws-datasource-availability-zones") %>>
2021
<a href="/docs/providers/aws/d/availability_zones.html">aws_availability_zones</a>
2122
</li>
23+
<li<%= sidebar_current("docs-aws-datasource-caller-identity") %>>
24+
<a href="/docs/providers/aws/d/caller_identity.html">aws_caller_identity</a>
25+
</li>
2226
<li<%= sidebar_current("docs-aws-datasource-ecs-container-definition") %>>
2327
<a href="/docs/providers/aws/d/ecs_container_definition.html">aws_ecs_container_definition</a>
2428
</li>

0 commit comments

Comments
 (0)