Skip to content

Commit

Permalink
test: Support getting cluster info with project
Browse files Browse the repository at this point in the history
  • Loading branch information
EspenAlbert committed Jul 12, 2024
1 parent 0c77dbd commit 6cd901b
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 11 deletions.
10 changes: 8 additions & 2 deletions internal/testutil/acc/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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,
Expand Down
18 changes: 10 additions & 8 deletions internal/testutil/acc/config_formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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,
Expand All @@ -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
Expand All @@ -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()
Expand All @@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion internal/testutil/acc/config_formatter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
Expand Down

0 comments on commit 6cd901b

Please sign in to comment.