2015-03-18 11:11:56 +01:00
|
|
|
package cloudstack
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
"github.com/xanzy/go-cloudstack/cloudstack"
|
|
|
|
)
|
|
|
|
|
2015-04-11 17:50:06 +02:00
|
|
|
func TestAccCloudStackTemplate_basic(t *testing.T) {
|
2015-03-18 11:11:56 +01:00
|
|
|
var template cloudstack.Template
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckCloudStackTemplateDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
2015-04-11 17:50:06 +02:00
|
|
|
Config: testAccCloudStackTemplate_basic,
|
2015-03-18 11:11:56 +01:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckCloudStackTemplateExists("cloudstack_template.foo", &template),
|
|
|
|
testAccCheckCloudStackTemplateBasicAttributes(&template),
|
2015-04-11 17:50:06 +02:00
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"cloudstack_template.foo", "display_text", "terraform-test"),
|
2015-03-18 11:11:56 +01:00
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-04-11 17:50:06 +02:00
|
|
|
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 {
|
2015-03-18 11:11:56 +01:00
|
|
|
return func(s *terraform.State) error {
|
|
|
|
rs, ok := s.RootModule().Resources[n]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Not found: %s", n)
|
|
|
|
}
|
|
|
|
|
|
|
|
if rs.Primary.ID == "" {
|
|
|
|
return fmt.Errorf("No template ID is set")
|
|
|
|
}
|
|
|
|
|
|
|
|
cs := testAccProvider.Meta().(*cloudstack.CloudStackClient)
|
2015-04-11 17:50:06 +02:00
|
|
|
tmpl, _, err := cs.Template.GetTemplateByID(rs.Primary.ID, "executable")
|
2015-03-18 11:11:56 +01:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if tmpl.Id != rs.Primary.ID {
|
|
|
|
return fmt.Errorf("Template not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
*template = *tmpl
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-11 17:50:06 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-18 11:11:56 +01:00
|
|
|
func testAccCheckCloudStackTemplateDestroy(s *terraform.State) error {
|
|
|
|
cs := testAccProvider.Meta().(*cloudstack.CloudStackClient)
|
|
|
|
|
|
|
|
for _, rs := range s.RootModule().Resources {
|
|
|
|
if rs.Type != "cloudstack_template" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if rs.Primary.ID == "" {
|
|
|
|
return fmt.Errorf("No template ID is set")
|
|
|
|
}
|
|
|
|
|
2015-06-09 12:38:05 +02:00
|
|
|
_, _, err := cs.Template.GetTemplateByID(rs.Primary.ID, "executable")
|
|
|
|
if err == nil {
|
|
|
|
return fmt.Errorf("Template %s still exists", rs.Primary.ID)
|
2015-03-18 11:11:56 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var testAccCloudStackTemplate_basic = fmt.Sprintf(`
|
|
|
|
resource "cloudstack_template" "foo" {
|
2015-04-11 17:50:06 +02:00
|
|
|
name = "terraform-test"
|
|
|
|
format = "%s"
|
2015-03-18 11:11:56 +01:00
|
|
|
hypervisor = "%s"
|
2015-04-11 17:50:06 +02:00
|
|
|
os_type = "%s"
|
|
|
|
url = "%s"
|
2015-03-18 11:11:56 +01:00
|
|
|
zone = "%s"
|
|
|
|
}
|
|
|
|
`,
|
2015-04-11 17:50:06 +02:00
|
|
|
CLOUDSTACK_TEMPLATE_FORMAT,
|
2015-03-18 11:11:56 +01:00
|
|
|
CLOUDSTACK_HYPERVISOR,
|
|
|
|
CLOUDSTACK_TEMPLATE_OS_TYPE,
|
2015-04-11 17:50:06 +02:00
|
|
|
CLOUDSTACK_TEMPLATE_URL,
|
2015-03-18 11:11:56 +01:00
|
|
|
CLOUDSTACK_ZONE)
|
|
|
|
|
2015-04-11 17:50:06 +02:00
|
|
|
var testAccCloudStackTemplate_update = fmt.Sprintf(`
|
2015-03-18 11:11:56 +01:00
|
|
|
resource "cloudstack_template" "foo" {
|
2015-04-11 17:50:06 +02:00
|
|
|
name = "terraform-test"
|
|
|
|
display_text = "terraform-updated"
|
|
|
|
format = "%s"
|
2015-03-18 11:11:56 +01:00
|
|
|
hypervisor = "%s"
|
|
|
|
os_type = "%s"
|
2015-04-11 17:50:06 +02:00
|
|
|
url = "%s"
|
2015-03-18 11:11:56 +01:00
|
|
|
zone = "%s"
|
|
|
|
is_dynamically_scalable = true
|
2015-04-11 17:50:06 +02:00
|
|
|
password_enabled = true
|
2015-03-18 11:11:56 +01:00
|
|
|
}
|
|
|
|
`,
|
2015-04-11 17:50:06 +02:00
|
|
|
CLOUDSTACK_TEMPLATE_FORMAT,
|
2015-03-18 11:11:56 +01:00
|
|
|
CLOUDSTACK_HYPERVISOR,
|
|
|
|
CLOUDSTACK_TEMPLATE_OS_TYPE,
|
2015-04-11 17:50:06 +02:00
|
|
|
CLOUDSTACK_TEMPLATE_URL,
|
|
|
|
CLOUDSTACK_ZONE)
|