-
Notifications
You must be signed in to change notification settings - Fork 0
/
definitions.go
138 lines (114 loc) · 3.47 KB
/
definitions.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
package main
import (
"encoding/xml"
"regexp"
"strings"
"github.com/oriser/regroup"
)
// APIProxy structures
// APIProxy - APIGEE API Proxy definition
type APIProxy struct {
Basepaths string `xml:"Basepaths"`
Version ConfigurationVersion `xml:"ConfigurationVersion"`
CreatedAt int `xml:"CreatedAt"`
CreatedBy string `xml:"CreatedBy"`
Description string `xml:"Description"`
DisplayName string `xml:"DisplayName"`
LastModifiedAt int `xml:"LastModifiedAt"`
LastModifiedBy string `xml:"LastModifiedBy"`
ManifestVersion string `xml:"ManifestVersion"`
Policies Policies `xml:"Policies"`
ProxyEndpoints ProxyEndpoints `xml:"ProxyEndpoints"`
Resources string `xml:"Resources"`
Spec string `xml:"Spec"`
TargetServers string `xml:"TargetServers"`
TargetEndpoints string `xml:"TargetEndpoints"`
parsedEndpoints []ProxyEndpoint
}
// ConfigurationVersion - APIGEE API Proxy version
type ConfigurationVersion struct {
Major string `xml:"majorVersion,attr"`
Minor string `xml:"minorVersion,attr"`
}
// Policies - APIGEE API Proxy Policy filenames
type Policies struct {
Policies []string `xml:"Policy"`
}
// ProxyEndpoints - APIGEE API Proxy Endpoint filenames
type ProxyEndpoints struct {
ProxyEndpoint []string `xml:"ProxyEndpoint"`
}
// ProxyEndpoint - APIGEE Proxy Endpoint file structure
type ProxyEndpoint struct {
Name string `xml:"name,attr"`
PreFlow PreFlow `xml:"PreFlow"`
Flows Flows `xml:"Flows"`
}
// PreFlow - APIGEE Proxy endpoint preflow
type PreFlow struct {
Name string `xml:"name,attr"`
Request Request `xml:"Request"`
Response Response `xml:"Response"`
}
// Request - APIGEE Proxy request steps
type Request struct {
Step []Step `xml:"Step"`
}
// Response - APIGEE Proxy response steps
type Response struct {
Step []Step `xml:"Step"`
}
// Step - APIGEE Proxy step names
type Step struct {
Name string `xml:"Name"`
}
// Flows - APIGEE Proxy flows
type Flows struct {
Flow []Flow `xml:"Flow"`
}
// Flow - APIGEE proxy flow
type Flow struct {
Name string `xml:"name,attr"`
Description string `xml:"Description"`
Request Request `xml:"Request"`
Response Response `xml:"Response"`
Conditions Conditions `xml:"Condition"`
}
// Conditions - array of conditions
type Conditions struct {
Condition []Condition
}
// Condition -- parsed representation of APIGEE condition
type Condition struct {
Variable string
Operator string
Value string
}
//UnmarshalXML - custom XML unmarshall to parse APIGEE flow conditions
func (c *Conditions) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
var el string
if err := d.DecodeElement(&el, &start); err != nil {
return err
}
if el == "null" {
return nil
}
// Split all conditions
re := regexp.MustCompile("\\).*\\(")
conditions := re.Split(el, -1)
// iterate conditions
for _, condition := range conditions {
r := regroup.MustCompile("^\\({0,1}(?P<var>[a-z\\.]*) (?P<op>.*) (?P<val>.*\")\\){0,1}$")
matches, err := r.Groups(condition)
if err != nil {
return err
}
// add each condition to the array
c.Condition = append(c.Condition, Condition{
Variable: strings.Trim(matches["var"], "\""),
Operator: strings.Trim(matches["op"], "\""),
Value: strings.Trim(matches["val"], "\""),
})
}
return nil
}