github.com/tkak/terraform@v0.5.4-0.20150712180941-7f738dc27225/builtin/providers/cloudstack/resource_cloudstack_instance.go (about) 1 package cloudstack 2 3 import ( 4 "crypto/sha1" 5 "encoding/base64" 6 "encoding/hex" 7 "fmt" 8 "log" 9 "strings" 10 11 "github.com/hashicorp/terraform/helper/schema" 12 "github.com/xanzy/go-cloudstack/cloudstack" 13 ) 14 15 func resourceCloudStackInstance() *schema.Resource { 16 return &schema.Resource{ 17 Create: resourceCloudStackInstanceCreate, 18 Read: resourceCloudStackInstanceRead, 19 Update: resourceCloudStackInstanceUpdate, 20 Delete: resourceCloudStackInstanceDelete, 21 22 Schema: map[string]*schema.Schema{ 23 "name": &schema.Schema{ 24 Type: schema.TypeString, 25 Required: true, 26 ForceNew: true, 27 }, 28 29 "display_name": &schema.Schema{ 30 Type: schema.TypeString, 31 Optional: true, 32 Computed: true, 33 }, 34 35 "service_offering": &schema.Schema{ 36 Type: schema.TypeString, 37 Required: true, 38 }, 39 40 "network": &schema.Schema{ 41 Type: schema.TypeString, 42 Optional: true, 43 ForceNew: true, 44 }, 45 46 "ipaddress": &schema.Schema{ 47 Type: schema.TypeString, 48 Optional: true, 49 Computed: true, 50 ForceNew: true, 51 }, 52 53 "template": &schema.Schema{ 54 Type: schema.TypeString, 55 Required: true, 56 ForceNew: true, 57 }, 58 59 "project": &schema.Schema{ 60 Type: schema.TypeString, 61 Optional: true, 62 ForceNew: true, 63 }, 64 65 "zone": &schema.Schema{ 66 Type: schema.TypeString, 67 Required: true, 68 ForceNew: true, 69 }, 70 71 "keypair": &schema.Schema{ 72 Type: schema.TypeString, 73 Optional: true, 74 }, 75 76 "user_data": &schema.Schema{ 77 Type: schema.TypeString, 78 Optional: true, 79 ForceNew: true, 80 StateFunc: func(v interface{}) string { 81 switch v.(type) { 82 case string: 83 hash := sha1.Sum([]byte(v.(string))) 84 return hex.EncodeToString(hash[:]) 85 default: 86 return "" 87 } 88 }, 89 }, 90 91 "expunge": &schema.Schema{ 92 Type: schema.TypeBool, 93 Optional: true, 94 Default: false, 95 }, 96 }, 97 } 98 } 99 100 func resourceCloudStackInstanceCreate(d *schema.ResourceData, meta interface{}) error { 101 cs := meta.(*cloudstack.CloudStackClient) 102 103 // Retrieve the service_offering UUID 104 serviceofferingid, e := retrieveUUID(cs, "service_offering", d.Get("service_offering").(string)) 105 if e != nil { 106 return e.Error() 107 } 108 109 // Retrieve the zone object 110 zone, _, err := cs.Zone.GetZoneByName(d.Get("zone").(string)) 111 if err != nil { 112 return err 113 } 114 115 // Retrieve the template UUID 116 templateid, e := retrieveTemplateUUID(cs, zone.Id, d.Get("template").(string)) 117 if e != nil { 118 return e.Error() 119 } 120 121 // Create a new parameter struct 122 p := cs.VirtualMachine.NewDeployVirtualMachineParams(serviceofferingid, templateid, zone.Id) 123 124 // Set the name 125 name := d.Get("name").(string) 126 p.SetName(name) 127 128 // Set the display name 129 if displayname, ok := d.GetOk("display_name"); ok { 130 p.SetDisplayname(displayname.(string)) 131 } else { 132 p.SetDisplayname(name) 133 } 134 135 if zone.Networktype == "Advanced" { 136 // Retrieve the network UUID 137 networkid, e := retrieveUUID(cs, "network", d.Get("network").(string)) 138 if e != nil { 139 return e.Error() 140 } 141 // Set the default network ID 142 p.SetNetworkids([]string{networkid}) 143 } 144 145 // If there is a ipaddres supplied, add it to the parameter struct 146 if ipaddres, ok := d.GetOk("ipaddress"); ok { 147 p.SetIpaddress(ipaddres.(string)) 148 } 149 150 // If there is a project supplied, we retreive and set the project id 151 if project, ok := d.GetOk("project"); ok { 152 // Retrieve the project UUID 153 projectid, e := retrieveUUID(cs, "project", project.(string)) 154 if e != nil { 155 return e.Error() 156 } 157 // Set the default project ID 158 p.SetProjectid(projectid) 159 } 160 161 // If a keypair is supplied, add it to the parameter struct 162 if keypair, ok := d.GetOk("keypair"); ok { 163 p.SetKeypair(keypair.(string)) 164 } 165 166 // If the user data contains any info, it needs to be base64 encoded and 167 // added to the parameter struct 168 if userData, ok := d.GetOk("user_data"); ok { 169 ud := base64.StdEncoding.EncodeToString([]byte(userData.(string))) 170 171 // deployVirtualMachine uses POST, so max userdata is 32K 172 // https://github.com/xanzy/go-cloudstack/commit/c767de689df1faedfec69233763a7c5334bee1f6 173 if len(ud) > 32768 { 174 return fmt.Errorf( 175 "The supplied user_data contains %d bytes after encoding, "+ 176 "this exeeds the limit of 32768 bytes", len(ud)) 177 } 178 179 p.SetUserdata(ud) 180 } 181 182 // Create the new instance 183 r, err := cs.VirtualMachine.DeployVirtualMachine(p) 184 if err != nil { 185 return fmt.Errorf("Error creating the new instance %s: %s", name, err) 186 } 187 188 d.SetId(r.Id) 189 190 // Set the connection info for any configured provisioners 191 d.SetConnInfo(map[string]string{ 192 "host": r.Nic[0].Ipaddress, 193 "password": r.Password, 194 }) 195 196 return resourceCloudStackInstanceRead(d, meta) 197 } 198 199 func resourceCloudStackInstanceRead(d *schema.ResourceData, meta interface{}) error { 200 cs := meta.(*cloudstack.CloudStackClient) 201 202 // Get the virtual machine details 203 vm, count, err := cs.VirtualMachine.GetVirtualMachineByID(d.Id()) 204 if err != nil { 205 if count == 0 { 206 log.Printf("[DEBUG] Instance %s does no longer exist", d.Get("name").(string)) 207 // Clear out all details so it's obvious the instance is gone 208 d.SetId("") 209 return nil 210 } 211 212 return err 213 } 214 215 // Update the config 216 d.Set("name", vm.Name) 217 d.Set("display_name", vm.Displayname) 218 d.Set("ipaddress", vm.Nic[0].Ipaddress) 219 d.Set("zone", vm.Zonename) 220 //NB cloudstack sometimes sends back the wrong keypair name, so dont update it 221 222 setValueOrUUID(d, "network", vm.Nic[0].Networkname, vm.Nic[0].Networkid) 223 setValueOrUUID(d, "service_offering", vm.Serviceofferingname, vm.Serviceofferingid) 224 setValueOrUUID(d, "template", vm.Templatename, vm.Templateid) 225 setValueOrUUID(d, "project", vm.Project, vm.Projectid) 226 227 return nil 228 } 229 230 func resourceCloudStackInstanceUpdate(d *schema.ResourceData, meta interface{}) error { 231 cs := meta.(*cloudstack.CloudStackClient) 232 d.Partial(true) 233 234 name := d.Get("name").(string) 235 236 // Check if the display name is changed and if so, update the virtual machine 237 if d.HasChange("display_name") { 238 log.Printf("[DEBUG] Display name changed for %s, starting update", name) 239 240 // Create a new parameter struct 241 p := cs.VirtualMachine.NewUpdateVirtualMachineParams(d.Id()) 242 243 // Set the new display name 244 p.SetDisplayname(d.Get("display_name").(string)) 245 246 // Update the display name 247 _, err := cs.VirtualMachine.UpdateVirtualMachine(p) 248 if err != nil { 249 return fmt.Errorf( 250 "Error updating the display name for instance %s: %s", name, err) 251 } 252 253 d.SetPartial("display_name") 254 } 255 256 // Attributes that require reboot to update 257 if d.HasChange("service_offering") || d.HasChange("keypair") { 258 // Before we can actually make these changes, the virtual machine must be stopped 259 _, err := cs.VirtualMachine.StopVirtualMachine(cs.VirtualMachine.NewStopVirtualMachineParams(d.Id())) 260 if err != nil { 261 return fmt.Errorf( 262 "Error stopping instance %s before making changes: %s", name, err) 263 } 264 265 // Check if the service offering is changed and if so, update the offering 266 if d.HasChange("service_offering") { 267 log.Printf("[DEBUG] Service offering changed for %s, starting update", name) 268 269 // Retrieve the service_offering UUID 270 serviceofferingid, e := retrieveUUID(cs, "service_offering", d.Get("service_offering").(string)) 271 if e != nil { 272 return e.Error() 273 } 274 275 // Create a new parameter struct 276 p := cs.VirtualMachine.NewChangeServiceForVirtualMachineParams(d.Id(), serviceofferingid) 277 278 // Change the service offering 279 _, err = cs.VirtualMachine.ChangeServiceForVirtualMachine(p) 280 if err != nil { 281 return fmt.Errorf( 282 "Error changing the service offering for instance %s: %s", name, err) 283 } 284 d.SetPartial("service_offering") 285 } 286 287 if d.HasChange("keypair") { 288 log.Printf("[DEBUG] SSH keypair changed for %s, starting update", name) 289 290 p := cs.SSH.NewResetSSHKeyForVirtualMachineParams(d.Id(), d.Get("keypair").(string)) 291 292 // Change the ssh keypair 293 _, err = cs.SSH.ResetSSHKeyForVirtualMachine(p) 294 if err != nil { 295 return fmt.Errorf( 296 "Error changing the SSH keypair for instance %s: %s", name, err) 297 } 298 d.SetPartial("keypair") 299 } 300 301 // Start the virtual machine again 302 _, err = cs.VirtualMachine.StartVirtualMachine(cs.VirtualMachine.NewStartVirtualMachineParams(d.Id())) 303 if err != nil { 304 return fmt.Errorf( 305 "Error starting instance %s after making changes", name) 306 } 307 } 308 d.Partial(false) 309 return resourceCloudStackInstanceRead(d, meta) 310 } 311 312 func resourceCloudStackInstanceDelete(d *schema.ResourceData, meta interface{}) error { 313 cs := meta.(*cloudstack.CloudStackClient) 314 315 // Create a new parameter struct 316 p := cs.VirtualMachine.NewDestroyVirtualMachineParams(d.Id()) 317 318 if d.Get("expunge").(bool) { 319 p.SetExpunge(true) 320 } 321 322 log.Printf("[INFO] Destroying instance: %s", d.Get("name").(string)) 323 if _, err := cs.VirtualMachine.DestroyVirtualMachine(p); err != nil { 324 // This is a very poor way to be told the UUID does no longer exist :( 325 if strings.Contains(err.Error(), fmt.Sprintf( 326 "Invalid parameter id value=%s due to incorrect long value format, "+ 327 "or entity does not exist", d.Id())) { 328 return nil 329 } 330 331 return fmt.Errorf("Error destroying instance: %s", err) 332 } 333 334 return nil 335 }