Merge pull request #2115 from jalemieux/master

provider/cloudstack: add project support for CloudStack Instances
This commit is contained in:
Sander van Harmelen 2015-06-08 17:34:24 +02:00
commit ec5cef4de8
4 changed files with 101 additions and 1 deletions

View File

@ -89,3 +89,6 @@ var CLOUDSTACK_TEMPLATE_FORMAT = ""
var CLOUDSTACK_TEMPLATE_URL = ""
var CLOUDSTACK_TEMPLATE_OS_TYPE = ""
var CLOUDSTACK_ZONE = ""
var CLOUDSTACK_PROJECT_NAME = ""
var CLOUDSTACK_PROJECT_ID = ""

View File

@ -87,6 +87,13 @@ func resourceCloudStackInstance() *schema.Resource {
Optional: true,
Default: false,
},
"project": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
},
}
}
@ -157,6 +164,16 @@ func resourceCloudStackInstanceCreate(d *schema.ResourceData, meta interface{})
p.SetUserdata(ud)
}
// If project contains any info, we retreive the project id
if project, ok := d.GetOk("project"); ok {
projectid, e := retrieveUUID(cs, "project", project.(string))
if e != nil {
return e.Error()
}
log.Printf("[DEBUG] project id %s", projectid)
p.SetProjectid(projectid)
}
// Create the new instance
r, err := cs.VirtualMachine.DeployVirtualMachine(p)
if err != nil {
@ -200,6 +217,7 @@ func resourceCloudStackInstanceRead(d *schema.ResourceData, meta interface{}) er
setValueOrUUID(d, "network", vm.Nic[0].Networkname, vm.Nic[0].Networkid)
setValueOrUUID(d, "service_offering", vm.Serviceofferingname, vm.Serviceofferingid)
setValueOrUUID(d, "template", vm.Templatename, vm.Templateid)
setValueOrUUID(d, "project", vm.Project, vm.Projectid)
return nil
}

View File

@ -99,6 +99,48 @@ func TestAccCloudStackInstance_fixedIP(t *testing.T) {
})
}
func TestAccCloudStackInstance_Projectname(t *testing.T) {
var instance cloudstack.VirtualMachine
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckCloudStackInstanceDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccCloudStackInstance_projectname,
Check: resource.ComposeTestCheckFunc(
testAccCheckCloudStackInstanceExists(
"cloudstack_instance.foobar", &instance),
resource.TestCheckResourceAttr(
"cloudstack_instance.foobar", "project", CLOUDSTACK_PROJECT_NAME),
),
},
},
})
}
func TestAccCloudStackInstance_Projectid(t *testing.T) {
var instance cloudstack.VirtualMachine
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckCloudStackInstanceDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccCloudStackInstance_projectid,
Check: resource.ComposeTestCheckFunc(
testAccCheckCloudStackInstanceExists(
"cloudstack_instance.foobar", &instance),
resource.TestCheckResourceAttr(
"cloudstack_instance.foobar", "project", CLOUDSTACK_PROJECT_ID),
),
},
},
})
}
func testAccCheckCloudStackInstanceExists(
n string, instance *cloudstack.VirtualMachine) resource.TestCheckFunc {
return func(s *terraform.State) error {
@ -249,3 +291,38 @@ resource "cloudstack_instance" "foobar" {
CLOUDSTACK_NETWORK_1_IPADDRESS,
CLOUDSTACK_TEMPLATE,
CLOUDSTACK_ZONE)
var testAccCloudStackInstance_projectname = fmt.Sprintf(`
resource "cloudstack_instance" "foobar" {
name = "terraform-test"
display_name = "terraform"
service_offering= "%s"
network = "%s"
template = "%s"
zone = "%s"
expunge = true
project = "%s"
}`,
CLOUDSTACK_SERVICE_OFFERING_1,
CLOUDSTACK_NETWORK_1,
CLOUDSTACK_TEMPLATE,
CLOUDSTACK_ZONE,
CLOUDSTACK_PROJECT_NAME)
var testAccCloudStackInstance_projectid = fmt.Sprintf(`
resource "cloudstack_instance" "foobar" {
name = "terraform-test"
display_name = "terraform"
service_offering= "%s"
network = "%s"
template = "%s"
zone = "%s"
expunge = true
project = "%s"
}`,
CLOUDSTACK_SERVICE_OFFERING_1,
CLOUDSTACK_NETWORK_1,
CLOUDSTACK_TEMPLATE,
CLOUDSTACK_ZONE,
CLOUDSTACK_PROJECT_ID)

View File

@ -79,6 +79,8 @@ func retrieveUUID(cs *cloudstack.CloudStackClient, name, value string) (uuid str
break
}
err = fmt.Errorf("Could not find UUID of OS Type: %s", value)
case "project":
uuid, err = cs.Project.GetProjectID(value)
default:
return uuid, &retrieveError{name: name, value: value,
err: fmt.Errorf("Unknown request: %s", name)}