diff --git a/internal/testutil/acc/cluster.go b/internal/testutil/acc/cluster.go index aa12465563..d229286428 100644 --- a/internal/testutil/acc/cluster.go +++ b/internal/testutil/acc/cluster.go @@ -31,6 +31,8 @@ type ClusterInfo struct { ClusterTerraformStr string } +const DefaultClusterResourceSuffix = "cluster_info" + // GetClusterInfo is used to obtain a project and cluster configuration resource. // When `MONGODB_ATLAS_CLUSTER_NAME` and `MONGODB_ATLAS_PROJECT_ID` are defined, creation of resources is avoided. This is useful for local execution but not intended for CI executions. // Clusters will be created in project ProjectIDExecution. @@ -51,11 +53,15 @@ func GetClusterInfo(tb testing.TB, req *ClusterRequest) ClusterInfo { } } projectID = ProjectIDExecution(tb) - clusterTerraformStr, clusterName, err := ClusterResourceHcl(projectID, req) + return GetClusterInfoWithProject(tb, req, projectID, DefaultClusterResourceSuffix) +} + +func GetClusterInfoWithProject(tb testing.TB, req *ClusterRequest, projectID, resourceSuffix string) ClusterInfo { + tb.Helper() + clusterTerraformStr, clusterName, clusterResourceName, err := ClusterResourceHcl(projectID, req, resourceSuffix) if err != nil { tb.Error(err) } - clusterResourceName := "mongodbatlas_advanced_cluster.cluster_info" return ClusterInfo{ ProjectIDStr: fmt.Sprintf("%q", projectID), ProjectID: projectID, diff --git a/internal/testutil/acc/config_formatter.go b/internal/testutil/acc/config_formatter.go index 8dc2de7710..6512dbf22f 100644 --- a/internal/testutil/acc/config_formatter.go +++ b/internal/testutil/acc/config_formatter.go @@ -82,7 +82,7 @@ var ( } ) -func ClusterResourceHcl(projectID string, req *ClusterRequest) (configStr, clusterName string, err error) { +func ClusterResourceHcl(projectID string, req *ClusterRequest, resourceSuffix string) (configStr, clusterName, resourceName string, err error) { if req == nil { req = new(ClusterRequest) } @@ -105,7 +105,8 @@ func ClusterResourceHcl(projectID string, req *ClusterRequest) (configStr, clust f := hclwrite.NewEmptyFile() root := f.Body() - cluster := root.AppendNewBlock("resource", []string{"mongodbatlas_advanced_cluster", "cluster_info"}).Body() + resourceType := "mongodbatlas_advanced_cluster" + cluster := root.AppendNewBlock("resource", []string{resourceType, resourceSuffix}).Body() clusterRootAttributes := map[string]any{ "cluster_type": clusterTypeStr, "name": clusterName, @@ -115,7 +116,7 @@ func ClusterResourceHcl(projectID string, req *ClusterRequest) (configStr, clust if strings.Contains(projectID, ".") { err = setAttributeHcl(cluster, fmt.Sprintf("project_id = %s", projectID)) if err != nil { - return "", "", fmt.Errorf("failed to set project_id = %s", projectID) + return "", "", "", fmt.Errorf("failed to set project_id = %s", projectID) } } else { clusterRootAttributes["project_id"] = projectID @@ -131,7 +132,7 @@ func ClusterResourceHcl(projectID string, req *ClusterRequest) (configStr, clust if len(req.AdvancedConfiguration) > 0 { for _, key := range sortStringMapKeysAny(req.AdvancedConfiguration) { if !knownAdvancedConfig[key] { - return "", "", fmt.Errorf("unknown key in advanced configuration: %s", key) + return "", "", "", fmt.Errorf("unknown key in advanced configuration: %s", key) } } advancedClusterBlock := cluster.AppendNewBlock("advanced_configuration", nil).Body() @@ -141,20 +142,21 @@ func ClusterResourceHcl(projectID string, req *ClusterRequest) (configStr, clust for i, spec := range specs { err = writeReplicationSpec(cluster, spec) if err != nil { - return "", "", fmt.Errorf("error writing hcl for replication spec %d: %w", i, err) + return "", "", "", fmt.Errorf("error writing hcl for replication spec %d: %w", i, err) } } cluster.AppendNewline() if req.ResourceDependencyName != "" { if !strings.Contains(req.ResourceDependencyName, ".") { - return "", "", fmt.Errorf("req.ResourceDependencyName must have a '.'") + return "", "", "", fmt.Errorf("req.ResourceDependencyName must have a '.'") } err = setAttributeHcl(cluster, fmt.Sprintf("depends_on = [%s]", req.ResourceDependencyName)) if err != nil { - return "", "", err + return "", "", "", err } } - return "\n" + string(f.Bytes()), clusterName, err + clusterResourceName := fmt.Sprintf("%s.%s", resourceType, resourceSuffix) + return "\n" + string(f.Bytes()), clusterName, clusterResourceName, err } func writeReplicationSpec(cluster *hclwrite.Body, spec admin.ReplicationSpec) error { diff --git a/internal/testutil/acc/config_formatter_test.go b/internal/testutil/acc/config_formatter_test.go index ca76a449c6..3e000deb25 100644 --- a/internal/testutil/acc/config_formatter_test.go +++ b/internal/testutil/acc/config_formatter_test.go @@ -375,8 +375,9 @@ func Test_ClusterResourceHcl(t *testing.T) { } else { projectID = "project" } - config, actualClusterName, err := acc.ClusterResourceHcl(projectID, &tc.req) + config, actualClusterName, actualResourceName, err := acc.ClusterResourceHcl(projectID, &tc.req, acc.DefaultClusterResourceSuffix) require.NoError(t, err) + assert.Equal(t, "mongodbatlas_advanced_cluster.cluster_info", actualResourceName) assert.Equal(t, clusterName, actualClusterName) assert.Equal(t, expectedConfig, config) })