-
Notifications
You must be signed in to change notification settings - Fork 7
/
resource_compute_profile.go
362 lines (317 loc) · 7.95 KB
/
resource_compute_profile.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
package main
import (
"bytes"
"context"
"encoding/json"
"errors"
"time"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
hcx "github.com/adeleporte/terraform-provider-hcx/hcx"
)
func resourceComputeProfile() *schema.Resource {
return &schema.Resource{
CreateContext: resourceComputeProfileCreate,
ReadContext: resourceComputeProfileRead,
UpdateContext: resourceComputeProfileUpdate,
DeleteContext: resourceComputeProfileDelete,
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
},
"datacenter": {
Type: schema.TypeString,
Required: true,
},
"cluster": {
Type: schema.TypeString,
Required: true,
},
"datastore": {
Type: schema.TypeString,
Optional: true,
Default: "",
},
"management_network": {
Type: schema.TypeString,
Required: true,
},
"replication_network": {
Type: schema.TypeString,
Optional: true,
Default: "",
},
"uplink_network": {
Type: schema.TypeString,
Optional: true,
Default: "",
},
"vmotion_network": {
Type: schema.TypeString,
Optional: true,
Default: "",
},
"dvs": {
Type: schema.TypeString,
Required: true,
},
"service": {
Type: schema.TypeList,
Description: "Rules description",
Required: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
},
},
},
},
},
}
}
func resourceComputeProfileCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
client := m.(*hcx.Client)
name := d.Get("name").(string)
cluster := d.Get("cluster").(string)
res, err := hcx.GetVcInventory(client)
if err != nil {
return diag.FromErr(err)
}
// Get Cluster info
var cluster_id string
var cluster_name string
found := false
for _, j := range res.Children[0].Children {
if j.Name == cluster {
cluster_id = j.Entity_id
cluster_name = j.Name
found = true
}
}
if !found {
return diag.FromErr(errors.New("cluster not found"))
}
// Get Datastore info
datastore := d.Get("datastore").(string)
datastore_from_api, err := hcx.GetVcDatastore(client, datastore, res.Entity_id, cluster_id)
if err != nil {
return diag.FromErr(err)
}
// Get DVS info
dvs := d.Get("dvs").(string)
dvs_from_api, err := hcx.GetVcDvs(client, dvs, res.Entity_id, cluster_id)
if err != nil {
return diag.FromErr(err)
}
// Get Services from schema
services := d.Get("service").([]interface{})
services_from_schema := []hcx.Service{}
for _, j := range services {
s := j.(map[string]interface{})
name := s["name"].(string)
s_tmp := hcx.Service{
Name: name,
}
services_from_schema = append(services_from_schema, s_tmp)
}
// Create network list with tags
management_network := d.Get("management_network").(string)
replication_network := d.Get("replication_network").(string)
uplink_network := d.Get("uplink_network").(string)
vmotion_network := d.Get("vmotion_network").(string)
networks_list := []hcx.Network{}
np, err := hcx.GetNetworkProfileById(client, management_network)
if err != nil {
return diag.FromErr(err)
}
management_network_name := np.Name
management_network_id := np.ObjectId
np, err = hcx.GetNetworkProfileById(client, replication_network)
if err != nil {
return diag.FromErr(err)
}
replication_network_name := np.Name
replication_network_id := np.ObjectId
np, err = hcx.GetNetworkProfileById(client, uplink_network)
if err != nil {
return diag.FromErr(err)
}
uplink_network_name := np.Name
uplink_network_id := np.ObjectId
np, err = hcx.GetNetworkProfileById(client, vmotion_network)
if err != nil {
return diag.FromErr(err)
}
vmotion_network_name := np.Name
vmotion_network_id := np.ObjectId
net_tmp := hcx.Network{
Name: management_network_name,
ID: management_network_id,
Tags: []string{"management"},
Status: hcx.Status{
State: "REALIZED",
},
}
networks_list = append(networks_list, net_tmp)
found = false
index := 0
for i, j := range networks_list {
if j.Name == replication_network_name {
found = true
index = i
break
}
}
if found {
networks_list[index].Tags = append(networks_list[index].Tags, "replication")
} else {
net_tmp := hcx.Network{
Name: replication_network_name,
ID: replication_network_id,
Tags: []string{"replication"},
Status: hcx.Status{
State: "REALIZED",
},
}
networks_list = append(networks_list, net_tmp)
}
found = false
index = 0
for i, j := range networks_list {
if j.Name == uplink_network_name {
found = true
index = i
break
}
}
if found {
networks_list[index].Tags = append(networks_list[index].Tags, "uplink")
} else {
net_tmp := hcx.Network{
Name: uplink_network_name,
ID: uplink_network_id,
Tags: []string{"uplink"},
Status: hcx.Status{
State: "REALIZED",
},
}
networks_list = append(networks_list, net_tmp)
}
found = false
index = 0
for i, j := range networks_list {
if j.Name == vmotion_network_name {
found = true
index = i
break
}
}
if found {
networks_list[index].Tags = append(networks_list[index].Tags, "vmotion")
} else {
net_tmp := hcx.Network{
Name: vmotion_network_name,
ID: vmotion_network_id,
Tags: []string{"vmotion"},
Status: hcx.Status{
State: "REALIZED",
},
}
networks_list = append(networks_list, net_tmp)
}
body := hcx.InsertComputeProfileBody{
Name: name,
Services: services_from_schema,
Computes: []hcx.Compute{{
CmpId: res.Entity_id,
CmpType: "VC",
CmpName: res.Name,
ID: res.Children[0].Entity_id,
Name: res.Children[0].Name,
Type: res.Children[0].EntityType,
}},
DeploymentContainers: hcx.DeploymentContainer{
Computes: []hcx.Compute{{
CmpId: res.Entity_id,
CmpType: "VC",
CmpName: res.Name,
ID: cluster_id,
Name: cluster_name,
Type: "ClusterComputeResource",
},
},
Storage: []hcx.Storage{{
CmpId: res.Entity_id,
CmpType: "VC",
CmpName: res.Name,
ID: datastore_from_api.ID,
Name: datastore_from_api.Name,
Type: datastore_from_api.EntityType,
}},
},
Networks: networks_list,
Switches: []hcx.Switch{{
CmpID: res.Entity_id,
MaxMTU: dvs_from_api.MaxMtu,
ID: dvs_from_api.ID,
Name: dvs_from_api.Name,
Type: dvs_from_api.Type,
}},
}
buf := new(bytes.Buffer)
json.NewEncoder(buf).Encode(body)
res2, err := hcx.InsertComputeProfile(client, body)
if err != nil {
//return diag.FromErr(errors.New(fmt.Sprintf("%s", buf)))
return diag.FromErr(err)
}
// Wait for task completion
for {
jr, err := hcx.GetTaskResult(client, res2.Data.InterconnectTaskId)
if err != nil {
return diag.FromErr(err)
}
if jr.Status == "SUCCESS" {
break
}
if jr.Status == "FAILED" {
return diag.FromErr(errors.New("Task Failed"))
}
time.Sleep(5 * time.Second)
}
d.SetId(res2.Data.ComputeProfileId)
return resourceComputeProfileRead(ctx, d, m)
}
func resourceComputeProfileRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
var diags diag.Diagnostics
return diags
}
func resourceComputeProfileUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
return resourceComputeProfileRead(ctx, d, m)
}
func resourceComputeProfileDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
var diags diag.Diagnostics
client := m.(*hcx.Client)
res, err := hcx.DeleteComputeProfile(client, d.Id())
if err != nil {
return diag.FromErr(err)
}
// Wait for task completion
for {
jr, err := hcx.GetTaskResult(client, res.Data.InterconnectTaskId)
if err != nil {
return diag.FromErr(err)
}
if jr.Status == "SUCCESS" {
break
}
if jr.Status == "FAILED" {
return diag.FromErr(errors.New("Task Failed"))
}
time.Sleep(5 * time.Second)
}
return diags
}