forked from hashicorp/terraform
-
Notifications
You must be signed in to change notification settings - Fork 2
/
resources.go
137 lines (117 loc) · 3.27 KB
/
resources.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package cloudstack
import (
"fmt"
"log"
"regexp"
"time"
"github.com/hashicorp/terraform/helper/schema"
"github.com/xanzy/go-cloudstack/cloudstack"
)
type retrieveError struct {
name string
value string
err error
}
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) {
return value, nil
}
log.Printf("[DEBUG] Retrieving UUID of %s: %s", name, value)
var err error
switch name {
case "disk_offering":
uuid, err = cs.DiskOffering.GetDiskOfferingID(value)
case "virtual_machine":
uuid, err = cs.VirtualMachine.GetVirtualMachineID(value)
case "service_offering":
uuid, err = cs.ServiceOffering.GetServiceOfferingID(value)
case "network_offering":
uuid, err = cs.NetworkOffering.GetNetworkOfferingID(value)
case "vpc_offering":
uuid, err = cs.VPC.GetVPCOfferingID(value)
case "vpc":
uuid, err = cs.VPC.GetVPCID(value)
case "network":
uuid, err = cs.Network.GetNetworkID(value)
case "zone":
uuid, err = cs.Zone.GetZoneID(value)
case "ipaddress":
p := cs.Address.NewListPublicIpAddressesParams()
p.SetIpaddress(value)
l, e := cs.Address.ListPublicIpAddresses(p)
if e != nil {
err = e
break
}
if l.Count == 1 {
uuid = l.PublicIpAddresses[0].Id
break
}
err = fmt.Errorf("Could not find UUID of IP address: %s", value)
case "os_type":
p := cs.GuestOS.NewListOsTypesParams()
p.SetDescription(value)
l, e := cs.GuestOS.ListOsTypes(p)
if e != nil {
err = e
break
}
if l.Count == 1 {
uuid = l.OsTypes[0].Id
break
}
err = fmt.Errorf("Could not find UUID of OS Type: %s", value)
case "project":
uuid, err = cs.Project.GetProjectID(value)
default:
return uuid, &retrieveError{name: name, value: value,
err: fmt.Errorf("Unknown request: %s", name)}
}
if err != nil {
return uuid, &retrieveError{name: name, value: value, err: err}
}
return uuid, nil
}
func retrieveTemplateUUID(cs *cloudstack.CloudStackClient, zoneid, value string) (uuid string, e *retrieveError) {
// If the supplied value isn't a UUID, try to retrieve the UUID ourselves
if isUUID(value) {
return value, nil
}
log.Printf("[DEBUG] Retrieving UUID of template: %s", value)
uuid, err := cs.Template.GetTemplateID(value, "executable", zoneid)
if err != nil {
return uuid, &retrieveError{name: "template", value: value, err: err}
}
return uuid, nil
}
func isUUID(s 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}$`)
return re.MatchString(s)
}
// RetryFunc is the function retried n times
type RetryFunc func() (interface{}, error)
// Retry is a wrapper around a RetryFunc that will retry a function
// n times or until it succeeds.
func Retry(n int, f RetryFunc) (interface{}, error) {
var lastErr error
for i := 0; i < n; i++ {
r, err := f()
if err == nil {
return r, nil
}
lastErr = err
time.Sleep(30 * time.Second)
}
return nil, lastErr
}