From 8f622a166d5d7cb0167be720378cad9e6698009e Mon Sep 17 00:00:00 2001 From: Beni Cherniavsky-Paskin Date: Tue, 4 Jul 2023 14:19:12 +0300 Subject: [PATCH] improve GetCluster message when Subscription exists but is inactive First step towards #517. At least doesn't lie to user and gives hint how to find past data: Error: Can't retrieve cluster for key '0e68a621-c00e-4bbf-a4bb-9915ae6cc947': Cluster was Deprovisioned, see `ocm get subscription 2S434jffVG4fSgDdK4cbkDTxVNp` for details This affects ALL commands that use `GetCluster` helper. Behavior change: when there is 1 active cluster and also 1+ past clusters reusing same display_name, previously the active one was used; now will refuse treating them as ambiguous: > ocm get subs -p search="display_name in ('servheredia')" | jq .items[].status | sort | uniq -c 4 "Deprovisioned" 1 "Reserved" > ocm describe cluster servheredia Error: Can't retrieve cluster for key 'servheredia': There are 5 subscriptions with cluster identifier or name 'servheredia' --- pkg/cluster/cluster.go | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/pkg/cluster/cluster.go b/pkg/cluster/cluster.go index 234c3df0..10d3d214 100644 --- a/pkg/cluster/cluster.go +++ b/pkg/cluster/cluster.go @@ -148,8 +148,7 @@ func GetCluster(connection *sdk.Connection, key string) (cluster *cmv1.Cluster, // Try to find a matching subscription: subsSearch := fmt.Sprintf( - "(display_name = '%s' or cluster_id = '%s' or external_cluster_id = '%s') and "+ - "status in ('Reserved', 'Active')", + "(display_name = '%s' or cluster_id = '%s' or external_cluster_id = '%s')", key, key, key, ) subsListResponse, err := subsResource.List(). @@ -164,7 +163,14 @@ func GetCluster(connection *sdk.Connection, key string) (cluster *cmv1.Cluster, // If there is exactly one matching subscription then return the corresponding cluster: subsTotal := subsListResponse.Total() if subsTotal == 1 { - id, ok := subsListResponse.Items().Slice()[0].GetClusterID() + sub := subsListResponse.Items().Slice()[0] + status, ok := sub.GetStatus() + subID, _ := sub.GetID() + if !ok || (status != "Reserved" && status != "Active") { + err = fmt.Errorf("Cluster was %s, see `ocm get subscription %s` for details", status, subID) + return + } + id, ok := sub.GetClusterID() if ok { var clusterGetResponse *cmv1.ClusterGetResponse clusterGetResponse, err = clustersResource.Cluster(id).Get().