|
| 1 | +/* |
| 2 | +Copyright (c) 2024 Red Hat, Inc. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +// This example shows how create a connection using the OpenID refresh token grant for |
| 18 | +// FedRAMP authentication. |
| 19 | + |
| 20 | +package main |
| 21 | + |
| 22 | +import ( |
| 23 | + "context" |
| 24 | + "fmt" |
| 25 | + "os" |
| 26 | + |
| 27 | + sdk "github.com/openshift-online/ocm-sdk-go" |
| 28 | + "github.com/openshift-online/ocm-sdk-go/authentication" |
| 29 | + cmv1 "github.com/openshift-online/ocm-sdk-go/clustersmgmt/v1" |
| 30 | + "github.com/openshift-online/ocm-sdk-go/logging" |
| 31 | +) |
| 32 | + |
| 33 | +func main() { |
| 34 | + // Create a context: |
| 35 | + ctx := context.Background() |
| 36 | + |
| 37 | + // Create a logger that has the debug level enabled: |
| 38 | + logger, err := logging.NewGoLoggerBuilder(). |
| 39 | + Debug(true). |
| 40 | + Build() |
| 41 | + if err != nil { |
| 42 | + fmt.Fprintf(os.Stderr, "Can't build logger: %v\n", err) |
| 43 | + os.Exit(1) |
| 44 | + } |
| 45 | + |
| 46 | + // Create the connection, and remember to close it: |
| 47 | + connection, err := sdk.NewConnectionBuilder(). |
| 48 | + Logger(logger). |
| 49 | + URL(sdk.FedRAMPURL). |
| 50 | + TokenURL(authentication.FedRAMPTokenURL). |
| 51 | + Tokens(os.Getenv("OCM_REFRESH_TOKEN")). |
| 52 | + Client(authentication.FedRAMPClientID, ""). |
| 53 | + BuildContext(ctx) |
| 54 | + if err != nil { |
| 55 | + fmt.Fprintf(os.Stderr, "Can't build connection: %v\n", err) |
| 56 | + os.Exit(1) |
| 57 | + } |
| 58 | + defer connection.Close() |
| 59 | + |
| 60 | + // Get the client for the service that manages the collection of clusters: |
| 61 | + collection := connection.ClustersMgmt().V1().Clusters() |
| 62 | + |
| 63 | + // Retrieve the collection of clusters: |
| 64 | + response, err := collection.List(). |
| 65 | + Search("name like 'my%'"). |
| 66 | + Page(1). |
| 67 | + Size(10). |
| 68 | + SendContext(ctx) |
| 69 | + if err != nil { |
| 70 | + fmt.Fprintf(os.Stderr, "Can't retrieve clusters: %v\n", err) |
| 71 | + os.Exit(1) |
| 72 | + } |
| 73 | + |
| 74 | + // Print the result: |
| 75 | + response.Items().Each(func(cluster *cmv1.Cluster) bool { |
| 76 | + fmt.Printf("%s - %s\n", cluster.ID(), cluster.Name()) |
| 77 | + return true |
| 78 | + }) |
| 79 | +} |
0 commit comments