forked from vmware/terraform-provider-nsxt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_source_nsxt_policy_vm.go
90 lines (79 loc) · 2.63 KB
/
data_source_nsxt_policy_vm.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
/* Copyright © 2019 VMware, Inc. All Rights Reserved.
SPDX-License-Identifier: MPL-2.0 */
package nsxt
import (
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/vmware/vsphere-automation-sdk-go/runtime/bindings"
"github.com/vmware/vsphere-automation-sdk-go/services/nsxt/model"
)
var nsxtPolicyVMIDSchemaKeys = []string{
"external_id",
"bios_id",
"instance_id",
}
func dataSourceNsxtPolicyVM() *schema.Resource {
return &schema.Resource{
Read: dataSourceNsxtPolicyVMIDRead,
Schema: map[string]*schema.Schema{
"display_name": getDataSourceDisplayNameSchema(),
"description": getDataSourceDescriptionSchema(),
"external_id": getDataSourceStringSchema("External ID of the Virtual Machine"),
"bios_id": getDataSourceStringSchema("BIOS UUID of the Virtual Machine"),
"instance_id": getDataSourceStringSchema("Instance UUID of the Virtual Machine"),
},
}
}
func getNsxtPolicyVMIDFromSchema(d *schema.ResourceData) string {
for _, k := range nsxtPolicyVMIDSchemaKeys {
v := d.Get(k).(string)
if v != "" {
return v
}
}
return ""
}
func dataSourceNsxtPolicyVMIDRead(d *schema.ResourceData, m interface{}) error {
converter := bindings.NewTypeConverter()
converter.SetMode(bindings.REST)
var vmModel model.VirtualMachine
connector := getPolicyConnector(m)
// TODO: test with KVM based VM
objID := getNsxtPolicyVMIDFromSchema(d)
if objID != "" {
vmObj, err := findNsxtPolicyVMByID(connector, objID, m)
if err != nil {
return fmt.Errorf("Error while reading Virtual Machine %s: %v", objID, err)
}
vmModel = vmObj
} else {
displayName := d.Get("display_name").(string)
perfectMatch, prefixMatch, err := findNsxtPolicyVMByNamePrefix(connector, displayName, m)
if err != nil {
return err
}
foundLen := len(perfectMatch) + len(prefixMatch)
if foundLen == 0 {
return fmt.Errorf("Unable to find Virtual Machine with name prefix: %s", displayName)
}
if foundLen > 1 {
return fmt.Errorf("Found %v Virtual Machines with name prefix: %s", foundLen, displayName)
}
if len(perfectMatch) > 0 {
vmModel = perfectMatch[0]
} else {
vmModel = prefixMatch[0]
}
}
computeIDMap := collectSeparatedStringListToMap(vmModel.ComputeIds, ":")
if vmModel.ExternalId == nil {
return fmt.Errorf("Unable to read external ID for Virtual Machine with name %s", *vmModel.DisplayName)
}
d.SetId(*vmModel.ExternalId)
d.Set("display_name", vmModel.DisplayName)
d.Set("description", vmModel.Description)
d.Set("external_id", *vmModel.ExternalId)
d.Set("bios_id", computeIDMap[nsxtPolicyBiosUUIDKey])
d.Set("instance_id", computeIDMap[nsxtPolicyInstanceUUIDKey])
return nil
}