diff --git a/spectrocloud/resource_cluster_custom_cloud_import.go b/spectrocloud/resource_cluster_custom_cloud_import.go index bfe7dce3..d7d57900 100644 --- a/spectrocloud/resource_cluster_custom_cloud_import.go +++ b/spectrocloud/resource_cluster_custom_cloud_import.go @@ -36,15 +36,20 @@ func ParseResourceCustomCloudImportID(d *schema.ResourceData) (string, string, s // Example: If the ID is a combination of ClusterId, then name of context/scope: `project` or `tenant` and then cloud type // and if scope is then followed by projectID "cluster456:project:nutanix" or "cluster456:tenant:oracle" parts := strings.Split(d.Id(), ":") - + errMsg := "invalid cluster ID format specified for import custom cloud %s, Ex: it should cluster_id:context:custom_cloud_name, `cluster456:project:nutanix`" scope := "invalid" clusterID := "" customCloudName := "" if len(parts) == 3 && (parts[1] == "tenant" || parts[1] == "project") { - clusterID, scope, customCloudName = parts[0], parts[1], parts[2] + if strings.TrimSpace(parts[0]) == "" || strings.TrimSpace(parts[1]) == "" || strings.TrimSpace(parts[2]) == "" { + return "", "", "", fmt.Errorf(errMsg, d.Id()) + } else { + clusterID, scope, customCloudName = parts[0], parts[1], parts[2] + } + } if scope == "invalid" { - return "", "", "", fmt.Errorf("invalid cluster ID format specified for import custom cloud %s, Ex: it should cluster_id:context:custom_cloud_name, `cluster456:project:nutanix`", d.Id()) + return "", "", "", fmt.Errorf(errMsg, d.Id()) } return clusterID, scope, customCloudName, nil } diff --git a/spectrocloud/resource_cluster_custom_cloud_test.go b/spectrocloud/resource_cluster_custom_cloud_test.go index b66fe060..a75a3500 100644 --- a/spectrocloud/resource_cluster_custom_cloud_test.go +++ b/spectrocloud/resource_cluster_custom_cloud_test.go @@ -245,3 +245,72 @@ func TestFlattenMachinePoolConfigsCustomCloud(t *testing.T) { }) } } + +func TestParseResourceCustomCloudImportID(t *testing.T) { + tests := []struct { + id string + expectedClusterID string + expectedScope string + expectedCloudName string + expectedError bool + expectedErrorString string + }{ + { + id: "cluster456:project:nutanix", + expectedClusterID: "cluster456", + expectedScope: "project", + expectedCloudName: "nutanix", + expectedError: false, + }, + { + id: "cluster789:tenant:oracle", + expectedClusterID: "cluster789", + expectedScope: "tenant", + expectedCloudName: "oracle", + expectedError: false, + }, + { + id: "cluster123:invalid:gcp", + expectedClusterID: "", + expectedScope: "", + expectedCloudName: "", + expectedError: true, + expectedErrorString: "invalid cluster ID format specified for import custom cloud cluster123:invalid:gcp, Ex: it should cluster_id:context:custom_cloud_name, `cluster456:project:nutanix`", + }, + { + id: "cluster456:project", + expectedClusterID: "", + expectedScope: "", + expectedCloudName: "", + expectedError: true, + expectedErrorString: "invalid cluster ID format specified for import custom cloud cluster456:project, Ex: it should cluster_id:context:custom_cloud_name, `cluster456:project:nutanix`", + }, + { + id: "cluster456:tenant:", + expectedClusterID: "", + expectedScope: "", + expectedCloudName: "", + expectedError: true, + expectedErrorString: "invalid cluster ID format specified for import custom cloud cluster456:tenant:, Ex: it should cluster_id:context:custom_cloud_name, `cluster456:project:nutanix`", + }, + } + + for _, test := range tests { + t.Run(test.id, func(t *testing.T) { + resourceData := resourceClusterCustomCloud().TestResourceData() + resourceData.SetId(test.id) + + clusterID, scope, customCloudName, err := ParseResourceCustomCloudImportID(resourceData) + + if test.expectedError { + assert.Error(t, err) + assert.EqualError(t, err, test.expectedErrorString) + } else { + assert.NoError(t, err) + assert.Equal(t, test.expectedClusterID, clusterID) + assert.Equal(t, test.expectedScope, scope) + assert.Equal(t, test.expectedCloudName, customCloudName) + } + }) + } +}