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