forked from hashicorp/terraform
/
resource_profitbricks_datacenter.go
180 lines (153 loc) · 4.68 KB
/
resource_profitbricks_datacenter.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package profitbricks
import (
"fmt"
"github.com/hashicorp/terraform/helper/schema"
"github.com/profitbricks/profitbricks-sdk-go"
"log"
"regexp"
"runtime"
"strings"
"time"
)
func resourceProfitBricksDatacenter() *schema.Resource {
return &schema.Resource{
Create: resourceProfitBricksDatacenterCreate,
Read: resourceProfitBricksDatacenterRead,
Update: resourceProfitBricksDatacenterUpdate,
Delete: resourceProfitBricksDatacenterDelete,
Schema: map[string]*schema.Schema{
//Datacenter parameters
"name": {
Type: schema.TypeString,
Required: true,
},
"location": {
Type: schema.TypeString,
Required: true,
},
"description": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
},
}
}
func resourceProfitBricksDatacenterCreate(d *schema.ResourceData, meta interface{}) error {
datacenter := profitbricks.Datacenter{
Properties: profitbricks.DatacenterProperties{
Name: d.Get("name").(string),
Location: d.Get("location").(string),
},
}
if attr, ok := d.GetOk("description"); ok {
datacenter.Properties.Description = attr.(string)
}
dc := profitbricks.CreateDatacenter(datacenter)
if dc.StatusCode > 299 {
return fmt.Errorf(
"Error creating data center (%s) (%s)", d.Id(), dc.Response)
}
d.SetId(dc.Id)
log.Printf("[INFO] DataCenter Id: %s", d.Id())
err := waitTillProvisioned(meta, dc.Headers.Get("Location"))
if err != nil {
return err
}
return resourceProfitBricksDatacenterRead(d, meta)
}
func resourceProfitBricksDatacenterRead(d *schema.ResourceData, meta interface{}) error {
datacenter := profitbricks.GetDatacenter(d.Id())
if datacenter.StatusCode > 299 {
return fmt.Errorf("Error while fetching a data center ID %s %s", d.Id(), datacenter.Response)
}
d.Set("name", datacenter.Properties.Name)
d.Set("location", datacenter.Properties.Location)
d.Set("description", datacenter.Properties.Description)
return nil
}
func resourceProfitBricksDatacenterUpdate(d *schema.ResourceData, meta interface{}) error {
obj := profitbricks.DatacenterProperties{}
if d.HasChange("name") {
_, newName := d.GetChange("name")
obj.Name = newName.(string)
}
if d.HasChange("description") {
_, newDescription := d.GetChange("description")
obj.Description = newDescription.(string)
}
resp := profitbricks.PatchDatacenter(d.Id(), obj)
waitTillProvisioned(meta, resp.Headers.Get("Location"))
return resourceProfitBricksDatacenterRead(d, meta)
}
func resourceProfitBricksDatacenterDelete(d *schema.ResourceData, meta interface{}) error {
dcid := d.Id()
resp := profitbricks.DeleteDatacenter(dcid)
if resp.StatusCode > 299 {
return fmt.Errorf("An error occured while deleting the data center ID %s %s", d.Id(), string(resp.Body))
}
err := waitTillProvisioned(meta, resp.Headers.Get("Location"))
if err != nil {
return err
}
d.SetId("")
return nil
}
func waitTillProvisioned(meta interface{}, path string) error {
config := meta.(*Config)
waitCount := 50
if config.Retries != 0 {
waitCount = config.Retries
}
for i := 0; i < waitCount; i++ {
request := profitbricks.GetRequestStatus(path)
pc, _, _, ok := runtime.Caller(1)
details := runtime.FuncForPC(pc)
if ok && details != nil {
log.Printf("[DEBUG] Called from %s", details.Name())
}
log.Printf("[DEBUG] Request status: %s", request.Metadata.Status)
log.Printf("[DEBUG] Request status path: %s", path)
if request.Metadata.Status == "DONE" {
return nil
}
if request.Metadata.Status == "FAILED" {
return fmt.Errorf("Request failed with following error: %s", request.Metadata.Message)
}
time.Sleep(10 * time.Second)
i++
}
return fmt.Errorf("Timeout has expired")
}
func getImageId(dcId string, imageName string, imageType string) string {
if imageName == "" {
return ""
}
dc := profitbricks.GetDatacenter(dcId)
if dc.StatusCode > 299 {
log.Print(fmt.Errorf("Error while fetching a data center ID %s %s", dcId, dc.Response))
}
images := profitbricks.ListImages()
if images.StatusCode > 299 {
log.Print(fmt.Errorf("Error while fetching the list of images %s", images.Response))
}
if len(images.Items) > 0 {
for _, i := range images.Items {
imgName := ""
if i.Properties.Name != "" {
imgName = i.Properties.Name
}
if imageType == "SSD" {
imageType = "HDD"
}
if imgName != "" && strings.Contains(strings.ToLower(imgName), strings.ToLower(imageName)) && i.Properties.ImageType == imageType && i.Properties.Location == dc.Properties.Location && i.Properties.Public == true {
return i.Id
}
}
}
return ""
}
func IsValidUUID(uuid string) bool {
r := regexp.MustCompile("^[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}$")
return r.MatchString(uuid)
}