From a09afbb4aa8a9b333455193b934d02860b8496fa Mon Sep 17 00:00:00 2001 From: _jac Date: Wed, 27 May 2015 18:33:57 -0700 Subject: [PATCH 1/5] add project support https://github.com/xanzy/go-cloudstack/pull/21/files --- .../cloudstack/resource_cloudstack_instance.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/builtin/providers/cloudstack/resource_cloudstack_instance.go b/builtin/providers/cloudstack/resource_cloudstack_instance.go index c07678f3e..eb4f2fc05 100644 --- a/builtin/providers/cloudstack/resource_cloudstack_instance.go +++ b/builtin/providers/cloudstack/resource_cloudstack_instance.go @@ -82,6 +82,11 @@ func resourceCloudStackInstance() *schema.Resource { Optional: true, Default: false, }, + "project_name": &schema.Schema{ + Type: schema.TypeString, + Optional: true, + Default: nil, + }, }, } } @@ -148,6 +153,17 @@ func resourceCloudStackInstanceCreate(d *schema.ResourceData, meta interface{}) p.SetUserdata(ud) } + // If the project_name contains any info, we retreive the project_id + if projectName, ok := d.GetOk("project_name"); ok { + project, _, err := cs.Project.GetProjectByName(projectName.(string)) + if err != nil { + return err + } + log.Printf("[DEBUG] project id %s", project.Id) + p.SetProjectid(project.Id) + d.Set("project_id", project.Id) + } + // Create the new instance r, err := cs.VirtualMachine.DeployVirtualMachine(p) if err != nil { From 7e49714c3dc9723e0cc18544b9c3c104467a09ff Mon Sep 17 00:00:00 2001 From: Jacques Lemieux Date: Thu, 28 May 2015 18:41:58 -0700 Subject: [PATCH 2/5] fixed project schema, added project support to retrieveUUID --- .../resource_cloudstack_instance.go | 24 ++++++++++--------- builtin/providers/cloudstack/resources.go | 2 ++ 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/builtin/providers/cloudstack/resource_cloudstack_instance.go b/builtin/providers/cloudstack/resource_cloudstack_instance.go index eb4f2fc05..a37661f45 100644 --- a/builtin/providers/cloudstack/resource_cloudstack_instance.go +++ b/builtin/providers/cloudstack/resource_cloudstack_instance.go @@ -82,11 +82,13 @@ func resourceCloudStackInstance() *schema.Resource { Optional: true, Default: false, }, - "project_name": &schema.Schema{ + + "project": &schema.Schema{ Type: schema.TypeString, Optional: true, - Default: nil, + ForceNew: true, }, + }, } } @@ -153,15 +155,14 @@ func resourceCloudStackInstanceCreate(d *schema.ResourceData, meta interface{}) p.SetUserdata(ud) } - // If the project_name contains any info, we retreive the project_id - if projectName, ok := d.GetOk("project_name"); ok { - project, _, err := cs.Project.GetProjectByName(projectName.(string)) - if err != nil { - return err + // 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", project.Id) - p.SetProjectid(project.Id) - d.Set("project_id", project.Id) + log.Printf("[DEBUG] project id %s", projectid) + p.SetProjectid(projectid) } // Create the new instance @@ -206,7 +207,8 @@ 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 } diff --git a/builtin/providers/cloudstack/resources.go b/builtin/providers/cloudstack/resources.go index 85fa0bd5a..5d0e5eb55 100644 --- a/builtin/providers/cloudstack/resources.go +++ b/builtin/providers/cloudstack/resources.go @@ -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)} From 5687045f1013cfe693c81260307f61616538469b Mon Sep 17 00:00:00 2001 From: Jacques Lemieux Date: Mon, 1 Jun 2015 17:56:43 -0700 Subject: [PATCH 3/5] added test for instance creation with project name --- .../resource_cloudstack_instance_test.go | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/builtin/providers/cloudstack/resource_cloudstack_instance_test.go b/builtin/providers/cloudstack/resource_cloudstack_instance_test.go index 2d1be9c53..587bf9f11 100644 --- a/builtin/providers/cloudstack/resource_cloudstack_instance_test.go +++ b/builtin/providers/cloudstack/resource_cloudstack_instance_test.go @@ -87,6 +87,27 @@ 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 testAccCheckCloudStackInstanceExists( n string, instance *cloudstack.VirtualMachine) resource.TestCheckFunc { return func(s *terraform.State) error { @@ -233,3 +254,21 @@ 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) From 6c7f632c22ea039be416dbd986e32cdc17575acb Mon Sep 17 00:00:00 2001 From: Jacques Lemieux Date: Mon, 1 Jun 2015 18:01:28 -0700 Subject: [PATCH 4/5] added test creating instance with project id, added const in provider_test --- builtin/providers/cloudstack/provider_test.go | 3 ++ .../resource_cloudstack_instance_test.go | 38 +++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/builtin/providers/cloudstack/provider_test.go b/builtin/providers/cloudstack/provider_test.go index c9da7dc8a..6b4e09747 100644 --- a/builtin/providers/cloudstack/provider_test.go +++ b/builtin/providers/cloudstack/provider_test.go @@ -87,3 +87,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 = "" + diff --git a/builtin/providers/cloudstack/resource_cloudstack_instance_test.go b/builtin/providers/cloudstack/resource_cloudstack_instance_test.go index 587bf9f11..6c180f136 100644 --- a/builtin/providers/cloudstack/resource_cloudstack_instance_test.go +++ b/builtin/providers/cloudstack/resource_cloudstack_instance_test.go @@ -108,6 +108,27 @@ func TestAccCloudStackInstance_Projectname(t *testing.T) { }) } +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 { @@ -272,3 +293,20 @@ resource "cloudstack_instance" "foobar" { 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) From 1d9df96fc1ba3b8a763f77832c42af4d0d6c3f80 Mon Sep 17 00:00:00 2001 From: Jacques Lemieux Date: Wed, 3 Jun 2015 11:25:07 -0700 Subject: [PATCH 5/5] typo --- .../providers/cloudstack/resource_cloudstack_instance_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/builtin/providers/cloudstack/resource_cloudstack_instance_test.go b/builtin/providers/cloudstack/resource_cloudstack_instance_test.go index 6c180f136..dd65d0724 100644 --- a/builtin/providers/cloudstack/resource_cloudstack_instance_test.go +++ b/builtin/providers/cloudstack/resource_cloudstack_instance_test.go @@ -309,4 +309,4 @@ resource "cloudstack_instance" "foobar" { CLOUDSTACK_NETWORK_1, CLOUDSTACK_TEMPLATE, CLOUDSTACK_ZONE, - CLOUDSTACK_PROJECT_id) + CLOUDSTACK_PROJECT_ID)