/
proxy.go
161 lines (135 loc) · 4.04 KB
/
proxy.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
package apigee
import (
"archive/zip"
"bytes"
"encoding/json"
"encoding/xml"
"fmt"
"io"
"net/http"
"github.com/Axway/agents-apigee/client/pkg/apigee/models"
)
// proxyXML
type proxyXML struct {
XMLName xml.Name `xml:"ProxyEndpoint"`
HTTPProxyConnection *HTTPProxyConnection `xml:"HTTPProxyConnection"`
}
// HTTPProxyConnection
type HTTPProxyConnection struct {
XMLName xml.Name `xml:"HTTPProxyConnection"`
BasePath string `xml:"BasePath"`
VirtualHost string `xml:"VirtualHost"`
}
// Products
type Proxies []string
// GetAllProxies - get all proxies
func (a *ApigeeClient) GetAllProxies() (Proxies, error) {
response, err := a.newRequest(http.MethodGet, fmt.Sprintf("%s/apis", a.orgURL),
WithDefaultHeaders(),
).Execute()
if err != nil {
return nil, err
}
proxies := Proxies{}
err = json.Unmarshal(response.Body, &proxies)
if err != nil {
return nil, err
}
return proxies, nil
}
// GetProxy - get a proxy with a name
func (a *ApigeeClient) GetProxy(proxyName string) (*models.ApiProxy, error) {
response, err := a.newRequest(http.MethodGet, fmt.Sprintf("%s/apis/%s", a.orgURL, proxyName),
WithDefaultHeaders(),
).Execute()
if err != nil {
return nil, err
}
proxy := &models.ApiProxy{}
err = json.Unmarshal(response.Body, proxy)
if err != nil {
return nil, err
}
return proxy, nil
}
// GetRevision - get a revision of a proxy with a name
func (a *ApigeeClient) GetRevision(proxyName, revision string) (*models.ApiProxyRevision, error) {
response, err := a.newRequest(http.MethodGet, fmt.Sprintf("%s/apis/%s/revisions/%s", a.orgURL, proxyName, revision),
WithDefaultHeaders(),
).Execute()
if err != nil {
return nil, err
}
proxyRevision := &models.ApiProxyRevision{}
json.Unmarshal(response.Body, proxyRevision)
if err != nil {
return nil, err
}
return proxyRevision, nil
}
// GetRevisionConnectionType - get a revision bundle and open the proxy config file
func (a *ApigeeClient) GetRevisionConnectionType(proxyName, revision string) (*HTTPProxyConnection, error) {
response, err := a.newRequest(http.MethodGet, fmt.Sprintf("%s/apis/%s/revisions/%s", a.orgURL, proxyName, revision),
WithDefaultHeaders(),
WithQueryParam("format", "bundle"),
).Execute()
if err != nil {
return nil, err
}
// response is a zip file, lets open it and find the proxy config file
zipReader, err := zip.NewReader(bytes.NewReader(response.Body), int64(len(response.Body)))
if err != nil {
return nil, err
}
// Read all the files from the zip archive
var fileBytes []byte
for _, zipFile := range zipReader.File {
if zipFile.Name != "apiproxy/proxies/default.xml" {
continue
}
fileBytes, err = readZipFile(zipFile)
if err != nil {
return nil, err
}
break
}
if len(fileBytes) == 0 {
return nil, fmt.Errorf("could not find the proxy configuration file in the api revision bundle")
}
data := &proxyXML{}
xml.Unmarshal(fileBytes, data)
return data.HTTPProxyConnection, nil
}
func readZipFile(zf *zip.File) ([]byte, error) {
f, err := zf.Open()
if err != nil {
return nil, err
}
defer f.Close()
return io.ReadAll(f)
}
// GetProxy - get a proxy with a name
func (a *ApigeeClient) GetRevisionResourceFile(proxyName, revision, resourceType, resourceName string) ([]byte, error) {
response, err := a.newRequest(http.MethodGet, fmt.Sprintf("%s/apis/%s/revisions/%s/resourcefiles/%s/%s", a.orgURL, proxyName, revision, resourceType, resourceName),
WithDefaultHeaders(),
).Execute()
if err != nil {
return nil, err
}
return response.Body, nil
}
// GetRevisionPolicyByName - get the details about a named policy on a revision
func (a *ApigeeClient) GetRevisionPolicyByName(proxyName, revision, policyName string) (*PolicyDetail, error) {
response, err := a.newRequest(http.MethodGet, fmt.Sprintf("%s/apis/%s/revisions/%s/policies/%s", a.orgURL, proxyName, revision, policyName),
WithDefaultHeaders(),
).Execute()
if err != nil {
return nil, err
}
policyDetails := &PolicyDetail{}
json.Unmarshal(response.Body, policyDetails)
if err != nil {
return nil, err
}
return policyDetails, nil
}