Skip to content

Commit

Permalink
Merge pull request #33 from hashicorp/hvt-4974-cluster-state-errors
Browse files Browse the repository at this point in the history
add error handling for creating and locked clusters
  • Loading branch information
ccapurso authored Jan 22, 2024
2 parents 1123df6 + 8e06241 commit 5cd0bf9
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 1 deletion.
10 changes: 9 additions & 1 deletion connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -336,9 +336,17 @@ func (c *HCPConnectCommand) listClusters(organizationID string, projectID string

default:
cluster := clustersResp.GetPayload().Clusters[0]
if *cluster.State != hcpvsm.HashicorpCloudVault20201125ClusterStateRUNNING {

clusterState := *cluster.State

if clusterState == hcpvsm.HashicorpCloudVault20201125ClusterStateLOCKED || clusterState == hcpvsm.HashicorpCloudVault20201125ClusterStateLOCKING {
return "", errors.New("cluster is locked")
} else if clusterState == hcpvsm.HashicorpCloudVault20201125ClusterStateCREATING {
return "", errors.New("cluster is still being created")
} else if clusterState != hcpvsm.HashicorpCloudVault20201125ClusterStateRUNNING {
return "", errors.New("cluster is not running")
}

if *cluster.Config.NetworkConfig.HTTPProxyOption == hcpvsm.HashicorpCloudVault20201125HTTPProxyOptionDISABLED {
return "", ErrorProxyDisabled
}
Expand Down
99 changes: 99 additions & 0 deletions connect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down

0 comments on commit 5cd0bf9

Please sign in to comment.