@@ -11,7 +11,9 @@ import (
11
11
"github.com/hashicorp/terraform-plugin-framework/path"
12
12
"github.com/hashicorp/terraform-plugin-framework/resource"
13
13
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
14
+ "github.com/hashicorp/terraform-plugin-framework/resource/schema/objectdefault"
14
15
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
16
+ "github.com/hashicorp/terraform-plugin-framework/resource/schema/stringdefault"
15
17
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
16
18
"github.com/hashicorp/terraform-plugin-framework/types"
17
19
"github.com/hashicorp/terraform-plugin-log/tflog"
@@ -33,13 +35,14 @@ type HypercoreVMResource struct {
33
35
34
36
// HypercoreVMResourceModel describes the resource data model.
35
37
type HypercoreVMResourceModel struct {
36
- Group types.String `tfsdk:"group"`
37
- Name types.String `tfsdk:"name"`
38
- Description types.String `tfsdk:"description"`
39
- VCPU types.Int32 `tfsdk:"vcpu"`
40
- Memory types.Int64 `tfsdk:"memory"`
41
- Clone CloneModel `tfsdk:"clone"`
42
- Id types.String `tfsdk:"id"`
38
+ Group types.String `tfsdk:"group"`
39
+ Name types.String `tfsdk:"name"`
40
+ Description types.String `tfsdk:"description"`
41
+ VCPU types.Int32 `tfsdk:"vcpu"`
42
+ Memory types.Int64 `tfsdk:"memory"`
43
+ Clone CloneModel `tfsdk:"clone"`
44
+ AffinityStrategy AffinityStrategyModel `tfsdk:"affinity_strategy"`
45
+ Id types.String `tfsdk:"id"`
43
46
}
44
47
45
48
type CloneModel struct {
@@ -48,6 +51,12 @@ type CloneModel struct {
48
51
MetaData types.String `tfsdk:"meta_data"`
49
52
}
50
53
54
+ type AffinityStrategyModel struct {
55
+ StrictAffinity types.Bool `tfsdk:"strict_affinity"`
56
+ PreferredNodeUUID types.String `tfsdk:"preferred_node_uuid"`
57
+ BackupNodeUUID types.String `tfsdk:"backup_node_uuid"`
58
+ }
59
+
51
60
func (r * HypercoreVMResource ) Metadata (ctx context.Context , req resource.MetadataRequest , resp * resource.MetadataResponse ) {
52
61
resp .TypeName = req .ProviderTypeName + "_vm"
53
62
}
@@ -93,6 +102,30 @@ func (r *HypercoreVMResource) Schema(ctx context.Context, req resource.SchemaReq
93
102
"meta_data" : types .StringType ,
94
103
},
95
104
},
105
+ "affinity_strategy" : schema.ObjectAttribute {
106
+ MarkdownDescription : "VM node affinity." ,
107
+ Optional : true ,
108
+ AttributeTypes : map [string ]attr.Type {
109
+ "strict_affinity" : types .BoolType ,
110
+ "preferred_node_uuid" : types .StringType ,
111
+ "backup_node_uuid" : types .StringType ,
112
+ },
113
+ Computed : true ,
114
+ Default : objectdefault .StaticValue (
115
+ types .ObjectValueMust (
116
+ map [string ]attr.Type {
117
+ "strict_affinity" : types .BoolType ,
118
+ "preferred_node_uuid" : types .StringType ,
119
+ "backup_node_uuid" : types .StringType ,
120
+ },
121
+ map [string ]attr.Value {
122
+ "strict_affinity" : types .BoolValue (false ),
123
+ "preferred_node_uuid" : types .StringValue ("" ),
124
+ "backup_node_uuid" : types .StringValue ("" ),
125
+ },
126
+ ),
127
+ ),
128
+ },
96
129
"id" : schema.StringAttribute {
97
130
Computed : true ,
98
131
MarkdownDescription : "HypercoreVM identifier" ,
@@ -176,6 +209,9 @@ func (r *HypercoreVMResource) Create(ctx context.Context, req resource.CreateReq
176
209
data .VCPU .ValueInt32Pointer (),
177
210
data .Memory .ValueInt64Pointer (),
178
211
nil ,
212
+ data .AffinityStrategy .StrictAffinity .ValueBool (),
213
+ data .AffinityStrategy .PreferredNodeUUID .ValueString (),
214
+ data .AffinityStrategy .BackupNodeUUID .ValueString (),
179
215
)
180
216
changed , msg := vmClone .Create (* r .client , ctx )
181
217
tflog .Info (ctx , fmt .Sprintf ("Changed: %t, Message: %s\n " , changed , msg ))
@@ -244,6 +280,11 @@ func (r *HypercoreVMResource) Read(ctx context.Context, req resource.ReadRequest
244
280
data .VCPU = types .Int32Value (int32 (utils .AnyToInteger64 (hc3_vm ["numVCPU" ])))
245
281
data .Memory = types .Int64Value (utils .AnyToInteger64 (hc3_vm ["mem" ]) / 1024 / 1024 )
246
282
283
+ affinityStrategy := utils .AnyToMap (hc3_vm ["affinityStrategy" ])
284
+ data .AffinityStrategy .StrictAffinity = types .BoolValue (utils .AnyToBool (affinityStrategy ["strictAffinity" ]))
285
+ data .AffinityStrategy .PreferredNodeUUID = types .StringValue (utils .AnyToString (affinityStrategy ["preferredNodeUUID" ]))
286
+ data .AffinityStrategy .BackupNodeUUID = types .StringValue (utils .AnyToString (affinityStrategy ["backupNodeUUID" ]))
287
+
247
288
// ==============================================================================
248
289
249
290
// Save updated data into Terraform state
@@ -296,6 +337,20 @@ func (r *HypercoreVMResource) Update(ctx context.Context, req resource.UpdateReq
296
337
updatePayload ["numVCPU" ] = data .VCPU .ValueInt32 ()
297
338
}
298
339
340
+ affinityStrategy := map [string ]any {}
341
+ if data_state .AffinityStrategy .StrictAffinity != data .AffinityStrategy .StrictAffinity {
342
+ affinityStrategy ["strictAffinity" ] = data .AffinityStrategy .StrictAffinity .ValueBool ()
343
+ }
344
+ if data_state .AffinityStrategy .PreferredNodeUUID != data .AffinityStrategy .PreferredNodeUUID {
345
+ affinityStrategy ["preferredNodeUUID" ] = data .AffinityStrategy .PreferredNodeUUID .ValueString ()
346
+ }
347
+ if data_state .AffinityStrategy .BackupNodeUUID != data .AffinityStrategy .BackupNodeUUID {
348
+ affinityStrategy ["backupNodeUUID" ] = data .AffinityStrategy .BackupNodeUUID .ValueString ()
349
+ }
350
+ if len (affinityStrategy ) > 0 {
351
+ updatePayload ["affinityStrategy" ] = affinityStrategy
352
+ }
353
+
299
354
taskTag , _ := restClient .UpdateRecord ( /**/
300
355
fmt .Sprintf ("/rest/v1/VirDomain/%s" , vm_uuid ),
301
356
updatePayload ,
0 commit comments