-
Notifications
You must be signed in to change notification settings - Fork 7
/
rest_acltemplate.go
163 lines (148 loc) · 5.1 KB
/
rest_acltemplate.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
package ne
import (
"net/http"
"net/url"
"github.com/equinix/ne-go/internal/api"
"github.com/equinix/rest-go"
)
// CreateACLTemplate creates new ACL template with a given model
// On successful creation, template's UUID is returned
func (c RestClient) CreateACLTemplate(template ACLTemplate) (*string, error) {
path := "/ne/v1/aclTemplates"
reqBody := mapACLTemplateDomainToAPI(template)
req := c.R().SetBody(&reqBody)
resp, err := c.Do(http.MethodPost, path, req)
if err != nil {
return nil, err
}
uuid, err := getResourceIDFromLocationHeader(resp)
if err != nil {
return nil, err
}
return uuid, nil
}
// GetACLTemplates retrieves list of all ACL templates along with their details
func (c RestClient) GetACLTemplates() ([]ACLTemplate, error) {
path := "/ne/v1/aclTemplates"
content, err := c.GetOffsetPaginated(path, &api.ACLTemplatesResponse{},
rest.DefaultOffsetPagingConfig())
if err != nil {
return nil, err
}
transformed := make([]ACLTemplate, len(content))
for i := range content {
transformed[i] = mapACLTemplateAPIToDomain(content[i].(api.ACLTemplate))
}
return transformed, nil
}
// GetACLTemplate retrieves ACL template with a given UUID
func (c RestClient) GetACLTemplate(uuid string) (*ACLTemplate, error) {
path := "/ne/v1/aclTemplates/" + url.PathEscape(uuid)
respBody := api.ACLTemplate{}
req := c.R().SetResult(&respBody)
if err := c.Execute(req, http.MethodGet, path); err != nil {
return nil, err
}
template := mapACLTemplateAPIToDomain(respBody)
return &template, nil
}
// ReplaceACLTemplate replaces ACL template under given UUID with
// a new one with a given model
func (c RestClient) ReplaceACLTemplate(uuid string, template ACLTemplate) error {
path := "/ne/v1/aclTemplates/" + url.PathEscape(uuid)
updateTemplate := ACLTemplate{
Name: template.Name,
Description: template.Description,
MetroCode: template.MetroCode,
InboundRules: template.InboundRules,
}
reqBody := mapACLTemplateDomainToAPI(updateTemplate)
req := c.R().SetBody(&reqBody)
if err := c.Execute(req, http.MethodPut, path); err != nil {
return err
}
return nil
}
// DeleteACLTemplate removes ACL template with a given UUID
func (c RestClient) DeleteACLTemplate(uuid string) error {
path := "/ne/v1/aclTemplates/" + url.PathEscape(uuid)
if err := c.Execute(c.R(), http.MethodDelete, path); err != nil {
return err
}
return nil
}
func mapACLTemplateDomainToAPI(template ACLTemplate) api.ACLTemplate {
return api.ACLTemplate{
UUID: template.UUID,
Name: template.Name,
Description: template.Description,
MetroCode: template.MetroCode,
DeviceACLStatus: template.DeviceACLStatus,
InboundRules: mapACLTemplateInboundRulesDomainToAPI(template.InboundRules),
ProjectID: template.ProjectID,
}
}
func mapACLTemplateInboundRulesDomainToAPI(rules []ACLTemplateInboundRule) []api.ACLTemplateInboundRule {
transformed := make([]api.ACLTemplateInboundRule, len(rules))
for i := range rules {
transformed[i] = mapACLTemplateInboundRuleDomainToAPI(rules[i])
}
return transformed
}
func mapACLTemplateInboundRuleDomainToAPI(rule ACLTemplateInboundRule) api.ACLTemplateInboundRule {
return api.ACLTemplateInboundRule{
SrcType: rule.SrcType,
Protocol: rule.Protocol,
SrcPort: rule.SrcPort,
DstPort: rule.DstPort,
Subnet: rule.Subnet,
Subnets: rule.Subnets,
SeqNO: rule.SeqNo,
Description: rule.Description,
}
}
func mapACLTemplateAPIToDomain(apiTemplate api.ACLTemplate) ACLTemplate {
return ACLTemplate{
UUID: apiTemplate.UUID,
Name: apiTemplate.Name,
Description: apiTemplate.Description,
MetroCode: apiTemplate.MetroCode,
DeviceACLStatus: apiTemplate.DeviceACLStatus,
InboundRules: mapACLTemplateInboundRulesAPIToDomain(apiTemplate.InboundRules),
DeviceDetails: mapACLTemplateDeviceDetailsAPIToDomain(apiTemplate.DeviceDetails),
ProjectID: apiTemplate.ProjectID,
}
}
func mapACLTemplateInboundRulesAPIToDomain(apiRules []api.ACLTemplateInboundRule) []ACLTemplateInboundRule {
transformed := make([]ACLTemplateInboundRule, len(apiRules))
for i := range apiRules {
transformed[i] = mapACLTemplateInboundRuleAPIToDomain(apiRules[i])
}
return transformed
}
func mapACLTemplateInboundRuleAPIToDomain(apiRule api.ACLTemplateInboundRule) ACLTemplateInboundRule {
return ACLTemplateInboundRule{
SrcType: apiRule.SrcType,
Protocol: apiRule.Protocol,
SrcPort: apiRule.SrcPort,
DstPort: apiRule.DstPort,
Subnets: apiRule.Subnets,
Subnet: apiRule.Subnet,
SeqNo: apiRule.SeqNO,
Description: apiRule.Description,
}
}
func mapACLTemplateDeviceDetailsAPIToDomain(apiRules []api.ACLTemplateDeviceDetails) []ACLTemplateDeviceDetails {
transformed := make([]ACLTemplateDeviceDetails, len(apiRules))
for i := range apiRules {
transformed[i] = mapACLTemplateDeviceDetailAPIToDomain(apiRules[i])
}
return transformed
}
func mapACLTemplateDeviceDetailAPIToDomain(apiRule api.ACLTemplateDeviceDetails) ACLTemplateDeviceDetails {
return ACLTemplateDeviceDetails{
UUID: apiRule.UUID,
Name: apiRule.Name,
ACLStatus: apiRule.ACLStatus,
}
}