/
app_domain.go
178 lines (148 loc) · 4.22 KB
/
app_domain.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
package goaviatrix
import (
"context"
"fmt"
"log"
"strings"
)
type AppDomainMatchExpression struct {
CIDR string `json:"cidr,omitempty"`
Type string `json:"type,omitempty"`
ResId string `json:"res_id,omitempty"`
AccountId string `json:"account_id,omitempty"`
AccountName string `json:"account_name,omitempty"`
Region string `json:"region,omitempty"`
Zone string `json:"zone,omitempty"`
Tags map[string]string
}
type AppDomainSelector struct {
Expressions []*AppDomainMatchExpression
}
type AppDomain struct {
Name string
UUID string
Selector AppDomainSelector
}
func appDomainFilterToMap(filter *AppDomainMatchExpression) map[string]string {
filterMap := make(map[string]string)
if len(filter.Type) > 0 {
filterMap["type"] = filter.Type
}
if len(filter.CIDR) > 0 {
filterMap["cidr"] = filter.CIDR
}
if len(filter.ResId) > 0 {
filterMap["res_id"] = filter.ResId
}
if len(filter.AccountId) > 0 {
filterMap["account_id"] = filter.AccountId
}
if len(filter.AccountName) > 0 {
filterMap["account_name"] = filter.AccountName
}
if len(filter.Region) > 0 {
filterMap["region"] = filter.Region
}
if len(filter.Zone) > 0 {
filterMap["zone"] = filter.Zone
}
if len(filter.Tags) > 0 {
for key, value := range filter.Tags {
filterMap[fmt.Sprintf("tags.%s", key)] = value
}
}
log.Printf("[DEBUG] POST filter map: %v\n", filterMap)
return filterMap
}
func makeAppDomainForm(appDomain *AppDomain) map[string]interface{} {
form := map[string]interface{}{
"name": appDomain.Name,
}
var or []map[string]map[string]string
for _, appDomainSelector := range appDomain.Selector.Expressions {
and := map[string]map[string]string{
"all": appDomainFilterToMap(appDomainSelector),
}
or = append(or, and)
}
form["selector"] = map[string]interface{}{
"any": or,
}
return form
}
func (c *Client) CreateAppDomain(ctx context.Context, appDomain *AppDomain) (string, error) {
endpoint := "app-domains"
form := makeAppDomainForm(appDomain)
type AppDomainResp struct {
UUID string `json:"uuid"`
}
var data AppDomainResp
err := c.PostAPIContext25(ctx, &data, endpoint, form)
if err != nil {
return "", err
}
return data.UUID, nil
}
func (c *Client) GetAppDomain(ctx context.Context, uuid string) (*AppDomain, error) {
endpoint := "app-domains"
type AppDomainMatchExpressionResult struct {
All map[string]string `json:"all"`
}
type AppDomainAnyResult struct {
Any []AppDomainMatchExpressionResult `json:"any"`
}
type AppDomainResult struct {
UUID string `json:"uuid"`
Name string `json:"name"`
Selector AppDomainAnyResult `json:"selector"`
}
type AppDomainResp struct {
AppDomains []AppDomainResult `json:"app_domains"`
}
var data AppDomainResp
err := c.GetAPIContext25(ctx, &data, endpoint, nil)
if err != nil {
return nil, err
}
for _, appDomainResult := range data.AppDomains {
if appDomainResult.UUID == uuid {
appDomain := &AppDomain{
Name: appDomainResult.Name,
UUID: appDomainResult.UUID,
}
for _, filterResult := range appDomainResult.Selector.Any {
filterMap := filterResult.All
filter := &AppDomainMatchExpression{
CIDR: filterMap["cidr"],
Type: filterMap["type"],
ResId: filterMap["res_id"],
AccountId: filterMap["account_id"],
AccountName: filterMap["account_name"],
Region: filterMap["region"],
Zone: filterMap["zone"],
}
tags := make(map[string]string)
for key, value := range filterMap {
if strings.HasPrefix(key, "tags.") {
tags[strings.TrimPrefix(key, "tags.")] = value
}
}
if len(tags) > 0 {
filter.Tags = tags
}
appDomain.Selector.Expressions = append(appDomain.Selector.Expressions, filter)
}
return appDomain, nil
}
}
return nil, ErrNotFound
}
func (c *Client) UpdateAppDomain(ctx context.Context, appDomain *AppDomain, uuid string) error {
endpoint := fmt.Sprintf("app-domains/%s", uuid)
form := makeAppDomainForm(appDomain)
return c.PutAPIContext25(ctx, endpoint, form)
}
func (c *Client) DeleteAppDomain(ctx context.Context, uuid string) error {
endpoint := fmt.Sprintf("app-domains/%s", uuid)
return c.DeleteAPIContext25(ctx, endpoint, nil)
}