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

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"time"
     6  
     7  	"github.com/aws/aws-sdk-go/service/kms"
     8  	"github.com/hashicorp/errwrap"
     9  	"github.com/hashicorp/terraform/helper/schema"
    10  )
    11  
    12  func dataSourceAwsKmsAlias() *schema.Resource {
    13  	return &schema.Resource{
    14  		Read: dataSourceAwsKmsAliasRead,
    15  		Schema: map[string]*schema.Schema{
    16  			"name": {
    17  				Type:         schema.TypeString,
    18  				Required:     true,
    19  				ValidateFunc: validateAwsKmsName,
    20  			},
    21  			"arn": {
    22  				Type:     schema.TypeString,
    23  				Computed: true,
    24  			},
    25  			"target_key_id": {
    26  				Type:     schema.TypeString,
    27  				Computed: true,
    28  			},
    29  		},
    30  	}
    31  }
    32  
    33  func dataSourceAwsKmsAliasRead(d *schema.ResourceData, meta interface{}) error {
    34  	conn := meta.(*AWSClient).kmsconn
    35  	params := &kms.ListAliasesInput{}
    36  
    37  	target := d.Get("name")
    38  	var alias *kms.AliasListEntry
    39  	err := conn.ListAliasesPages(params, func(page *kms.ListAliasesOutput, lastPage bool) bool {
    40  		for _, entity := range page.Aliases {
    41  			if *entity.AliasName == target {
    42  				alias = entity
    43  				return false
    44  			}
    45  		}
    46  
    47  		return true
    48  	})
    49  	if err != nil {
    50  		return errwrap.Wrapf("Error fetch KMS alias list: {{err}}", err)
    51  	}
    52  
    53  	if alias == nil {
    54  		return fmt.Errorf("No alias with name %q found in this region.", target)
    55  	}
    56  
    57  	d.SetId(time.Now().UTC().String())
    58  	d.Set("arn", alias.AliasArn)
    59  	d.Set("target_key_id", alias.TargetKeyId)
    60  
    61  	return nil
    62  }