/
vm_processor.go
258 lines (222 loc) · 9.91 KB
/
vm_processor.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
package api
import (
"encoding/json"
"fmt"
"log"
"text/template"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func DefaultVmProcessors() (interface{}, error) {
result := make([]vmProcessor, 0)
vmProcessor := vmProcessor{
CompatibilityForMigrationEnabled: false,
CompatibilityForOlderOperatingSystemsEnabled: false,
HwThreadCountPerCore: 0,
Maximum: 100,
Reserve: 0,
RelativeWeight: 100,
MaximumCountPerNumaNode: 0,
MaximumCountPerNumaSocket: 0,
EnableHostResourceProtection: false,
ExposeVirtualizationExtensions: false,
}
result = append(result, vmProcessor)
return result, nil
}
func DiffSuppressVmProcessorMaximumCountPerNumaNode(key, old, new string, d *schema.ResourceData) bool {
log.Printf("[DEBUG] '[%s]' Comparing old value '[%v]' with new value '[%v]' ", key, old, new)
if new == "0" {
//We have not explicitly set a value, so allow any value as we are not tracking it
return true
}
return new == old
}
func DiffSuppressVmProcessorMaximumCountPerNumaSocket(key, old, new string, d *schema.ResourceData) bool {
log.Printf("[DEBUG] '[%s]' Comparing old value '[%v]' with new value '[%v]' ", key, old, new)
if new == "0" {
//We have not explicitly set a value, so allow any value as we are not tracking it
return true
}
return new == old
}
func ExpandVmProcessors(d *schema.ResourceData) ([]vmProcessor, error) {
expandedVmProcessors := make([]vmProcessor, 0)
if v, ok := d.GetOk("vm_processor"); ok {
vmProcessors := v.([]interface{})
for _, processor := range vmProcessors {
processor, ok := processor.(map[string]interface{})
if !ok {
return nil, fmt.Errorf("[ERROR][hyperv] vm_processor should be a Hash - was '%+v'", processor)
}
log.Printf("[DEBUG] processor = [%+v]", processor)
expandedVmProcessor := vmProcessor{
CompatibilityForMigrationEnabled: processor["compatibility_for_migration_enabled"].(bool),
CompatibilityForOlderOperatingSystemsEnabled: processor["compatibility_for_older_operating_systems_enabled"].(bool),
HwThreadCountPerCore: int64(processor["hw_thread_count_per_core"].(int)),
Maximum: int64(processor["maximum"].(int)),
Reserve: int64(processor["reserve"].(int)),
RelativeWeight: int32(processor["relative_weight"].(int)),
MaximumCountPerNumaNode: int32(processor["maximum_count_per_numa_node"].(int)),
MaximumCountPerNumaSocket: int32(processor["maximum_count_per_numa_socket"].(int)),
EnableHostResourceProtection: processor["enable_host_resource_protection"].(bool),
ExposeVirtualizationExtensions: processor["expose_virtualization_extensions"].(bool),
}
expandedVmProcessors = append(expandedVmProcessors, expandedVmProcessor)
}
}
return expandedVmProcessors, nil
}
func FlattenVmProcessors(vmProcessors *[]vmProcessor) []interface{} {
flattenedVmProcessors := make([]interface{}, 0)
if vmProcessors != nil {
for _, vmProcessor := range *vmProcessors {
flattenedVmProcessor := make(map[string]interface{})
flattenedVmProcessor["compatibility_for_migration_enabled"] = vmProcessor.CompatibilityForMigrationEnabled
flattenedVmProcessor["compatibility_for_older_operating_systems_enabled"] = vmProcessor.CompatibilityForOlderOperatingSystemsEnabled
flattenedVmProcessor["hw_thread_count_per_core"] = vmProcessor.HwThreadCountPerCore
flattenedVmProcessor["maximum"] = vmProcessor.Maximum
flattenedVmProcessor["reserve"] = vmProcessor.Reserve
flattenedVmProcessor["relative_weight"] = vmProcessor.RelativeWeight
flattenedVmProcessor["maximum_count_per_numa_node"] = vmProcessor.MaximumCountPerNumaNode
flattenedVmProcessor["maximum_count_per_numa_socket"] = vmProcessor.MaximumCountPerNumaSocket
flattenedVmProcessor["enable_host_resource_protection"] = vmProcessor.EnableHostResourceProtection
flattenedVmProcessor["expose_virtualization_extensions"] = vmProcessor.ExposeVirtualizationExtensions
flattenedVmProcessors = append(flattenedVmProcessors, flattenedVmProcessor)
}
}
return flattenedVmProcessors
}
type vmProcessor struct {
VmName string
CompatibilityForMigrationEnabled bool
CompatibilityForOlderOperatingSystemsEnabled bool
HwThreadCountPerCore int64
Maximum int64
Reserve int64
RelativeWeight int32
MaximumCountPerNumaNode int32
MaximumCountPerNumaSocket int32
EnableHostResourceProtection bool
ExposeVirtualizationExtensions bool
}
type createOrUpdateVmProcessorArgs struct {
VmProcessorJson string
}
var createOrUpdateVmProcessorTemplate = template.Must(template.New("CreateOrUpdateVmProcessor").Parse(`
$ErrorActionPreference = 'Stop'
Import-Module Hyper-V
$vmProcessor = '{{.VmProcessorJson}}' | ConvertFrom-Json
$SetVMProcessorArgs = @{}
$SetVMProcessorArgs.VMName=$vmProcessor.VmName
#$SetVMProcessorArgs.Count=$vmProcessor.ProcessorCount
$SetVMProcessorArgs.CompatibilityForMigrationEnabled=$vmProcessor.CompatibilityForMigrationEnabled
$SetVMProcessorArgs.CompatibilityForOlderOperatingSystemsEnabled=$vmProcessor.CompatibilityForOlderOperatingSystemsEnabled
$SetVMProcessorArgs.HwThreadCountPerCore=$vmProcessor.HwThreadCountPerCore
$SetVMProcessorArgs.Maximum=$vmProcessor.Maximum
$SetVMProcessorArgs.Reserve=$vmProcessor.Reserve
$SetVMProcessorArgs.RelativeWeight=$vmProcessor.RelativeWeight
if ($vmProcessor.MaximumCountPerNumaNode -eq 0){
$vmProcessor.MaximumCountPerNumaNode = (Get-WmiObject -class Win32_ComputerSystem).numberoflogicalprocessors
}
$SetVMProcessorArgs.MaximumCountPerNumaNode=$vmProcessor.MaximumCountPerNumaNode
if ($vmProcessor.MaximumCountPerNumaSocket -eq 0){
$vmProcessor.MaximumCountPerNumaSocket = (Get-WmiObject -class Win32_ComputerSystem).numberofprocessors
}
$SetVMProcessorArgs.MaximumCountPerNumaSocket=$vmProcessor.MaximumCountPerNumaSocket
$SetVMProcessorArgs.EnableHostResourceProtection=$vmProcessor.EnableHostResourceProtection
$SetVMProcessorArgs.ExposeVirtualizationExtensions=$vmProcessor.ExposeVirtualizationExtensions
Set-VMProcessor @SetVMProcessorArgs
`))
func (c *HypervClient) CreateOrUpdateVmProcessor(
vmName string,
compatibilityForMigrationEnabled bool,
compatibilityForOlderOperatingSystemsEnabled bool,
hwThreadCountPerCore int64,
maximum int64,
reserve int64,
relativeWeight int32,
maximumCountPerNumaNode int32,
maximumCountPerNumaSocket int32,
enableHostResourceProtection bool,
exposeVirtualizationExtensions bool,
) (err error) {
vmProcessorJson, err := json.Marshal(vmProcessor{
VmName: vmName,
CompatibilityForMigrationEnabled: compatibilityForMigrationEnabled,
CompatibilityForOlderOperatingSystemsEnabled: compatibilityForOlderOperatingSystemsEnabled,
HwThreadCountPerCore: hwThreadCountPerCore,
Maximum: maximum,
Reserve: reserve,
RelativeWeight: relativeWeight,
MaximumCountPerNumaNode: maximumCountPerNumaNode,
MaximumCountPerNumaSocket: maximumCountPerNumaSocket,
EnableHostResourceProtection: enableHostResourceProtection,
ExposeVirtualizationExtensions: exposeVirtualizationExtensions,
})
if err != nil {
return err
}
err = c.runFireAndForgetScript(createOrUpdateVmProcessorTemplate, createOrUpdateVmProcessorArgs{
VmProcessorJson: string(vmProcessorJson),
})
return err
}
type getVmProcessorArgs struct {
VmName string
}
var getVmProcessorTemplate = template.Must(template.New("GetVmProcessor").Parse(`
$ErrorActionPreference = 'Stop'
$vmProcessorObject = Get-VMProcessor -VMName '{{.VmName}}' | %{ @{
CompatibilityForMigrationEnabled=$_.CompatibilityForMigrationEnabled
CompatibilityForOlderOperatingSystemsEnabled=$_.CompatibilityForOlderOperatingSystemsEnabled
HwThreadCountPerCore=$_.HwThreadCountPerCore
Maximum=$_.Maximum
Reserve=$_.Reserve
RelativeWeight=$_.RelativeWeight
MaximumCountPerNumaNode=$_.MaximumCountPerNumaNode
MaximumCountPerNumaSocket=$_.MaximumCountPerNumaSocket
EnableHostResourceProtection=$_.EnableHostResourceProtection
ExposeVirtualizationExtensions=$_.ExposeVirtualizationExtensions
}}
if ($vmProcessorObject) {
$vmProcessor = ConvertTo-Json -InputObject $vmProcessorObject
$vmProcessor
} else {
"{}"
}
`))
func (c *HypervClient) GetVmProcessor(vmName string) (result vmProcessor, err error) {
err = c.runScriptWithResult(getVmProcessorTemplate, getVmProcessorArgs{
VmName: vmName,
}, &result)
return result, err
}
func (c *HypervClient) GetVmProcessors(vmName string) (result []vmProcessor, err error) {
result = make([]vmProcessor, 0)
vmProcessor, err := c.GetVmProcessor(vmName)
if err != nil {
return result, err
}
result = append(result, vmProcessor)
return result, err
}
func (c *HypervClient) CreateOrUpdateVmProcessors(vmName string, vmProcessors []vmProcessor) (err error) {
if len(vmProcessors) == 0 {
return nil
}
if len(vmProcessors) > 1 {
return fmt.Errorf("Only 1 vm processor setting allowed per a vm")
}
vmProcessor := vmProcessors[0]
return c.CreateOrUpdateVmProcessor(vmName,
vmProcessor.CompatibilityForMigrationEnabled,
vmProcessor.CompatibilityForOlderOperatingSystemsEnabled,
vmProcessor.HwThreadCountPerCore,
vmProcessor.Maximum,
vmProcessor.Reserve,
vmProcessor.RelativeWeight,
vmProcessor.MaximumCountPerNumaNode,
vmProcessor.MaximumCountPerNumaSocket,
vmProcessor.EnableHostResourceProtection,
vmProcessor.ExposeVirtualizationExtensions)
}