-
Notifications
You must be signed in to change notification settings - Fork 165
/
adminapi.go
129 lines (108 loc) · 3.15 KB
/
adminapi.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
package e2e
// Copyright (c) Microsoft Corporation.
// Licensed under the Apache License 2.0.
import (
"bytes"
"context"
"crypto/tls"
"encoding/json"
"errors"
"io"
"net/http"
"net/url"
. "github.com/onsi/gomega"
"github.com/Azure/ARO-RP/pkg/api/admin"
"github.com/Azure/ARO-RP/pkg/env"
)
func adminRequest(ctx context.Context, method, path string, params url.Values, strict bool, in, out interface{}) (*http.Response, error) {
if !env.IsLocalDevelopmentMode() {
return nil, errors.New("only development RP mode is supported")
}
if params == nil {
params = url.Values{}
}
params.Add("api-version", admin.APIVersion)
adminAPIBaseURI := "https://localhost:8443"
adminURL, err := url.Parse(adminAPIBaseURI + path)
if err != nil {
return nil, err
}
adminURL.RawQuery = params.Encode()
req, err := http.NewRequestWithContext(ctx, method, adminURL.String(), nil)
if err != nil {
return nil, err
}
if in != nil {
buf := &bytes.Buffer{}
err := json.NewEncoder(buf).Encode(in)
if err != nil {
return nil, err
}
req.Body = io.NopCloser(buf)
req.Header.Set("Content-Type", "application/json")
}
client := &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
},
}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer func() {
_, _ = resp.Body.Read(nil)
_ = resp.Body.Close()
}()
if out != nil && resp.Header.Get("Content-Type") == "application/json" {
decoder := json.NewDecoder(resp.Body)
// If strict is set, enable DisallowUnknownFields. This is used to
// verify that the response doesn't contain any fields that are not
// defined, namely systemData.
if strict {
decoder.DisallowUnknownFields()
}
return resp, decoder.Decode(&out)
} else if out != nil && resp.Header.Get("Content-Type") == "text/plain" {
strOut := out.(*string)
p, err := io.ReadAll(resp.Body)
if err == nil {
*strOut = string(p)
}
return resp, err
}
return resp, nil
}
// adminGetCluster returns admin representation of an ARO cluster
func adminGetCluster(g Gomega, ctx context.Context, resourceID string) *admin.OpenShiftCluster {
var oc admin.OpenShiftCluster
resp, err := adminRequest(ctx, http.MethodGet, resourceID, nil, true, nil, &oc)
g.Expect(err).NotTo(HaveOccurred())
g.Expect(resp.StatusCode).To(Equal(http.StatusOK))
return &oc
}
// adminListClusters returns a list of ARO clusters in admin representation.
// It handles pagination: function returns all the clusters from all pages.
func adminListClusters(g Gomega, ctx context.Context, path string) []*admin.OpenShiftCluster {
ocs := make([]*admin.OpenShiftCluster, 0)
params := url.Values{}
for {
var list admin.OpenShiftClusterList
resp, err := adminRequest(ctx, http.MethodGet, path, params, true, nil, &list)
g.Expect(err).NotTo(HaveOccurred())
g.Expect(resp.StatusCode).To(Equal(http.StatusOK))
ocs = append(ocs, list.OpenShiftClusters...)
if list.NextLink == "" {
break
}
params = nextParams(g, list.NextLink)
}
return ocs
}
func nextParams(g Gomega, nextLink string) url.Values {
url, err := url.Parse(nextLink)
g.Expect(err).NotTo(HaveOccurred())
return url.Query()
}