Skip to content

Commit

Permalink
Fixes #1: sets the rights values based on isUUID
Browse files Browse the repository at this point in the history
  • Loading branch information
av1nashs1ngh authored and Sander van Harmelen committed May 5, 2015
1 parent 61509fd commit 1411d40
Show file tree
Hide file tree
Showing 18 changed files with 71 additions and 33 deletions.
22 changes: 22 additions & 0 deletions builtin/providers/cloudstack/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,28 @@ func TestProvider_impl(t *testing.T) {
var _ terraform.ResourceProvider = Provider()
}

func testSetValueOnResourceData(t *testing.T) {
d := schema.ResourceData{}
d.Set("id", "name")

setValueOrUUID(&d, "id", "name", "54711781-274e-41b2-83c0-17194d0108f7")

if d.Get("id").(string) != "name" {
t.Fatal("err: 'id' does not match 'name'")
}
}

func testSetUuidOnResourceData(t *testing.T) {
d := schema.ResourceData{}
d.Set("id", "54711781-274e-41b2-83c0-17194d0108f7")

setValueOrUUID(&d, "id", "name", "54711781-274e-41b2-83c0-17194d0108f7")

if d.Get("id").(string) != "54711781-274e-41b2-83c0-17194d0108f7" {
t.Fatal("err: 'id' doest not match '54711781-274e-41b2-83c0-17194d0108f7'")
}
}

func testAccPreCheck(t *testing.T) {
if v := os.Getenv("CLOUDSTACK_API_URL"); v == "" {
t.Fatal("CLOUDSTACK_API_URL must be set for acceptance tests")
Expand Down
7 changes: 4 additions & 3 deletions builtin/providers/cloudstack/resource_cloudstack_disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,11 @@ func resourceCloudStackDiskRead(d *schema.ResourceData, meta interface{}) error
}

d.Set("name", v.Name)
d.Set("attach", v.Attached != "") // If attached this will contain a timestamp when attached
d.Set("disk_offering", v.Diskofferingname)
d.Set("attach", v.Attached != "") // If attached this will contain a timestamp when attached
d.Set("size", int(v.Size/(1024*1024*1024))) // Needed to get GB's again
d.Set("zone", v.Zonename)

setValueOrUUID(d, "disk_offering", v.Diskofferingname, v.Diskofferingid)
setValueOrUUID(d, "zone", v.Zonename, v.Zoneid)

if v.Attached != "" {
// Get the virtual machine details
Expand Down
7 changes: 4 additions & 3 deletions builtin/providers/cloudstack/resource_cloudstack_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,12 +184,13 @@ func resourceCloudStackInstanceRead(d *schema.ResourceData, meta interface{}) er
// Update the config
d.Set("name", vm.Name)
d.Set("display_name", vm.Displayname)
d.Set("service_offering", vm.Serviceofferingname)
d.Set("network", vm.Nic[0].Networkname)
d.Set("ipaddress", vm.Nic[0].Ipaddress)
d.Set("template", vm.Templatename)
d.Set("zone", vm.Zonename)

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)

return nil
}

Expand Down
5 changes: 3 additions & 2 deletions builtin/providers/cloudstack/resource_cloudstack_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,9 @@ func resourceCloudStackNetworkRead(d *schema.ResourceData, meta interface{}) err
d.Set("name", n.Name)
d.Set("display_text", n.Displaytext)
d.Set("cidr", n.Cidr)
d.Set("network_offering", n.Networkofferingname)
d.Set("zone", n.Zonename)

setValueOrUUID(d, "network_offering", n.Networkofferingname, n.Networkofferingid)
setValueOrUUID(d, "zone", n.Zonename, n.Zoneid)

return nil
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func resourceCloudStackNetworkACLRead(d *schema.ResourceData, meta interface{})
return err
}

d.Set("vpc", v.Name)
setValueOrUUID(d, "vpc", v.Name, v.Id)

return nil
}
Expand Down
4 changes: 2 additions & 2 deletions builtin/providers/cloudstack/resource_cloudstack_nic.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,9 @@ func resourceCloudStackNICRead(d *schema.ResourceData, meta interface{}) error {
found := false
for _, n := range vm.Nic {
if n.Id == d.Id() {
d.Set("network", n.Networkname)
d.Set("ipaddress", n.Ipaddress)
d.Set("virtual_machine", vm.Name)
setValueOrUUID(d, "network", n.Networkname, n.Networkid)
setValueOrUUID(d, "virtual_machine", vm.Name, vm.Id)
found = true
break
}
Expand Down
5 changes: 3 additions & 2 deletions builtin/providers/cloudstack/resource_cloudstack_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,15 +205,16 @@ func resourceCloudStackTemplateRead(d *schema.ResourceData, meta interface{}) er
d.Set("display_text", t.Displaytext)
d.Set("format", t.Format)
d.Set("hypervisor", t.Hypervisor)
d.Set("os_type", t.Ostypename)
d.Set("zone", t.Zonename)
d.Set("is_dynamically_scalable", t.Isdynamicallyscalable)
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)

setValueOrUUID(d, "os_type", t.Ostypename, t.Ostypeid)
setValueOrUUID(d, "zone", t.Zonename, t.Zoneid)

return nil
}

Expand Down
5 changes: 3 additions & 2 deletions builtin/providers/cloudstack/resource_cloudstack_vpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,15 +105,16 @@ func resourceCloudStackVPCRead(d *schema.ResourceData, meta interface{}) error {
d.Set("name", v.Name)
d.Set("display_text", v.Displaytext)
d.Set("cidr", v.Cidr)
d.Set("zone", v.Zonename)

setValueOrUUID(d, "zone", v.Zonename, v.Zoneid)

// Get the VPC offering details
o, _, err := cs.VPC.GetVPCOfferingByID(v.Vpcofferingid)
if err != nil {
return err
}

d.Set("vpc_offering", o.Name)
setValueOrUUID(d, "vpc_offering", o.Name, v.Vpcofferingid)

return nil
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ func resourceCloudStackVPNGatewayRead(d *schema.ResourceData, meta interface{})
return err
}

setValueOrUUID(d, "vpc", d.Get("vpc").(string), v.Vpcid)

d.Set("public_ip", v.Publicip)

return nil
Expand Down
9 changes: 9 additions & 0 deletions builtin/providers/cloudstack/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"log"
"regexp"

"github.com/hashicorp/terraform/helper/schema"
"github.com/xanzy/go-cloudstack/cloudstack"
)

Expand All @@ -18,6 +19,14 @@ func (e *retrieveError) Error() error {
return fmt.Errorf("Error retrieving UUID of %s %s: %s", e.name, e.value, e.err)
}

func setValueOrUUID(d *schema.ResourceData, key string, value string, uuid string) {
if isUUID(d.Get(key).(string)) {
d.Set(key, uuid)
} else {
d.Set(key, value)
}
}

func retrieveUUID(cs *cloudstack.CloudStackClient, name, value string) (uuid string, e *retrieveError) {
// If the supplied value isn't a UUID, try to retrieve the UUID ourselves
if isUUID(value) {
Expand Down
6 changes: 3 additions & 3 deletions website/source/docs/providers/cloudstack/r/disk.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ The following arguments are supported:

* `device` - (Optional) The device to map the disk volume to within the guest OS.

* `disk_offering` - (Required) The name of the disk offering to use for this
disk volume.
* `disk_offering` - (Required) The name or ID of the disk offering to use for
this disk volume.

* `size` - (Optional) The size of the disk volume in gigabytes.

Expand All @@ -47,7 +47,7 @@ The following arguments are supported:
* `virtual_machine` - (Optional) The name of the virtual machine to which you
want to attach the disk volume.

* `zone` - (Required) The name of the zone where this disk volume will be available.
* `zone` - (Required) The name or ID of the zone where this disk volume will be available.
Changing this forces a new resource to be created.

## Attributes Reference
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@ The following arguments are supported:

* `display_name` - (Optional) The display name of the instance.

* `service_offering` - (Required) The service offering used for this instance.
* `service_offering` - (Required) The name or ID of the service offering used for this instance.

* `network` - (Optional) The name of the network to connect this instance to.
* `network` - (Optional) The name or ID of the network to connect this instance to.
Changing this forces a new resource to be created.

* `ipaddress` - (Optional) The IP address to assign to this instance. Changing
this forces a new resource to be created.

* `template` - (Required) The name of the template used for this instance.
* `template` - (Required) The name or ID of the template used for this instance.
Changing this forces a new resource to be created.

* `zone` - (Required) The name of the zone where this instance will be created.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,16 @@ The following arguments are supported:
* `cidr` - (Required) The CIDR block for the network. Changing this forces a new
resource to be created.

* `network_offering` - (Required) The name of the network offering to use for
this network.
* `network_offering` - (Required) The name or ID of the network offering to use
for this network.

* `vpc` - (Optional) The name of the VPC to create this network for. Changing
this forces a new resource to be created.

* `aclid` - (Optional) The ID of a network ACL that should be attached to the
network. Changing this forces a new resource to be created.

* `zone` - (Required) The name of the zone where this disk volume will be
* `zone` - (Required) The name or ID of the zone where this disk volume will be
available. Changing this forces a new resource to be created.

## Attributes Reference
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ The following arguments are supported:
to be created.
* `description` - (Optional) The description of the ACL. Changing this forces a
new resource to be created.
* `vpc` - (Required) The name of the VPC to create this ACL for. Changing this
forces a new resource to be created.
* `vpc` - (Required) The name or ID of the VPC to create this ACL for. Changing
this forces a new resource to be created.

## Attributes Reference

Expand Down
6 changes: 3 additions & 3 deletions website/source/docs/providers/cloudstack/r/nic.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ resource "cloudstack_nic" "test" {

The following arguments are supported:

* `network` - (Required) The name of the network to plug the NIC into. Changing
* `network` - (Required) The name or ID of the network to plug the NIC into. Changing
this forces a new resource to be created.

* `ipaddress` - (Optional) The IP address to assign to the NIC. Changing this
forces a new resource to be created.

* `virtual_machine` - (Required) The name of the virtual machine to which to
attach the NIC. Changing this forces a new resource to be created.
* `virtual_machine` - (Required) The name or ID of the virtual machine to which
to attach the NIC. Changing this forces a new resource to be created.

## Attributes Reference

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ The following arguments are supported:
* `url` - (Required) The URL of where the template is hosted. Changing this
forces a new resource to be created.

* `zone` - (Required) The name of the zone where this template will be created.
* `zone` - (Required) The name or ID of the zone where this template will be created.
Changing this forces a new resource to be created.

* `is_dynamically_scalable` - (Optional) Set to indicate if the template contains
Expand Down
4 changes: 2 additions & 2 deletions website/source/docs/providers/cloudstack/r/vpc.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ The following arguments are supported:
* `cidr` - (Required) The CIDR block for the VPC. Changing this forces a new
resource to be created.

* `vpc_offering` - (Required) The name of the VPC offering to use for this VPC.
* `vpc_offering` - (Required) The name or ID of the VPC offering to use for this VPC.
Changing this forces a new resource to be created.

* `zone` - (Required) The name of the zone where this disk volume will be
* `zone` - (Required) The name or ID of the zone where this disk volume will be
available. Changing this forces a new resource to be created.

## Attributes Reference
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ resource "cloudstack_vpn_gateway" "default" {

The following arguments are supported:

* `vpc` - (Required) The name of the VPC for which to create the VPN Gateway.
* `vpc` - (Required) The name or ID of the VPC for which to create the VPN Gateway.
Changing this forces a new resource to be created.

## Attributes Reference
Expand Down

0 comments on commit 1411d40

Please sign in to comment.