/
two_stage_l3_clos_configlet.go
178 lines (160 loc) · 4.68 KB
/
two_stage_l3_clos_configlet.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 apstra
import (
"context"
"fmt"
"net/http"
)
const (
apiUrlBlueprintConfiglets = apiUrlBlueprintById + apiUrlPathDelim + "configlets"
apiUrlBlueprintConfigletsPrefix = apiUrlBlueprintConfiglets + apiUrlPathDelim
apiUrlBlueprintConfigletsById = apiUrlBlueprintConfigletsPrefix + "%s"
)
type rawTwoStageL3ClosConfigletData struct {
Label string `json:"label"`
Condition string `json:"condition"`
Data rawConfigletData `json:"configlet"`
}
type TwoStageL3ClosConfigletData struct {
Label string
Condition string
Data *ConfigletData
}
type rawTwoStageL3ClosConfiglet struct {
Id ObjectId `json:"id"`
Condition string `json:"condition"`
Label string `json:"label"`
Data rawConfigletData `json:"configlet"`
}
type TwoStageL3ClosConfiglet struct {
Id ObjectId
Data *TwoStageL3ClosConfigletData
}
func (o *TwoStageL3ClosConfigletData) raw() *rawTwoStageL3ClosConfigletData {
return &rawTwoStageL3ClosConfigletData{
Label: o.Label,
Condition: o.Condition,
Data: *o.Data.raw(),
}
}
func (o *TwoStageL3ClosConfiglet) raw() *rawTwoStageL3ClosConfiglet {
d := o.Data.raw()
return &rawTwoStageL3ClosConfiglet{
Id: o.Id,
Label: d.Label,
Condition: d.Condition,
Data: d.Data,
}
}
func (o *rawTwoStageL3ClosConfiglet) polish() (*TwoStageL3ClosConfiglet, error) {
d, err := o.Data.polish()
if err != nil {
return nil, err
}
return &TwoStageL3ClosConfiglet{
Id: o.Id,
Data: &TwoStageL3ClosConfigletData{
Data: d,
Condition: o.Condition,
Label: o.Label,
},
}, nil
}
func (o *TwoStageL3ClosClient) getAllConfiglets(ctx context.Context) ([]rawTwoStageL3ClosConfiglet, error) {
response := &struct {
Items []rawTwoStageL3ClosConfiglet `json:"items"`
}{}
err := o.client.talkToApstra(ctx, &talkToApstraIn{
method: http.MethodGet,
urlStr: fmt.Sprintf(apiUrlBlueprintConfiglets, o.blueprintId.String()),
apiResponse: response,
})
if err != nil {
return nil, convertTtaeToAceWherePossible(err)
}
return response.Items, nil
}
func (o *TwoStageL3ClosClient) getAllConfigletIds(ctx context.Context) ([]ObjectId, error) {
configlets, err := o.getAllConfiglets(ctx)
if err != nil {
return nil, convertTtaeToAceWherePossible(err)
}
ids := make([]ObjectId, len(configlets))
for i, c := range configlets {
ids[i] = c.Id
}
return ids, nil
}
func (o *TwoStageL3ClosClient) getConfiglet(ctx context.Context, id ObjectId) (*rawTwoStageL3ClosConfiglet, error) {
response := &rawTwoStageL3ClosConfiglet{}
err := o.client.talkToApstra(ctx, &talkToApstraIn{
method: http.MethodGet,
urlStr: fmt.Sprintf(apiUrlBlueprintConfigletsById, o.blueprintId.String(), id.String()),
apiResponse: response,
})
if err != nil {
return nil, convertTtaeToAceWherePossible(err)
}
return response, nil
}
func (o *TwoStageL3ClosClient) getConfigletByName(ctx context.Context, name string) (*rawTwoStageL3ClosConfiglet,
error) {
cgs, err := o.getAllConfiglets(ctx)
if err != nil {
return nil, convertTtaeToAceWherePossible(err)
}
idx := -1
for i, t := range cgs {
if t.Label == name {
if idx == -1 {
idx = i
} else { // This is clearly the second occurrence
return nil, ClientErr{
errType: ErrMultipleMatch,
err: fmt.Errorf("name '%s' does not uniquely identify a configlet", name),
}
}
}
}
if idx != -1 {
return &cgs[idx], nil
}
return nil, ClientErr{
errType: ErrNotfound,
err: fmt.Errorf("no Configlet with name '%s' found", name),
}
}
func (o *TwoStageL3ClosClient) createConfiglet(ctx context.Context, in *rawTwoStageL3ClosConfigletData) (ObjectId,
error) {
response := &objectIdResponse{}
if len(in.Label) == 0 {
in.Label = in.Data.DisplayName
}
err := o.client.talkToApstra(ctx, &talkToApstraIn{
method: http.MethodPost,
urlStr: fmt.Sprintf(apiUrlBlueprintConfiglets, o.blueprintId.String()),
apiInput: in,
apiResponse: response,
})
if err != nil {
return "", convertTtaeToAceWherePossible(err)
}
return response.Id, nil
}
func (o *TwoStageL3ClosClient) updateConfiglet(ctx context.Context, id ObjectId,
in *rawTwoStageL3ClosConfigletData) error {
err := o.client.talkToApstra(ctx, &talkToApstraIn{
method: http.MethodPut,
urlStr: fmt.Sprintf(apiUrlBlueprintConfigletsById, o.blueprintId.String(), id),
apiInput: in,
})
if err != nil {
return convertTtaeToAceWherePossible(err)
}
return nil
}
func (o *TwoStageL3ClosClient) deleteConfiglet(ctx context.Context, id ObjectId) error {
return o.client.talkToApstra(ctx, &talkToApstraIn{
method: http.MethodDelete,
urlStr: fmt.Sprintf(apiUrlBlueprintConfigletsById, o.blueprintId.String(), id.String()),
})
}