github.com/bendemaree/terraform@v0.5.4-0.20150613200311-f50d97d6eee6/builtin/providers/aws/resource_aws_network_interface.go (about) 1 package aws 2 3 import ( 4 "bytes" 5 "fmt" 6 "log" 7 "strconv" 8 "time" 9 10 "github.com/aws/aws-sdk-go/aws" 11 "github.com/aws/aws-sdk-go/aws/awserr" 12 "github.com/aws/aws-sdk-go/service/ec2" 13 "github.com/hashicorp/terraform/helper/hashcode" 14 "github.com/hashicorp/terraform/helper/resource" 15 "github.com/hashicorp/terraform/helper/schema" 16 ) 17 18 func resourceAwsNetworkInterface() *schema.Resource { 19 return &schema.Resource{ 20 Create: resourceAwsNetworkInterfaceCreate, 21 Read: resourceAwsNetworkInterfaceRead, 22 Update: resourceAwsNetworkInterfaceUpdate, 23 Delete: resourceAwsNetworkInterfaceDelete, 24 25 Schema: map[string]*schema.Schema{ 26 27 "subnet_id": &schema.Schema{ 28 Type: schema.TypeString, 29 Required: true, 30 ForceNew: true, 31 }, 32 33 "private_ips": &schema.Schema{ 34 Type: schema.TypeSet, 35 Optional: true, 36 ForceNew: true, 37 Elem: &schema.Schema{Type: schema.TypeString}, 38 Set: schema.HashString, 39 }, 40 41 "security_groups": &schema.Schema{ 42 Type: schema.TypeSet, 43 Optional: true, 44 Computed: true, 45 Elem: &schema.Schema{Type: schema.TypeString}, 46 Set: schema.HashString, 47 }, 48 49 "attachment": &schema.Schema{ 50 Type: schema.TypeSet, 51 Optional: true, 52 Elem: &schema.Resource{ 53 Schema: map[string]*schema.Schema{ 54 "instance": &schema.Schema{ 55 Type: schema.TypeString, 56 Required: true, 57 }, 58 "device_index": &schema.Schema{ 59 Type: schema.TypeInt, 60 Required: true, 61 }, 62 "attachment_id": &schema.Schema{ 63 Type: schema.TypeString, 64 Computed: true, 65 }, 66 }, 67 }, 68 Set: resourceAwsEniAttachmentHash, 69 }, 70 71 "tags": tagsSchema(), 72 }, 73 } 74 } 75 76 func resourceAwsNetworkInterfaceCreate(d *schema.ResourceData, meta interface{}) error { 77 78 conn := meta.(*AWSClient).ec2conn 79 80 request := &ec2.CreateNetworkInterfaceInput{ 81 SubnetID: aws.String(d.Get("subnet_id").(string)), 82 } 83 84 security_groups := d.Get("security_groups").(*schema.Set).List() 85 if len(security_groups) != 0 { 86 request.Groups = expandStringList(security_groups) 87 } 88 89 private_ips := d.Get("private_ips").(*schema.Set).List() 90 if len(private_ips) != 0 { 91 request.PrivateIPAddresses = expandPrivateIPAddesses(private_ips) 92 } 93 94 log.Printf("[DEBUG] Creating network interface") 95 resp, err := conn.CreateNetworkInterface(request) 96 if err != nil { 97 return fmt.Errorf("Error creating ENI: %s", err) 98 } 99 100 d.SetId(*resp.NetworkInterface.NetworkInterfaceID) 101 log.Printf("[INFO] ENI ID: %s", d.Id()) 102 return resourceAwsNetworkInterfaceUpdate(d, meta) 103 } 104 105 func resourceAwsNetworkInterfaceRead(d *schema.ResourceData, meta interface{}) error { 106 107 conn := meta.(*AWSClient).ec2conn 108 describe_network_interfaces_request := &ec2.DescribeNetworkInterfacesInput{ 109 NetworkInterfaceIDs: []*string{aws.String(d.Id())}, 110 } 111 describeResp, err := conn.DescribeNetworkInterfaces(describe_network_interfaces_request) 112 113 if err != nil { 114 if ec2err, ok := err.(awserr.Error); ok && ec2err.Code() == "InvalidNetworkInterfaceID.NotFound" { 115 // The ENI is gone now, so just remove it from the state 116 d.SetId("") 117 return nil 118 } 119 120 return fmt.Errorf("Error retrieving ENI: %s", err) 121 } 122 if len(describeResp.NetworkInterfaces) != 1 { 123 return fmt.Errorf("Unable to find ENI: %#v", describeResp.NetworkInterfaces) 124 } 125 126 eni := describeResp.NetworkInterfaces[0] 127 d.Set("subnet_id", eni.SubnetID) 128 d.Set("private_ips", flattenNetworkInterfacesPrivateIPAddesses(eni.PrivateIPAddresses)) 129 d.Set("security_groups", flattenGroupIdentifiers(eni.Groups)) 130 131 // Tags 132 d.Set("tags", tagsToMap(eni.TagSet)) 133 134 if eni.Attachment != nil { 135 attachment := []map[string]interface{}{flattenAttachment(eni.Attachment)} 136 d.Set("attachment", attachment) 137 } else { 138 d.Set("attachment", nil) 139 } 140 141 return nil 142 } 143 144 func networkInterfaceAttachmentRefreshFunc(conn *ec2.EC2, id string) resource.StateRefreshFunc { 145 return func() (interface{}, string, error) { 146 147 describe_network_interfaces_request := &ec2.DescribeNetworkInterfacesInput{ 148 NetworkInterfaceIDs: []*string{aws.String(id)}, 149 } 150 describeResp, err := conn.DescribeNetworkInterfaces(describe_network_interfaces_request) 151 152 if err != nil { 153 log.Printf("[ERROR] Could not find network interface %s. %s", id, err) 154 return nil, "", err 155 } 156 157 eni := describeResp.NetworkInterfaces[0] 158 hasAttachment := strconv.FormatBool(eni.Attachment != nil) 159 log.Printf("[DEBUG] ENI %s has attachment state %s", id, hasAttachment) 160 return eni, hasAttachment, nil 161 } 162 } 163 164 func resourceAwsNetworkInterfaceDetach(oa *schema.Set, meta interface{}, eniId string) error { 165 // if there was an old attachment, remove it 166 if oa != nil && len(oa.List()) > 0 { 167 old_attachment := oa.List()[0].(map[string]interface{}) 168 detach_request := &ec2.DetachNetworkInterfaceInput{ 169 AttachmentID: aws.String(old_attachment["attachment_id"].(string)), 170 Force: aws.Boolean(true), 171 } 172 conn := meta.(*AWSClient).ec2conn 173 _, detach_err := conn.DetachNetworkInterface(detach_request) 174 if detach_err != nil { 175 return fmt.Errorf("Error detaching ENI: %s", detach_err) 176 } 177 178 log.Printf("[DEBUG] Waiting for ENI (%s) to become dettached", eniId) 179 stateConf := &resource.StateChangeConf{ 180 Pending: []string{"true"}, 181 Target: "false", 182 Refresh: networkInterfaceAttachmentRefreshFunc(conn, eniId), 183 Timeout: 10 * time.Minute, 184 } 185 if _, err := stateConf.WaitForState(); err != nil { 186 return fmt.Errorf( 187 "Error waiting for ENI (%s) to become dettached: %s", eniId, err) 188 } 189 } 190 191 return nil 192 } 193 194 func resourceAwsNetworkInterfaceUpdate(d *schema.ResourceData, meta interface{}) error { 195 conn := meta.(*AWSClient).ec2conn 196 d.Partial(true) 197 198 if d.HasChange("attachment") { 199 oa, na := d.GetChange("attachment") 200 201 detach_err := resourceAwsNetworkInterfaceDetach(oa.(*schema.Set), meta, d.Id()) 202 if detach_err != nil { 203 return detach_err 204 } 205 206 // if there is a new attachment, attach it 207 if na != nil && len(na.(*schema.Set).List()) > 0 { 208 new_attachment := na.(*schema.Set).List()[0].(map[string]interface{}) 209 di := new_attachment["device_index"].(int) 210 attach_request := &ec2.AttachNetworkInterfaceInput{ 211 DeviceIndex: aws.Long(int64(di)), 212 InstanceID: aws.String(new_attachment["instance"].(string)), 213 NetworkInterfaceID: aws.String(d.Id()), 214 } 215 _, attach_err := conn.AttachNetworkInterface(attach_request) 216 if attach_err != nil { 217 return fmt.Errorf("Error attaching ENI: %s", attach_err) 218 } 219 } 220 221 d.SetPartial("attachment") 222 } 223 224 if d.HasChange("security_groups") { 225 request := &ec2.ModifyNetworkInterfaceAttributeInput{ 226 NetworkInterfaceID: aws.String(d.Id()), 227 Groups: expandStringList(d.Get("security_groups").(*schema.Set).List()), 228 } 229 230 _, err := conn.ModifyNetworkInterfaceAttribute(request) 231 if err != nil { 232 return fmt.Errorf("Failure updating ENI: %s", err) 233 } 234 235 d.SetPartial("security_groups") 236 } 237 238 if err := setTags(conn, d); err != nil { 239 return err 240 } else { 241 d.SetPartial("tags") 242 } 243 244 d.Partial(false) 245 246 return resourceAwsNetworkInterfaceRead(d, meta) 247 } 248 249 func resourceAwsNetworkInterfaceDelete(d *schema.ResourceData, meta interface{}) error { 250 conn := meta.(*AWSClient).ec2conn 251 252 log.Printf("[INFO] Deleting ENI: %s", d.Id()) 253 254 detach_err := resourceAwsNetworkInterfaceDetach(d.Get("attachment").(*schema.Set), meta, d.Id()) 255 if detach_err != nil { 256 return detach_err 257 } 258 259 deleteEniOpts := ec2.DeleteNetworkInterfaceInput{ 260 NetworkInterfaceID: aws.String(d.Id()), 261 } 262 if _, err := conn.DeleteNetworkInterface(&deleteEniOpts); err != nil { 263 return fmt.Errorf("Error deleting ENI: %s", err) 264 } 265 266 return nil 267 } 268 269 func resourceAwsEniAttachmentHash(v interface{}) int { 270 var buf bytes.Buffer 271 m := v.(map[string]interface{}) 272 buf.WriteString(fmt.Sprintf("%s-", m["instance"].(string))) 273 buf.WriteString(fmt.Sprintf("%d-", m["device_index"].(int))) 274 return hashcode.String(buf.String()) 275 }