github.com/jrasell/terraform@v0.6.17-0.20160523115548-2652f5232949/builtin/providers/cloudstack/resource_cloudstack_secondary_ipaddress.go (about) 1 package cloudstack 2 3 import ( 4 "errors" 5 "fmt" 6 "log" 7 "strings" 8 9 "github.com/hashicorp/terraform/helper/schema" 10 "github.com/xanzy/go-cloudstack/cloudstack" 11 ) 12 13 func resourceCloudStackSecondaryIPAddress() *schema.Resource { 14 return &schema.Resource{ 15 Create: resourceCloudStackSecondaryIPAddressCreate, 16 Read: resourceCloudStackSecondaryIPAddressRead, 17 Delete: resourceCloudStackSecondaryIPAddressDelete, 18 19 Schema: map[string]*schema.Schema{ 20 "ip_address": &schema.Schema{ 21 Type: schema.TypeString, 22 Optional: true, 23 Computed: true, 24 ForceNew: true, 25 }, 26 27 "ipaddress": &schema.Schema{ 28 Type: schema.TypeString, 29 Optional: true, 30 ForceNew: true, 31 Deprecated: "Please use the `ip_address` field instead", 32 }, 33 34 "nic_id": &schema.Schema{ 35 Type: schema.TypeString, 36 Optional: true, 37 Computed: true, 38 ForceNew: true, 39 }, 40 41 "nicid": &schema.Schema{ 42 Type: schema.TypeString, 43 Optional: true, 44 ForceNew: true, 45 Deprecated: "Please use the `nic_id` field instead", 46 }, 47 48 "virtual_machine_id": &schema.Schema{ 49 Type: schema.TypeString, 50 Optional: true, 51 Computed: true, 52 ForceNew: true, 53 }, 54 55 "virtual_machine": &schema.Schema{ 56 Type: schema.TypeString, 57 Optional: true, 58 ForceNew: true, 59 Deprecated: "Please use the `virtual_machine_id` field instead", 60 }, 61 }, 62 } 63 } 64 65 func resourceCloudStackSecondaryIPAddressCreate(d *schema.ResourceData, meta interface{}) error { 66 cs := meta.(*cloudstack.CloudStackClient) 67 68 nicid, ok := d.GetOk("nic_id") 69 if !ok { 70 nicid, ok = d.GetOk("nicid") 71 } 72 if !ok { 73 virtualmachine, ok := d.GetOk("virtual_machine_id") 74 if !ok { 75 virtualmachine, ok = d.GetOk("virtual_machine") 76 } 77 if !ok { 78 return errors.New( 79 "Either `virtual_machine_id` or [deprecated] `virtual_machine` must be provided.") 80 } 81 82 // Retrieve the virtual_machine ID 83 virtualmachineid, e := retrieveID(cs, "virtual_machine", virtualmachine.(string)) 84 if e != nil { 85 return e.Error() 86 } 87 88 // Get the virtual machine details 89 vm, count, err := cs.VirtualMachine.GetVirtualMachineByID(virtualmachineid) 90 if err != nil { 91 if count == 0 { 92 log.Printf("[DEBUG] Virtual Machine %s does no longer exist", virtualmachineid) 93 d.SetId("") 94 return nil 95 } 96 return err 97 } 98 99 nicid = vm.Nic[0].Id 100 } 101 102 // Create a new parameter struct 103 p := cs.Nic.NewAddIpToNicParams(nicid.(string)) 104 105 // If there is a ipaddres supplied, add it to the parameter struct 106 ipaddress, ok := d.GetOk("ip_address") 107 if !ok { 108 ipaddress, ok = d.GetOk("ipaddress") 109 } 110 if ok { 111 p.SetIpaddress(ipaddress.(string)) 112 } 113 114 ip, err := cs.Nic.AddIpToNic(p) 115 if err != nil { 116 return err 117 } 118 119 d.SetId(ip.Id) 120 121 return nil 122 } 123 124 func resourceCloudStackSecondaryIPAddressRead(d *schema.ResourceData, meta interface{}) error { 125 cs := meta.(*cloudstack.CloudStackClient) 126 127 virtualmachine, ok := d.GetOk("virtual_machine_id") 128 if !ok { 129 virtualmachine, ok = d.GetOk("virtual_machine") 130 } 131 if !ok { 132 return errors.New( 133 "Either `virtual_machine_id` or [deprecated] `virtual_machine` must be provided.") 134 } 135 136 // Retrieve the virtual_machine ID 137 virtualmachineid, e := retrieveID(cs, "virtual_machine", virtualmachine.(string)) 138 if e != nil { 139 return e.Error() 140 } 141 142 // Get the virtual machine details 143 vm, count, err := cs.VirtualMachine.GetVirtualMachineByID(virtualmachineid) 144 if err != nil { 145 if count == 0 { 146 log.Printf("[DEBUG] Virtual Machine %s does no longer exist", virtualmachineid) 147 d.SetId("") 148 return nil 149 } 150 return err 151 } 152 153 nicid, ok := d.GetOk("nic_id") 154 if !ok { 155 nicid, ok = d.GetOk("nicid") 156 } 157 if !ok { 158 nicid = vm.Nic[0].Id 159 } 160 161 p := cs.Nic.NewListNicsParams(virtualmachineid) 162 p.SetNicid(nicid.(string)) 163 164 l, err := cs.Nic.ListNics(p) 165 if err != nil { 166 return err 167 } 168 169 if l.Count == 0 { 170 log.Printf("[DEBUG] NIC %s does no longer exist", d.Get("nicid").(string)) 171 d.SetId("") 172 return nil 173 } 174 175 if l.Count > 1 { 176 return fmt.Errorf("Found more then one possible result: %v", l.Nics) 177 } 178 179 for _, ip := range l.Nics[0].Secondaryip { 180 if ip.Id == d.Id() { 181 d.Set("ip_address", ip.Ipaddress) 182 d.Set("nic_id", l.Nics[0].Id) 183 d.Set("virtual_machine_id", l.Nics[0].Virtualmachineid) 184 return nil 185 } 186 } 187 188 log.Printf("[DEBUG] IP %s no longer exist", d.Get("ip_address").(string)) 189 d.SetId("") 190 191 return nil 192 } 193 194 func resourceCloudStackSecondaryIPAddressDelete(d *schema.ResourceData, meta interface{}) error { 195 cs := meta.(*cloudstack.CloudStackClient) 196 197 // Create a new parameter struct 198 p := cs.Nic.NewRemoveIpFromNicParams(d.Id()) 199 200 log.Printf("[INFO] Removing secondary IP address: %s", d.Get("ip_address").(string)) 201 if _, err := cs.Nic.RemoveIpFromNic(p); err != nil { 202 // This is a very poor way to be told the ID does no longer exist :( 203 if strings.Contains(err.Error(), fmt.Sprintf( 204 "Invalid parameter id value=%s due to incorrect long value format, "+ 205 "or entity does not exist", d.Id())) { 206 return nil 207 } 208 209 return fmt.Errorf("Error removing secondary IP address: %s", err) 210 } 211 212 return nil 213 }