forked from vmware/terraform-provider-nsxt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_source_nsxt_policy_lb_persistence_profile.go
129 lines (114 loc) · 4.35 KB
/
data_source_nsxt_policy_lb_persistence_profile.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
/* Copyright © 2019 VMware, Inc. All Rights Reserved.
SPDX-License-Identifier: MPL-2.0 */
package nsxt
import (
"fmt"
"strings"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/vmware/vsphere-automation-sdk-go/runtime/bindings"
"github.com/vmware/vsphere-automation-sdk-go/services/nsxt/infra"
"github.com/vmware/vsphere-automation-sdk-go/services/nsxt/model"
)
var lbPersistenceTypeValues = []string{"SOURCE_IP", "COOKIE", "GENERIC", "ANY"}
var lbPersistenceTypeMap = map[string]string{
model.LBPersistenceProfile_RESOURCE_TYPE_LBSOURCEIPPERSISTENCEPROFILE: "SOURCE_IP",
model.LBPersistenceProfile_RESOURCE_TYPE_LBCOOKIEPERSISTENCEPROFILE: "COOKIE",
model.LBPersistenceProfile_RESOURCE_TYPE_LBGENERICPERSISTENCEPROFILE: "GENERIC",
}
func dataSourceNsxtPolicyLbPersistenceProfile() *schema.Resource {
return &schema.Resource{
Read: dataSourceNsxtPolicyLbPersistenceProfileRead,
Schema: map[string]*schema.Schema{
"id": getDataSourceIDSchema(),
"type": {
Type: schema.TypeString,
Description: "Load Balancer Persistence Type",
Optional: true,
Default: "ANY",
ValidateFunc: validation.StringInSlice(lbPersistenceTypeValues, false),
},
"display_name": getDataSourceDisplayNameSchema(),
"description": getDataSourceDescriptionSchema(),
"path": getPathSchema(),
},
}
}
func dataSourceNsxtPolicyLbPersistenceProfileTypeMatches(profile model.LBPersistenceProfile, profileType string) bool {
if profileType == "ANY" {
return true
}
if lbPersistenceTypeMap[profile.ResourceType] == profileType {
return true
}
return false
}
func dataSourceNsxtPolicyLbPersistenceProfileRead(d *schema.ResourceData, m interface{}) error {
connector := getPolicyConnector(m)
client := infra.NewLbPersistenceProfilesClient(connector)
converter := bindings.NewTypeConverter()
objID := d.Get("id").(string)
objName := d.Get("display_name").(string)
objTypeValue, typeSet := d.GetOk("type")
objType := objTypeValue.(string)
var obj model.LBPersistenceProfile
if objID != "" {
// Get by id
objGet, err := client.Get(objID)
if err != nil {
return handleDataSourceReadError(d, "LbPersistenceProfile", objID, err)
}
profile, errs := converter.ConvertToGolang(objGet, model.LBPersistenceProfileBindingType())
if errs != nil {
return errs[0]
}
obj = profile.(model.LBPersistenceProfile)
} else if objName == "" && !typeSet {
return fmt.Errorf("Error obtaining LbPersistenceProfile name or type during read")
} else {
// Get by full name/prefix
includeMarkForDeleteObjectsParam := false
objList, err := client.List(nil, &includeMarkForDeleteObjectsParam, nil, nil, nil, nil)
if err != nil {
return handleListError("LbPersistenceProfile", err)
}
// go over the list to find the correct one (prefer a perfect match. If not - prefix match)
var perfectMatch, prefixMatch []model.LBPersistenceProfile
for _, objInList := range objList.Results {
profile, errs := converter.ConvertToGolang(objInList, model.LBPersistenceProfileBindingType())
if errs != nil {
return errs[0]
}
lbProfile := profile.(model.LBPersistenceProfile)
if objName != "" && strings.HasPrefix(*lbProfile.DisplayName, objName) && dataSourceNsxtPolicyLbPersistenceProfileTypeMatches(lbProfile, objType) {
prefixMatch = append(prefixMatch, lbProfile)
}
if *lbProfile.DisplayName == objName {
perfectMatch = append(perfectMatch, lbProfile)
}
if objName == "" && typeSet && dataSourceNsxtPolicyLbPersistenceProfileTypeMatches(lbProfile, objType) {
// match only by type
perfectMatch = append(perfectMatch, lbProfile)
}
}
if len(perfectMatch) > 0 {
if len(perfectMatch) > 1 {
return fmt.Errorf("Found multiple LbPersistenceProfiles with name '%s' and type '%s'", objName, objType)
}
obj = perfectMatch[0]
} else if len(prefixMatch) > 0 {
if len(prefixMatch) > 1 {
return fmt.Errorf("Found multiple LbPersistenceProfiles with name starting with '%s'", objName)
}
obj = prefixMatch[0]
} else {
return fmt.Errorf("LbPersistenceProfile with name '%s' and type '%s' was not found", objName, objType)
}
}
d.SetId(*obj.Id)
d.Set("display_name", obj.DisplayName)
d.Set("description", obj.Description)
d.Set("type", lbPersistenceTypeMap[obj.ResourceType])
d.Set("path", obj.Path)
return nil
}