in cloudstack/resource_cloudstack_template.go [134:237]
func resourceCloudStackTemplateCreate(d *schema.ResourceData, meta interface{}) error {
cs := meta.(*cloudstack.CloudStackClient)
if err := verifyTemplateParams(d); err != nil {
return err
}
name := d.Get("name").(string)
// Compute/set the display text
displaytext := d.Get("display_text").(string)
if displaytext == "" {
displaytext = name
}
// Create a new parameter struct
p := cs.Template.NewRegisterTemplateParams(
displaytext,
d.Get("format").(string),
d.Get("hypervisor").(string),
name,
d.Get("url").(string),
)
// Retrieve the os_type ID
ostypeid, e := retrieveID(cs, "os_type", d.Get("os_type").(string))
if e == nil {
p.SetOstypeid(ostypeid)
}
// Set optional parameters
if v, ok := d.GetOk("is_dynamically_scalable"); ok {
p.SetIsdynamicallyscalable(v.(bool))
}
if v, ok := d.GetOk("is_extractable"); ok {
p.SetIsextractable(v.(bool))
}
if v, ok := d.GetOk("is_featured"); ok {
p.SetIsfeatured(v.(bool))
}
if v, ok := d.GetOk("is_public"); ok {
p.SetIspublic(v.(bool))
}
if v, ok := d.GetOk("password_enabled"); ok {
p.SetPasswordenabled(v.(bool))
}
// Retrieve the zone ID
if v, ok := d.GetOk("zone"); ok {
if v.(string) != "all" {
zoneid, e := retrieveID(cs, "zone", v.(string))
if e != nil {
return e.Error()
}
p.SetZoneid(zoneid)
} else {
p.SetZoneid("-1")
}
}
// If there is a project supplied, we retrieve and set the project id
if err := setProjectid(p, cs, d); err != nil {
return err
}
// Create the new template
r, err := cs.Template.RegisterTemplate(p)
if err != nil {
return fmt.Errorf("Error creating template %s: %s", name, err)
}
d.SetId(r.RegisterTemplate[0].Id)
// Set tags if necessary
if err = setTags(cs, d, "Template"); err != nil {
return fmt.Errorf("Error setting tags on the template %s: %s", name, err)
}
// 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 {
// 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)
err := resourceCloudStackTemplateRead(d, meta)
if err != nil {
return err
}
if d.Get("is_ready").(bool) {
return nil
}
if time.Now().Unix()-currentTime > timeout {
return fmt.Errorf("Timeout while waiting for template to become ready")
}
}
}