github.com/gabrielperezs/terraform@v0.7.0-rc2.0.20160715084931-f7da2612946f/builtin/providers/aws/resource_aws_eip_association.go (about) 1 package aws 2 3 import ( 4 "fmt" 5 "log" 6 7 "github.com/aws/aws-sdk-go/aws" 8 "github.com/aws/aws-sdk-go/aws/awserr" 9 "github.com/aws/aws-sdk-go/service/ec2" 10 "github.com/hashicorp/terraform/helper/schema" 11 ) 12 13 func resourceAwsEipAssociation() *schema.Resource { 14 return &schema.Resource{ 15 Create: resourceAwsEipAssociationCreate, 16 Read: resourceAwsEipAssociationRead, 17 Delete: resourceAwsEipAssociationDelete, 18 19 Schema: map[string]*schema.Schema{ 20 "allocation_id": &schema.Schema{ 21 Type: schema.TypeString, 22 Optional: true, 23 Computed: true, 24 ForceNew: true, 25 }, 26 27 "allow_reassociation": &schema.Schema{ 28 Type: schema.TypeBool, 29 Optional: true, 30 ForceNew: true, 31 }, 32 33 "instance_id": &schema.Schema{ 34 Type: schema.TypeString, 35 Optional: true, 36 Computed: true, 37 ForceNew: true, 38 }, 39 40 "network_interface_id": &schema.Schema{ 41 Type: schema.TypeString, 42 Optional: true, 43 Computed: true, 44 ForceNew: true, 45 }, 46 47 "private_ip_address": &schema.Schema{ 48 Type: schema.TypeString, 49 Optional: true, 50 Computed: true, 51 ForceNew: true, 52 }, 53 54 "public_ip": &schema.Schema{ 55 Type: schema.TypeString, 56 Optional: true, 57 Computed: true, 58 ForceNew: true, 59 }, 60 }, 61 } 62 } 63 64 func resourceAwsEipAssociationCreate(d *schema.ResourceData, meta interface{}) error { 65 conn := meta.(*AWSClient).ec2conn 66 67 request := &ec2.AssociateAddressInput{} 68 69 if v, ok := d.GetOk("allocation_id"); ok { 70 request.AllocationId = aws.String(v.(string)) 71 } 72 if v, ok := d.GetOk("allow_reassociation"); ok { 73 request.AllowReassociation = aws.Bool(v.(bool)) 74 } 75 if v, ok := d.GetOk("instance_id"); ok { 76 request.InstanceId = aws.String(v.(string)) 77 } 78 if v, ok := d.GetOk("network_interface_id"); ok { 79 request.NetworkInterfaceId = aws.String(v.(string)) 80 } 81 if v, ok := d.GetOk("private_ip_address"); ok { 82 request.PrivateIpAddress = aws.String(v.(string)) 83 } 84 if v, ok := d.GetOk("public_ip"); ok { 85 request.PublicIp = aws.String(v.(string)) 86 } 87 88 log.Printf("[DEBUG] EIP association configuration: %#v", request) 89 90 resp, err := conn.AssociateAddress(request) 91 if err != nil { 92 if awsErr, ok := err.(awserr.Error); ok { 93 return fmt.Errorf("[WARN] Error attaching EIP, message: \"%s\", code: \"%s\"", 94 awsErr.Message(), awsErr.Code()) 95 } 96 return err 97 } 98 99 d.SetId(*resp.AssociationId) 100 101 return resourceAwsEipAssociationRead(d, meta) 102 } 103 104 func resourceAwsEipAssociationRead(d *schema.ResourceData, meta interface{}) error { 105 conn := meta.(*AWSClient).ec2conn 106 107 request := &ec2.DescribeAddressesInput{ 108 Filters: []*ec2.Filter{ 109 &ec2.Filter{ 110 Name: aws.String("association-id"), 111 Values: []*string{aws.String(d.Id())}, 112 }, 113 }, 114 } 115 116 response, err := conn.DescribeAddresses(request) 117 if err != nil { 118 return fmt.Errorf("Error reading EC2 Elastic IP %s: %#v", d.Get("allocation_id").(string), err) 119 } 120 121 if response.Addresses == nil || len(response.Addresses) == 0 { 122 return fmt.Errorf("Unable to find EIP Association: %s", d.Id()) 123 } 124 125 return readAwsEipAssociation(d, response.Addresses[0]) 126 } 127 128 func resourceAwsEipAssociationDelete(d *schema.ResourceData, meta interface{}) error { 129 conn := meta.(*AWSClient).ec2conn 130 131 opts := &ec2.DisassociateAddressInput{ 132 AssociationId: aws.String(d.Id()), 133 } 134 135 _, err := conn.DisassociateAddress(opts) 136 if err != nil { 137 return fmt.Errorf("Error deleting Elastic IP association: %s", err) 138 } 139 140 return nil 141 } 142 143 func readAwsEipAssociation(d *schema.ResourceData, address *ec2.Address) error { 144 if err := d.Set("allocation_id", address.AllocationId); err != nil { 145 return err 146 } 147 if err := d.Set("instance_id", address.InstanceId); err != nil { 148 return err 149 } 150 if err := d.Set("network_interface_id", address.NetworkInterfaceId); err != nil { 151 return err 152 } 153 if err := d.Set("private_ip_address", address.PrivateIpAddress); err != nil { 154 return err 155 } 156 if err := d.Set("public_ip", address.PublicIp); err != nil { 157 return err 158 } 159 160 return nil 161 }