github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/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 log.Printf("[INFO] EIP Association ID Not Found. Refreshing from state") 123 d.SetId("") 124 return nil 125 } 126 127 return readAwsEipAssociation(d, response.Addresses[0]) 128 } 129 130 func resourceAwsEipAssociationDelete(d *schema.ResourceData, meta interface{}) error { 131 conn := meta.(*AWSClient).ec2conn 132 133 opts := &ec2.DisassociateAddressInput{ 134 AssociationId: aws.String(d.Id()), 135 } 136 137 _, err := conn.DisassociateAddress(opts) 138 if err != nil { 139 return fmt.Errorf("Error deleting Elastic IP association: %s", err) 140 } 141 142 return nil 143 } 144 145 func readAwsEipAssociation(d *schema.ResourceData, address *ec2.Address) error { 146 if err := d.Set("allocation_id", address.AllocationId); err != nil { 147 return err 148 } 149 if err := d.Set("instance_id", address.InstanceId); err != nil { 150 return err 151 } 152 if err := d.Set("network_interface_id", address.NetworkInterfaceId); err != nil { 153 return err 154 } 155 if err := d.Set("private_ip_address", address.PrivateIpAddress); err != nil { 156 return err 157 } 158 if err := d.Set("public_ip", address.PublicIp); err != nil { 159 return err 160 } 161 162 return nil 163 }