/
rosetta.go
300 lines (249 loc) · 7.92 KB
/
rosetta.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
package utils
import (
"errors"
"fmt"
"strings"
"github.com/Juniper/apstra-go-sdk/apstra"
)
const (
junOSTopLevelHierarchical = "top_level_hierarchical"
junOSTopLevelSetDelete = "top_level_set_delete"
junOSInterfaceLevelHierarchical = "interface_level_hierarchical"
junOSInterfaceLevelSet = "interface_level_set"
junOSInterfaceLevelDelete = "interface_level_delete"
asnAllocationUnique = "unique"
overlayControlProtocolStatic = "static"
refDesignDataCenter = "datacenter"
nodeDeployModeNotSet = "not_set"
resourceGroupNameVxlanVnIds = "vni_virtual_network_ids"
resourceGroupNameLeafL3PeerLinksIpv4 = "leaf_l3_peer_links"
resourceGroupNameLeafL3PeerLinksIpv6 = "leaf_l3_peer_links_ipv6"
)
type StringerWithFromString interface {
String() string
FromString(string) error
}
// StringersToFriendlyString accepts stringers (probably apstra-go-sdk
// string-able iota or enum types) and returns a string that better reflects
// terminology used by the Apstra web UI.
//
// For example, the API uses "distinct" where the web UI uses "unique".
// This function turns apstra.AsnAllocationSchemeDistinct into "unique".
func StringersToFriendlyString(in ...fmt.Stringer) string {
if len(in) == 0 {
return ""
}
switch in0 := in[0].(type) {
case apstra.AsnAllocationScheme:
return asnAllocationSchemeToFriendlyString(in0)
case apstra.ConfigletSection:
return configletSectionToFriendlyString(in0, in[1:]...)
case apstra.DeployMode:
return deployModeToFriendlyString(in0)
case apstra.OverlayControlProtocol:
return overlayControlProtocolToFriendlyString(in0)
case apstra.PolicyRuleProtocol:
return policyRuleProtocolToFriendlyString(in0)
case apstra.RefDesign:
return refDesignToFriendlyString(in0)
case apstra.ResourceGroupName:
return resourceGroupNameToFriendlyString(in0)
}
return in[0].String()
}
// ApiStringerFromFriendlyString attempts to populate a StringerWithFromString
// using one or more friendly 'in' strings. It is used to turn friendly strings
// used in the web UI into types used by the SDK and ultimately the API. For
// example, we can get apstra.AsnAllocationSchemeDistinct directly from a string
// by invoking apstra.AsnAllocationScheme.FromString("distinct"). But the web UI
// uses "unique", rather than "distinct". This method will be able to translate
// "unique" into an apstra.AsnAllocationScheme value.
func ApiStringerFromFriendlyString(target StringerWithFromString, in ...string) error {
if len(in) == 0 {
return errors.New("ApiStringerFromFriendlyString called with no string input")
}
switch target := target.(type) {
case *apstra.AsnAllocationScheme:
return asnAllocationSchemeFromFriendlyString(target, in...)
case *apstra.ConfigletSection:
return configletSectionFromFriendlyString(target, in...)
case *apstra.DeployMode:
return nodeDeployModeFromFriendlyString(target, in...)
case *apstra.OverlayControlProtocol:
return overlayControlProtocolFromFriendlyString(target, in...)
case *apstra.PolicyRuleProtocol:
return policyRuleProtocolFromFriendlyString(target, in[0])
case *apstra.RefDesign:
return refDesignFromFriendlyString(target, in...)
case *apstra.ResourceGroupName:
return resourceGroupNameFromFriendlyString(target, in...)
}
return target.FromString(in[0])
}
func asnAllocationSchemeToFriendlyString(in apstra.AsnAllocationScheme) string {
switch in {
case apstra.AsnAllocationSchemeDistinct:
return asnAllocationUnique
}
return in.String()
}
func configletSectionToFriendlyString(in apstra.ConfigletSection, additionalInfo ...fmt.Stringer) string {
if len(additionalInfo) == 0 {
return in.String()
}
os, ok := additionalInfo[0].(apstra.PlatformOS)
if !ok {
return in.String()
}
switch os {
case apstra.PlatformOSJunos:
switch in {
case apstra.ConfigletSectionSystem:
return junOSTopLevelHierarchical
case apstra.ConfigletSectionSetBasedSystem:
return junOSTopLevelSetDelete
case apstra.ConfigletSectionSetBasedInterface:
return junOSInterfaceLevelSet
case apstra.ConfigletSectionDeleteBasedInterface:
return junOSInterfaceLevelDelete
case apstra.ConfigletSectionInterface:
return junOSInterfaceLevelHierarchical
}
}
return in.String()
}
func deployModeToFriendlyString(in apstra.DeployMode) string {
switch in {
case apstra.DeployModeNone:
return nodeDeployModeNotSet
}
return in.String()
}
func overlayControlProtocolToFriendlyString(in apstra.OverlayControlProtocol) string {
switch in {
case apstra.OverlayControlProtocolNone:
return overlayControlProtocolStatic
}
return in.String()
}
func policyRuleProtocolToFriendlyString(in apstra.PolicyRuleProtocol) string {
return strings.ToLower(in.String())
}
func refDesignToFriendlyString(in apstra.RefDesign) string {
switch in {
case apstra.RefDesignTwoStageL3Clos:
return refDesignDataCenter
}
return in.String()
}
func resourceGroupNameToFriendlyString(in apstra.ResourceGroupName) string {
switch in {
case apstra.ResourceGroupNameLeafL3PeerLinkLinkIp4:
return resourceGroupNameLeafL3PeerLinksIpv4
case apstra.ResourceGroupNameLeafL3PeerLinkLinkIp6:
return resourceGroupNameLeafL3PeerLinksIpv6
case apstra.ResourceGroupNameVxlanVnIds:
return resourceGroupNameVxlanVnIds
}
return in.String()
}
func asnAllocationSchemeFromFriendlyString(target *apstra.AsnAllocationScheme, in ...string) error {
if len(in) == 0 {
return target.FromString("")
}
switch in[0] {
case asnAllocationUnique:
*target = apstra.AsnAllocationSchemeDistinct
default:
return target.FromString(in[0])
}
return nil
}
func configletSectionFromFriendlyString(target *apstra.ConfigletSection, in ...string) error {
switch len(in) {
case 0:
return target.FromString("")
case 1:
return target.FromString(in[0])
}
section := in[0]
platform := in[1]
if platform != apstra.PlatformOSJunos.String() {
return target.FromString(section)
}
switch section {
case junOSTopLevelHierarchical:
*target = apstra.ConfigletSectionSystem
case junOSInterfaceLevelHierarchical:
*target = apstra.ConfigletSectionInterface
case junOSTopLevelSetDelete:
*target = apstra.ConfigletSectionSetBasedSystem
case junOSInterfaceLevelDelete:
*target = apstra.ConfigletSectionDeleteBasedInterface
case junOSInterfaceLevelSet:
*target = apstra.ConfigletSectionSetBasedInterface
default:
return target.FromString(section)
}
return nil
}
func nodeDeployModeFromFriendlyString(target *apstra.DeployMode, in ...string) error {
if len(in) == 0 {
return target.FromString("")
}
switch in[0] {
case nodeDeployModeNotSet:
*target = apstra.DeployModeNone
default:
return target.FromString(in[0])
}
return nil
}
func overlayControlProtocolFromFriendlyString(target *apstra.OverlayControlProtocol, in ...string) error {
if len(in) == 0 {
return target.FromString("")
}
switch in[0] {
case overlayControlProtocolStatic:
*target = apstra.OverlayControlProtocolNone
default:
return target.FromString(in[0])
}
return nil
}
func policyRuleProtocolFromFriendlyString(target *apstra.PolicyRuleProtocol, s string) error {
t := apstra.PolicyRuleProtocols.Parse(strings.ToUpper(s))
if t == nil {
return fmt.Errorf("cannot parse PolicyRuleProtocol %q", s)
}
target.Value = t.Value
return nil
}
func refDesignFromFriendlyString(target *apstra.RefDesign, in ...string) error {
if len(in) == 0 {
return target.FromString("")
}
switch in[0] {
case refDesignDataCenter:
*target = apstra.RefDesignTwoStageL3Clos
default:
return target.FromString(in[0])
}
return nil
}
func resourceGroupNameFromFriendlyString(target *apstra.ResourceGroupName, in ...string) error {
if len(in) == 0 {
return target.FromString("")
}
switch in[0] {
case resourceGroupNameLeafL3PeerLinksIpv4:
*target = apstra.ResourceGroupNameLeafL3PeerLinkLinkIp4
case resourceGroupNameLeafL3PeerLinksIpv6:
*target = apstra.ResourceGroupNameLeafL3PeerLinkLinkIp6
case resourceGroupNameVxlanVnIds:
*target = apstra.ResourceGroupNameVxlanVnIds
default:
return target.FromString(in[0])
}
return nil
}