From 4af2376aa464dffa160b9ff520bdfe7533bed84c Mon Sep 17 00:00:00 2001 From: Chris Capurso <1036769+ccapurso@users.noreply.github.com> Date: Wed, 17 Jan 2024 10:14:35 -0500 Subject: [PATCH 1/2] add error handling for creating and locked clusters --- connect.go | 6 ++- connect_test.go | 99 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 104 insertions(+), 1 deletion(-) diff --git a/connect.go b/connect.go index 9b1790b..74d3500 100644 --- a/connect.go +++ b/connect.go @@ -336,7 +336,11 @@ func (c *HCPConnectCommand) listClusters(organizationID string, projectID string default: cluster := clustersResp.GetPayload().Clusters[0] - if *cluster.State != hcpvsm.HashicorpCloudVault20201125ClusterStateRUNNING { + if *cluster.State == hcpvsm.HashicorpCloudVault20201125ClusterStateLOCKED || *cluster.State == hcpvsm.HashicorpCloudVault20201125ClusterStateLOCKING { + return "", errors.New("cluster is locked") + } else if *cluster.State == hcpvsm.HashicorpCloudVault20201125ClusterStateCREATING { + return "", errors.New("cluster is still being created") + } else if *cluster.State != hcpvsm.HashicorpCloudVault20201125ClusterStateRUNNING { return "", errors.New("cluster is not running") } if *cluster.Config.NetworkConfig.HTTPProxyOption == hcpvsm.HashicorpCloudVault20201125HTTPProxyOptionDISABLED { diff --git a/connect_test.go b/connect_test.go index 41501b8..1992a0c 100644 --- a/connect_test.go +++ b/connect_test.go @@ -476,6 +476,105 @@ func Test_getCluster(t *testing.T) { expectedError: errors.New("invalid cluster: cluster-4"), }, + // Test error handling for cluster still being created + // UI interaction required + "cluster in creating": { + userInputCluster: "cluster-2", + listClustersServiceListResponse: &hcpvs.ListOK{ + Payload: &hcpvsm.HashicorpCloudVault20201125ListResponse{ + Clusters: []*hcpvsm.HashicorpCloudVault20201125Cluster{ + { + ID: "cluster-1", + DNSNames: &hcpvsm.HashicorpCloudVault20201125ClusterDNSNames{Proxy: "hcp-proxy-cluster-1.addr:8200"}, + State: hcpvsm.NewHashicorpCloudVault20201125ClusterState(hcpvsm.HashicorpCloudVault20201125ClusterStateRUNNING), + Config: &hcpvsm.HashicorpCloudVault20201125ClusterConfig{ + NetworkConfig: &hcpvsm.HashicorpCloudVault20201125NetworkConfig{ + HTTPProxyOption: hcpvsm.NewHashicorpCloudVault20201125HTTPProxyOption(hcpvsm.HashicorpCloudVault20201125HTTPProxyOptionENABLED), + }, + }, + }, + { + ID: "cluster-2", + DNSNames: &hcpvsm.HashicorpCloudVault20201125ClusterDNSNames{Proxy: "hcp-proxy-cluster-2.addr:8200"}, + State: hcpvsm.NewHashicorpCloudVault20201125ClusterState(hcpvsm.HashicorpCloudVault20201125ClusterStateCREATING), + Config: &hcpvsm.HashicorpCloudVault20201125ClusterConfig{ + NetworkConfig: &hcpvsm.HashicorpCloudVault20201125NetworkConfig{ + HTTPProxyOption: hcpvsm.NewHashicorpCloudVault20201125HTTPProxyOption(hcpvsm.HashicorpCloudVault20201125HTTPProxyOptionENABLED), + }, + }, + }, + }, + }, + }, + expectedError: errors.New("cluster is still being created"), + }, + + // Test error handling for cluster is locked + // UI interaction required + "cluster locked": { + userInputCluster: "cluster-2", + listClustersServiceListResponse: &hcpvs.ListOK{ + Payload: &hcpvsm.HashicorpCloudVault20201125ListResponse{ + Clusters: []*hcpvsm.HashicorpCloudVault20201125Cluster{ + { + ID: "cluster-1", + DNSNames: &hcpvsm.HashicorpCloudVault20201125ClusterDNSNames{Proxy: "hcp-proxy-cluster-1.addr:8200"}, + State: hcpvsm.NewHashicorpCloudVault20201125ClusterState(hcpvsm.HashicorpCloudVault20201125ClusterStateLOCKED), + Config: &hcpvsm.HashicorpCloudVault20201125ClusterConfig{ + NetworkConfig: &hcpvsm.HashicorpCloudVault20201125NetworkConfig{ + HTTPProxyOption: hcpvsm.NewHashicorpCloudVault20201125HTTPProxyOption(hcpvsm.HashicorpCloudVault20201125HTTPProxyOptionENABLED), + }, + }, + }, + { + ID: "cluster-2", + DNSNames: &hcpvsm.HashicorpCloudVault20201125ClusterDNSNames{Proxy: "hcp-proxy-cluster-2.addr:8200"}, + State: hcpvsm.NewHashicorpCloudVault20201125ClusterState(hcpvsm.HashicorpCloudVault20201125ClusterStateCREATING), + Config: &hcpvsm.HashicorpCloudVault20201125ClusterConfig{ + NetworkConfig: &hcpvsm.HashicorpCloudVault20201125NetworkConfig{ + HTTPProxyOption: hcpvsm.NewHashicorpCloudVault20201125HTTPProxyOption(hcpvsm.HashicorpCloudVault20201125HTTPProxyOptionENABLED), + }, + }, + }, + }, + }, + }, + expectedError: errors.New("cluster is locked"), + }, + + // Test error handling for cluster is locked + // UI interaction required + "cluster locking": { + userInputCluster: "cluster-2", + listClustersServiceListResponse: &hcpvs.ListOK{ + Payload: &hcpvsm.HashicorpCloudVault20201125ListResponse{ + Clusters: []*hcpvsm.HashicorpCloudVault20201125Cluster{ + { + ID: "cluster-1", + DNSNames: &hcpvsm.HashicorpCloudVault20201125ClusterDNSNames{Proxy: "hcp-proxy-cluster-1.addr:8200"}, + State: hcpvsm.NewHashicorpCloudVault20201125ClusterState(hcpvsm.HashicorpCloudVault20201125ClusterStateLOCKING), + Config: &hcpvsm.HashicorpCloudVault20201125ClusterConfig{ + NetworkConfig: &hcpvsm.HashicorpCloudVault20201125NetworkConfig{ + HTTPProxyOption: hcpvsm.NewHashicorpCloudVault20201125HTTPProxyOption(hcpvsm.HashicorpCloudVault20201125HTTPProxyOptionENABLED), + }, + }, + }, + { + ID: "cluster-2", + DNSNames: &hcpvsm.HashicorpCloudVault20201125ClusterDNSNames{Proxy: "hcp-proxy-cluster-2.addr:8200"}, + State: hcpvsm.NewHashicorpCloudVault20201125ClusterState(hcpvsm.HashicorpCloudVault20201125ClusterStateCREATING), + Config: &hcpvsm.HashicorpCloudVault20201125ClusterConfig{ + NetworkConfig: &hcpvsm.HashicorpCloudVault20201125NetworkConfig{ + HTTPProxyOption: hcpvsm.NewHashicorpCloudVault20201125HTTPProxyOption(hcpvsm.HashicorpCloudVault20201125HTTPProxyOptionENABLED), + }, + }, + }, + }, + }, + }, + expectedError: errors.New("cluster is locked"), + }, + // Test generic expectedError returned "expectedError": { expectedError: errors.New("error getting cluster"), From 8e0624182f605dca528e89d0c55c62813c892d0a Mon Sep 17 00:00:00 2001 From: Chris Capurso <1036769+ccapurso@users.noreply.github.com> Date: Mon, 22 Jan 2024 12:53:49 -0500 Subject: [PATCH 2/2] reduce cluster state dereferences --- connect.go | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/connect.go b/connect.go index 74d3500..57f69b3 100644 --- a/connect.go +++ b/connect.go @@ -336,13 +336,17 @@ func (c *HCPConnectCommand) listClusters(organizationID string, projectID string default: cluster := clustersResp.GetPayload().Clusters[0] - if *cluster.State == hcpvsm.HashicorpCloudVault20201125ClusterStateLOCKED || *cluster.State == hcpvsm.HashicorpCloudVault20201125ClusterStateLOCKING { + + clusterState := *cluster.State + + if clusterState == hcpvsm.HashicorpCloudVault20201125ClusterStateLOCKED || clusterState == hcpvsm.HashicorpCloudVault20201125ClusterStateLOCKING { return "", errors.New("cluster is locked") - } else if *cluster.State == hcpvsm.HashicorpCloudVault20201125ClusterStateCREATING { + } else if clusterState == hcpvsm.HashicorpCloudVault20201125ClusterStateCREATING { return "", errors.New("cluster is still being created") - } else if *cluster.State != hcpvsm.HashicorpCloudVault20201125ClusterStateRUNNING { + } else if clusterState != hcpvsm.HashicorpCloudVault20201125ClusterStateRUNNING { return "", errors.New("cluster is not running") } + if *cluster.Config.NetworkConfig.HTTPProxyOption == hcpvsm.HashicorpCloudVault20201125HTTPProxyOptionDISABLED { return "", ErrorProxyDisabled }