From 03d7c1fa7d8a4dc1e5bf190f6accbba6f1060b5b Mon Sep 17 00:00:00 2001 From: Hany Fahim Date: Thu, 24 Sep 2015 15:57:25 -0400 Subject: [PATCH 1/7] Allow for -1 for Zone ID, which is valid in CloudStack --- .../providers/cloudstack/resource_cloudstack_template.go | 9 +++++++-- builtin/providers/cloudstack/resources.go | 3 +++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/builtin/providers/cloudstack/resource_cloudstack_template.go b/builtin/providers/cloudstack/resource_cloudstack_template.go index 15c6ebec4..139a1ef52 100644 --- a/builtin/providers/cloudstack/resource_cloudstack_template.go +++ b/builtin/providers/cloudstack/resource_cloudstack_template.go @@ -219,9 +219,14 @@ func resourceCloudStackTemplateRead(d *schema.ResourceData, meta interface{}) er d.Set("password_enabled", t.Passwordenabled) d.Set("is_ready", t.Isready) + if t.Zoneid == "" { + d.Set("zone", "-1") + } else { + setValueOrUUID(d, "zone", t.Zonename, t.Zoneid) + } + setValueOrUUID(d, "os_type", t.Ostypename, t.Ostypeid) - setValueOrUUID(d, "zone", t.Zonename, t.Zoneid) - + return nil } diff --git a/builtin/providers/cloudstack/resources.go b/builtin/providers/cloudstack/resources.go index cc826492f..6e259ba5f 100644 --- a/builtin/providers/cloudstack/resources.go +++ b/builtin/providers/cloudstack/resources.go @@ -53,6 +53,9 @@ func retrieveUUID(cs *cloudstack.CloudStackClient, name, value string) (uuid str case "network": uuid, err = cs.Network.GetNetworkID(value) case "zone": + if value == "-1" { + return value, nil + } uuid, err = cs.Zone.GetZoneID(value) case "ipaddress": p := cs.Address.NewListPublicIpAddressesParams() From aa950be63bf5120fa1200824e0332fe0fde52b5b Mon Sep 17 00:00:00 2001 From: Hany Fahim Date: Thu, 24 Sep 2015 16:16:12 -0400 Subject: [PATCH 2/7] Use constant for global resources --- .../providers/cloudstack/resource_cloudstack_template.go | 6 +++--- builtin/providers/cloudstack/resources.go | 4 +++- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/builtin/providers/cloudstack/resource_cloudstack_template.go b/builtin/providers/cloudstack/resource_cloudstack_template.go index 139a1ef52..6762e3467 100644 --- a/builtin/providers/cloudstack/resource_cloudstack_template.go +++ b/builtin/providers/cloudstack/resource_cloudstack_template.go @@ -219,12 +219,12 @@ func resourceCloudStackTemplateRead(d *schema.ResourceData, meta interface{}) er d.Set("password_enabled", t.Passwordenabled) d.Set("is_ready", t.Isready) - if t.Zoneid == "" { - d.Set("zone", "-1") + if t.Zoneid == IS_GLOBAL_RESOURCE { + setValueOrUUID(d, "zone", t.Zonename, IS_GLOBAL_RESOURCE) } else { setValueOrUUID(d, "zone", t.Zonename, t.Zoneid) } - + setValueOrUUID(d, "os_type", t.Ostypename, t.Ostypeid) return nil diff --git a/builtin/providers/cloudstack/resources.go b/builtin/providers/cloudstack/resources.go index 6e259ba5f..954bfd36c 100644 --- a/builtin/providers/cloudstack/resources.go +++ b/builtin/providers/cloudstack/resources.go @@ -10,6 +10,8 @@ import ( "github.com/xanzy/go-cloudstack/cloudstack" ) +const IS_GLOBAL_RESOURCE = "-1" + type retrieveError struct { name string value string @@ -53,7 +55,7 @@ func retrieveUUID(cs *cloudstack.CloudStackClient, name, value string) (uuid str case "network": uuid, err = cs.Network.GetNetworkID(value) case "zone": - if value == "-1" { + if value == IS_GLOBAL_RESOURCE { return value, nil } uuid, err = cs.Zone.GetZoneID(value) From 7b4bb968d62d7b64de60afabde30554f7e082d9d Mon Sep 17 00:00:00 2001 From: Hany Fahim Date: Thu, 24 Sep 2015 16:28:40 -0400 Subject: [PATCH 3/7] Add project parameter to cloudstack_template resource --- .../resource_cloudstack_template.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/builtin/providers/cloudstack/resource_cloudstack_template.go b/builtin/providers/cloudstack/resource_cloudstack_template.go index 6762e3467..a045816dd 100644 --- a/builtin/providers/cloudstack/resource_cloudstack_template.go +++ b/builtin/providers/cloudstack/resource_cloudstack_template.go @@ -51,6 +51,12 @@ func resourceCloudStackTemplate() *schema.Resource { ForceNew: true, }, + "project": &schema.Schema{ + Type: schema.TypeString, + Optional: true, + ForceNew: true, + }, + "zone": &schema.Schema{ Type: schema.TypeString, Required: true, @@ -124,6 +130,17 @@ func resourceCloudStackTemplateCreate(d *schema.ResourceData, meta interface{}) return e.Error() } + // If there is a project supplied, we retrieve and set the project id + if project, ok := d.GetOk("project"); ok { + // Retrieve the project UUID + projectid, e := retrieveUUID(cs, "project", project.(string)) + if e != nil { + return e.Error() + } + // Set the default project ID + p.SetProjectid(projectid) + } + // Retrieve the zone UUID zoneid, e := retrieveUUID(cs, "zone", d.Get("zone").(string)) if e != nil { @@ -219,6 +236,8 @@ func resourceCloudStackTemplateRead(d *schema.ResourceData, meta interface{}) er d.Set("password_enabled", t.Passwordenabled) d.Set("is_ready", t.Isready) + setValueOrUUID(d, "project", t.Project, t.Projectid) + if t.Zoneid == IS_GLOBAL_RESOURCE { setValueOrUUID(d, "zone", t.Zonename, IS_GLOBAL_RESOURCE) } else { From fc89f576ca2acf6d1d1446da2999542af8a70233 Mon Sep 17 00:00:00 2001 From: Hany Fahim Date: Thu, 24 Sep 2015 16:49:21 -0400 Subject: [PATCH 4/7] Change IS_GLOBAL_RESOURCE to UnlimitedResourceID to keep terminology in sync with CloudStack --- builtin/providers/cloudstack/resource_cloudstack_template.go | 4 ++-- builtin/providers/cloudstack/resources.go | 5 +++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/builtin/providers/cloudstack/resource_cloudstack_template.go b/builtin/providers/cloudstack/resource_cloudstack_template.go index a045816dd..2d5c42497 100644 --- a/builtin/providers/cloudstack/resource_cloudstack_template.go +++ b/builtin/providers/cloudstack/resource_cloudstack_template.go @@ -238,8 +238,8 @@ func resourceCloudStackTemplateRead(d *schema.ResourceData, meta interface{}) er setValueOrUUID(d, "project", t.Project, t.Projectid) - if t.Zoneid == IS_GLOBAL_RESOURCE { - setValueOrUUID(d, "zone", t.Zonename, IS_GLOBAL_RESOURCE) + if t.Zoneid == UnlimitedResourceID { + setValueOrUUID(d, "zone", t.Zonename, UnlimitedResourceID) } else { setValueOrUUID(d, "zone", t.Zonename, t.Zoneid) } diff --git a/builtin/providers/cloudstack/resources.go b/builtin/providers/cloudstack/resources.go index 954bfd36c..02d7a81ec 100644 --- a/builtin/providers/cloudstack/resources.go +++ b/builtin/providers/cloudstack/resources.go @@ -10,7 +10,8 @@ import ( "github.com/xanzy/go-cloudstack/cloudstack" ) -const IS_GLOBAL_RESOURCE = "-1" +// CloudStack uses a "special" ID of -1 to define an unlimited resource +const UnlimitedResourceID = "-1" type retrieveError struct { name string @@ -55,7 +56,7 @@ func retrieveUUID(cs *cloudstack.CloudStackClient, name, value string) (uuid str case "network": uuid, err = cs.Network.GetNetworkID(value) case "zone": - if value == IS_GLOBAL_RESOURCE { + if value == UnlimitedResourceID { return value, nil } uuid, err = cs.Zone.GetZoneID(value) From 7852248f0eeb0564501d059bbbc127f27b3bb680 Mon Sep 17 00:00:00 2001 From: Hany Fahim Date: Thu, 24 Sep 2015 17:04:45 -0400 Subject: [PATCH 5/7] Moved project block down --- .../resource_cloudstack_template.go | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/builtin/providers/cloudstack/resource_cloudstack_template.go b/builtin/providers/cloudstack/resource_cloudstack_template.go index 2d5c42497..12de2152a 100644 --- a/builtin/providers/cloudstack/resource_cloudstack_template.go +++ b/builtin/providers/cloudstack/resource_cloudstack_template.go @@ -130,17 +130,6 @@ func resourceCloudStackTemplateCreate(d *schema.ResourceData, meta interface{}) return e.Error() } - // If there is a project supplied, we retrieve and set the project id - if project, ok := d.GetOk("project"); ok { - // Retrieve the project UUID - projectid, e := retrieveUUID(cs, "project", project.(string)) - if e != nil { - return e.Error() - } - // Set the default project ID - p.SetProjectid(projectid) - } - // Retrieve the zone UUID zoneid, e := retrieveUUID(cs, "zone", d.Get("zone").(string)) if e != nil { @@ -178,6 +167,17 @@ func resourceCloudStackTemplateCreate(d *schema.ResourceData, meta interface{}) p.SetPasswordenabled(v.(bool)) } + // If there is a project supplied, we retrieve and set the project id + if project, ok := d.GetOk("project"); ok { + // Retrieve the project UUID + projectid, e := retrieveUUID(cs, "project", project.(string)) + if e != nil { + return e.Error() + } + // Set the default project ID + p.SetProjectid(projectid) + } + // Create the new template r, err := cs.Template.RegisterTemplate(p) if err != nil { From a9b86636c80d8462dc6f0bed2f760da8a562aed6 Mon Sep 17 00:00:00 2001 From: Hany Fahim Date: Mon, 28 Sep 2015 13:31:08 -0400 Subject: [PATCH 6/7] Check for proper empty response instead of UnlimitedResourceID --- builtin/providers/cloudstack/resource_cloudstack_template.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/builtin/providers/cloudstack/resource_cloudstack_template.go b/builtin/providers/cloudstack/resource_cloudstack_template.go index 12de2152a..c2d9459a7 100644 --- a/builtin/providers/cloudstack/resource_cloudstack_template.go +++ b/builtin/providers/cloudstack/resource_cloudstack_template.go @@ -238,7 +238,7 @@ func resourceCloudStackTemplateRead(d *schema.ResourceData, meta interface{}) er setValueOrUUID(d, "project", t.Project, t.Projectid) - if t.Zoneid == UnlimitedResourceID { + if t.Zoneid == "" { setValueOrUUID(d, "zone", t.Zonename, UnlimitedResourceID) } else { setValueOrUUID(d, "zone", t.Zonename, t.Zoneid) From 1e90f986f22604f808f7af3857a16fa21f7b2ba0 Mon Sep 17 00:00:00 2001 From: Hany Fahim Date: Wed, 30 Sep 2015 14:03:14 -0400 Subject: [PATCH 7/7] Update conditional to set UnlimitedResourceID for Zonename as well as Zoneid --- builtin/providers/cloudstack/resource_cloudstack_template.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/builtin/providers/cloudstack/resource_cloudstack_template.go b/builtin/providers/cloudstack/resource_cloudstack_template.go index c2d9459a7..2cce32ae7 100644 --- a/builtin/providers/cloudstack/resource_cloudstack_template.go +++ b/builtin/providers/cloudstack/resource_cloudstack_template.go @@ -239,7 +239,7 @@ func resourceCloudStackTemplateRead(d *schema.ResourceData, meta interface{}) er setValueOrUUID(d, "project", t.Project, t.Projectid) if t.Zoneid == "" { - setValueOrUUID(d, "zone", t.Zonename, UnlimitedResourceID) + setValueOrUUID(d, "zone", UnlimitedResourceID, UnlimitedResourceID) } else { setValueOrUUID(d, "zone", t.Zonename, t.Zoneid) }