-
Notifications
You must be signed in to change notification settings - Fork 13
/
catalogitem.go
213 lines (180 loc) · 5.46 KB
/
catalogitem.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
package apic
// TODO - this file should be able to be removed once Unified Catalog support has been removed
// the category stuff within this file may need to be saved, perhaps moved
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"strconv"
coreapi "github.com/Axway/agent-sdk/pkg/api"
v1 "github.com/Axway/agent-sdk/pkg/apic/apiserver/models/api/v1"
catalog "github.com/Axway/agent-sdk/pkg/apic/apiserver/models/catalog/v1alpha1"
utilerrors "github.com/Axway/agent-sdk/pkg/util/errors"
"github.com/tidwall/gjson"
)
// getCatalogItemIDForConsumerInstance -
func (c *ServiceClient) getCatalogItemIDForConsumerInstance(instanceID string) (string, error) {
headers, err := c.createHeader()
if err != nil {
return "", err
}
params := map[string]string{
"query": fmt.Sprintf("relationships.type==API_SERVER_CONSUMER_INSTANCE_ID;relationships.value==%s", instanceID),
}
request := coreapi.Request{
Method: coreapi.GET,
URL: c.cfg.GetCatalogItemsURL(),
Headers: headers,
QueryParams: params,
}
response, err := c.apiClient.Send(request)
if err != nil {
return "", err
}
if response.Code != http.StatusOK {
responseErr := readResponseErrors(response.Code, response.Body)
return "", utilerrors.Wrap(ErrRequestQuery, responseErr)
}
// the response is an array of IDs
ids := gjson.Get(string(response.Body), "#.id")
if !ids.Exists() {
return "", nil
}
// the array should only contain 1 item,
// since we have asked for a specific one
catalogIDs := make([]string, 0)
json.Unmarshal([]byte(ids.Raw), &catalogIDs)
if len(catalogIDs) == 0 {
return "", errors.New("Unable to find catalogID for consumerInstance " + instanceID)
}
return catalogIDs[0], nil
}
// GetCatalogItemName -
func (c *ServiceClient) GetCatalogItemName(ID string) (string, error) {
headers, err := c.createHeader()
if err != nil {
return "", err
}
request := coreapi.Request{
Method: coreapi.GET,
URL: c.cfg.GetCatalogItemByIDURL(ID),
Headers: headers,
}
response, err := c.apiClient.Send(request)
if err != nil {
return "", err
}
if response.Code != http.StatusOK {
responseErr := readResponseErrors(response.Code, response.Body)
return "", utilerrors.Wrap(ErrRequestQuery, responseErr)
}
name := gjson.Get(string(response.Body), "name").String()
return name, nil
}
func (c *ServiceClient) getSubscriptionsForCatalogItem(states []string, catalogItemID string) ([]CentralSubscription, error) {
queryParams := make(map[string]string)
searchQuery := ""
for _, state := range states {
if searchQuery != "" {
searchQuery += ","
}
searchQuery += "state==" + state
}
queryParams["query"] = searchQuery
subscriptions, err := c.sendSubscriptionsRequest(c.cfg.GetCatalogItemSubscriptionsURL(catalogItemID), queryParams)
if err != nil {
if err.Error() != strconv.Itoa(http.StatusNotFound) {
return nil, err
}
return make([]CentralSubscription, 0), nil
}
centralSubscriptions := make([]CentralSubscription, 0)
json.Unmarshal(subscriptions, ¢ralSubscriptions)
return centralSubscriptions, nil
}
func (c *ServiceClient) getSubscriptionDefinitionPropertiesForCatalogItem(catalogItemID, propertyKey string) (SubscriptionSchema, error) {
headers, err := c.createHeader()
if err != nil {
return nil, err
}
request := coreapi.Request{
Method: coreapi.GET,
URL: fmt.Sprintf("%s/%s", c.cfg.GetCatalogItemSubscriptionDefinitionPropertiesURL(catalogItemID), propertyKey),
Headers: headers,
}
response, err := c.apiClient.Send(request)
if err != nil {
return nil, err
}
if response.Code != http.StatusOK {
responseErr := readResponseErrors(response.Code, response.Body)
return nil, utilerrors.Wrap(ErrRequestQuery, responseErr)
}
ss := NewSubscriptionSchema("")
err = json.Unmarshal(response.Body, &ss)
if err != nil {
return nil, err
}
return ss, nil
}
func (c *ServiceClient) updateSubscriptionDefinitionPropertiesForCatalogItem(catalogItemID, propertyKey string, subscriptionSchema SubscriptionSchema) error {
headers, err := c.createHeader()
if err != nil {
return err
}
body, err := json.Marshal(subscriptionSchema)
if err != nil {
return err
}
request := coreapi.Request{
Method: coreapi.PUT,
URL: fmt.Sprintf("%s/%s", c.cfg.GetCatalogItemSubscriptionDefinitionPropertiesURL(catalogItemID), propertyKey),
Headers: headers,
Body: body,
}
response, err := c.apiClient.Send(request)
if err != nil {
return err
}
if response.Code != http.StatusOK {
responseErr := readResponseErrors(response.Code, response.Body)
return utilerrors.Wrap(ErrRequestQuery, responseErr)
}
return nil
}
// CreateCategory - Adds a new category
func (c *ServiceClient) CreateCategory(title string) (*catalog.Category, error) {
spec := catalog.CategorySpec{
Description: "",
}
category := catalog.Category{
ResourceMeta: v1.ResourceMeta{Title: title},
Spec: spec,
}
buffer, err := json.Marshal(category)
if err != nil {
return nil, err
}
headers, err := c.createHeader()
if err != nil {
return nil, err
}
request := coreapi.Request{
Method: coreapi.POST,
URL: c.cfg.GetCategoriesURL(),
Headers: headers,
Body: buffer,
}
response, err := c.apiClient.Send(request)
if err != nil {
return nil, err
}
if response.Code != http.StatusCreated {
responseErr := readResponseErrors(response.Code, response.Body)
return nil, utilerrors.Wrap(ErrRequestQuery, responseErr)
}
var newCategory catalog.Category
err = json.Unmarshal(response.Body, &newCategory)
return &newCategory, err
}