github.com/hobbeswalsh/terraform@v0.3.7-0.20150619183303-ad17cf55a0fa/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 case "project": 83 uuid, err = cs.Project.GetProjectID(value) 84 default: 85 return uuid, &retrieveError{name: name, value: value, 86 err: fmt.Errorf("Unknown request: %s", name)} 87 } 88 89 if err != nil { 90 return uuid, &retrieveError{name: name, value: value, err: err} 91 } 92 93 return uuid, nil 94 } 95 96 func retrieveTemplateUUID(cs *cloudstack.CloudStackClient, zoneid, value string) (uuid string, e *retrieveError) { 97 // If the supplied value isn't a UUID, try to retrieve the UUID ourselves 98 if isUUID(value) { 99 return value, nil 100 } 101 102 log.Printf("[DEBUG] Retrieving UUID of template: %s", value) 103 104 uuid, err := cs.Template.GetTemplateID(value, "executable", zoneid) 105 if err != nil { 106 return uuid, &retrieveError{name: "template", value: value, err: err} 107 } 108 109 return uuid, nil 110 } 111 112 func isUUID(s string) bool { 113 re := regexp.MustCompile(`^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$`) 114 return re.MatchString(s) 115 }