Use the new functionality offered by the `go-cloudstack` package

The updated functionality offers a new means to work with projects.
This commit uses that new functionality for the `cloudstack_network`
resource.
This commit is contained in:
Sander van Harmelen 2016-04-21 17:31:53 +02:00
parent cbea101ecf
commit 5a65cc9cb6
8 changed files with 117 additions and 50 deletions

View File

@ -141,7 +141,10 @@ func resourceCloudStackDiskRead(d *schema.ResourceData, meta interface{}) error
cs := meta.(*cloudstack.CloudStackClient)
// Get the volume details
v, count, err := cs.Volume.GetVolumeByID(d.Id())
v, count, err := cs.Volume.GetVolumeByID(
d.Id(),
cloudstack.WithProject(d.Get("project").(string)),
)
if err != nil {
if count == 0 {
d.SetId("")
@ -152,7 +155,7 @@ 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("attach", v.Attached != "") // If attached this contains a timestamp when attached
d.Set("size", int(v.Size/(1024*1024*1024))) // Needed to get GB's again
setValueOrID(d, "disk_offering", v.Diskofferingname, v.Diskofferingid)
@ -161,7 +164,10 @@ func resourceCloudStackDiskRead(d *schema.ResourceData, meta interface{}) error
if v.Attached != "" {
// Get the virtual machine details
vm, _, err := cs.VirtualMachine.GetVirtualMachineByID(v.Virtualmachineid)
vm, _, err := cs.VirtualMachine.GetVirtualMachineByID(
v.Virtualmachineid,
cloudstack.WithProject(d.Get("project").(string)),
)
if err != nil {
return err
}
@ -290,12 +296,17 @@ func resourceCloudStackDiskAttach(d *schema.ResourceData, meta interface{}) erro
cs := meta.(*cloudstack.CloudStackClient)
// First check if the disk isn't already attached
if attached, err := isAttached(cs, d.Id()); err != nil || attached {
if attached, err := isAttached(d, meta); err != nil || attached {
return err
}
// Retrieve the virtual_machine ID
virtualmachineid, e := retrieveID(cs, "virtual_machine", d.Get("virtual_machine").(string))
virtualmachineid, e := retrieveID(
cs,
"virtual_machine",
d.Get("virtual_machine").(string),
cloudstack.WithProject(d.Get("project").(string)),
)
if e != nil {
return e.Error()
}
@ -329,7 +340,7 @@ func resourceCloudStackDiskDetach(d *schema.ResourceData, meta interface{}) erro
cs := meta.(*cloudstack.CloudStackClient)
// Check if the volume is actually attached, before detaching
if attached, err := isAttached(cs, d.Id()); err != nil || !attached {
if attached, err := isAttached(d, meta); err != nil || !attached {
return err
}
@ -342,7 +353,12 @@ func resourceCloudStackDiskDetach(d *schema.ResourceData, meta interface{}) erro
// Detach the currently attached volume
if _, err := cs.Volume.DetachVolume(p); err != nil {
// Retrieve the virtual_machine ID
virtualmachineid, e := retrieveID(cs, "virtual_machine", d.Get("virtual_machine").(string))
virtualmachineid, e := retrieveID(
cs,
"virtual_machine",
d.Get("virtual_machine").(string),
cloudstack.WithProject(d.Get("project").(string)),
)
if e != nil {
return e.Error()
}
@ -372,9 +388,14 @@ func resourceCloudStackDiskDetach(d *schema.ResourceData, meta interface{}) erro
return nil
}
func isAttached(cs *cloudstack.CloudStackClient, id string) (bool, error) {
func isAttached(d *schema.ResourceData, meta interface{}) (bool, error) {
cs := meta.(*cloudstack.CloudStackClient)
// Get the volume details
v, _, err := cs.Volume.GetVolumeByID(id)
v, _, err := cs.Volume.GetVolumeByID(
d.Id(),
cloudstack.WithProject(d.Get("project").(string)),
)
if err != nil {
return false, err
}

View File

@ -174,7 +174,12 @@ func resourceCloudStackInstanceCreate(d *schema.ResourceData, meta interface{})
}
// Retrieve the network ID
networkid, e := retrieveID(cs, "network", network.(string))
networkid, e := retrieveID(
cs,
"network",
network.(string),
cloudstack.WithProject(d.Get("project").(string)),
)
if e != nil {
return e.Error()
}
@ -250,7 +255,10 @@ func resourceCloudStackInstanceRead(d *schema.ResourceData, meta interface{}) er
cs := meta.(*cloudstack.CloudStackClient)
// Get the virtual machine details
vm, count, err := cs.VirtualMachine.GetVirtualMachineByID(d.Id())
vm, count, err := cs.VirtualMachine.GetVirtualMachineByID(
d.Id(),
cloudstack.WithProject(d.Get("project").(string)),
)
if err != nil {
if count == 0 {
log.Printf("[DEBUG] Instance %s does no longer exist", d.Get("name").(string))

View File

@ -74,7 +74,12 @@ func resourceCloudStackIPAddressCreate(d *schema.ResourceData, meta interface{})
}
if ok {
// Retrieve the network ID
networkid, e := retrieveID(cs, "network", network.(string))
networkid, e := retrieveID(
cs,
"network",
network.(string),
cloudstack.WithProject(d.Get("project").(string)),
)
if e != nil {
return e.Error()
}
@ -89,7 +94,12 @@ func resourceCloudStackIPAddressCreate(d *schema.ResourceData, meta interface{})
}
if ok {
// Retrieve the vpc ID
vpcid, e := retrieveID(cs, "vpc", vpc.(string))
vpcid, e := retrieveID(
cs,
"vpc",
vpc.(string),
cloudstack.WithProject(d.Get("project").(string)),
)
if e != nil {
return e.Error()
}
@ -118,7 +128,10 @@ func resourceCloudStackIPAddressRead(d *schema.ResourceData, meta interface{}) e
cs := meta.(*cloudstack.CloudStackClient)
// Get the IP address details
ip, count, err := cs.Address.GetPublicIpAddressByID(d.Id())
ip, count, err := cs.Address.GetPublicIpAddressByID(
d.Id(),
cloudstack.WithProject(d.Get("project").(string)),
)
if err != nil {
if count == 0 {
log.Printf(

View File

@ -161,7 +161,12 @@ func resourceCloudStackNetworkCreate(d *schema.ResourceData, meta interface{}) e
}
if ok {
// Retrieve the vpc ID
vpcid, e := retrieveID(cs, "vpc", vpc.(string))
vpcid, e := retrieveID(
cs,
"vpc",
vpc.(string),
cloudstack.WithProject(d.Get("project").(string)),
)
if e != nil {
return e.Error()
}
@ -205,7 +210,10 @@ func resourceCloudStackNetworkRead(d *schema.ResourceData, meta interface{}) err
cs := meta.(*cloudstack.CloudStackClient)
// Get the virtual machine details
n, count, err := cs.Network.GetNetworkByID(d.Id())
n, count, err := cs.Network.GetNetworkByID(
d.Id(),
cloudstack.WithProject(d.Get("project").(string)),
)
if err != nil {
if count == 0 {
log.Printf(

View File

@ -103,7 +103,12 @@ func resourceCloudStackPortForwardCreate(d *schema.ResourceData, meta interface{
}
// Retrieve the ipaddress ID
ipaddressid, e := retrieveID(cs, "ip_address", ipaddress.(string))
ipaddressid, e := retrieveID(
cs,
"ip_address",
ipaddress.(string),
cloudstack.WithProject(d.Get("project").(string)),
)
if e != nil {
return e.Error()
}
@ -189,12 +194,20 @@ func createPortForward(
}
// Retrieve the virtual_machine ID
virtualmachineid, e := retrieveID(cs, "virtual_machine", virtualmachine.(string))
virtualmachineid, e := retrieveID(
cs,
"virtual_machine",
virtualmachine.(string),
cloudstack.WithProject(d.Get("project").(string)),
)
if e != nil {
return e.Error()
}
vm, _, err := cs.VirtualMachine.GetVirtualMachineByID(virtualmachineid)
vm, _, err := cs.VirtualMachine.GetVirtualMachineByID(
virtualmachineid,
cloudstack.WithProject(d.Get("project").(string)),
)
if err != nil {
return err
}
@ -326,9 +339,9 @@ func resourceCloudStackPortForwardUpdate(d *schema.ResourceData, meta interface{
// set to make sure we end up in a consistent state
forwards := o.(*schema.Set).Intersection(n.(*schema.Set))
// First loop through all the new forwards and create (before destroy) them
if nrs.Len() > 0 {
err := createPortForwards(d, meta, forwards, nrs)
// First loop through all the old forwards and delete them
if ors.Len() > 0 {
err := deletePortForwards(d, meta, forwards, ors)
// We need to update this first to preserve the correct state
d.Set("forward", forwards)
@ -338,9 +351,9 @@ func resourceCloudStackPortForwardUpdate(d *schema.ResourceData, meta interface{
}
}
// Then loop through all the old forwards and delete them
if ors.Len() > 0 {
err := deletePortForwards(d, meta, forwards, ors)
// Then loop through all the new forwards and create them
if nrs.Len() > 0 {
err := createPortForwards(d, meta, forwards, nrs)
// We need to update this first to preserve the correct state
d.Set("forward", forwards)

View File

@ -207,7 +207,11 @@ func resourceCloudStackTemplateRead(d *schema.ResourceData, meta interface{}) er
cs := meta.(*cloudstack.CloudStackClient)
// Get the template details
t, count, err := cs.Template.GetTemplateByID(d.Id(), "executable")
t, count, err := cs.Template.GetTemplateByID(
d.Id(),
"executable",
cloudstack.WithProject(d.Get("project").(string)),
)
if err != nil {
if count == 0 {
log.Printf(

View File

@ -125,7 +125,10 @@ func resourceCloudStackVPCRead(d *schema.ResourceData, meta interface{}) error {
cs := meta.(*cloudstack.CloudStackClient)
// Get the VPC details
v, count, err := cs.VPC.GetVPCByID(d.Id())
v, count, err := cs.VPC.GetVPCByID(
d.Id(),
cloudstack.WithProject(d.Get("project").(string)),
)
if err != nil {
if count == 0 {
log.Printf(

View File

@ -11,9 +11,6 @@ import (
"github.com/xanzy/go-cloudstack/cloudstack"
)
// UnlimitedResourceID is a "special" ID to define an unlimited resource
const UnlimitedResourceID = "-1"
// Define a regexp for parsing the port
var splitPorts = regexp.MustCompile(`^(\d+)(?:-(\d+))?$`)
@ -28,11 +25,11 @@ func (e *retrieveError) Error() error {
}
func setValueOrID(d *schema.ResourceData, key string, value string, id string) {
if isID(d.Get(key).(string)) {
if cloudstack.IsID(d.Get(key).(string)) {
// If the given id is an empty string, check if the configured value matches
// the UnlimitedResourceID in which case we set id to UnlimitedResourceID
if id == "" && d.Get(key).(string) == UnlimitedResourceID {
id = UnlimitedResourceID
if id == "" && d.Get(key).(string) == cloudstack.UnlimitedResourceID {
id = cloudstack.UnlimitedResourceID
}
d.Set(key, id)
@ -41,9 +38,13 @@ func setValueOrID(d *schema.ResourceData, key string, value string, id string) {
}
}
func retrieveID(cs *cloudstack.CloudStackClient, name, value string) (id string, e *retrieveError) {
func retrieveID(
cs *cloudstack.CloudStackClient,
name string,
value string,
opts ...cloudstack.OptionFunc) (id string, e *retrieveError) {
// If the supplied value isn't a ID, try to retrieve the ID ourselves
if isID(value) {
if cloudstack.IsID(value) {
return value, nil
}
@ -54,7 +55,7 @@ func retrieveID(cs *cloudstack.CloudStackClient, name, value string) (id string,
case "disk_offering":
id, err = cs.DiskOffering.GetDiskOfferingID(value)
case "virtual_machine":
id, err = cs.VirtualMachine.GetVirtualMachineID(value)
id, err = cs.VirtualMachine.GetVirtualMachineID(value, opts...)
case "service_offering":
id, err = cs.ServiceOffering.GetServiceOfferingID(value)
case "network_offering":
@ -64,14 +65,20 @@ func retrieveID(cs *cloudstack.CloudStackClient, name, value string) (id string,
case "vpc_offering":
id, err = cs.VPC.GetVPCOfferingID(value)
case "vpc":
id, err = cs.VPC.GetVPCID(value)
id, err = cs.VPC.GetVPCID(value, opts...)
case "network":
id, err = cs.Network.GetNetworkID(value)
id, err = cs.Network.GetNetworkID(value, opts...)
case "zone":
id, err = cs.Zone.GetZoneID(value)
case "ip_address":
p := cs.Address.NewListPublicIpAddressesParams()
p.SetIpaddress(value)
for _, fn := range opts {
if e := fn(cs, p); e != nil {
err = e
break
}
}
l, e := cs.Address.ListPublicIpAddresses(p)
if e != nil {
err = e
@ -109,7 +116,7 @@ func retrieveID(cs *cloudstack.CloudStackClient, name, value string) (id string,
func retrieveTemplateID(cs *cloudstack.CloudStackClient, zoneid, value string) (id string, e *retrieveError) {
// If the supplied value isn't a ID, try to retrieve the ID ourselves
if isID(value) {
if cloudstack.IsID(value) {
return value, nil
}
@ -123,12 +130,6 @@ func retrieveTemplateID(cs *cloudstack.CloudStackClient, zoneid, value string) (
return id, nil
}
// ID can be either a UUID or a UnlimitedResourceID
func isID(id string) bool {
re := regexp.MustCompile(`^([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}|-1)$`)
return re.MatchString(id)
}
// RetryFunc is the function retried n times
type RetryFunc func() (interface{}, error)
@ -183,12 +184,8 @@ func setCidrList(rule map[string]interface{}, cidrList string) {
rule["cidr_list"] = cidrs
}
type projectidSetter interface {
SetProjectid(string)
}
// If there is a project supplied, we retrieve and set the project id
func setProjectid(p projectidSetter, cs *cloudstack.CloudStackClient, d *schema.ResourceData) error {
func setProjectid(p cloudstack.ProjectIDSetter, cs *cloudstack.CloudStackClient, d *schema.ResourceData) error {
if project, ok := d.GetOk("project"); ok {
projectid, e := retrieveID(cs, "project", project.(string))
if e != nil {