github.com/paulmey/terraform@v0.5.2-0.20150519145237-046e9b4c884d/builtin/providers/cloudstack/resources.go (about)

     1  package cloudstack
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"regexp"
     7  
     8  	"github.com/hashicorp/terraform/helper/schema"
     9  	"github.com/xanzy/go-cloudstack/cloudstack"
    10  )
    11  
    12  type retrieveError struct {
    13  	name  string
    14  	value string
    15  	err   error
    16  }
    17  
    18  func (e *retrieveError) Error() error {
    19  	return fmt.Errorf("Error retrieving UUID of %s %s: %s", e.name, e.value, e.err)
    20  }
    21  
    22  func setValueOrUUID(d *schema.ResourceData, key string, value string, uuid string) {
    23  	if isUUID(d.Get(key).(string)) {
    24  		d.Set(key, uuid)
    25  	} else {
    26  		d.Set(key, value)
    27  	}
    28  }
    29  
    30  func retrieveUUID(cs *cloudstack.CloudStackClient, name, value string) (uuid string, e *retrieveError) {
    31  	// If the supplied value isn't a UUID, try to retrieve the UUID ourselves
    32  	if isUUID(value) {
    33  		return value, nil
    34  	}
    35  
    36  	log.Printf("[DEBUG] Retrieving UUID of %s: %s", name, value)
    37  
    38  	var err error
    39  	switch name {
    40  	case "disk_offering":
    41  		uuid, err = cs.DiskOffering.GetDiskOfferingID(value)
    42  	case "virtual_machine":
    43  		uuid, err = cs.VirtualMachine.GetVirtualMachineID(value)
    44  	case "service_offering":
    45  		uuid, err = cs.ServiceOffering.GetServiceOfferingID(value)
    46  	case "network_offering":
    47  		uuid, err = cs.NetworkOffering.GetNetworkOfferingID(value)
    48  	case "vpc_offering":
    49  		uuid, err = cs.VPC.GetVPCOfferingID(value)
    50  	case "vpc":
    51  		uuid, err = cs.VPC.GetVPCID(value)
    52  	case "network":
    53  		uuid, err = cs.Network.GetNetworkID(value)
    54  	case "zone":
    55  		uuid, err = cs.Zone.GetZoneID(value)
    56  	case "ipaddress":
    57  		p := cs.Address.NewListPublicIpAddressesParams()
    58  		p.SetIpaddress(value)
    59  		l, e := cs.Address.ListPublicIpAddresses(p)
    60  		if e != nil {
    61  			err = e
    62  			break
    63  		}
    64  		if l.Count == 1 {
    65  			uuid = l.PublicIpAddresses[0].Id
    66  			break
    67  		}
    68  		err = fmt.Errorf("Could not find UUID of IP address: %s", value)
    69  	case "os_type":
    70  		p := cs.GuestOS.NewListOsTypesParams()
    71  		p.SetDescription(value)
    72  		l, e := cs.GuestOS.ListOsTypes(p)
    73  		if e != nil {
    74  			err = e
    75  			break
    76  		}
    77  		if l.Count == 1 {
    78  			uuid = l.OsTypes[0].Id
    79  			break
    80  		}
    81  		err = fmt.Errorf("Could not find UUID of OS Type: %s", value)
    82  	default:
    83  		return uuid, &retrieveError{name: name, value: value,
    84  			err: fmt.Errorf("Unknown request: %s", name)}
    85  	}
    86  
    87  	if err != nil {
    88  		return uuid, &retrieveError{name: name, value: value, err: err}
    89  	}
    90  
    91  	return uuid, nil
    92  }
    93  
    94  func retrieveTemplateUUID(cs *cloudstack.CloudStackClient, zoneid, value string) (uuid string, e *retrieveError) {
    95  	// If the supplied value isn't a UUID, try to retrieve the UUID ourselves
    96  	if isUUID(value) {
    97  		return value, nil
    98  	}
    99  
   100  	log.Printf("[DEBUG] Retrieving UUID of template: %s", value)
   101  
   102  	uuid, err := cs.Template.GetTemplateID(value, "executable", zoneid)
   103  	if err != nil {
   104  		return uuid, &retrieveError{name: "template", value: value, err: err}
   105  	}
   106  
   107  	return uuid, nil
   108  }
   109  
   110  func isUUID(s string) bool {
   111  	re := regexp.MustCompile(`^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$`)
   112  	return re.MatchString(s)
   113  }