forked from bf2fc6cc711aee1a0c2a/kas-fleet-manager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
615 lines (530 loc) · 21.9 KB
/
client.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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
package ocm
import (
"fmt"
"net/http"
"time"
"github.com/patrickmn/go-cache"
pkgerrors "github.com/pkg/errors"
"github.com/bf2fc6cc711aee1a0c2a/kas-fleet-manager/pkg/errors"
sdkClient "github.com/openshift-online/ocm-sdk-go"
amsv1 "github.com/openshift-online/ocm-sdk-go/accountsmgmt/v1"
v1 "github.com/openshift-online/ocm-sdk-go/authorizations/v1"
clustersmgmtv1 "github.com/openshift-online/ocm-sdk-go/clustersmgmt/v1"
)
const TERMS_SITECODE = "OCM"
const TERMS_EVENTCODE_ONLINE_SERVICE = "onlineService"
const TERMS_EVENTCODE_REGISTER = "register"
//go:generate moq -out client_moq.go . Client
type Client interface {
CreateCluster(cluster *clustersmgmtv1.Cluster) (*clustersmgmtv1.Cluster, error)
GetClusterIngresses(clusterID string) (*clustersmgmtv1.IngressesListResponse, error)
GetCluster(clusterID string) (*clustersmgmtv1.Cluster, error)
GetClusterStatus(id string) (*clustersmgmtv1.ClusterStatus, error)
GetCloudProviders() (*clustersmgmtv1.CloudProviderList, error)
GetRegions(provider *clustersmgmtv1.CloudProvider) (*clustersmgmtv1.CloudRegionList, error)
GetAddon(clusterId string, addonId string) (*clustersmgmtv1.AddOnInstallation, error)
CreateAddonWithParams(clusterId string, addonId string, parameters []Parameter) (*clustersmgmtv1.AddOnInstallation, error)
CreateAddon(clusterId string, addonId string) (*clustersmgmtv1.AddOnInstallation, error)
UpdateAddonParameters(clusterId string, addonId string, parameters []Parameter) (*clustersmgmtv1.AddOnInstallation, error)
GetClusterDNS(clusterID string) (string, error)
CreateSyncSet(clusterID string, syncset *clustersmgmtv1.Syncset) (*clustersmgmtv1.Syncset, error)
UpdateSyncSet(clusterID string, syncSetID string, syncset *clustersmgmtv1.Syncset) (*clustersmgmtv1.Syncset, error)
GetSyncSet(clusterID string, syncSetID string) (*clustersmgmtv1.Syncset, error)
DeleteSyncSet(clusterID string, syncsetID string) (int, error)
CreateIdentityProvider(clusterID string, identityProvider *clustersmgmtv1.IdentityProvider) (*clustersmgmtv1.IdentityProvider, error)
GetIdentityProviderList(clusterID string) (*clustersmgmtv1.IdentityProviderList, error)
DeleteCluster(clusterID string) (int, error)
ClusterAuthorization(cb *amsv1.ClusterAuthorizationRequest) (*amsv1.ClusterAuthorizationResponse, error)
DeleteSubscription(id string) (int, error)
FindSubscriptions(query string) ([]*amsv1.Subscription, error)
GetSubscriptionByID(subscriptionID string) (*amsv1.Subscription, bool, error)
GetRequiresTermsAcceptance(username string) (termsRequired bool, redirectUrl string, err error)
GetOrganisationIdFromExternalId(externalId string) (string, error)
Connection() *sdkClient.Connection
GetMachinePool(clusterID string, machinePoolID string) (*clustersmgmtv1.MachinePool, error)
CreateMachinePool(clusterID string, machinePool *clustersmgmtv1.MachinePool) (*clustersmgmtv1.MachinePool, error)
// GetQuotaCosts returns a list of quota cost for the given organizationID.
// Each quota cost contains information on the usage and max allowed ocm resources quota given to the specified oganization.
//
// relatedResourceFilters will only be applied when fetchRelatedResources is set to true.
GetQuotaCosts(organizationID string, fetchRelatedResources, fetchCloudAccounts bool, filters ...QuotaCostRelatedResourceFilter) ([]*amsv1.QuotaCost, error)
GetQuotaCostsForProduct(organizationID, resourceName, product string) ([]*amsv1.QuotaCost, error)
// GetCurrentAccount returns the account information of the current authenticated user
GetCurrentAccount() (*amsv1.Account, error)
}
var _ Client = &client{}
type client struct {
connection *sdkClient.Connection
cache *cache.Cache
}
type AMSClient Client
type ClusterManagementClient Client
func NewOCMConnection(ocmConfig *OCMConfig, BaseUrl string) (*sdkClient.Connection, func(), error) {
if ocmConfig.EnableMock && ocmConfig.MockMode != MockModeEmulateServer {
return nil, func() {}, nil
}
builder := sdkClient.NewConnectionBuilder().
URL(BaseUrl).
MetricsSubsystem("api_outbound")
if !ocmConfig.EnableMock {
// Create a logger that has the debug level enabled:
logger, err := sdkClient.NewGoLoggerBuilder().
Debug(ocmConfig.Debug).
Build()
if err != nil {
return nil, nil, err
}
builder = builder.Logger(logger)
}
if ocmConfig.ClientID != "" && ocmConfig.ClientSecret != "" {
builder = builder.Client(ocmConfig.ClientID, ocmConfig.ClientSecret)
} else if ocmConfig.SelfToken != "" {
builder = builder.Tokens(ocmConfig.SelfToken)
} else {
return nil, nil, fmt.Errorf("can't build OCM client connection,no Client/Secret or Token has been provided")
}
connection, err := builder.Build()
if err != nil {
return nil, nil, err
}
return connection, func() {
_ = connection.Close()
}, nil
}
func NewClient(connection *sdkClient.Connection) Client {
return &client{
connection: connection,
cache: cache.New(168*time.Hour, 1*time.Hour),
}
}
func (c *client) Connection() *sdkClient.Connection {
return c.connection
}
func (c *client) Close() {
if c.connection != nil {
_ = c.connection.Close()
}
}
func (c *client) CreateCluster(cluster *clustersmgmtv1.Cluster) (*clustersmgmtv1.Cluster, error) {
clusterResource := c.connection.ClustersMgmt().V1().Clusters()
response, err := clusterResource.Add().Body(cluster).Send()
if err != nil {
return &clustersmgmtv1.Cluster{}, errors.New(errors.ErrorGeneral, err.Error())
}
createdCluster := response.Body()
return createdCluster, nil
}
func (c *client) GetOrganisationIdFromExternalId(externalId string) (string, error) {
orgId, cached := c.cache.Get(externalId)
if cached {
orgId, ok := orgId.(string)
if ok && orgId != "" {
return orgId, nil
}
}
res, err := c.connection.AccountsMgmt().V1().Organizations().List().Search(fmt.Sprintf("external_id='%s'", externalId)).Send()
if err != nil {
return "", err
}
items := res.Items()
if items.Len() < 1 {
// should never happen...
return "", errors.New(errors.ErrorGeneral, "organisation with external_id '%s' can't be found", externalId)
}
organisationId := items.Get(0).ID()
c.cache.Set(externalId, organisationId, cache.DefaultExpiration)
return organisationId, nil
}
func (c *client) GetRequiresTermsAcceptance(username string) (termsRequired bool, redirectUrl string, err error) {
// Check for Appendix 4 Terms
request, err := v1.NewTermsReviewRequest().AccountUsername(username).SiteCode(TERMS_SITECODE).EventCode(TERMS_EVENTCODE_REGISTER).Build()
if err != nil {
return false, "", err
}
selfTermsReview := c.connection.Authorizations().V1().TermsReview()
postResp, err := selfTermsReview.Post().Request(request).Send()
if err != nil {
return false, "", err
}
response, ok := postResp.GetResponse()
if !ok {
return false, "", fmt.Errorf("empty response from authorization post request")
}
redirectUrl, _ = response.GetRedirectUrl()
return response.TermsRequired(), redirectUrl, nil
}
// GetClusterIngresses sends a GET request to ocm to retrieve the ingresses of an OSD cluster
func (c *client) GetClusterIngresses(clusterID string) (*clustersmgmtv1.IngressesListResponse, error) {
clusterIngresses := c.connection.ClustersMgmt().V1().Clusters().Cluster(clusterID).Ingresses()
ingressList, err := clusterIngresses.List().Send()
if err != nil {
return nil, err
}
return ingressList, nil
}
func (c client) GetCluster(clusterID string) (*clustersmgmtv1.Cluster, error) {
resp, err := c.connection.ClustersMgmt().V1().Clusters().Cluster(clusterID).Get().Send()
if err != nil {
return nil, err
}
return resp.Body(), nil
}
func (c client) GetClusterStatus(id string) (*clustersmgmtv1.ClusterStatus, error) {
resp, err := c.connection.ClustersMgmt().V1().Clusters().Cluster(id).Status().Get().Send()
if err != nil {
return nil, err
}
return resp.Body(), nil
}
func (c *client) GetCloudProviders() (*clustersmgmtv1.CloudProviderList, error) {
providersCollection := c.connection.ClustersMgmt().V1().CloudProviders()
providersResponse, err := providersCollection.List().Send()
if err != nil {
return nil, pkgerrors.Wrap(err, "error retrieving cloud provider list")
}
cloudProviderList := providersResponse.Items()
return cloudProviderList, nil
}
func (c *client) GetRegions(provider *clustersmgmtv1.CloudProvider) (*clustersmgmtv1.CloudRegionList, error) {
regionsCollection := c.connection.ClustersMgmt().V1().CloudProviders().CloudProvider(provider.ID()).Regions()
regionsResponse, err := regionsCollection.List().Send()
if err != nil {
return nil, pkgerrors.Wrap(err, "error retrieving cloud region list")
}
regionList := regionsResponse.Items()
return regionList, nil
}
func (c client) CreateAddonWithParams(clusterId string, addonId string, params []Parameter) (*clustersmgmtv1.AddOnInstallation, error) {
addon := clustersmgmtv1.NewAddOn().ID(addonId)
addonParameters := newAddonParameterListBuilder(params)
addonInstallationBuilder := clustersmgmtv1.NewAddOnInstallation().Addon(addon)
if addonParameters != nil {
addonInstallationBuilder = addonInstallationBuilder.Parameters(addonParameters)
}
addonInstallation, err := addonInstallationBuilder.Build()
if err != nil {
return nil, err
}
resp, err := c.connection.ClustersMgmt().V1().Clusters().Cluster(clusterId).Addons().Add().Body(addonInstallation).Send()
if err != nil {
return nil, err
}
return resp.Body(), nil
}
func (c client) CreateAddon(clusterId string, addonId string) (*clustersmgmtv1.AddOnInstallation, error) {
return c.CreateAddonWithParams(clusterId, addonId, []Parameter{})
}
func (c client) GetAddon(clusterId string, addonId string) (*clustersmgmtv1.AddOnInstallation, error) {
resp, err := c.connection.ClustersMgmt().V1().Clusters().Cluster(clusterId).Addons().List().Send()
if err != nil {
return nil, err
}
addon := &clustersmgmtv1.AddOnInstallation{}
resp.Items().Each(func(addOnInstallation *clustersmgmtv1.AddOnInstallation) bool {
if addOnInstallation.ID() == addonId {
addon = addOnInstallation
return false
}
return true
})
return addon, nil
}
func (c client) UpdateAddonParameters(clusterId string, addonInstallationId string, parameters []Parameter) (*clustersmgmtv1.AddOnInstallation, error) {
addonInstallationResp, err := c.connection.ClustersMgmt().V1().Clusters().Cluster(clusterId).Addons().Addoninstallation(addonInstallationId).Get().Send()
if err != nil {
return nil, err
}
if existingParameters, ok := addonInstallationResp.Body().GetParameters(); ok {
if sameParameters(existingParameters, parameters) {
return addonInstallationResp.Body(), nil
}
}
addonInstallationBuilder := clustersmgmtv1.NewAddOnInstallation()
updatedParamsListBuilder := newAddonParameterListBuilder(parameters)
if updatedParamsListBuilder != nil {
addonInstallation, err := addonInstallationBuilder.Parameters(updatedParamsListBuilder).Build()
if err != nil {
return nil, err
}
resp, err := c.connection.ClustersMgmt().V1().Clusters().Cluster(clusterId).Addons().Addoninstallation(addonInstallationId).Update().Body(addonInstallation).Send()
if err != nil {
return nil, err
}
return resp.Body(), nil
}
return addonInstallationResp.Body(), nil
}
func (c *client) GetClusterDNS(clusterID string) (string, error) {
if clusterID == "" {
return "", errors.Validation("clusterID cannot be empty")
}
ingresses, err := c.GetClusterIngresses(clusterID)
if err != nil {
return "", err
}
var clusterDNS string
ingresses.Items().Each(func(ingress *clustersmgmtv1.Ingress) bool {
if ingress.Default() {
clusterDNS = ingress.DNSName()
return false
}
return true
})
if clusterDNS == "" {
return "", errors.NotFound("cluster %s: DNS is empty", clusterID)
}
return clusterDNS, nil
}
func (c client) CreateSyncSet(clusterID string, syncset *clustersmgmtv1.Syncset) (*clustersmgmtv1.Syncset, error) {
clustersResource := c.connection.ClustersMgmt().V1().Clusters()
response, syncsetErr := clustersResource.Cluster(clusterID).
ExternalConfiguration().
Syncsets().
Add().
Body(syncset).
Send()
var err error
if syncsetErr != nil {
err = errors.NewErrorFromHTTPStatusCode(response.Status(), "ocm client failed to create syncset: %s", syncsetErr)
}
return response.Body(), err
}
func (c client) UpdateSyncSet(clusterID string, syncSetID string, syncset *clustersmgmtv1.Syncset) (*clustersmgmtv1.Syncset, error) {
clustersResource := c.connection.ClustersMgmt().V1().Clusters()
response, syncsetErr := clustersResource.Cluster(clusterID).
ExternalConfiguration().
Syncsets().
Syncset(syncSetID).
Update().
Body(syncset).
Send()
var err error
if syncsetErr != nil {
err = errors.NewErrorFromHTTPStatusCode(response.Status(), "ocm client failed to update syncset '%s': %s", syncSetID, syncsetErr)
}
return response.Body(), err
}
func (c client) CreateIdentityProvider(clusterID string, identityProvider *clustersmgmtv1.IdentityProvider) (*clustersmgmtv1.IdentityProvider, error) {
clustersResource := c.connection.ClustersMgmt().V1().Clusters()
response, identityProviderErr := clustersResource.Cluster(clusterID).
IdentityProviders().
Add().
Body(identityProvider).
Send()
var err error
if identityProviderErr != nil {
err = errors.NewErrorFromHTTPStatusCode(response.Status(), "ocm client failed to create identity provider: %s", identityProviderErr)
}
return response.Body(), err
}
func (c client) GetIdentityProviderList(clusterID string) (*clustersmgmtv1.IdentityProviderList, error) {
clusterResource := c.connection.ClustersMgmt().V1().Clusters()
response, getIDPErr := clusterResource.Cluster(clusterID).
IdentityProviders().
List().
Send()
if getIDPErr != nil {
return nil, errors.NewErrorFromHTTPStatusCode(response.Status(), "ocm client failed to get list of identity providers, err: %s", getIDPErr.Error())
}
return response.Items(), nil
}
func (c client) GetSyncSet(clusterID string, syncSetID string) (*clustersmgmtv1.Syncset, error) {
clustersResource := c.connection.ClustersMgmt().V1().Clusters()
response, syncsetErr := clustersResource.Cluster(clusterID).
ExternalConfiguration().
Syncsets().
Syncset(syncSetID).
Get().
Send()
var err error
if syncsetErr != nil {
err = errors.NewErrorFromHTTPStatusCode(response.Status(), "ocm client failed to get syncset '%s': %s", syncSetID, syncsetErr)
}
return response.Body(), err
}
// Status returns the response status code.
func (c client) DeleteSyncSet(clusterID string, syncsetID string) (int, error) {
clustersResource := c.connection.ClustersMgmt().V1().Clusters()
response, syncsetErr := clustersResource.Cluster(clusterID).
ExternalConfiguration().
Syncsets().
Syncset(syncsetID).
Delete().
Send()
return response.Status(), syncsetErr
}
func newAddonParameterListBuilder(params []Parameter) *clustersmgmtv1.AddOnInstallationParameterListBuilder {
if len(params) > 0 {
var items []*clustersmgmtv1.AddOnInstallationParameterBuilder
for _, p := range params {
pb := clustersmgmtv1.NewAddOnInstallationParameter().ID(p.Id).Value(p.Value)
items = append(items, pb)
}
return clustersmgmtv1.NewAddOnInstallationParameterList().Items(items...)
}
return nil
}
func sameParameters(parameterList *clustersmgmtv1.AddOnInstallationParameterList, params []Parameter) bool {
if parameterList.Len() != len(params) {
return false
}
paramsMap := map[string]string{}
for _, p := range params {
paramsMap[p.Id] = p.Value
}
match := true
parameterList.Each(func(item *clustersmgmtv1.AddOnInstallationParameter) bool {
if paramsMap[item.ID()] != item.Value() {
match = false
return false
}
return true
})
return match
}
func (c client) DeleteCluster(clusterID string) (int, error) {
clustersResource := c.connection.ClustersMgmt().V1().Clusters()
response, deleteClusterError := clustersResource.Cluster(clusterID).Delete().Send()
var err error
if deleteClusterError != nil {
err = errors.NewErrorFromHTTPStatusCode(response.Status(), "OCM client failed to delete cluster '%s': %s", clusterID, deleteClusterError)
}
return response.Status(), err
}
func (c client) ClusterAuthorization(cb *amsv1.ClusterAuthorizationRequest) (*amsv1.ClusterAuthorizationResponse, error) {
r, err := c.connection.AccountsMgmt().V1().
ClusterAuthorizations().
Post().Request(cb).Send()
if err != nil && r.Status() != http.StatusTooManyRequests {
err = errors.NewErrorFromHTTPStatusCode(r.Status(), "OCM client failed to create cluster authorization")
return nil, err
}
resp, _ := r.GetResponse()
return resp, nil
}
func (c client) DeleteSubscription(id string) (int, error) {
r := c.connection.AccountsMgmt().V1().Subscriptions().Subscription(id).Delete()
resp, err := r.Send()
return resp.Status(), err
}
func (c client) FindSubscriptions(query string) ([]*amsv1.Subscription, error) {
r, err := c.connection.AccountsMgmt().V1().Subscriptions().List().Search(query).Send()
if err != nil {
return nil, err
}
return r.Items().Slice(), nil
}
func (c client) GetSubscriptionByID(subscriptionID string) (*amsv1.Subscription, bool, error) {
resp, err := c.connection.AccountsMgmt().V1().Subscriptions().Subscription(subscriptionID).Get().Send()
if resp != nil && resp.Status() == http.StatusNotFound {
return resp.Body(), false, nil
}
if err != nil {
return nil, false, err
}
return resp.Body(), true, nil
}
// GetMachinePool returns the machinePoolID associated to clusterID.
// If the cluster does not exist the returned MachinePool is nil
func (c *client) GetMachinePool(clusterID string, machinePoolID string) (*clustersmgmtv1.MachinePool, error) {
machinePoolsClient := c.connection.ClustersMgmt().V1().Clusters().Cluster(clusterID).MachinePools()
resp, err := machinePoolsClient.MachinePool(machinePoolID).Get().Send()
if resp.Status() == http.StatusNotFound {
return nil, nil
}
if err != nil {
return nil, err
}
return resp.Body(), nil
}
// CreateMachinePool creates the provided MachinePool in OCM.
// The created MachinePool or an error is returned
func (c *client) CreateMachinePool(clusterID string, machinePool *clustersmgmtv1.MachinePool) (*clustersmgmtv1.MachinePool, error) {
clusterClient := c.connection.ClustersMgmt().V1().Clusters().Cluster(clusterID)
machinePoolsClient := clusterClient.MachinePools()
response, err := machinePoolsClient.Add().Body(machinePool).Send()
if err != nil {
return nil, errors.New(errors.ErrorGeneral, err.Error())
}
createdMachinePool := response.Body()
return createdMachinePool, nil
}
// QuotaCostRelatedResourceFilter represents the properties of the related resource, associated
// to each quota cost, that can be used to filter the result of the get quota costs request.
// Any property set to nil will not be applied as a filter.
type QuotaCostRelatedResourceFilter struct {
ResourceName *string
ResourceType *string
Product *string
BillingModel *string
}
// IsMatch returns true if all the given properties of the filter matches that of the given related resource.
// If a filter property was not specified, the check for that property will always return true
func (qcf *QuotaCostRelatedResourceFilter) IsMatch(relatedResource *amsv1.RelatedResource) bool {
resourceNameMatches := (qcf.ResourceName == nil || relatedResource.ResourceName() == *qcf.ResourceName)
resourceTypeMatches := (qcf.ResourceType == nil || relatedResource.ResourceType() == *qcf.ResourceType)
productMatches := (qcf.Product == nil || relatedResource.Product() == *qcf.Product)
billingModelMatches := (qcf.BillingModel == nil || relatedResource.BillingModel() == *qcf.BillingModel)
return resourceNameMatches && resourceTypeMatches && productMatches && billingModelMatches
}
// GetQuotaCosts returns a list of quota cost for the given organizationID.
// Each quota cost contains information on the usage and max allowed ocm resources quota given to the specified oganization.
//
// relatedResourceFilters will only be applied when fetchRelatedResources is set to true.
// When relatedResourceFilters is not specified, all the quotas are returned.
// When relatedResourceFilters is specified, a quota is returned if one of the filters matches the related resources.
func (c client) GetQuotaCosts(organizationID string, fetchRelatedResources, fetchCloudAccounts bool, relatedResourceFilters ...QuotaCostRelatedResourceFilter) ([]*amsv1.QuotaCost, error) {
organizationClient := c.connection.AccountsMgmt().V1().Organizations()
quotaCostClient := organizationClient.Organization(organizationID).QuotaCost()
quotaCostResp, err := quotaCostClient.List().Parameter("fetchRelatedResources", fetchRelatedResources).Parameter("fetchCloudAccounts", fetchCloudAccounts).Send()
if err != nil {
return nil, err
}
quotaCostList := quotaCostResp.Items()
if !fetchRelatedResources || len(relatedResourceFilters) == 0 {
return quotaCostList.Slice(), nil
}
var quotaCosts []*amsv1.QuotaCost
// iterates through all Quota and only return the quota whose resources matches the given filters
quotaCostList.Each(func(qc *amsv1.QuotaCost) bool {
relatedResources := qc.RelatedResources()
quotaMatchesFilters := false
for _, relatedResource := range relatedResources {
for _, filter := range relatedResourceFilters {
if filter.IsMatch(relatedResource) {
quotaMatchesFilters = true
break
}
}
if quotaMatchesFilters {
quotaCosts = append(quotaCosts, qc)
break
}
}
return true
})
return quotaCosts, nil
}
// GetQuotaCostsForProduct gets the AMS QuotaCosts in the given organizationID
// whose relatedResources contains at least a relatedResource that has the
// given resourceName and product
func (c client) GetQuotaCostsForProduct(organizationID, resourceName, product string) ([]*amsv1.QuotaCost, error) {
quotaCostList, err := c.GetQuotaCosts(organizationID, true, true, QuotaCostRelatedResourceFilter{
ResourceName: &resourceName,
Product: &product,
})
if err != nil {
return nil, err
}
return quotaCostList, nil
}
// GetCurrentAccount returns the account information of the current authenticated user
func (c *client) GetCurrentAccount() (*amsv1.Account, error) {
currentAccountClient := c.connection.AccountsMgmt().V1().CurrentAccount()
response, err := currentAccountClient.Get().Send()
if err != nil {
return nil, err
}
currentAccount := response.Body()
return currentAccount, nil
}