This repository has been archived by the owner on Dec 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
resourceUtils.go
465 lines (384 loc) · 19.5 KB
/
resourceUtils.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
package api
import (
"context"
"encoding/json"
"net/http"
"net/url"
"strings"
"github.com/keptn/go-utils/pkg/api/models"
v2 "github.com/keptn/go-utils/pkg/api/utils/v2"
"github.com/keptn/go-utils/pkg/common/httputils"
)
const pathToResource = "/resource"
const pathToService = "/service"
const pathToStage = "/stage"
const configurationServiceBaseURL = "resource-service"
var ResourceNotFoundError = v2.ResourceNotFoundError
type ResourcesV1Interface interface {
// CreateResources creates a resource for the specified entity.
CreateResources(project string, stage string, service string, resources []*models.Resource) (*models.EventContext, *models.Error)
// CreateProjectResources creates multiple project resources.
CreateProjectResources(project string, resources []*models.Resource) (string, error)
// GetProjectResource retrieves a project resource from the configuration service.
// Deprecated: use GetResource instead.
GetProjectResource(project string, resourceURI string) (*models.Resource, error)
// UpdateProjectResource updates a project resource.
// Deprecated: use UpdateResource instead.
UpdateProjectResource(project string, resource *models.Resource) (string, error)
// DeleteProjectResource deletes a project resource.
// Deprecated: use DeleteResource instead.
DeleteProjectResource(project string, resourceURI string) error
// UpdateProjectResources updates multiple project resources.
UpdateProjectResources(project string, resources []*models.Resource) (string, error)
// CreateStageResources creates a stage resource.
// Deprecated: use CreateResource instead.
CreateStageResources(project string, stage string, resources []*models.Resource) (string, error)
// GetStageResource retrieves a stage resource from the configuration service.
// Deprecated: use GetResource instead.
GetStageResource(project string, stage string, resourceURI string) (*models.Resource, error)
// UpdateStageResource updates a stage resource.
// Deprecated: use UpdateResource instead.
UpdateStageResource(project string, stage string, resource *models.Resource) (string, error)
// UpdateStageResources updates multiple stage resources.
// Deprecated: use UpdateResource instead.
UpdateStageResources(project string, stage string, resources []*models.Resource) (string, error)
// DeleteStageResource deletes a stage resource.
// Deprecated: use DeleteResource instead.
DeleteStageResource(project string, stage string, resourceURI string) error
// CreateServiceResources creates a service resource.
// Deprecated: use CreateResource instead.
CreateServiceResources(project string, stage string, service string, resources []*models.Resource) (string, error)
// GetServiceResource retrieves a service resource from the configuration service.
// Deprecated: use GetResource instead.
GetServiceResource(project string, stage string, service string, resourceURI string) (*models.Resource, error)
// UpdateServiceResource updates a service resource.
// Deprecated: use UpdateResource instead.
UpdateServiceResource(project string, stage string, service string, resource *models.Resource) (string, error)
// UpdateServiceResources updates multiple service resources.
UpdateServiceResources(project string, stage string, service string, resources []*models.Resource) (string, error)
// DeleteServiceResource deletes a service resource.
// Deprecated: use DeleteResource instead.
DeleteServiceResource(project string, stage string, service string, resourceURI string) error
// GetAllStageResources returns a list of all resources.
GetAllStageResources(project string, stage string) ([]*models.Resource, error)
// GetAllServiceResources returns a list of all resources.
GetAllServiceResources(project string, stage string, service string) ([]*models.Resource, error)
}
// ResourceHandler handles resources
type ResourceHandler struct {
resourceHandler *v2.ResourceHandler
BaseURL string
AuthToken string
AuthHeader string
HTTPClient *http.Client
Scheme string
}
type resourceRequest struct {
Resources []*models.Resource `json:"resources"`
}
// ResourceScope contains the necessary information to get a resource
type ResourceScope struct {
project string
stage string
service string
resource string
}
// NewResourceScope returns an empty ResourceScope to fill in calling Project Stage Service or Resource functions
func NewResourceScope() *ResourceScope {
return &ResourceScope{}
}
// Project sets the resource scope project value
func (s *ResourceScope) Project(project string) *ResourceScope {
s.project = project
return s
}
// Stage sets the resource scope stage value
func (s *ResourceScope) Stage(stage string) *ResourceScope {
s.stage = stage
return s
}
// Service sets the resource scope service value
func (s *ResourceScope) Service(service string) *ResourceScope {
s.service = service
return s
}
// Resource sets the resource scope resource
func (s *ResourceScope) Resource(resource string) *ResourceScope {
s.resource = resource
return s
}
// GetProject returns the project name
func (s *ResourceScope) GetProject() string {
return s.project
}
// GetStage returns the stage name
func (s *ResourceScope) GetStage() string {
return s.stage
}
// GetService returns the service name
func (s *ResourceScope) GetService() string {
return s.service
}
// GetResource returns the resource name
func (s *ResourceScope) GetResource() string {
return s.resource
}
// GetProjectPath returns a string to construct the url to path eg. /<api-version>/project/<project-name>
//or an empty string if the project is not set
func (s *ResourceScope) GetProjectPath() string {
return buildPath(v1ProjectPath, s.project)
}
// GetStagePath returns a string to construct the url to a stage eg. /stage/<stage-name>
//or an empty string if the stage is unset
func (s *ResourceScope) GetStagePath() string {
return buildPath(pathToStage, s.stage)
}
// GetServicePath returns a string to construct the url to a service eg. /service/<service-name>
//or an empty string if the service is unset
func (s *ResourceScope) GetServicePath() string {
return buildPath(pathToService, url.QueryEscape(s.service))
}
// GetResourcePath returns a string to construct the url to a resource eg. /resource/<escaped-resource-name>
//or /resource if the resource scope is empty
func (s *ResourceScope) GetResourcePath() string {
path := pathToResource
if s.resource != "" {
path += "/" + url.QueryEscape(s.resource)
}
return path
}
func (r *ResourceHandler) buildResourceURI(scope ResourceScope) string {
buildURI := r.Scheme + "://" + r.BaseURL + scope.GetProjectPath() + scope.GetStagePath() + scope.GetServicePath() + scope.GetResourcePath()
return buildURI
}
// URIOption returns a function that modifies an url
type URIOption func(url string) string
// AppendQuery returns an option function that can modify an URI by appending a map of url query values
func AppendQuery(queryParams url.Values) URIOption {
return func(buildURI string) string {
if queryParams != nil {
buildURI = buildURI + "?" + queryParams.Encode()
}
return buildURI
}
}
func (r *ResourceHandler) applyOptions(buildURI string, options []URIOption) string {
for _, option := range options {
buildURI = option(buildURI)
}
return buildURI
}
// ToJSON converts object to JSON string
func (r *resourceRequest) ToJSON() ([]byte, error) {
return json.Marshal(r)
}
func (r *resourceRequest) FromJSON(b []byte) error {
var res resourceRequest
if err := json.Unmarshal(b, &res); err != nil {
return err
}
*r = res
return nil
}
// NewResourceHandler returns a new ResourceHandler which sends all requests directly to the resource-service
func NewResourceHandler(baseURL string) *ResourceHandler {
return NewResourceHandlerWithHTTPClient(baseURL, &http.Client{Transport: wrapOtelTransport(getClientTransport(nil))})
}
// NewResourceHandlerWithHTTPClient returns a new ResourceHandler which sends all requests directly to the resource-service using the specified http.Client
func NewResourceHandlerWithHTTPClient(baseURL string, httpClient *http.Client) *ResourceHandler {
return &ResourceHandler{
BaseURL: httputils.TrimHTTPScheme(baseURL),
HTTPClient: httpClient,
Scheme: "http",
resourceHandler: v2.NewResourceHandlerWithHTTPClient(baseURL, httpClient),
}
}
// NewAuthenticatedResourceHandler returns a new ResourceHandler that authenticates at the api via the provided token
// and sends all requests directly to the resource-service
// Deprecated: use APISet instead
func NewAuthenticatedResourceHandler(baseURL string, authToken string, authHeader string, httpClient *http.Client, scheme string) *ResourceHandler {
if httpClient == nil {
httpClient = &http.Client{}
}
httpClient.Transport = wrapOtelTransport(getClientTransport(httpClient.Transport))
return createAuthenticatedResourceHandler(baseURL, authToken, authHeader, httpClient, scheme)
}
func createAuthenticatedResourceHandler(baseURL string, authToken string, authHeader string, httpClient *http.Client, scheme string) *ResourceHandler {
v2ResourceHandler := v2.NewAuthenticatedResourceHandler(baseURL, authToken, authHeader, httpClient, scheme)
baseURL = strings.TrimRight(baseURL, "/")
if !strings.HasSuffix(baseURL, configurationServiceBaseURL) {
baseURL += "/" + configurationServiceBaseURL
}
return &ResourceHandler{
BaseURL: httputils.TrimHTTPScheme(baseURL),
AuthHeader: authHeader,
AuthToken: authToken,
HTTPClient: httpClient,
Scheme: scheme,
resourceHandler: v2ResourceHandler,
}
}
func (r *ResourceHandler) getBaseURL() string {
return r.BaseURL
}
func (r *ResourceHandler) getAuthToken() string {
return r.AuthToken
}
func (r *ResourceHandler) getAuthHeader() string {
return r.AuthHeader
}
func (r *ResourceHandler) getHTTPClient() *http.Client {
return r.HTTPClient
}
// CreateResources creates a resource for the specified entity.
func (r *ResourceHandler) CreateResources(project string, stage string, service string, resources []*models.Resource) (*models.EventContext, *models.Error) {
r.ensureHandlerIsSet()
return r.resourceHandler.CreateResources(context.TODO(), project, stage, service, resources, v2.ResourcesCreateResourcesOptions{})
}
// CreateProjectResources creates multiple project resources.
func (r *ResourceHandler) CreateProjectResources(project string, resources []*models.Resource) (string, error) {
r.ensureHandlerIsSet()
return r.resourceHandler.CreateProjectResources(context.TODO(), project, resources, v2.ResourcesCreateProjectResourcesOptions{})
}
// GetProjectResource retrieves a project resource from the configuration service.
// Deprecated: use GetResource instead.
func (r *ResourceHandler) GetProjectResource(project string, resourceURI string) (*models.Resource, error) {
r.ensureHandlerIsSet()
buildURI := r.Scheme + "://" + r.BaseURL + v1ProjectPath + "/" + project + pathToResource + "/" + url.QueryEscape(resourceURI)
return r.resourceHandler.GetResourceByURI(context.TODO(), buildURI)
}
// UpdateProjectResource updates a project resource.
// Deprecated: use UpdateResource instead.
func (r *ResourceHandler) UpdateProjectResource(project string, resource *models.Resource) (string, error) {
r.ensureHandlerIsSet()
return r.resourceHandler.UpdateResourceByURI(context.TODO(), r.Scheme+"://"+r.BaseURL+v1ProjectPath+"/"+project+pathToResource+"/"+url.QueryEscape(*resource.ResourceURI), resource)
}
// DeleteProjectResource deletes a project resource.
// Deprecated: use DeleteResource instead.
func (r *ResourceHandler) DeleteProjectResource(project string, resourceURI string) error {
r.ensureHandlerIsSet()
return r.resourceHandler.DeleteResourceByURI(context.TODO(), r.Scheme+"://"+r.BaseURL+v1ProjectPath+"/"+project+pathToResource+"/"+url.QueryEscape(resourceURI))
}
// UpdateProjectResources updates multiple project resources.
func (r *ResourceHandler) UpdateProjectResources(project string, resources []*models.Resource) (string, error) {
r.ensureHandlerIsSet()
return r.resourceHandler.UpdateProjectResources(context.TODO(), project, resources, v2.ResourcesUpdateProjectResourcesOptions{})
}
// CreateStageResources creates a stage resource.
// Deprecated: use CreateResource instead.
func (r *ResourceHandler) CreateStageResources(project string, stage string, resources []*models.Resource) (string, error) {
r.ensureHandlerIsSet()
return r.resourceHandler.CreateResourcesByURI(context.TODO(), r.Scheme+"://"+r.BaseURL+v1ProjectPath+"/"+project+pathToStage+"/"+stage+pathToResource, resources)
}
// GetStageResource retrieves a stage resource from the configuration service.
// Deprecated: use GetResource instead.
func (r *ResourceHandler) GetStageResource(project string, stage string, resourceURI string) (*models.Resource, error) {
r.ensureHandlerIsSet()
buildURI := r.Scheme + "://" + r.BaseURL + v1ProjectPath + "/" + project + pathToStage + "/" + stage + pathToResource + "/" + url.QueryEscape(resourceURI)
return r.resourceHandler.GetResourceByURI(context.TODO(), buildURI)
}
// UpdateStageResource updates a stage resource.
// Deprecated: use UpdateResource instead.
func (r *ResourceHandler) UpdateStageResource(project string, stage string, resource *models.Resource) (string, error) {
r.ensureHandlerIsSet()
return r.resourceHandler.UpdateResourceByURI(context.TODO(), r.Scheme+"://"+r.BaseURL+v1ProjectPath+"/"+project+pathToStage+"/"+stage+pathToResource+"/"+url.QueryEscape(*resource.ResourceURI), resource)
}
// UpdateStageResources updates multiple stage resources.
// Deprecated: use UpdateResource instead.
func (r *ResourceHandler) UpdateStageResources(project string, stage string, resources []*models.Resource) (string, error) {
r.ensureHandlerIsSet()
return r.resourceHandler.UpdateResourcesByURI(context.TODO(), r.Scheme+"://"+r.BaseURL+v1ProjectPath+"/"+project+pathToStage+"/"+stage+pathToResource, resources)
}
// DeleteStageResource deletes a stage resource.
// Deprecated: use DeleteResource instead.
func (r *ResourceHandler) DeleteStageResource(project string, stage string, resourceURI string) error {
r.ensureHandlerIsSet()
return r.resourceHandler.DeleteResourceByURI(context.TODO(), r.Scheme+"://"+r.BaseURL+v1ProjectPath+"/"+project+pathToStage+"/"+stage+pathToResource+"/"+url.QueryEscape(resourceURI))
}
// CreateServiceResources creates a service resource.
// Deprecated: use CreateResource instead.
func (r *ResourceHandler) CreateServiceResources(project string, stage string, service string, resources []*models.Resource) (string, error) {
r.ensureHandlerIsSet()
return r.resourceHandler.CreateResourcesByURI(context.TODO(), r.Scheme+"://"+r.BaseURL+v1ProjectPath+"/"+project+pathToStage+"/"+stage+pathToService+"/"+service+pathToResource, resources)
}
// GetServiceResource retrieves a service resource from the configuration service.
// Deprecated: use GetResource instead.
func (r *ResourceHandler) GetServiceResource(project string, stage string, service string, resourceURI string) (*models.Resource, error) {
r.ensureHandlerIsSet()
buildURI := r.Scheme + "://" + r.BaseURL + v1ProjectPath + "/" + project + pathToStage + "/" + stage + pathToService + "/" + url.QueryEscape(service) + pathToResource + "/" + url.QueryEscape(resourceURI)
return r.resourceHandler.GetResourceByURI(context.TODO(), buildURI)
}
// UpdateServiceResource updates a service resource.
// Deprecated: use UpdateResource instead.
func (r *ResourceHandler) UpdateServiceResource(project string, stage string, service string, resource *models.Resource) (string, error) {
r.ensureHandlerIsSet()
return r.resourceHandler.UpdateResourceByURI(context.TODO(), r.Scheme+"://"+r.BaseURL+v1ProjectPath+"/"+project+pathToStage+"/"+stage+pathToService+"/"+url.QueryEscape(service)+pathToResource+"/"+url.QueryEscape(*resource.ResourceURI), resource)
}
// UpdateServiceResources updates multiple service resources.
func (r *ResourceHandler) UpdateServiceResources(project string, stage string, service string, resources []*models.Resource) (string, error) {
r.ensureHandlerIsSet()
return r.resourceHandler.UpdateServiceResources(context.TODO(), project, stage, service, resources, v2.ResourcesUpdateServiceResourcesOptions{})
}
// DeleteServiceResource deletes a service resource.
// Deprecated: use DeleteResource instead.
func (r *ResourceHandler) DeleteServiceResource(project string, stage string, service string, resourceURI string) error {
r.ensureHandlerIsSet()
return r.resourceHandler.DeleteResourceByURI(context.TODO(), r.Scheme+"://"+r.BaseURL+v1ProjectPath+"/"+project+pathToStage+"/"+stage+pathToService+"/"+url.QueryEscape(service)+pathToResource+"/"+url.QueryEscape(resourceURI))
}
//GetResource returns a resource from the defined ResourceScope after applying all URI change configured in the options.
func (r *ResourceHandler) GetResource(scope ResourceScope, options ...URIOption) (*models.Resource, error) {
r.ensureHandlerIsSet()
return r.resourceHandler.GetResource(context.TODO(), toV2ResourceScope(scope), v2.ResourcesGetResourceOptions{URIOptions: toV2URIOptions(options)})
}
//DeleteResource delete a resource from the URI defined by ResourceScope and modified by the URIOption.
func (r *ResourceHandler) DeleteResource(scope ResourceScope, options ...URIOption) error {
r.ensureHandlerIsSet()
return r.resourceHandler.DeleteResource(context.TODO(), toV2ResourceScope(scope), v2.ResourcesDeleteResourceOptions{URIOptions: toV2URIOptions(options)})
}
//UpdateResource updates a resource from the URI defined by ResourceScope and modified by the URIOption.
func (r *ResourceHandler) UpdateResource(resource *models.Resource, scope ResourceScope, options ...URIOption) (string, error) {
r.ensureHandlerIsSet()
return r.resourceHandler.UpdateResource(context.TODO(), resource, toV2ResourceScope(scope), v2.ResourcesUpdateResourceOptions{URIOptions: toV2URIOptions(options)})
}
//CreateResource creates one or more resources at the URI defined by ResourceScope and modified by the URIOption.
func (r *ResourceHandler) CreateResource(resource []*models.Resource, scope ResourceScope, options ...URIOption) (string, error) {
r.ensureHandlerIsSet()
return r.resourceHandler.CreateResource(context.TODO(), resource, toV2ResourceScope(scope), v2.ResourcesCreateResourceOptions{URIOptions: toV2URIOptions(options)})
}
// GetAllStageResources returns a list of all resources.
func (r *ResourceHandler) GetAllStageResources(project string, stage string) ([]*models.Resource, error) {
r.ensureHandlerIsSet()
return r.resourceHandler.GetAllStageResources(context.TODO(), project, stage, v2.ResourcesGetAllStageResourcesOptions{})
}
// GetAllServiceResources returns a list of all resources.
func (r *ResourceHandler) GetAllServiceResources(project string, stage string, service string) ([]*models.Resource, error) {
r.ensureHandlerIsSet()
return r.resourceHandler.GetAllServiceResources(context.TODO(), project, stage, service, v2.ResourcesGetAllServiceResourcesOptions{})
}
func buildPath(base, name string) string {
path := ""
if name != "" {
path = base + "/" + name
}
return path
}
func toV2URIOptions(uriOptions []URIOption) []v2.URIOption {
var v2URIOptions []v2.URIOption
for _, v := range uriOptions {
v2URIOptions = append(v2URIOptions, v2.URIOption(v))
}
return v2URIOptions
}
func toV2ResourceScope(scope ResourceScope) v2.ResourceScope {
return *(v2.NewResourceScope().Project(scope.project).Stage(scope.stage).Service(scope.service).Resource(scope.resource))
}
func (r *ResourceHandler) ensureHandlerIsSet() {
if r.resourceHandler != nil {
return
}
if r.AuthToken != "" {
r.resourceHandler = v2.NewAuthenticatedResourceHandler(r.BaseURL, r.AuthToken, r.AuthHeader, r.HTTPClient, r.Scheme)
} else {
r.resourceHandler = v2.NewResourceHandlerWithHTTPClient(r.BaseURL, r.HTTPClient)
}
}