diff --git a/pkg/rosa/runtime.go b/pkg/rosa/runtime.go index dcb9e83d31..edabdcaa0f 100644 --- a/pkg/rosa/runtime.go +++ b/pkg/rosa/runtime.go @@ -3,6 +3,7 @@ package rosa import ( "os" + cmv1 "github.com/openshift-online/ocm-sdk-go/clustersmgmt/v1" "github.com/openshift/rosa/pkg/aws" "github.com/openshift/rosa/pkg/logging" "github.com/openshift/rosa/pkg/ocm" @@ -17,6 +18,7 @@ type Runtime struct { AWSClient aws.Client Creator *aws.Creator ClusterKey string + Cluster *cmv1.Cluster } func NewRuntime() *Runtime { @@ -67,3 +69,30 @@ func (r *Runtime) GetClusterKey() string { r.ClusterKey = clusterKey return clusterKey } + +func (r *Runtime) FetchCluster() *cmv1.Cluster { + if r.Cluster != nil { + return r.Cluster + } + + // We don't want to lazy init the OCM client since it requires cleanup + if r.OCMClient == nil { + r.Reporter.Errorf("Tried to fetch a cluster without initializing the OCM client, exiting.") + os.Exit(1) + } + if r.ClusterKey == "" { + r.GetClusterKey() + } + if r.Creator == nil { + r.WithAWS() + } + + r.Reporter.Debugf("Loading cluster '%s'", r.ClusterKey) + cluster, err := r.OCMClient.GetCluster(r.ClusterKey, r.Creator) + if err != nil { + r.Reporter.Errorf("Failed to get cluster '%s': %v", r.ClusterKey, err) + os.Exit(1) + } + r.Cluster = cluster + return cluster +}