Skip to content

Commit 9ac6ade

Browse files
add VM affinity policies
1 parent 81e4404 commit 9ac6ade

20 files changed

Lines changed: 1847 additions & 8 deletions
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
terraform {
2+
required_providers {
3+
nutanix = {
4+
source = "nutanix/nutanix"
5+
version = "2.0.0"
6+
}
7+
}
8+
}
9+
10+
#defining nutanix configuration
11+
provider "nutanix" {
12+
username = var.nutanix_username
13+
password = var.nutanix_password
14+
endpoint = var.nutanix_endpoint
15+
port = 9440
16+
insecure = true
17+
}
18+
19+
#creating category
20+
resource "nutanix_category_v2" "category" {
21+
key = "AntiAffinityPolicy"
22+
value = "tf-anti-affinity-policy"
23+
}
24+
25+
# Create policy
26+
resource "nutanix_vm_anti_affinity_policy_v2" "policy" {
27+
name = "temp-anti-affinity"
28+
description = "a description"
29+
categories = [ nutanix_category_v2.category.id ]
30+
}
31+
32+
# Get all policies
33+
data "nutanix_vm_anti_affinity_policies_v2" "policies" {}
34+
35+
# Get specific policy
36+
data "nutanix_vm_anti_affinity_policy_v2" "policy" {
37+
ext_id = nutanix_vm_anti_affinity_policy_v2.policy.ext_id
38+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#define values to the variables to be used in terraform file
2+
nutanix_username = "admin"
3+
nutanix_password = "password"
4+
nutanix_endpoint = "10.xx.xx.xx"
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#define the type of variables to be used in terraform file
2+
variable "nutanix_username" {
3+
type = string
4+
}
5+
variable "nutanix_password" {
6+
type = string
7+
}
8+
variable "nutanix_endpoint" {
9+
type = string
10+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
terraform {
2+
required_providers {
3+
nutanix = {
4+
source = "nutanix/nutanix"
5+
version = "2.0.0"
6+
}
7+
}
8+
}
9+
10+
#defining nutanix configuration
11+
provider "nutanix" {
12+
username = var.nutanix_username
13+
password = var.nutanix_password
14+
endpoint = var.nutanix_endpoint
15+
port = 9440
16+
insecure = true
17+
}
18+
19+
#creating Host category
20+
resource "nutanix_category_v2" "host_category" {
21+
key = "HostAffinityPolicy"
22+
value = "tf-host-affinity-host-category"
23+
}
24+
25+
# Create VM category
26+
resource "nutanix_category_v2" "vm_category" {
27+
key = "HostAffinityPolicy"
28+
value = "tf-host-affinity-vm-category"
29+
}
30+
31+
# Create policy
32+
resource "nutanix_vm_host_affinity_policy_v2" "policy" {
33+
name = "temp-host-affinity"
34+
description = "a description"
35+
host_categories = [ nutanix_category_v2.host_category.id ]
36+
vm_categories = [ nutanix_category_v2.vm_category.id ]
37+
}
38+
39+
# Get all policies
40+
data "nutanix_vm_host_affinity_policies_v2" "policies" {}
41+
42+
# Get specific policy
43+
data "nutanix_vm_host_affinity_policy_v2" "policy" {
44+
ext_id = nutanix_vm_host_affinity_policy_v2.policy.ext_id
45+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#define values to the variables to be used in terraform file
2+
nutanix_username = "admin"
3+
nutanix_password = "password"
4+
nutanix_endpoint = "10.xx.xx.xx"
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#define the type of variables to be used in terraform file
2+
variable "nutanix_username" {
3+
type = string
4+
}
5+
variable "nutanix_password" {
6+
type = string
7+
}
8+
variable "nutanix_endpoint" {
9+
type = string
10+
}

nutanix/provider/provider.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,10 @@ func Provider() *schema.Provider {
306306
"nutanix_ngt_configuration_v2": vmmv2.DatasourceNutanixNGTConfigurationV4(),
307307
"nutanix_image_placement_policy_v2": vmmv2.DatasourceNutanixImagePlacementV4(),
308308
"nutanix_image_placement_policies_v2": vmmv2.DatasourceNutanixImagePlacementsV4(),
309+
"nutanix_vm_anti_affinity_policy_v2": vmmv2.DatasourceNutanixVMAntiAffinityPolicyV2(),
310+
"nutanix_vm_anti_affinity_policies_v2": vmmv2.DatasourceNutanixVMAntiAffinityPoliciesV2(),
311+
"nutanix_vm_host_affinity_policy_v2": vmmv2.DatasourceNutanixVMHostAffinityPolicyV2(),
312+
"nutanix_vm_host_affinity_policies_v2": vmmv2.DatasourceNutanixVMHostAffinityPoliciesV2(),
309313
"nutanix_cluster_v2": clustersv2.DatasourceNutanixClusterEntityV2(),
310314
"nutanix_clusters_v2": clustersv2.DatasourceNutanixClusterEntitiesV2(),
311315
"nutanix_host_v2": clustersv2.DatasourceNutanixHostEntityV2(),
@@ -427,6 +431,8 @@ func Provider() *schema.Provider {
427431
"nutanix_vm_network_device_assign_ip_v2": vmmv2.ResourceNutanixVmsNetworkDeviceAssignIPV2(),
428432
"nutanix_vm_network_device_migrate_v2": vmmv2.ResourceNutanixVmsNetworkDeviceMigrateV2(),
429433
"nutanix_image_placement_policy_v2": vmmv2.ResourceNutanixImagePlacementV2(),
434+
"nutanix_vm_host_affinity_policy_v2": vmmv2.ResourceNutanixVMHostAffinityPolicyV2(),
435+
"nutanix_vm_anti_affinity_policy_v2": vmmv2.ResourceNutanixVMAntiAffinityPolicyV2(),
430436
"nutanix_cluster_v2": clustersv2.ResourceNutanixClusterV2(),
431437
"nutanix_cluster_add_node_v2": clustersv2.ResourceNutanixClusterAddNodeV2(),
432438
"nutanix_clusters_discover_unconfigured_nodes_v2": clustersv2.ResourceNutanixClusterDiscoverUnconfiguredNodesV2(),

nutanix/sdks/v4/vmm/vmm.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@ import (
77
)
88

99
type Client struct {
10-
ImagesAPIInstance *api.ImagesApi
11-
TemplatesAPIInstance *api.TemplatesApi
12-
VMAPIInstance *api.VmApi
13-
ImagesPlacementAPIInstance *api.ImagePlacementPoliciesApi
10+
ImagesAPIInstance *api.ImagesApi
11+
TemplatesAPIInstance *api.TemplatesApi
12+
VMAPIInstance *api.VmApi
13+
ImagesPlacementAPIInstance *api.ImagePlacementPoliciesApi
14+
VMAntiAffinityPolicyAPIInstance *api.VmAntiAffinityPoliciesApi
15+
VMHostAffinityPolicyAPIInstance *api.VmHostAffinityPoliciesApi
1416
}
1517

1618
func NewVmmClient(credentials client.Credentials) (*Client, error) {
@@ -30,10 +32,12 @@ func NewVmmClient(credentials client.Credentials) (*Client, error) {
3032
}
3133

3234
f := &Client{
33-
ImagesAPIInstance: api.NewImagesApi(baseClient),
34-
TemplatesAPIInstance: api.NewTemplatesApi(baseClient),
35-
VMAPIInstance: api.NewVmApi(baseClient),
36-
ImagesPlacementAPIInstance: api.NewImagePlacementPoliciesApi(baseClient),
35+
ImagesAPIInstance: api.NewImagesApi(baseClient),
36+
TemplatesAPIInstance: api.NewTemplatesApi(baseClient),
37+
VMAPIInstance: api.NewVmApi(baseClient),
38+
ImagesPlacementAPIInstance: api.NewImagePlacementPoliciesApi(baseClient),
39+
VMAntiAffinityPolicyAPIInstance: api.NewVmAntiAffinityPoliciesApi(baseClient),
40+
VMHostAffinityPolicyAPIInstance: api.NewVmHostAffinityPoliciesApi(baseClient),
3741
}
3842

3943
return f, nil
Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
package vmmv2
2+
3+
import (
4+
"context"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
9+
"github.com/nutanix/ntnx-api-golang-clients/vmm-go-client/v4/models/vmm/v4/ahv/policies"
10+
conns "github.com/terraform-providers/terraform-provider-nutanix/nutanix"
11+
"github.com/terraform-providers/terraform-provider-nutanix/utils"
12+
)
13+
14+
func DatasourceNutanixVMAntiAffinityPoliciesV2() *schema.Resource {
15+
return &schema.Resource{
16+
ReadContext: DatasourceNutanixVMAntiAffinityPoliciesV2Read,
17+
Schema: map[string]*schema.Schema{
18+
"page": {
19+
Type: schema.TypeInt,
20+
Optional: true,
21+
},
22+
"limit": {
23+
Type: schema.TypeInt,
24+
Optional: true,
25+
},
26+
"filter": {
27+
Type: schema.TypeString,
28+
Optional: true,
29+
},
30+
"order_by": {
31+
Type: schema.TypeString,
32+
Optional: true,
33+
},
34+
"policies": {
35+
Type: schema.TypeList,
36+
Computed: true,
37+
Elem: &schema.Resource{
38+
Schema: map[string]*schema.Schema{
39+
"ext_id": {
40+
Type: schema.TypeString,
41+
Computed: true,
42+
},
43+
"name": {
44+
Type: schema.TypeString,
45+
Computed: true,
46+
},
47+
"description": {
48+
Type: schema.TypeString,
49+
Computed: true,
50+
},
51+
"create_time": {
52+
Type: schema.TypeString,
53+
Computed: true,
54+
},
55+
"update_time": {
56+
Type: schema.TypeString,
57+
Computed: true,
58+
},
59+
"created_by": {
60+
Type: schema.TypeMap,
61+
Computed: true,
62+
Elem: &schema.Schema{
63+
Type: schema.TypeString,
64+
},
65+
},
66+
"updated_by": {
67+
Type: schema.TypeMap,
68+
Computed: true,
69+
Elem: &schema.Schema{
70+
Type: schema.TypeString,
71+
},
72+
},
73+
"categories": {
74+
Type: schema.TypeSet,
75+
Computed: true,
76+
Elem: &schema.Schema{
77+
Type: schema.TypeString,
78+
},
79+
},
80+
},
81+
},
82+
},
83+
},
84+
}
85+
}
86+
87+
func DatasourceNutanixVMAntiAffinityPoliciesV2Read(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
88+
conn := meta.(*conns.Client).VmmAPI
89+
90+
var filter, orderBy *string
91+
var page, limit *int
92+
93+
if pagef, ok := d.GetOk("page"); ok {
94+
page = utils.IntPtr(pagef.(int))
95+
} else {
96+
page = nil
97+
}
98+
if limitf, ok := d.GetOk("limit"); ok {
99+
limit = utils.IntPtr(limitf.(int))
100+
} else {
101+
limit = nil
102+
}
103+
if filterf, ok := d.GetOk("filter"); ok {
104+
filter = utils.StringPtr(filterf.(string))
105+
} else {
106+
filter = nil
107+
}
108+
if order, ok := d.GetOk("order_by"); ok {
109+
orderBy = utils.StringPtr(order.(string))
110+
} else {
111+
orderBy = nil
112+
}
113+
114+
resp, err := conn.VMAntiAffinityPolicyAPIInstance.ListVmAntiAffinityPolicies(page, limit, filter, orderBy)
115+
116+
if err != nil {
117+
return diag.Errorf("error while fetching policies : %v", err)
118+
}
119+
120+
if resp.Data == nil {
121+
if err := d.Set("policies", make([]interface{}, 0)); err != nil {
122+
return diag.FromErr(err)
123+
}
124+
125+
d.SetId(utils.GenUUID())
126+
127+
return diag.Diagnostics{{
128+
Severity: diag.Warning,
129+
Summary: "🫙 No data found.",
130+
Detail: "The API returned an empty list of policies.",
131+
}}
132+
}
133+
134+
getResp := resp.Data.GetValue().([]policies.VmAntiAffinityPolicy)
135+
136+
if err := d.Set("policies", flattenVMAntiAffinityPolicyEntities(getResp)); err != nil {
137+
return diag.FromErr(err)
138+
}
139+
140+
d.SetId(resource.UniqueId())
141+
142+
return nil
143+
}
144+
145+
func flattenVMAntiAffinityPolicyEntities(pr []policies.VmAntiAffinityPolicy) []interface{} {
146+
if len(pr) > 0 {
147+
policies := make([]interface{}, len(pr))
148+
149+
for k, v := range pr {
150+
policy := make(map[string]interface{})
151+
152+
if v.ExtId != nil {
153+
policy["ext_id"] = v.ExtId
154+
}
155+
if v.Name != nil {
156+
policy["name"] = v.Name
157+
}
158+
if v.Description != nil {
159+
policy["description"] = v.Description
160+
}
161+
if v.CreateTime != nil {
162+
policy["create_time"] = v.CreateTime.String()
163+
}
164+
if v.UpdateTime != nil {
165+
policy["update_time"] = v.UpdateTime.String()
166+
}
167+
if v.CreatedBy != nil {
168+
if v.CreatedBy.ExtId != nil {
169+
policy["created_by"] = map[string]string{
170+
"ext_id": *v.CreatedBy.ExtId,
171+
}
172+
}
173+
}
174+
if v.UpdatedBy != nil {
175+
if v.UpdatedBy.ExtId != nil {
176+
policy["updated_by"] = map[string]string{
177+
"ext_id": *v.UpdatedBy.ExtId,
178+
}
179+
}
180+
}
181+
if v.Categories != nil {
182+
policy["categories"] = flattenPolicyCategoryReference(v.Categories)
183+
}
184+
policies[k] = policy
185+
}
186+
return policies
187+
}
188+
return nil
189+
}

0 commit comments

Comments
 (0)