2015-03-18 11:11:56 +01:00
|
|
|
package cloudstack
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
|
|
|
"github.com/xanzy/go-cloudstack/cloudstack"
|
|
|
|
)
|
|
|
|
|
|
|
|
func resourceCloudStackTemplate() *schema.Resource {
|
|
|
|
return &schema.Resource{
|
|
|
|
Create: resourceCloudStackTemplateCreate,
|
|
|
|
Read: resourceCloudStackTemplateRead,
|
|
|
|
Update: resourceCloudStackTemplateUpdate,
|
|
|
|
Delete: resourceCloudStackTemplateDelete,
|
|
|
|
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"name": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"display_text": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
|
2015-04-11 17:50:06 +02:00
|
|
|
"format": &schema.Schema{
|
2015-03-18 11:11:56 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"hypervisor": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"os_type": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
|
2015-04-11 17:50:06 +02:00
|
|
|
"url": &schema.Schema{
|
2015-03-18 11:11:56 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
2015-09-24 22:28:40 +02:00
|
|
|
"project": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
2015-03-18 11:11:56 +01:00
|
|
|
"zone": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
2015-04-11 17:50:06 +02:00
|
|
|
"is_dynamically_scalable": &schema.Schema{
|
2015-03-18 11:11:56 +01:00
|
|
|
Type: schema.TypeBool,
|
|
|
|
Optional: true,
|
2015-04-11 17:50:06 +02:00
|
|
|
Computed: true,
|
2015-03-18 11:11:56 +01:00
|
|
|
},
|
|
|
|
|
2015-04-11 17:50:06 +02:00
|
|
|
"is_extractable": &schema.Schema{
|
2015-03-18 11:11:56 +01:00
|
|
|
Type: schema.TypeBool,
|
|
|
|
Optional: true,
|
2015-04-11 17:50:06 +02:00
|
|
|
Computed: true,
|
|
|
|
ForceNew: true,
|
2015-03-18 11:11:56 +01:00
|
|
|
},
|
|
|
|
|
2015-04-11 17:50:06 +02:00
|
|
|
"is_featured": &schema.Schema{
|
2015-03-18 11:11:56 +01:00
|
|
|
Type: schema.TypeBool,
|
|
|
|
Optional: true,
|
2015-04-11 17:50:06 +02:00
|
|
|
Computed: true,
|
|
|
|
ForceNew: true,
|
2015-03-18 11:11:56 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
"is_public": &schema.Schema{
|
|
|
|
Type: schema.TypeBool,
|
|
|
|
Optional: true,
|
2015-04-11 17:50:06 +02:00
|
|
|
Computed: true,
|
2015-03-18 11:11:56 +01:00
|
|
|
},
|
|
|
|
|
2015-04-11 17:50:06 +02:00
|
|
|
"password_enabled": &schema.Schema{
|
2015-03-18 11:11:56 +01:00
|
|
|
Type: schema.TypeBool,
|
|
|
|
Optional: true,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"is_ready": &schema.Schema{
|
|
|
|
Type: schema.TypeBool,
|
|
|
|
Computed: true,
|
|
|
|
},
|
2015-04-11 17:50:06 +02:00
|
|
|
|
|
|
|
"is_ready_timeout": &schema.Schema{
|
|
|
|
Type: schema.TypeInt,
|
|
|
|
Optional: true,
|
|
|
|
Default: 300,
|
|
|
|
},
|
2015-03-18 11:11:56 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceCloudStackTemplateCreate(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
cs := meta.(*cloudstack.CloudStackClient)
|
|
|
|
|
2015-05-05 11:37:23 +02:00
|
|
|
if err := verifyTemplateParams(d); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-03-18 11:11:56 +01:00
|
|
|
name := d.Get("name").(string)
|
|
|
|
|
2015-04-11 17:50:06 +02:00
|
|
|
// Compute/set the display text
|
|
|
|
displaytext := d.Get("display_text").(string)
|
|
|
|
if displaytext == "" {
|
|
|
|
displaytext = name
|
|
|
|
}
|
|
|
|
|
2015-10-05 14:05:21 +02:00
|
|
|
// Retrieve the os_type ID
|
|
|
|
ostypeid, e := retrieveID(cs, "os_type", d.Get("os_type").(string))
|
2015-03-18 11:11:56 +01:00
|
|
|
if e != nil {
|
|
|
|
return e.Error()
|
|
|
|
}
|
|
|
|
|
2015-10-05 14:05:21 +02:00
|
|
|
// Retrieve the zone ID
|
|
|
|
zoneid, e := retrieveID(cs, "zone", d.Get("zone").(string))
|
2015-03-18 11:11:56 +01:00
|
|
|
if e != nil {
|
|
|
|
return e.Error()
|
|
|
|
}
|
|
|
|
|
2015-04-11 17:50:06 +02:00
|
|
|
// Create a new parameter struct
|
|
|
|
p := cs.Template.NewRegisterTemplateParams(
|
|
|
|
displaytext,
|
|
|
|
d.Get("format").(string),
|
|
|
|
d.Get("hypervisor").(string),
|
|
|
|
name,
|
|
|
|
ostypeid,
|
|
|
|
d.Get("url").(string),
|
|
|
|
zoneid)
|
|
|
|
|
|
|
|
// Set optional parameters
|
|
|
|
if v, ok := d.GetOk("is_dynamically_scalable"); ok {
|
|
|
|
p.SetIsdynamicallyscalable(v.(bool))
|
2015-03-18 11:11:56 +01:00
|
|
|
}
|
|
|
|
|
2015-04-11 17:50:06 +02:00
|
|
|
if v, ok := d.GetOk("is_extractable"); ok {
|
|
|
|
p.SetIsextractable(v.(bool))
|
|
|
|
}
|
|
|
|
|
|
|
|
if v, ok := d.GetOk("is_featured"); ok {
|
|
|
|
p.SetIsfeatured(v.(bool))
|
2015-03-18 11:11:56 +01:00
|
|
|
}
|
2015-04-11 17:50:06 +02:00
|
|
|
|
|
|
|
if v, ok := d.GetOk("is_public"); ok {
|
|
|
|
p.SetIspublic(v.(bool))
|
|
|
|
}
|
|
|
|
|
|
|
|
if v, ok := d.GetOk("password_enabled"); ok {
|
|
|
|
p.SetPasswordenabled(v.(bool))
|
2015-03-18 11:11:56 +01:00
|
|
|
}
|
|
|
|
|
2015-09-24 23:04:45 +02:00
|
|
|
// If there is a project supplied, we retrieve and set the project id
|
2016-04-11 17:14:19 +02:00
|
|
|
if err := setProjectid(p, cs, d); err != nil {
|
|
|
|
return err
|
2015-09-24 23:04:45 +02:00
|
|
|
}
|
|
|
|
|
2015-03-18 11:11:56 +01:00
|
|
|
// Create the new template
|
|
|
|
r, err := cs.Template.RegisterTemplate(p)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error creating template %s: %s", name, err)
|
|
|
|
}
|
2015-04-11 17:50:06 +02:00
|
|
|
|
2015-03-18 11:11:56 +01:00
|
|
|
d.SetId(r.RegisterTemplate[0].Id)
|
|
|
|
|
2015-04-11 17:50:06 +02:00
|
|
|
// Wait until the template is ready to use, or timeout with an error...
|
|
|
|
currentTime := time.Now().Unix()
|
|
|
|
timeout := int64(d.Get("is_ready_timeout").(int))
|
|
|
|
for {
|
2015-05-05 11:37:23 +02:00
|
|
|
// Start with the sleep so the register action has a few seconds
|
|
|
|
// to process the registration correctly. Without this wait
|
|
|
|
time.Sleep(10 * time.Second)
|
|
|
|
|
2015-04-11 17:50:06 +02:00
|
|
|
err := resourceCloudStackTemplateRead(d, meta)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if d.Get("is_ready").(bool) {
|
|
|
|
return nil
|
|
|
|
}
|
2015-03-18 11:11:56 +01:00
|
|
|
|
2015-04-11 17:50:06 +02:00
|
|
|
if time.Now().Unix()-currentTime > timeout {
|
|
|
|
return fmt.Errorf("Timeout while waiting for template to become ready")
|
|
|
|
}
|
2015-03-18 11:11:56 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceCloudStackTemplateRead(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
cs := meta.(*cloudstack.CloudStackClient)
|
2015-04-11 17:50:06 +02:00
|
|
|
|
2015-03-18 11:11:56 +01:00
|
|
|
// Get the template details
|
2015-04-11 17:50:06 +02:00
|
|
|
t, count, err := cs.Template.GetTemplateByID(d.Id(), "executable")
|
2015-03-18 11:11:56 +01:00
|
|
|
if err != nil {
|
|
|
|
if count == 0 {
|
|
|
|
log.Printf(
|
|
|
|
"[DEBUG] Template %s no longer exists", d.Get("name").(string))
|
|
|
|
d.SetId("")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
2015-04-11 17:50:06 +02:00
|
|
|
|
2015-03-18 11:11:56 +01:00
|
|
|
d.Set("name", t.Name)
|
|
|
|
d.Set("display_text", t.Displaytext)
|
2015-04-11 17:50:06 +02:00
|
|
|
d.Set("format", t.Format)
|
2015-03-18 11:11:56 +01:00
|
|
|
d.Set("hypervisor", t.Hypervisor)
|
2015-04-11 17:50:06 +02:00
|
|
|
d.Set("is_dynamically_scalable", t.Isdynamicallyscalable)
|
|
|
|
d.Set("is_extractable", t.Isextractable)
|
2015-03-18 11:11:56 +01:00
|
|
|
d.Set("is_featured", t.Isfeatured)
|
|
|
|
d.Set("is_public", t.Ispublic)
|
2015-04-11 17:50:06 +02:00
|
|
|
d.Set("password_enabled", t.Passwordenabled)
|
2015-03-18 11:11:56 +01:00
|
|
|
d.Set("is_ready", t.Isready)
|
2015-04-11 17:50:06 +02:00
|
|
|
|
2015-10-05 14:05:21 +02:00
|
|
|
setValueOrID(d, "os_type", t.Ostypename, t.Ostypeid)
|
|
|
|
setValueOrID(d, "project", t.Project, t.Projectid)
|
|
|
|
setValueOrID(d, "zone", t.Zonename, t.Zoneid)
|
2015-09-24 22:28:40 +02:00
|
|
|
|
2015-03-18 11:11:56 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceCloudStackTemplateUpdate(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
cs := meta.(*cloudstack.CloudStackClient)
|
|
|
|
name := d.Get("name").(string)
|
|
|
|
|
2015-04-11 17:50:06 +02:00
|
|
|
// Create a new parameter struct
|
|
|
|
p := cs.Template.NewUpdateTemplateParams(d.Id())
|
|
|
|
|
|
|
|
if d.HasChange("name") {
|
|
|
|
p.SetName(name)
|
|
|
|
}
|
|
|
|
|
|
|
|
if d.HasChange("display_text") {
|
|
|
|
p.SetDisplaytext(d.Get("display_text").(string))
|
2015-03-18 11:11:56 +01:00
|
|
|
}
|
2015-04-11 17:50:06 +02:00
|
|
|
|
|
|
|
if d.HasChange("format") {
|
|
|
|
p.SetFormat(d.Get("format").(string))
|
|
|
|
}
|
|
|
|
|
|
|
|
if d.HasChange("is_dynamically_scalable") {
|
|
|
|
p.SetIsdynamicallyscalable(d.Get("is_dynamically_scalable").(bool))
|
|
|
|
}
|
|
|
|
|
2015-03-18 11:11:56 +01:00
|
|
|
if d.HasChange("os_type") {
|
2015-10-05 14:05:21 +02:00
|
|
|
ostypeid, e := retrieveID(cs, "os_type", d.Get("os_type").(string))
|
2015-03-18 11:11:56 +01:00
|
|
|
if e != nil {
|
|
|
|
return e.Error()
|
|
|
|
}
|
|
|
|
p.SetOstypeid(ostypeid)
|
2015-04-11 17:50:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if d.HasChange("password_enabled") {
|
|
|
|
p.SetPasswordenabled(d.Get("password_enabled").(bool))
|
|
|
|
}
|
2015-03-18 11:11:56 +01:00
|
|
|
|
2015-04-11 17:50:06 +02:00
|
|
|
_, err := cs.Template.UpdateTemplate(p)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error updating template %s: %s", name, err)
|
2015-03-18 11:11:56 +01:00
|
|
|
}
|
2015-04-11 17:50:06 +02:00
|
|
|
|
2015-03-18 11:11:56 +01:00
|
|
|
return resourceCloudStackTemplateRead(d, meta)
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceCloudStackTemplateDelete(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
cs := meta.(*cloudstack.CloudStackClient)
|
|
|
|
|
|
|
|
// Create a new parameter struct
|
|
|
|
p := cs.Template.NewDeleteTemplateParams(d.Id())
|
|
|
|
|
|
|
|
// Delete the template
|
2015-04-11 17:50:06 +02:00
|
|
|
log.Printf("[INFO] Deleting template: %s", d.Get("name").(string))
|
2015-03-18 11:11:56 +01:00
|
|
|
_, err := cs.Template.DeleteTemplate(p)
|
|
|
|
if err != nil {
|
2015-10-05 14:05:21 +02:00
|
|
|
// This is a very poor way to be told the ID does no longer exist :(
|
2015-03-18 11:11:56 +01:00
|
|
|
if strings.Contains(err.Error(), fmt.Sprintf(
|
|
|
|
"Invalid parameter id value=%s due to incorrect long value format, "+
|
|
|
|
"or entity does not exist", d.Id())) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Errorf("Error deleting template %s: %s", d.Get("name").(string), err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2015-05-05 11:37:23 +02:00
|
|
|
|
|
|
|
func verifyTemplateParams(d *schema.ResourceData) error {
|
|
|
|
format := d.Get("format").(string)
|
2015-06-03 17:46:56 +02:00
|
|
|
if format != "OVA" && format != "QCOW2" && format != "RAW" && format != "VHD" && format != "VMDK" {
|
2015-05-05 11:37:23 +02:00
|
|
|
return fmt.Errorf(
|
2015-06-03 17:46:56 +02:00
|
|
|
"%s is not a valid format. Valid options are 'OVA','QCOW2', 'RAW', 'VHD' and 'VMDK'", format)
|
2015-05-05 11:37:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|