forked from akamai/AkamaiOPEN-edgegrid-golang
/
asmap.go
219 lines (174 loc) · 6.02 KB
/
asmap.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
package gtm
import (
"context"
"fmt"
"net/http"
)
//
// Handle Operations on gtm asmaps
// Based on 1.4 schema
//
// ASMaps contains operations available on a ASmap resource.
type ASMaps interface {
// NewAsMap creates a new AsMap object.
NewAsMap(context.Context, string) *AsMap
// NewASAssignment instantiates new Assignment struct.
NewASAssignment(context.Context, *AsMap, int, string) *AsAssignment
// ListAsMaps retrieves all AsMaps.
//
// See: https://techdocs.akamai.com/gtm/reference/get-as-maps
ListAsMaps(context.Context, string) ([]*AsMap, error)
// GetAsMap retrieves a AsMap with the given name.
//
// See: https://techdocs.akamai.com/gtm/reference/get-as-map
GetAsMap(context.Context, string, string) (*AsMap, error)
// CreateAsMap creates the datacenter identified by the receiver argument in the specified domain.
//
// See: https://techdocs.akamai.com/gtm/reference/put-as-map
CreateAsMap(context.Context, *AsMap, string) (*AsMapResponse, error)
// DeleteAsMap deletes the datacenter identified by the receiver argument from the domain specified.
//
// See: https://techdocs.akamai.com/gtm/reference/delete-as-map
DeleteAsMap(context.Context, *AsMap, string) (*ResponseStatus, error)
// UpdateAsMap updates the datacenter identified in the receiver argument in the provided domain.
//
// See: https://techdocs.akamai.com/gtm/reference/put-as-map
UpdateAsMap(context.Context, *AsMap, string) (*ResponseStatus, error)
}
// AsAssignment represents a GTM asmap assignment structure
type AsAssignment struct {
DatacenterBase
AsNumbers []int64 `json:"asNumbers"`
}
// AsMap represents a GTM AsMap
type AsMap struct {
DefaultDatacenter *DatacenterBase `json:"defaultDatacenter"`
Assignments []*AsAssignment `json:"assignments,omitempty"`
Name string `json:"name"`
Links []*Link `json:"links,omitempty"`
}
// AsMapList represents the returned GTM AsMap List body
type AsMapList struct {
AsMapItems []*AsMap `json:"items"`
}
// Validate validates AsMap
func (asm *AsMap) Validate() error {
if len(asm.Name) < 1 {
return fmt.Errorf("AsMap is missing Name")
}
if asm.DefaultDatacenter == nil {
return fmt.Errorf("AsMap is missing DefaultDatacenter")
}
return nil
}
func (p *gtm) NewAsMap(ctx context.Context, name string) *AsMap {
logger := p.Log(ctx)
logger.Debug("NewAsMap")
asmap := &AsMap{Name: name}
return asmap
}
func (p *gtm) ListAsMaps(ctx context.Context, domainName string) ([]*AsMap, error) {
logger := p.Log(ctx)
logger.Debug("ListAsMaps")
var aslist AsMapList
getURL := fmt.Sprintf("/config-gtm/v1/domains/%s/as-maps", domainName)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, getURL, nil)
if err != nil {
return nil, fmt.Errorf("failed to create ListAsMaps request: %w", err)
}
setVersionHeader(req, schemaVersion)
resp, err := p.Exec(req, &aslist)
if err != nil {
return nil, fmt.Errorf("ListAsMaps request failed: %w", err)
}
if resp.StatusCode != http.StatusOK {
return nil, p.Error(resp)
}
return aslist.AsMapItems, nil
}
func (p *gtm) GetAsMap(ctx context.Context, name, domainName string) (*AsMap, error) {
logger := p.Log(ctx)
logger.Debug("GetAsMap")
var as AsMap
getURL := fmt.Sprintf("/config-gtm/v1/domains/%s/as-maps/%s", domainName, name)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, getURL, nil)
if err != nil {
return nil, fmt.Errorf("failed to create GetAsMap request: %w", err)
}
setVersionHeader(req, schemaVersion)
resp, err := p.Exec(req, &as)
if err != nil {
return nil, fmt.Errorf("GetAsMap request failed: %w", err)
}
if resp.StatusCode != http.StatusOK {
return nil, p.Error(resp)
}
return &as, nil
}
func (p *gtm) NewASAssignment(ctx context.Context, _ *AsMap, dcID int, nickname string) *AsAssignment {
logger := p.Log(ctx)
logger.Debug("NewAssignment")
asAssign := &AsAssignment{}
asAssign.DatacenterId = dcID
asAssign.Nickname = nickname
return asAssign
}
func (p *gtm) CreateAsMap(ctx context.Context, as *AsMap, domainName string) (*AsMapResponse, error) {
logger := p.Log(ctx)
logger.Debug("CreateAsMap")
// Use common code. Any specific validation needed?
return as.save(ctx, p, domainName)
}
func (p *gtm) UpdateAsMap(ctx context.Context, as *AsMap, domainName string) (*ResponseStatus, error) {
logger := p.Log(ctx)
logger.Debug("UpdateAsMap")
// common code
stat, err := as.save(ctx, p, domainName)
if err != nil {
return nil, err
}
return stat.Status, err
}
// save AsMap in given domain. Common path for Create and Update.
func (asm *AsMap) save(ctx context.Context, p *gtm, domainName string) (*AsMapResponse, error) {
if err := asm.Validate(); err != nil {
return nil, fmt.Errorf("AsMap validation failed. %w", err)
}
putURL := fmt.Sprintf("/config-gtm/v1/domains/%s/as-maps/%s", domainName, asm.Name)
req, err := http.NewRequestWithContext(ctx, http.MethodPut, putURL, nil)
if err != nil {
return nil, fmt.Errorf("failed to create AsMap request: %w", err)
}
var mapresp AsMapResponse
setVersionHeader(req, schemaVersion)
resp, err := p.Exec(req, &mapresp, asm)
if err != nil {
return nil, fmt.Errorf("AsMap request failed: %w", err)
}
if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusCreated {
return nil, p.Error(resp)
}
return &mapresp, nil
}
func (p *gtm) DeleteAsMap(ctx context.Context, as *AsMap, domainName string) (*ResponseStatus, error) {
logger := p.Log(ctx)
logger.Debug("DeleteAsMap")
if err := as.Validate(); err != nil {
return nil, fmt.Errorf("Resource validation failed. %w", err)
}
delURL := fmt.Sprintf("/config-gtm/v1/domains/%s/as-maps/%s", domainName, as.Name)
req, err := http.NewRequestWithContext(ctx, http.MethodDelete, delURL, nil)
if err != nil {
return nil, fmt.Errorf("failed to create Delete request: %w", err)
}
var mapresp ResponseBody
setVersionHeader(req, schemaVersion)
resp, err := p.Exec(req, &mapresp)
if err != nil {
return nil, fmt.Errorf("AsMap request failed: %w", err)
}
if resp.StatusCode != http.StatusOK {
return nil, p.Error(resp)
}
return mapresp.Status, nil
}