-
Notifications
You must be signed in to change notification settings - Fork 8
/
edge_cluster_operations.go
234 lines (194 loc) · 7.36 KB
/
edge_cluster_operations.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
// Copyright 2024 Broadcom. All Rights Reserved.
// SPDX-License-Identifier: MPL-2.0
package nsx_edge_cluster
import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/vmware/terraform-provider-vcf/internal/resource_utils"
"github.com/vmware/vcf-sdk-go/models"
)
const (
clusterTypeNsxT = "NSX-T"
)
func GetNsxEdgeClusterCreationSpec(data *schema.ResourceData) *models.EdgeClusterCreationSpec {
// No other types are supported yet
clusterType := clusterTypeNsxT
adminPassword := data.Get("admin_password").(string)
auditPassword := data.Get("audit_password").(string)
rootPassword := data.Get("root_password").(string)
name := data.Get("name").(string)
profileType := data.Get("profile_type").(string)
profileSpec := getClusterProfileSpec(data)
routingType := data.Get("routing_type").(string)
formFactor := data.Get("form_factor").(string)
highAvailability := data.Get("high_availability").(string)
tier0Name := data.Get("tier0_name").(string)
tier1Name := data.Get("tier1_name").(string)
mtu := int32(data.Get("mtu").(int))
asn := int64(data.Get("asn").(int))
tier1Unhosted := data.Get("tier1_unhosted").(bool)
transitSubnets := resource_utils.ToStringSlice(data.Get("transit_subnets").([]interface{}))
internalTransitSubnets := resource_utils.ToStringSlice(data.Get("internal_transit_subnets").([]interface{}))
nodes := data.Get("edge_node").([]interface{})
nodeSpecs := make([]*models.NsxTEdgeNodeSpec, 0, len(nodes))
for _, n := range nodes {
node := n.(map[string]interface{})
nodeSpec := getNodeSpec(node)
nodeSpecs = append(nodeSpecs, nodeSpec)
}
spec := &models.EdgeClusterCreationSpec{
EdgeAdminPassword: &adminPassword,
EdgeAuditPassword: &auditPassword,
EdgeClusterName: &name,
EdgeClusterProfileSpec: profileSpec,
EdgeClusterProfileType: &profileType,
EdgeClusterType: &clusterType,
EdgeFormFactor: &formFactor,
EdgeNodeSpecs: nodeSpecs,
EdgeRootPassword: &rootPassword,
InternalTransitSubnets: internalTransitSubnets,
Mtu: &mtu,
Asn: asn,
Tier0Name: &tier0Name,
Tier0RoutingType: &routingType,
Tier0ServicesHighAvailability: &highAvailability,
Tier1Name: &tier1Name,
Tier1Unhosted: tier1Unhosted,
TransitSubnets: transitSubnets,
}
return spec
}
func GetNsxEdgeClusterShrinkageSpec(currentNodes []*models.EdgeNodeReference, newNodes []interface{}) *models.EdgeClusterShrinkageSpec {
ids := make([]string, 0)
for _, currentNode := range currentNodes {
found := false
for _, newNode := range newNodes {
name := newNode.(map[string]interface{})["name"].(string)
if name == *currentNode.HostName {
found = true
}
}
if !found {
ids = append(ids, *currentNode.ID)
}
}
return &models.EdgeClusterShrinkageSpec{
EdgeNodeIds: ids,
}
}
func GetNsxEdgeClusterExpansionSpec(currentNodes []*models.EdgeNodeReference, newNodesRaw []interface{}) *models.EdgeClusterExpansionSpec {
newNodes := getNewNodes(currentNodes, newNodesRaw)
nodeSpecs := make([]*models.NsxTEdgeNodeSpec, 0, len(newNodes))
spec := &models.EdgeClusterExpansionSpec{}
for _, newNode := range newNodes {
node := newNode.(map[string]interface{})
adminPassword := node["admin_password"].(string)
auditPassword := node["audit_password"].(string)
rootPassword := node["root_password"].(string)
spec.EdgeNodeAdminPassword = &adminPassword
spec.EdgeNodeAuditPassword = &auditPassword
spec.EdgeNodeRootPassword = &rootPassword
nodeSpec := getNodeSpec(node)
nodeSpecs = append(nodeSpecs, nodeSpec)
}
spec.EdgeNodeSpecs = nodeSpecs
return spec
}
func getNewNodes(currentNodes []*models.EdgeNodeReference, newNodesRaw []interface{}) []interface{} {
result := make([]interface{}, 0)
for _, newNode := range newNodesRaw {
found := false
name := newNode.(map[string]interface{})["name"].(string)
for _, m := range currentNodes {
if name == *m.HostName {
found = true
}
}
if !found {
result = append(result, newNode)
}
}
return result
}
func getNodeSpec(node map[string]interface{}) *models.NsxTEdgeNodeSpec {
name := node["name"].(string)
clusterId := node["compute_cluster_id"].(string)
tep1IP := node["tep1_ip"].(string)
tep2IP := node["tep2_ip"].(string)
tepGateway := node["tep_gateway"].(string)
tepVlan := int32(node["tep_vlan"].(int))
managementIP := node["management_ip"].(string)
managementGateway := node["management_gateway"].(string)
firstVdsUplink := node["first_nsx_vds_uplink"].(string)
secondVdsUplink := node["second_nsx_vds_uplink"].(string)
interRackCluster := node["inter_rack_cluster"].(bool)
nodeSpec := &models.NsxTEdgeNodeSpec{
ClusterID: &clusterId,
EdgeNodeName: &name,
EdgeTep1IP: &tep1IP,
EdgeTep2IP: &tep2IP,
EdgeTepGateway: &tepGateway,
EdgeTepVlan: &tepVlan,
ManagementGateway: &managementGateway,
ManagementIP: &managementIP,
FirstNsxVdsUplink: firstVdsUplink,
SecondNsxVdsUplink: secondVdsUplink,
InterRackCluster: &interRackCluster,
UplinkNetwork: getUplinkNetworkSpecs(node),
}
return nodeSpec
}
func getUplinkNetworkSpecs(node map[string]interface{}) []*models.NsxTEdgeUplinkNetwork {
uplinks := node["uplink"].([]interface{})
specs := make([]*models.NsxTEdgeUplinkNetwork, 0, len(uplinks))
for _, uplink := range uplinks {
ip := uplink.(map[string]interface{})["interface_ip"].(string)
vlan := int32(uplink.(map[string]interface{})["vlan"].(int))
bgpPeersRaw := uplink.(map[string]interface{})["bgp_peer"].([]interface{})
spec := &models.NsxTEdgeUplinkNetwork{
UplinkInterfaceIP: &ip,
UplinkVlan: &vlan,
BgpPeers: getBgpPeerSpecs(bgpPeersRaw),
}
specs = append(specs, spec)
}
return specs
}
func getBgpPeerSpecs(bgpPeersRaw []interface{}) []*models.BgpPeerSpec {
peers := make([]*models.BgpPeerSpec, 0, len(bgpPeersRaw))
for _, peer := range bgpPeersRaw {
ip := peer.(map[string]interface{})["ip"].(string)
password := peer.(map[string]interface{})["password"].(string)
asn := int64(peer.(map[string]interface{})["asn"].(int))
peer := &models.BgpPeerSpec{
Asn: &asn,
IP: &ip,
Password: &password,
}
peers = append(peers, peer)
}
return peers
}
func getClusterProfileSpec(data *schema.ResourceData) *models.NsxTEdgeClusterProfileSpec {
var profileSpec *models.NsxTEdgeClusterProfileSpec = nil
profileType := data.Get("profile_type").(string)
if profileType == "CUSTOM" {
profileRaw := data.Get("profile").([]interface{})
if len(profileRaw) > 0 {
// there can be only one profile spec
profile := profileRaw[0].(map[string]interface{})
name := profile["name"].(string)
allowedHop := int64(profile["bfd_allowed_hop"].(int))
declareDeadMultiple := int64(profile["bfd_declare_dead_multiple"].(int))
probeInterval := int64(profile["bfd_probe_interval"].(int))
standbyRelocationThreshold := int64(profile["standby_relocation_threshold"].(int))
profileSpec = &models.NsxTEdgeClusterProfileSpec{
BfdAllowedHop: &allowedHop,
BfdDeclareDeadMultiple: &declareDeadMultiple,
BfdProbeInterval: &probeInterval,
EdgeClusterProfileName: &name,
StandbyRelocationThreshold: &standbyRelocationThreshold,
}
}
}
return profileSpec
}