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 dd7d62b commit dbf818e
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 9 deletions.
10 changes: 8 additions & 2 deletions internal/testutil/acc/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,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 @@ -47,11 +49,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
14 changes: 8 additions & 6 deletions internal/testutil/acc/config_formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func ToSnakeCase(str string) string {
return strings.ToLower(snake)
}

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 @@ -98,7 +98,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{
"project_id": projectID,
"cluster_type": clusterTypeStr,
Expand All @@ -113,20 +114,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 @@ -351,8 +351,9 @@ func Test_ClusterResourceHcl(t *testing.T) {
)
for name, tc := range testCases {
t.Run(name, func(t *testing.T) {
config, actualClusterName, err := acc.ClusterResourceHcl("project", &tc.req)
config, actualClusterName, actualResourceName, err := acc.ClusterResourceHcl("project", &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, tc.expected, config)
})
Expand Down

0 comments on commit dbf818e

Please sign in to comment.