This repository has been archived by the owner on Dec 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
endpoints.go
178 lines (167 loc) · 5.95 KB
/
endpoints.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
/*
* Licensed to the Apache Software Foundation (ASF) under one or more contributor license
* agreements. See the NOTICE file distributed with this work for additional information regarding
* copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the License. You may obtain a
* copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/
package common
import (
"encoding/json"
"net/http"
"strconv"
"strings"
"code.cloudfoundry.org/cli/cf/errors"
"github.com/gemfire/tanzu-gemfire-management-cf-plugin/domain"
"github.com/gemfire/tanzu-gemfire-management-cf-plugin/impl"
"github.com/gemfire/tanzu-gemfire-management-cf-plugin/impl/common/format"
)
// GetEndPoints retrieves available endpoint from the Swagger endpoint on the Geode/PCC locator
func GetEndPoints(commandData *domain.CommandData, processRequest impl.RequestHelper) error {
var urlResponse, apiDocURL string
var statusCode int
var err error
var responseMap map[string]interface{}
fallbackCodes := "401 403 404 407"
apiDocURLs := []string{
commandData.ConnnectionData.LocatorAddress + "/management/",
commandData.ConnnectionData.LocatorAddress + "/management/v1/api-docs",
commandData.ConnnectionData.LocatorAddress + "/management/experimental/api-docs",
}
for pos, URL := range apiDocURLs {
apiDocURL = URL
request, err := http.NewRequest("GET", URL, nil)
if err != nil {
return err
}
urlResponse, statusCode, err = processRequest(request)
if err != nil {
return errors.New("Unable to reach " + URL + ". Error: " + err.Error())
}
if !strings.Contains(fallbackCodes, strconv.Itoa(statusCode)) {
if statusCode == 200 {
if pos == 0 {
err = json.Unmarshal([]byte(urlResponse), &responseMap)
if err != nil {
return errors.New("Unable to parse response: " + urlResponse + ". Error: " + err.Error())
}
latestURL, Ok := responseMap["latest"]
if Ok {
apiDocURL = format.GetString(latestURL)
request, err := http.NewRequest("GET", apiDocURL, nil)
if err != nil {
return err
}
urlResponse, statusCode, err = processRequest(request)
if err != nil {
return errors.New("Unable to reach " + apiDocURL + ": " + err.Error())
}
} else {
return errors.New("Unable to determine latest API endpoint: " + urlResponse + ".")
}
}
break
}
return errors.New("Unable to reach " + URL + ". Status Code: " + strconv.Itoa(statusCode))
}
}
if statusCode != 200 {
return errors.New("Unable to reach " + apiDocURL + ". Status Code: " + strconv.Itoa(statusCode))
}
var apiPaths domain.RestAPI
err = json.Unmarshal([]byte(urlResponse), &apiPaths)
if err != nil {
return errors.New("invalid response " + urlResponse + ": " + err.Error())
}
commandData.ConnnectionData.UseToken = apiPaths.Info.TokenEnabled == "true"
commandData.AvailableEndpoints = make(map[string]domain.RestEndPoint)
for url, v := range apiPaths.Paths {
for methodType := range v {
var endpoint domain.RestEndPoint
endpoint.URL = url
endpoint.HTTPMethod = methodType
endpoint.Consumes = apiPaths.Paths[url][methodType].Consumes
endpoint.CommandName = apiPaths.Paths[url][methodType].CommandName
endpoint.JQFilter = apiPaths.Paths[url][methodType].JQFilter
endpoint.Parameters = []domain.RestAPIParam{}
endpoint.Parameters = apiPaths.Paths[url][methodType].Parameters
for index, parameter := range endpoint.Parameters {
if parameter.In == "body" {
schemaName := strings.ReplaceAll(parameter.Schema["$ref"], "#/definitions/", "")
if schemaName != "" {
endpoint.Parameters[index].BodyDefinition = buildStructure(apiPaths.Definitions[schemaName].Properties, apiPaths.Definitions)
}
}
}
commandData.AvailableEndpoints[endpoint.CommandName] = endpoint
}
}
return nil
}
func buildStructure(propertiesMap map[string]domain.PropertyDetail, definitions map[string]domain.DefinitionDetail) (structure map[string]interface{}) {
structure = make(map[string]interface{})
for key, property := range propertiesMap {
switch property.Type {
case "string":
if len(property.Enum) > 0 {
structure[key] = "ENUM, one of: " + strings.Join(property.Enum, ", ")
} else {
structure[key] = "string-value"
}
case "integer":
structure[key] = 42
case "boolean":
structure[key] = true
case "object":
structure[key] = map[string]string{"name": "value"}
case "array":
structure[key] = generateSampleArray(property.Items, definitions)
case "":
if len(property.Ref) > 0 {
refName := strings.ReplaceAll(property.Ref, "#/definitions/", "")
if refName != "" {
if refName == "DeclarableType" {
structure[key] = "DeclarableType"
} else {
subStructure := buildStructure(definitions[refName].Properties, definitions)
structure[key] = subStructure
}
}
}
default:
structure[key] = "unknown"
}
}
return
}
func generateSampleArray(itemMap map[string]string, definitions map[string]domain.DefinitionDetail) interface{} {
switch itemMap["type"] {
case "string":
return []string{"stringOne", "stringTwo"}
case "integer":
return []int{41, 42}
case "boolean":
return []bool{true, false}
case "":
if len(itemMap["$ref"]) > 0 {
refName := strings.ReplaceAll(itemMap["$ref"], "#/definitions/", "")
if refName != "" {
if refName == "DeclarableType" {
return []string{"DeclarableType", "DeclarableType"}
}
subStructure := buildStructure(definitions[refName].Properties, definitions)
return []interface{}{subStructure}
}
}
default:
return []interface{}{}
}
return []interface{}{}
}