|
| 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