github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/builtin/providers/cloudstack/resource_cloudstack_nic.go (about) 1 package cloudstack 2 3 import ( 4 "fmt" 5 "log" 6 "strings" 7 8 "github.com/hashicorp/terraform/helper/schema" 9 "github.com/xanzy/go-cloudstack/cloudstack" 10 ) 11 12 func resourceCloudStackNIC() *schema.Resource { 13 return &schema.Resource{ 14 Create: resourceCloudStackNICCreate, 15 Read: resourceCloudStackNICRead, 16 Delete: resourceCloudStackNICDelete, 17 18 Schema: map[string]*schema.Schema{ 19 "network_id": &schema.Schema{ 20 Type: schema.TypeString, 21 Required: true, 22 ForceNew: true, 23 }, 24 25 "ip_address": &schema.Schema{ 26 Type: schema.TypeString, 27 Optional: true, 28 Computed: true, 29 ForceNew: true, 30 }, 31 32 "virtual_machine_id": &schema.Schema{ 33 Type: schema.TypeString, 34 Required: true, 35 ForceNew: true, 36 }, 37 }, 38 } 39 } 40 41 func resourceCloudStackNICCreate(d *schema.ResourceData, meta interface{}) error { 42 cs := meta.(*cloudstack.CloudStackClient) 43 44 // Create a new parameter struct 45 p := cs.VirtualMachine.NewAddNicToVirtualMachineParams( 46 d.Get("network_id").(string), 47 d.Get("virtual_machine_id").(string), 48 ) 49 50 // If there is a ipaddres supplied, add it to the parameter struct 51 if ipaddress, ok := d.GetOk("ip_address"); ok { 52 p.SetIpaddress(ipaddress.(string)) 53 } 54 55 // Create and attach the new NIC 56 r, err := Retry(10, retryableAddNicFunc(cs, p)) 57 if err != nil { 58 return fmt.Errorf("Error creating the new NIC: %s", err) 59 } 60 61 found := false 62 for _, n := range r.(*cloudstack.AddNicToVirtualMachineResponse).Nic { 63 if n.Networkid == d.Get("network_id").(string) { 64 d.SetId(n.Id) 65 found = true 66 break 67 } 68 } 69 70 if !found { 71 return fmt.Errorf("Could not find NIC ID for network ID: %s", d.Get("network_id").(string)) 72 } 73 74 return resourceCloudStackNICRead(d, meta) 75 } 76 77 func resourceCloudStackNICRead(d *schema.ResourceData, meta interface{}) error { 78 cs := meta.(*cloudstack.CloudStackClient) 79 80 // Get the virtual machine details 81 vm, count, err := cs.VirtualMachine.GetVirtualMachineByID(d.Get("virtual_machine_id").(string)) 82 if err != nil { 83 if count == 0 { 84 log.Printf("[DEBUG] Instance %s does no longer exist", d.Get("virtual_machine_id").(string)) 85 d.SetId("") 86 return nil 87 } 88 89 return err 90 } 91 92 // Read NIC info 93 found := false 94 for _, n := range vm.Nic { 95 if n.Id == d.Id() { 96 d.Set("ip_address", n.Ipaddress) 97 d.Set("network_id", n.Networkid) 98 d.Set("virtual_machine_id", vm.Id) 99 found = true 100 break 101 } 102 } 103 104 if !found { 105 log.Printf("[DEBUG] NIC for network ID %s does no longer exist", d.Get("network_id").(string)) 106 d.SetId("") 107 } 108 109 return nil 110 } 111 112 func resourceCloudStackNICDelete(d *schema.ResourceData, meta interface{}) error { 113 cs := meta.(*cloudstack.CloudStackClient) 114 115 // Create a new parameter struct 116 p := cs.VirtualMachine.NewRemoveNicFromVirtualMachineParams( 117 d.Id(), 118 d.Get("virtual_machine_id").(string), 119 ) 120 121 // Remove the NIC 122 _, err := cs.VirtualMachine.RemoveNicFromVirtualMachine(p) 123 if err != nil { 124 // This is a very poor way to be told the ID does no longer exist :( 125 if strings.Contains(err.Error(), fmt.Sprintf( 126 "Invalid parameter id value=%s due to incorrect long value format, "+ 127 "or entity does not exist", d.Id())) { 128 return nil 129 } 130 131 return fmt.Errorf("Error deleting NIC: %s", err) 132 } 133 134 return nil 135 } 136 137 func retryableAddNicFunc(cs *cloudstack.CloudStackClient, p *cloudstack.AddNicToVirtualMachineParams) func() (interface{}, error) { 138 return func() (interface{}, error) { 139 r, err := cs.VirtualMachine.AddNicToVirtualMachine(p) 140 if err != nil { 141 return nil, err 142 } 143 return r, nil 144 } 145 }