Merge pull request #1491 from svanharmelen/f-cloudstack-refactor-template

provider/cloudstack: refactored the template provider to make it fit in nicely
This commit is contained in:
Sander van Harmelen 2015-04-11 17:53:39 +02:00
commit 3fc6397343
4 changed files with 227 additions and 232 deletions

View File

@ -43,6 +43,7 @@ func testAccPreCheck(t *testing.T) {
// SET THESE VALUES IN ORDER TO RUN THE ACC TESTS!!
var CLOUDSTACK_DISK_OFFERING_1 = ""
var CLOUDSTACK_DISK_OFFERING_2 = ""
var CLOUDSTACK_HYPERVISOR = ""
var CLOUDSTACK_SERVICE_OFFERING_1 = ""
var CLOUDSTACK_SERVICE_OFFERING_2 = ""
var CLOUDSTACK_NETWORK_1 = ""
@ -58,9 +59,7 @@ var CLOUDSTACK_VPC_NETWORK_CIDR = ""
var CLOUDSTACK_VPC_NETWORK_OFFERING = ""
var CLOUDSTACK_PUBLIC_IPADDRESS = ""
var CLOUDSTACK_TEMPLATE = ""
var CLOUDSTACK_ZONE = ""
var CLOUDSTACK_TEMPLATE_URL = ""
var CLOUDSTACK_HYPERVISOR = ""
var CLOUDSTACK_TEMPLATE_OS_TYPE = ""
var CLOUDSTACK_TEMPLATE_FORMAT = ""
var CLOUDSTACK_TEMPLATE_CHECKSUM = ""
var CLOUDSTACK_TEMPLATE_URL = ""
var CLOUDSTACK_TEMPLATE_OS_TYPE = ""
var CLOUDSTACK_ZONE = ""

View File

@ -29,10 +29,9 @@ func resourceCloudStackTemplate() *schema.Resource {
Computed: true,
},
"url": &schema.Schema{
"format": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"hypervisor": &schema.Schema{
@ -46,7 +45,7 @@ func resourceCloudStackTemplate() *schema.Resource {
Required: true,
},
"format": &schema.Schema{
"url": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
@ -58,58 +57,34 @@ func resourceCloudStackTemplate() *schema.Resource {
ForceNew: true,
},
"requires_hvm": &schema.Schema{
Type: schema.TypeBool,
Optional: true,
},
"is_featured": &schema.Schema{
Type: schema.TypeBool,
Optional: true,
},
"password_enabled": &schema.Schema{
Type: schema.TypeBool,
Optional: true,
},
"template_tag": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
"ssh_key_enabled": &schema.Schema{
Type: schema.TypeBool,
Optional: true,
},
"is_routing": &schema.Schema{
Type: schema.TypeBool,
Optional: true,
},
"is_public": &schema.Schema{
"is_dynamically_scalable": &schema.Schema{
Type: schema.TypeBool,
Optional: true,
Computed: true,
},
"is_extractable": &schema.Schema{
Type: schema.TypeBool,
Optional: true,
Computed: true,
ForceNew: true,
},
"bits": &schema.Schema{
Type: schema.TypeInt,
Optional: true,
},
"is_dynamically_scalable": &schema.Schema{
"is_featured": &schema.Schema{
Type: schema.TypeBool,
Optional: true,
Computed: true,
ForceNew: true,
},
"checksum": &schema.Schema{
Type: schema.TypeString,
"is_public": &schema.Schema{
Type: schema.TypeBool,
Optional: true,
Computed: true,
},
"password_enabled": &schema.Schema{
Type: schema.TypeBool,
Optional: true,
Computed: true,
},
@ -118,6 +93,12 @@ func resourceCloudStackTemplate() *schema.Resource {
Type: schema.TypeBool,
Computed: true,
},
"is_ready_timeout": &schema.Schema{
Type: schema.TypeInt,
Optional: true,
Default: 300,
},
},
}
}
@ -125,79 +106,90 @@ func resourceCloudStackTemplate() *schema.Resource {
func resourceCloudStackTemplateCreate(d *schema.ResourceData, meta interface{}) error {
cs := meta.(*cloudstack.CloudStackClient)
//Retrieving required parameters
format := d.Get("format").(string)
hypervisor := d.Get("hypervisor").(string)
name := d.Get("name").(string)
// Compute/set the display text
displaytext := d.Get("display_text").(string)
if displaytext == "" {
displaytext = name
}
// Retrieve the os_type UUID
ostypeid, e := retrieveUUID(cs, "os_type", d.Get("os_type").(string))
if e != nil {
return e.Error()
}
url := d.Get("url").(string)
//Retrieve the zone UUID
// Retrieve the zone UUID
zoneid, e := retrieveUUID(cs, "zone", d.Get("zone").(string))
if e != nil {
return e.Error()
}
// Compute/set the display text
displaytext, ok := d.GetOk("display_text")
if !ok {
displaytext = name
// 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))
}
// Create a new parameter struct
p := cs.Template.NewRegisterTemplateParams(displaytext.(string), format, hypervisor, name, ostypeid, url, zoneid)
//Set optional parameters
p.SetPasswordenabled(d.Get("password_enabled").(bool))
p.SetSshkeyenabled(d.Get("ssh_key_enabled").(bool))
p.SetIsdynamicallyscalable(d.Get("is_dynamically_scalable").(bool))
p.SetRequireshvm(d.Get("requires_hvm").(bool))
p.SetIsfeatured(d.Get("is_featured").(bool))
ttag := d.Get("template_tag").(string)
if ttag != "" {
//error if we give this a value as non-root
p.SetTemplatetag(ttag)
if v, ok := d.GetOk("is_extractable"); ok {
p.SetIsextractable(v.(bool))
}
ir := d.Get("is_routing").(bool)
if ir == true {
p.SetIsrouting(ir)
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))
}
p.SetIspublic(d.Get("is_public").(bool))
p.SetIsextractable(d.Get("is_extractable").(bool))
p.SetBits(d.Get("bits").(int))
p.SetChecksum(d.Get("checksum").(string))
//TODO: set project ref / details
// Create the new template
r, err := cs.Template.RegisterTemplate(p)
if err != nil {
return fmt.Errorf("Error creating template %s: %s", name, err)
}
log.Printf("[DEBUG] Register template response: %+v\n", r)
d.SetId(r.RegisterTemplate[0].Id)
//dont return until the template is ready to use
result := resourceCloudStackTemplateRead(d, meta)
for !d.Get("is_ready").(bool) {
time.Sleep(5 * time.Second)
result = resourceCloudStackTemplateRead(d, meta)
// 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 {
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")
}
time.Sleep(5 * time.Second)
}
return result
}
func resourceCloudStackTemplateRead(d *schema.ResourceData, meta interface{}) error {
cs := meta.(*cloudstack.CloudStackClient)
log.Printf("[DEBUG] looking for template %s", d.Id())
// Get the template details
t, count, err := cs.Template.GetTemplateByID(d.Id(), "all")
t, count, err := cs.Template.GetTemplateByID(d.Id(), "executable")
if err != nil {
if count == 0 {
log.Printf(
@ -208,74 +200,63 @@ func resourceCloudStackTemplateRead(d *schema.ResourceData, meta interface{}) er
return err
}
d.Set("name", t.Name)
d.Set("display_text", t.Displaytext)
d.Set("zone", t.Zonename)
d.Set("format", t.Format)
d.Set("hypervisor", t.Hypervisor)
d.Set("os_type", t.Ostypename)
d.Set("format", t.Format)
d.Set("zone", t.Zonename)
d.Set("is_featured", t.Isfeatured)
d.Set("password_enabled", t.Passwordenabled)
d.Set("template_tag", t.Templatetag)
d.Set("ssh_key_enabled", t.Sshkeyenabled)
d.Set("is_public", t.Ispublic)
d.Set("is_extractable", t.Isextractable)
d.Set("is_dynamically_scalable", t.Isdynamicallyscalable)
d.Set("checksum", t.Checksum)
d.Set("is_extractable", t.Isextractable)
d.Set("is_featured", t.Isfeatured)
d.Set("is_public", t.Ispublic)
d.Set("password_enabled", t.Passwordenabled)
d.Set("is_ready", t.Isready)
log.Printf("[DEBUG] Read template values: %+v\n", d)
return nil
}
func resourceCloudStackTemplateUpdate(d *schema.ResourceData, meta interface{}) error {
cs := meta.(*cloudstack.CloudStackClient)
d.Partial(true)
name := d.Get("name").(string)
sim_attrs := []string{"name", "display_text", "bootable", "is_dynamically_scalable",
"is_routing"}
for _, attr := range sim_attrs {
if d.HasChange(attr) {
log.Printf("[DEBUG] %s changed for %s, starting update", attr, name)
// Create a new parameter struct
p := cs.Template.NewUpdateTemplateParams(d.Id())
switch attr {
case "name":
if d.HasChange("name") {
p.SetName(name)
case "display_text":
p.SetDisplaytext(d.Get("display_name").(string))
case "bootable":
p.SetBootable(d.Get("bootable").(bool))
case "is_dynamically_scalable":
}
if d.HasChange("display_text") {
p.SetDisplaytext(d.Get("display_text").(string))
}
if d.HasChange("format") {
p.SetFormat(d.Get("format").(string))
}
if d.HasChange("is_dynamically_scalable") {
p.SetIsdynamicallyscalable(d.Get("is_dynamically_scalable").(bool))
case "is_routing":
p.SetIsrouting(d.Get("is_routing").(bool))
default:
return fmt.Errorf("Unhandleable updateable attribute was declared, fix the code here.")
}
_, err := cs.Template.UpdateTemplate(p)
if err != nil {
return fmt.Errorf("Error updating the %s for instance %s: %s", attr, name, err)
}
d.SetPartial(attr)
}
}
if d.HasChange("os_type") {
log.Printf("[DEBUG] OS type changed for %s, starting update", name)
p := cs.Template.NewUpdateTemplateParams(d.Id())
ostypeid, e := retrieveUUID(cs, "os_type", d.Get("os_type").(string))
if e != nil {
return e.Error()
}
p.SetOstypeid(ostypeid)
}
if d.HasChange("password_enabled") {
p.SetPasswordenabled(d.Get("password_enabled").(bool))
}
_, err := cs.Template.UpdateTemplate(p)
if err != nil {
return fmt.Errorf("Error updating the OS type for instance %s: %s", name, err)
return fmt.Errorf("Error updating template %s: %s", name, err)
}
d.SetPartial("os_type")
}
d.Partial(false)
return resourceCloudStackTemplateRead(d, meta)
}
@ -286,7 +267,7 @@ func resourceCloudStackTemplateDelete(d *schema.ResourceData, meta interface{})
p := cs.Template.NewDeleteTemplateParams(d.Id())
// Delete the template
log.Printf("[INFO] Destroying instance: %s", d.Get("name").(string))
log.Printf("[INFO] Deleting template: %s", d.Get("name").(string))
_, err := cs.Template.DeleteTemplate(p)
if err != nil {
// This is a very poor way to be told the UUID does no longer exist :(

View File

@ -9,7 +9,7 @@ import (
"github.com/xanzy/go-cloudstack/cloudstack"
)
func TestAccCloudStackTemplate_full(t *testing.T) {
func TestAccCloudStackTemplate_basic(t *testing.T) {
var template cloudstack.Template
resource.Test(t, resource.TestCase{
@ -18,18 +18,50 @@ func TestAccCloudStackTemplate_full(t *testing.T) {
CheckDestroy: testAccCheckCloudStackTemplateDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccCloudStackTemplate_options,
Config: testAccCloudStackTemplate_basic,
Check: resource.ComposeTestCheckFunc(
testAccCheckCloudStackTemplateExists("cloudstack_template.foo", &template),
testAccCheckCloudStackTemplateBasicAttributes(&template),
testAccCheckCloudStackTemplateOptionalAttributes(&template),
resource.TestCheckResourceAttr(
"cloudstack_template.foo", "display_text", "terraform-test"),
),
},
},
})
}
func testAccCheckCloudStackTemplateExists(n string, template *cloudstack.Template) resource.TestCheckFunc {
func TestAccCloudStackTemplate_update(t *testing.T) {
var template cloudstack.Template
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckCloudStackTemplateDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccCloudStackTemplate_basic,
Check: resource.ComposeTestCheckFunc(
testAccCheckCloudStackTemplateExists("cloudstack_template.foo", &template),
testAccCheckCloudStackTemplateBasicAttributes(&template),
),
},
resource.TestStep{
Config: testAccCloudStackTemplate_update,
Check: resource.ComposeTestCheckFunc(
testAccCheckCloudStackTemplateExists(
"cloudstack_template.foo", &template),
testAccCheckCloudStackTemplateUpdatedAttributes(&template),
resource.TestCheckResourceAttr(
"cloudstack_template.foo", "display_text", "terraform-updated"),
),
},
},
})
}
func testAccCheckCloudStackTemplateExists(
n string, template *cloudstack.Template) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
if !ok {
@ -41,7 +73,7 @@ func testAccCheckCloudStackTemplateExists(n string, template *cloudstack.Templat
}
cs := testAccProvider.Meta().(*cloudstack.CloudStackClient)
tmpl, _, err := cs.Template.GetTemplateByID(rs.Primary.ID, "all")
tmpl, _, err := cs.Template.GetTemplateByID(rs.Primary.ID, "executable")
if err != nil {
return err
@ -57,6 +89,54 @@ func testAccCheckCloudStackTemplateExists(n string, template *cloudstack.Templat
}
}
func testAccCheckCloudStackTemplateBasicAttributes(
template *cloudstack.Template) resource.TestCheckFunc {
return func(s *terraform.State) error {
if template.Name != "terraform-test" {
return fmt.Errorf("Bad name: %s", template.Name)
}
if template.Format != CLOUDSTACK_TEMPLATE_FORMAT {
return fmt.Errorf("Bad format: %s", template.Format)
}
if template.Hypervisor != CLOUDSTACK_HYPERVISOR {
return fmt.Errorf("Bad hypervisor: %s", template.Hypervisor)
}
if template.Ostypename != CLOUDSTACK_TEMPLATE_OS_TYPE {
return fmt.Errorf("Bad os type: %s", template.Ostypename)
}
if template.Zonename != CLOUDSTACK_ZONE {
return fmt.Errorf("Bad zone: %s", template.Zonename)
}
return nil
}
}
func testAccCheckCloudStackTemplateUpdatedAttributes(
template *cloudstack.Template) resource.TestCheckFunc {
return func(s *terraform.State) error {
if template.Displaytext != "terraform-updated" {
return fmt.Errorf("Bad name: %s", template.Displaytext)
}
if !template.Isdynamicallyscalable {
return fmt.Errorf("Bad is_dynamically_scalable: %t", template.Isdynamicallyscalable)
}
if !template.Passwordenabled {
return fmt.Errorf("Bad password_enabled: %t", template.Passwordenabled)
}
return nil
}
}
func testAccCheckCloudStackTemplateDestroy(s *terraform.State) error {
cs := testAccProvider.Meta().(*cloudstack.CloudStackClient)
@ -84,100 +164,35 @@ func testAccCheckCloudStackTemplateDestroy(s *terraform.State) error {
var testAccCloudStackTemplate_basic = fmt.Sprintf(`
resource "cloudstack_template" "foo" {
name = "terraform-acc-test"
url = "%s"
name = "terraform-test"
format = "%s"
hypervisor = "%s"
os_type = "%s"
format = "%s"
url = "%s"
zone = "%s"
}
`,
CLOUDSTACK_TEMPLATE_URL,
CLOUDSTACK_TEMPLATE_FORMAT,
CLOUDSTACK_HYPERVISOR,
CLOUDSTACK_TEMPLATE_OS_TYPE,
CLOUDSTACK_TEMPLATE_FORMAT,
CLOUDSTACK_TEMPLATE_URL,
CLOUDSTACK_ZONE)
func testAccCheckCloudStackTemplateBasicAttributes(template *cloudstack.Template) resource.TestCheckFunc {
return func(s *terraform.State) error {
if template.Name != "terraform-acc-test" {
return fmt.Errorf("Bad name: %s", template.Name)
}
//todo: could add size to schema and check that, would assure we downloaded/initialize the image properly
if template.Hypervisor != CLOUDSTACK_HYPERVISOR {
return fmt.Errorf("Bad hypervisor: %s", template.Hypervisor)
}
if template.Ostypename != CLOUDSTACK_TEMPLATE_OS_TYPE {
return fmt.Errorf("Bad os type: %s", template.Ostypename)
}
if template.Format != CLOUDSTACK_TEMPLATE_FORMAT {
return fmt.Errorf("Bad format: %s", template.Format)
}
if template.Zonename != CLOUDSTACK_ZONE {
return fmt.Errorf("Bad zone: %s", template.Zonename)
}
return nil
}
}
//may prove difficult to test isrouting, isfeatured, ispublic, bits so not set here
var testAccCloudStackTemplate_options = fmt.Sprintf(`
var testAccCloudStackTemplate_update = fmt.Sprintf(`
resource "cloudstack_template" "foo" {
name = "terraform-acc-test"
url = "%s"
name = "terraform-test"
display_text = "terraform-updated"
format = "%s"
hypervisor = "%s"
os_type = "%s"
format = "%s"
url = "%s"
zone = "%s"
password_enabled = true
template_tag = "acctest"
ssh_key_enabled = true
is_extractable = true
is_dynamically_scalable = true
checksum = "%s"
password_enabled = true
}
`,
CLOUDSTACK_TEMPLATE_URL,
CLOUDSTACK_TEMPLATE_FORMAT,
CLOUDSTACK_HYPERVISOR,
CLOUDSTACK_TEMPLATE_OS_TYPE,
CLOUDSTACK_TEMPLATE_FORMAT,
CLOUDSTACK_ZONE,
CLOUDSTACK_TEMPLATE_CHECKSUM)
func testAccCheckCloudStackTemplateOptionalAttributes(template *cloudstack.Template) resource.TestCheckFunc {
return func(s *terraform.State) error {
if !template.Passwordenabled {
return fmt.Errorf("Bad password_enabled: %s", template.Passwordenabled)
}
if template.Templatetag != "acctest" {
return fmt.Errorf("Bad template_tag: %s", template.Templatetag)
}
if !template.Sshkeyenabled {
return fmt.Errorf("Bad ssh_key_enabled: %s", template.Sshkeyenabled)
}
if !template.Isextractable {
return fmt.Errorf("Bad is_extractable: %s", template.Isextractable)
}
if !template.Isdynamicallyscalable {
return fmt.Errorf("Bad is_dynamically_scalable: %s", template.Isdynamicallyscalable)
}
if template.Checksum != CLOUDSTACK_TEMPLATE_CHECKSUM {
return fmt.Errorf("Bad checksum: %s", template.Checksum)
}
return nil
}
}
CLOUDSTACK_TEMPLATE_URL,
CLOUDSTACK_ZONE)

View File

@ -36,19 +36,6 @@ func retrieveUUID(cs *cloudstack.CloudStackClient, name, value string) (uuid str
uuid, err = cs.ServiceOffering.GetServiceOfferingID(value)
case "network_offering":
uuid, err = cs.NetworkOffering.GetNetworkOfferingID(value)
case "os_type":
p := cs.GuestOS.NewListOsTypesParams()
p.SetDescription(value)
o, e := cs.GuestOS.ListOsTypes(p)
if e != nil {
err = e
break
}
if o.Count == 1 {
uuid = o.OsTypes[0].Id
break
}
err = fmt.Errorf("Could not find UUID of OS Type: %s", value)
case "vpc_offering":
uuid, err = cs.VPC.GetVPCOfferingID(value)
case "vpc":
@ -70,6 +57,19 @@ func retrieveUUID(cs *cloudstack.CloudStackClient, name, value string) (uuid str
break
}
err = fmt.Errorf("Could not find UUID of IP address: %s", value)
case "os_type":
p := cs.GuestOS.NewListOsTypesParams()
p.SetDescription(value)
l, e := cs.GuestOS.ListOsTypes(p)
if e != nil {
err = e
break
}
if l.Count == 1 {
uuid = l.OsTypes[0].Id
break
}
err = fmt.Errorf("Could not find UUID of OS Type: %s", value)
default:
return uuid, &retrieveError{name: name, value: value,
err: fmt.Errorf("Unknown request: %s", name)}