github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/aws/data_source_aws_iam_account_alias.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"time"
     7  
     8  	"github.com/aws/aws-sdk-go/aws"
     9  	"github.com/aws/aws-sdk-go/service/iam"
    10  	"github.com/hashicorp/terraform/helper/schema"
    11  )
    12  
    13  func dataSourceAwsIamAccountAlias() *schema.Resource {
    14  	return &schema.Resource{
    15  		Read: dataSourceAwsIamAccountAliasRead,
    16  
    17  		Schema: map[string]*schema.Schema{
    18  			"account_alias": {
    19  				Type:     schema.TypeString,
    20  				Computed: true,
    21  			},
    22  		},
    23  	}
    24  }
    25  
    26  func dataSourceAwsIamAccountAliasRead(d *schema.ResourceData, meta interface{}) error {
    27  	conn := meta.(*AWSClient).iamconn
    28  
    29  	log.Printf("[DEBUG] Reading IAM Account Aliases.")
    30  	d.SetId(time.Now().UTC().String())
    31  
    32  	req := &iam.ListAccountAliasesInput{}
    33  	resp, err := conn.ListAccountAliases(req)
    34  	if err != nil {
    35  		return err
    36  	}
    37  	// 'AccountAliases': [] if there is no alias.
    38  	if resp == nil || len(resp.AccountAliases) == 0 {
    39  		return fmt.Errorf("no IAM account alias found")
    40  	}
    41  
    42  	alias := aws.StringValue(resp.AccountAliases[0])
    43  	log.Printf("[DEBUG] Setting AWS IAM Account Alias to %s.", alias)
    44  	d.Set("account_alias", alias)
    45  
    46  	return nil
    47  }