2016-08-15 23:43:24 +02:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"time"
|
|
|
|
|
2017-03-27 15:32:31 +02:00
|
|
|
"github.com/aws/aws-sdk-go/service/sts"
|
2016-08-15 23:43:24 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
|
|
|
)
|
|
|
|
|
|
|
|
func dataSourceAwsCallerIdentity() *schema.Resource {
|
|
|
|
return &schema.Resource{
|
|
|
|
Read: dataSourceAwsCallerIdentityRead,
|
|
|
|
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"account_id": {
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
2017-03-27 15:32:31 +02:00
|
|
|
|
|
|
|
"arn": {
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"user_id": {
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
2016-08-15 23:43:24 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func dataSourceAwsCallerIdentityRead(d *schema.ResourceData, meta interface{}) error {
|
2017-03-27 15:32:31 +02:00
|
|
|
client := meta.(*AWSClient).stsconn
|
|
|
|
|
|
|
|
res, err := client.GetCallerIdentity(&sts.GetCallerIdentityInput{})
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error getting Caller Identity: %v", err)
|
|
|
|
}
|
2016-08-15 23:43:24 +02:00
|
|
|
|
|
|
|
log.Printf("[DEBUG] Reading Caller Identity.")
|
|
|
|
d.SetId(time.Now().UTC().String())
|
|
|
|
|
2017-03-27 15:32:31 +02:00
|
|
|
if *res.Account == "" {
|
2016-08-15 23:43:24 +02:00
|
|
|
log.Println("[DEBUG] No Account ID available, failing")
|
|
|
|
return fmt.Errorf("No AWS Account ID is available to the provider. Please ensure that\n" +
|
|
|
|
"skip_requesting_account_id is not set on the AWS provider.")
|
|
|
|
}
|
|
|
|
|
2017-03-27 15:32:31 +02:00
|
|
|
log.Printf("[DEBUG] Setting AWS Account ID to %s.", *res.Account)
|
2017-04-03 20:10:57 +02:00
|
|
|
d.Set("account_id", res.Account)
|
|
|
|
d.Set("arn", res.Arn)
|
|
|
|
d.Set("user_id", res.UserId)
|
2016-08-15 23:43:24 +02:00
|
|
|
return nil
|
|
|
|
}
|