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

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/aws/aws-sdk-go/aws"
     7  	"github.com/aws/aws-sdk-go/service/iam"
     8  	"github.com/hashicorp/terraform/helper/schema"
     9  )
    10  
    11  func resourceAwsIamAccountAlias() *schema.Resource {
    12  	return &schema.Resource{
    13  		Create: resourceAwsIamAccountAliasCreate,
    14  		Read:   resourceAwsIamAccountAliasRead,
    15  		Delete: resourceAwsIamAccountAliasDelete,
    16  
    17  		Importer: &schema.ResourceImporter{
    18  			State: schema.ImportStatePassthrough,
    19  		},
    20  
    21  		Schema: map[string]*schema.Schema{
    22  			"account_alias": {
    23  				Type:         schema.TypeString,
    24  				Required:     true,
    25  				ForceNew:     true,
    26  				ValidateFunc: validateAccountAlias,
    27  			},
    28  		},
    29  	}
    30  }
    31  
    32  func resourceAwsIamAccountAliasCreate(d *schema.ResourceData, meta interface{}) error {
    33  	conn := meta.(*AWSClient).iamconn
    34  
    35  	account_alias := d.Get("account_alias").(string)
    36  
    37  	params := &iam.CreateAccountAliasInput{
    38  		AccountAlias: aws.String(account_alias),
    39  	}
    40  
    41  	_, err := conn.CreateAccountAlias(params)
    42  
    43  	if err != nil {
    44  		return fmt.Errorf("Error creating account alias with name %s", account_alias)
    45  	}
    46  
    47  	d.SetId(account_alias)
    48  
    49  	return nil
    50  }
    51  
    52  func resourceAwsIamAccountAliasRead(d *schema.ResourceData, meta interface{}) error {
    53  	conn := meta.(*AWSClient).iamconn
    54  
    55  	params := &iam.ListAccountAliasesInput{}
    56  
    57  	resp, err := conn.ListAccountAliases(params)
    58  
    59  	if err != nil {
    60  		return err
    61  	}
    62  
    63  	if resp == nil || len(resp.AccountAliases) == 0 {
    64  		d.SetId("")
    65  		return nil
    66  	}
    67  
    68  	account_alias := aws.StringValue(resp.AccountAliases[0])
    69  
    70  	d.SetId(account_alias)
    71  	d.Set("account_alias", account_alias)
    72  
    73  	return nil
    74  }
    75  
    76  func resourceAwsIamAccountAliasDelete(d *schema.ResourceData, meta interface{}) error {
    77  	conn := meta.(*AWSClient).iamconn
    78  
    79  	account_alias := d.Get("account_alias").(string)
    80  
    81  	params := &iam.DeleteAccountAliasInput{
    82  		AccountAlias: aws.String(account_alias),
    83  	}
    84  
    85  	_, err := conn.DeleteAccountAlias(params)
    86  
    87  	if err != nil {
    88  		return fmt.Errorf("Error deleting account alias with name %s", account_alias)
    89  	}
    90  
    91  	d.SetId("")
    92  
    93  	return nil
    94  }