forked from hashicorp/terraform-provider-vsphere
/
resource_vsphere_resource_pool.go
368 lines (343 loc) · 12.7 KB
/
resource_vsphere_resource_pool.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
363
364
365
366
367
368
package vsphere
import (
"fmt"
"log"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
"github.com/terraform-providers/terraform-provider-vsphere/vsphere/internal/helper/customattribute"
"github.com/terraform-providers/terraform-provider-vsphere/vsphere/internal/helper/resourcepool"
"github.com/terraform-providers/terraform-provider-vsphere/vsphere/internal/helper/structure"
"github.com/terraform-providers/terraform-provider-vsphere/vsphere/internal/helper/viapi"
"github.com/vmware/govmomi"
"github.com/vmware/govmomi/object"
"github.com/vmware/govmomi/vim25/types"
)
const resourceVSphereResourcePoolName = "vsphere_resource_pool"
var resourcePoolCPUSharesLevelAllowedValues = []string{
string(types.SharesLevelLow),
string(types.SharesLevelNormal),
string(types.SharesLevelHigh),
string(types.SharesLevelCustom),
}
var resourcePoolMemorySharesLevelAllowedValues = []string{
string(types.SharesLevelLow),
string(types.SharesLevelNormal),
string(types.SharesLevelHigh),
string(types.SharesLevelCustom),
}
func resourceVSphereResourcePool() *schema.Resource {
s := map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
Description: "Name of resource pool.",
},
"parent_resource_pool_id": {
Type: schema.TypeString,
Description: "The ID of the root resource pool of the compute resource the resource pool is in.",
Required: true,
},
"cpu_share_level": {
Type: schema.TypeString,
Description: "The allocation level. The level is a simplified view of shares. Levels map to a pre-determined set of numeric values for shares. Can be one of low, normal, high, or custom.",
Optional: true,
ValidateFunc: validation.StringInSlice(resourcePoolCPUSharesLevelAllowedValues, false),
Default: "normal",
},
"cpu_shares": {
Type: schema.TypeInt,
Description: "The number of shares allocated. Used to determine resource allocation in case of resource contention. If this is set, cpu_share_level must be custom.",
Computed: true,
Optional: true,
},
"cpu_reservation": {
Type: schema.TypeInt,
Description: "Amount of CPU (MHz) that is guaranteed available to the resource pool.",
Optional: true,
Default: 0,
},
"cpu_expandable": {
Type: schema.TypeBool,
Description: "Determines if the reservation on a resource pool can grow beyond the specified value, if the parent resource pool has unreserved resources.",
Optional: true,
Default: true,
},
"cpu_limit": {
Type: schema.TypeInt,
Description: "The utilization of a resource pool will not exceed this limit, even if there are available resources. Set to -1 for unlimited.",
Optional: true,
Default: -1,
},
"memory_share_level": {
Type: schema.TypeString,
Description: "The allocation level. The level is a simplified view of shares. Levels map to a pre-determined set of numeric values for shares. Can be one of low, normal, high, or custom.",
Optional: true,
ValidateFunc: validation.StringInSlice(resourcePoolMemorySharesLevelAllowedValues, false),
Default: "normal",
},
"memory_shares": {
Type: schema.TypeInt,
Description: "The number of shares allocated. Used to determine resource allocation in case of resource contention. If this is set, memory_share_level must be custom.",
Computed: true,
Optional: true,
},
"memory_reservation": {
Type: schema.TypeInt,
Description: "Amount of memory (MB) that is guaranteed available to the resource pool.",
Optional: true,
Default: 0,
},
"memory_expandable": {
Type: schema.TypeBool,
Description: "Determines if the reservation on a resource pool can grow beyond the specified value, if the parent resource pool has unreserved resources.",
Optional: true,
Default: true,
},
"memory_limit": {
Type: schema.TypeInt,
Description: "The utilization of a resource pool will not exceed this limit, even if there are available resources. Set to -1 for unlimited.",
Optional: true,
Default: -1,
},
vSphereTagAttributeKey: tagsSchema(),
customattribute.ConfigKey: customattribute.ConfigSchema(),
}
return &schema.Resource{
Create: resourceVSphereResourcePoolCreate,
Read: resourceVSphereResourcePoolRead,
Update: resourceVSphereResourcePoolUpdate,
Delete: resourceVSphereResourcePoolDelete,
Importer: &schema.ResourceImporter{
State: resourceVSphereResourcePoolImport,
},
Schema: s,
}
}
func resourceVSphereResourcePoolImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
client, err := resourceVSphereResourcePoolClient(meta)
if err != nil {
return nil, err
}
rp, err := resourcepool.FromPathOrDefault(client, d.Id(), nil)
if err != nil {
return nil, err
}
d.SetId(rp.Reference().Value)
return []*schema.ResourceData{d}, nil
}
func resourceVSphereResourcePoolCreate(d *schema.ResourceData, meta interface{}) error {
log.Printf("[DEBUG] %s: Beginning create", resourceVSphereResourcePoolIDString(d))
client, err := resourceVSphereResourcePoolClient(meta)
if err != nil {
return err
}
prp, err := resourcepool.FromID(client, d.Get("parent_resource_pool_id").(string))
if err != nil {
return err
}
rpSpec := expandResourcePoolConfigSpec(d)
rp, err := resourcepool.Create(prp, d.Get("name").(string), rpSpec)
if err != nil {
return err
}
if err = resourceVSphereResourcePoolApplyTags(d, meta, rp); err != nil {
return err
}
d.SetId(rp.Reference().Value)
log.Printf("[DEBUG] %s: Create finished successfully", resourceVSphereResourcePoolIDString(d))
return resourceVSphereResourcePoolRead(d, meta)
}
func resourceVSphereResourcePoolRead(d *schema.ResourceData, meta interface{}) error {
log.Printf("[DEBUG] %s: Beginning read", resourceVSphereResourcePoolIDString(d))
client, err := resourceVSphereResourcePoolClient(meta)
if err != nil {
return err
}
rp, err := resourcepool.FromID(client, d.Id())
if err != nil {
if viapi.IsManagedObjectNotFoundError(err) {
log.Printf("[DEBUG] %s: Resource has been deleted", resourceVSphereResourcePoolIDString(d))
d.SetId("")
return nil
}
return err
}
if err = resourceVSphereResourcePoolReadTags(d, meta, rp); err != nil {
return err
}
err = d.Set("name", rp.Name())
if err != nil {
return err
}
rpProps, err := resourcepool.Properties(rp)
if err != nil {
return err
}
if err = d.Set("parent_resource_pool_id", rpProps.Parent.Value); err != nil {
return err
}
err = flattenResourcePoolConfigSpec(d, rpProps.Config)
if err != nil {
return err
}
log.Printf("[DEBUG] %s: Read finished successfully", resourceVSphereResourcePoolIDString(d))
return nil
}
func resourceVSphereResourcePoolUpdate(d *schema.ResourceData, meta interface{}) error {
log.Printf("[DEBUG] %s: Beginning update", resourceVSphereResourcePoolIDString(d))
client, err := resourceVSphereResourcePoolClient(meta)
if err != nil {
return err
}
rp, err := resourcepool.FromID(client, d.Id())
if err != nil {
return err
}
if err = resourceVSphereResourcePoolApplyTags(d, meta, rp); err != nil {
return err
}
op, np := d.GetChange("parent_resource_pool_id")
if op != np {
log.Printf("[DEBUG] %s: Parent resource pool has changed. Moving from %s, to %s", resourceVSphereResourcePoolIDString(d), op, np)
p, err := resourcepool.FromID(client, np.(string))
if err != nil {
return err
}
err = resourcepool.MoveIntoResourcePool(p, rp.Reference())
if err != nil {
return err
}
log.Printf("[DEBUG] %s: Move finished successfully", resourceVSphereResourcePoolIDString(d))
}
rpSpec := expandResourcePoolConfigSpec(d)
err = resourcepool.Update(rp, d.Get("name").(string), rpSpec)
if err != nil {
return err
}
log.Printf("[DEBUG] %s: Update finished successfully", resourceVSphereResourcePoolIDString(d))
return nil
}
func resourceVSphereResourcePoolDelete(d *schema.ResourceData, meta interface{}) error {
log.Printf("[DEBUG] %s: Beginning delete", resourceVSphereResourcePoolIDString(d))
client, err := resourceVSphereResourcePoolClient(meta)
if err != nil {
return err
}
rp, err := resourcepool.FromID(client, d.Id())
if err != nil {
return err
}
err = resourceVSphereResourcePoolValidateEmpty(rp)
if err != nil {
return err
}
err = resourcepool.Delete(rp)
if err != nil {
return err
}
log.Printf("[DEBUG] %s: Deleted successfully", resourceVSphereResourcePoolIDString(d))
return nil
}
// resourceVSphereResourcePoolIDString prints a friendly string for the
// vsphere_virtual_machine resource.
func resourceVSphereResourcePoolIDString(d structure.ResourceIDStringer) string {
return structure.ResourceIDString(d, resourceVSphereResourcePoolName)
}
func flattenResourcePoolConfigSpec(d *schema.ResourceData, obj types.ResourceConfigSpec) error {
err := flattenResourcePoolMemoryAllocation(d, obj.MemoryAllocation)
if err != nil {
return err
}
return flattenResourcePoolCPUAllocation(d, obj.CpuAllocation)
}
func flattenResourcePoolCPUAllocation(d *schema.ResourceData, obj types.ResourceAllocationInfo) error {
return structure.SetBatch(d, map[string]interface{}{
"cpu_reservation": obj.Reservation,
"cpu_expandable": obj.ExpandableReservation,
"cpu_limit": obj.Limit,
"cpu_shares": obj.Shares.Shares,
"cpu_share_level": obj.Shares.Level,
})
}
func flattenResourcePoolMemoryAllocation(d *schema.ResourceData, obj types.ResourceAllocationInfo) error {
return structure.SetBatch(d, map[string]interface{}{
"memory_reservation": obj.Reservation,
"memory_expandable": obj.ExpandableReservation,
"memory_limit": obj.Limit,
"memory_shares": obj.Shares.Shares,
"memory_share_level": obj.Shares.Level,
})
}
func expandResourcePoolConfigSpec(d *schema.ResourceData) *types.ResourceConfigSpec {
return &types.ResourceConfigSpec{
CpuAllocation: expandResourcePoolCPUAllocation(d),
MemoryAllocation: expandResourcePoolMemoryAllocation(d),
}
}
func expandResourcePoolCPUAllocation(d *schema.ResourceData) types.ResourceAllocationInfo {
return types.ResourceAllocationInfo{
Reservation: structure.GetInt64Ptr(d, "cpu_reservation"),
ExpandableReservation: structure.GetBoolPtr(d, "cpu_expandable"),
Limit: structure.GetInt64Ptr(d, "cpu_limit"),
Shares: &types.SharesInfo{
Level: types.SharesLevel(d.Get("cpu_share_level").(string)),
Shares: int32(d.Get("cpu_shares").(int)),
},
}
}
func expandResourcePoolMemoryAllocation(d *schema.ResourceData) types.ResourceAllocationInfo {
return types.ResourceAllocationInfo{
Reservation: structure.GetInt64Ptr(d, "memory_reservation"),
ExpandableReservation: structure.GetBoolPtr(d, "memory_expandable"),
Limit: structure.GetInt64Ptr(d, "memory_limit"),
Shares: &types.SharesInfo{
Shares: int32(d.Get("memory_shares").(int)),
Level: types.SharesLevel(d.Get("memory_share_level").(string)),
},
}
}
func resourceVSphereResourcePoolClient(meta interface{}) (*govmomi.Client, error) {
client := meta.(*VSphereClient).vimClient
if err := viapi.ValidateVirtualCenter(client); err != nil {
return nil, err
}
return client, nil
}
func resourceVSphereResourcePoolValidateEmpty(rp *object.ResourcePool) error {
ne, err := resourcepool.HasChildren(rp)
if err != nil {
return fmt.Errorf("error checking contents of resource pool: %s", err)
}
if ne {
return fmt.Errorf("resource pool %q still has children resources. Please move or remove all items before deleting", rp.InventoryPath)
}
return nil
}
// resourceVSphereResourcePoolApplyTags processes the tags step for both create
// and update for vsphere_resource_pool.
func resourceVSphereResourcePoolApplyTags(d *schema.ResourceData, meta interface{}, rp *object.ResourcePool) error {
tagsClient, err := tagsClientIfDefined(d, meta)
if err != nil {
return err
}
// Apply any pending tags now.
if tagsClient == nil {
log.Printf("[DEBUG] %s: Tags unsupported on this connection, skipping", resourceVSphereComputeClusterIDString(d))
return nil
}
log.Printf("[DEBUG] %s: Applying any pending tags", resourceVSphereResourcePoolIDString(d))
return processTagDiff(tagsClient, d, rp)
}
// resourceVSphereResourcePoolReadTags reads the tags for
// vsphere_resource_pool.
func resourceVSphereResourcePoolReadTags(d *schema.ResourceData, meta interface{}, rp *object.ResourcePool) error {
if tagsClient, _ := meta.(*VSphereClient).TagsClient(); tagsClient != nil {
log.Printf("[DEBUG] %s: Reading tags", resourceVSphereResourcePoolIDString(d))
if err := readTagsForResource(tagsClient, rp, d); err != nil {
return err
}
} else {
log.Printf("[DEBUG] %s: Tags unsupported on this connection, skipping tag read", resourceVSphereResourcePoolIDString(d))
}
return nil
}