forked from vmware/cloud-provider-for-cloud-director
-
Notifications
You must be signed in to change notification settings - Fork 0
/
defined_entity_util.go
79 lines (70 loc) · 2.6 KB
/
defined_entity_util.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
package util
import (
"fmt"
swaggerClient "github.com/vmware/cloud-provider-for-cloud-director/pkg/vcdswaggerclient"
)
const (
EntityTypePrefix = "urn:vcloud:type"
CAPVCDEntityTypeVendor = "vmware"
CAPVCDEntityTypeNss = "capvcdCluster"
CAPVCDEntityTypeVersion = "1.0.0"
NativeClusterEntityTypeVendor = "cse"
NativeClusterEntityTypeNss = "nativeCluster"
NativeClusterEntityTypeVersion = "2.0.0"
)
var (
CAPVCDEntityTypeID = fmt.Sprintf("%s:%s:%s:%s", EntityTypePrefix, CAPVCDEntityTypeVendor, CAPVCDEntityTypeNss, CAPVCDEntityTypeVersion)
NativeEntityTypeID = fmt.Sprintf("%s:%s:%s:%s", EntityTypePrefix, NativeClusterEntityTypeVendor, NativeClusterEntityTypeNss, NativeClusterEntityTypeVersion)
)
func GetVirtualIPsFromRDE(rde *swaggerClient.DefinedEntity) ([]string, error) {
statusEntry, ok := rde.Entity["status"]
if !ok {
return nil, fmt.Errorf("could not find 'status' entry in defined entity")
}
statusMap, ok := statusEntry.(map[string]interface{})
if !ok {
return nil, fmt.Errorf("unable to convert [%T] to map", statusEntry)
}
var virtualIpInterfaces interface{}
if rde.EntityType == CAPVCDEntityTypeID {
virtualIpInterfaces = statusMap["virtualIPs"]
} else if rde.EntityType == NativeEntityTypeID {
virtualIpInterfaces = statusMap["virtual_IPs"]
} else {
return nil, fmt.Errorf("entity type %s not supported by CPI", rde.EntityType)
}
if virtualIpInterfaces == nil {
return make([]string, 0), nil
}
virtualIpInterfacesSlice, ok := virtualIpInterfaces.([]interface{})
if !ok {
return nil, fmt.Errorf("unable to convert [%T] to slice of interface", virtualIpInterfaces)
}
virtualIpStrs := make([]string, len(virtualIpInterfacesSlice))
for ind, ipInterface := range virtualIpInterfacesSlice {
currIp, ok := ipInterface.(string)
if !ok {
return nil, fmt.Errorf("unable to convert [%T] to string", ipInterface)
}
virtualIpStrs[ind] = currIp
}
return virtualIpStrs, nil
}
// ReplaceVirtualIPsInRDE replaces the virtual IPs array in the inputted rde. It does not make an API call to update
// the RDE.
func ReplaceVirtualIPsInRDE(rde *swaggerClient.DefinedEntity, updatedIps []string) (*swaggerClient.DefinedEntity, error) {
statusEntry, ok := rde.Entity["status"]
if !ok {
return nil, fmt.Errorf("could not find 'status' entry in defined entity")
}
statusMap, ok := statusEntry.(map[string]interface{})
if !ok {
return nil, fmt.Errorf("unable to convert [%T] to map", statusEntry)
}
if rde.EntityType == CAPVCDEntityTypeID {
statusMap["virtualIPs"] = updatedIps
} else if rde. EntityType == NativeEntityTypeID {
statusMap["virtual_IPs"] = updatedIps
}
return rde, nil
}