-
Notifications
You must be signed in to change notification settings - Fork 155
/
api.go
183 lines (146 loc) · 4.69 KB
/
api.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
/*
Copyright 2022 The Kubermatic Kubernetes Platform contributors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package nutanix
import (
"context"
"fmt"
"strings"
nutanixv3 "github.com/embik/nutanix-client-go/pkg/client/v3"
"k8s.io/utils/pointer"
)
func GetClusters(ctx context.Context, client *ClientSet) ([]nutanixv3.ClusterIntentResponse, error) {
resp, err := client.Prism.V3.ListAllCluster(ctx, "")
if err != nil {
return nil, wrapNutanixError(err)
}
var clusters []nutanixv3.ClusterIntentResponse
if resp != nil {
for _, entity := range resp.Entities {
if entity != nil {
clusters = append(clusters, *entity)
}
}
}
return clusters, nil
}
func GetProjects(ctx context.Context, client *ClientSet) ([]nutanixv3.Project, error) {
resp, err := client.Prism.V3.ListAllProject(ctx, "")
if err != nil {
return nil, wrapNutanixError(err)
}
var projects []nutanixv3.Project
if resp != nil {
for _, entity := range resp.Entities {
if entity != nil {
projects = append(projects, *entity)
}
}
}
return projects, nil
}
func GetSubnets(ctx context.Context, client *ClientSet, clusterName, projectName string) ([]nutanixv3.SubnetIntentResponse, error) {
resp, err := client.Prism.V3.ListAllSubnet(ctx, "")
if err != nil {
return nil, wrapNutanixError(err)
}
var (
subnets []nutanixv3.SubnetIntentResponse
projectAllowedUUIDs []string
)
// get all subnets associated with the project. We _could_ return that as a list, but we
// want to return the full information of subnets, so we only use this list of names as
// constraints for our loop later on.
if projectName != "" {
project, err := GetProjectByName(ctx, client, projectName)
if err != nil {
return nil, err
}
for _, subnetRef := range project.Spec.Resources.SubnetReferenceList {
if subnetRef != nil {
projectAllowedUUIDs = append(projectAllowedUUIDs, subnetRef.UUID)
}
}
}
cluster, err := GetClusterByName(ctx, client, clusterName)
if err != nil {
return nil, err
}
if cluster.Metadata == nil || cluster.Metadata.UUID == nil {
return nil, fmt.Errorf("Cluster '%s' has no valid metadata", clusterName)
}
if resp != nil {
for _, entity := range resp.Entities {
if entity != nil {
if entity.Status != nil &&
(entity.Status.ClusterReference == nil || (entity.Status.ClusterReference.UUID != nil && *entity.Status.ClusterReference.UUID == *cluster.Metadata.UUID)) &&
(projectName == "" || projectName == DefaultProject || contains(projectAllowedUUIDs, *entity.Metadata.UUID)) {
subnets = append(subnets, *entity)
}
}
}
}
return subnets, nil
}
func GetCategories(ctx context.Context, client *ClientSet) ([]nutanixv3.CategoryKeyStatus, error) {
resp, err := client.Prism.V3.ListCategories(ctx, &nutanixv3.CategoryListMetadata{Kind: pointer.String("category")})
if err != nil {
return nil, wrapNutanixError(err)
}
var categoryKeys []nutanixv3.CategoryKeyStatus
if resp != nil {
for _, entity := range resp.Entities {
if entity != nil {
categoryKeys = append(categoryKeys, *entity)
}
}
}
return categoryKeys, nil
}
func GetCategoryValues(ctx context.Context, client *ClientSet, category string) ([]nutanixv3.CategoryValueStatus, error) {
resp, err := client.Prism.V3.ListAllCategoryValues(ctx, category, "")
if err != nil {
return nil, wrapNutanixError(err)
}
var categoryValues []nutanixv3.CategoryValueStatus
if resp != nil {
for _, entity := range resp.Entities {
if entity != nil {
categoryValues = append(categoryValues, *entity)
}
}
}
return categoryValues, nil
}
func wrapNutanixError(initialErr error) error {
if initialErr == nil {
return nil
}
resp, err := ParseNutanixError(initialErr)
if err != nil {
// failed to parse error, let's make sure it doesn't have new lines at least.
return fmt.Errorf("api error: %s", strings.ReplaceAll(initialErr.Error(), "\n", ""))
}
var msgs []string
for _, msg := range resp.MessageList {
msgs = append(msgs, fmt.Sprintf("%s: %s", msg.Message, msg.Reason))
}
return fmt.Errorf("api error (%s, code %d): %s", resp.State, resp.Code, strings.Join(msgs, ", "))
}
func contains(sArr []string, s string) bool {
for _, elem := range sArr {
if elem == s {
return true
}
}
return false
}