Skip to content

Commit

Permalink
added list_quota_cost example
Browse files Browse the repository at this point in the history
  • Loading branch information
cristianoveiga committed Feb 4, 2022
1 parent e5694fa commit 00fac98
Showing 1 changed file with 76 additions and 0 deletions.
76 changes: 76 additions & 0 deletions examples/list_quota_cost.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
Copyright (c) 2018 Red Hat, Inc.
Licensed 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.
*/

// This example shows how to retrieve a list of quota cost

package main

import (
"context"
"fmt"
"os"

sdk "github.com/openshift-online/ocm-sdk-go"
amv1 "github.com/openshift-online/ocm-sdk-go/accountsmgmt/v1"
"github.com/openshift-online/ocm-sdk-go/logging"
)

func main() {
// Create a context:
ctx := context.Background()

// Create a logger that has the debug level enabled:
logger, err := logging.NewGoLoggerBuilder().
Debug(true).
Build()
if err != nil {
fmt.Fprintf(os.Stderr, "Can't build logger: %v\n", err)
os.Exit(1)
}

// Create the connection, and remember to close it:
token := os.Getenv("OCM_TOKEN")
connection, err := sdk.NewConnectionBuilder().
Logger(logger).
Tokens(token).
BuildContext(ctx)
if err != nil {
fmt.Fprintf(os.Stderr, "Can't build connection: %v\n", err)
os.Exit(1)
}
defer connection.Close()

organizationId := "" // update with your own organizationId

// Get the client for the resource that manages the collection of quota cost:
collection := connection.AccountsMgmt().V1().Organizations().Organization(organizationId).QuotaCost()

// Search quota cost items where quota_id starts with 'add-on':
response, err := collection.List().
Search("quota_id like 'add-on%'").
SendContext(ctx)
if err != nil {
fmt.Fprintf(os.Stderr, "Can't retrieve quota cost list for organization %s: %s\n", organizationId, err)
os.Exit(1)
}

// Prints quota cost items that were found in the previous step:
response.Items().Each(func(quota *amv1.QuotaCost) bool {
fmt.Printf("%s - %d - %d\n", quota.QuotaID(), quota.Allowed(), quota.Consumed())
return true
})

}

0 comments on commit 00fac98

Please sign in to comment.