Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support new tags attribute in cluster, advanced_cluster, and serverless_instance #1461

Merged
merged 6 commits into from
Sep 18, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions mongodbatlas/data_source_mongodbatlas_advanced_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,9 @@ func dataSourceMongoDBAtlasAdvancedCluster() *schema.Resource {
Computed: true,
},
"labels": {
Type: schema.TypeSet,
Computed: true,
Type: schema.TypeSet,
Computed: true,
Deprecated: fmt.Sprintf(DeprecationByDateWithReplacement, "November 2023", "tags"),
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"key": {
Expand All @@ -73,6 +74,7 @@ func dataSourceMongoDBAtlasAdvancedCluster() *schema.Resource {
},
},
},
"tags": &dsTagsSchema,
"mongo_db_major_version": {
Type: schema.TypeString,
Computed: true,
Expand Down Expand Up @@ -225,6 +227,23 @@ func dataSourceMongoDBAtlasAdvancedCluster() *schema.Resource {
}
}

var dsTagsSchema = schema.Schema{
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"key": {
Type: schema.TypeString,
Computed: true,
},
"value": {
Type: schema.TypeString,
Computed: true,
},
},
},
}

func dataSourceMongoDBAtlasAdvancedClusterRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
// Get client connection.
conn := meta.(*MongoDBClient).Atlas
Expand Down Expand Up @@ -272,6 +291,10 @@ func dataSourceMongoDBAtlasAdvancedClusterRead(ctx context.Context, d *schema.Re
return diag.FromErr(fmt.Errorf(errorClusterAdvancedSetting, "labels", clusterName, err))
}

if err := d.Set("tags", flattenTags(&cluster.Tags)); err != nil {
return diag.FromErr(fmt.Errorf(errorClusterAdvancedSetting, "tags", clusterName, err))
}

if err := d.Set("mongo_db_major_version", cluster.MongoDBMajorVersion); err != nil {
return diag.FromErr(fmt.Errorf(errorClusterAdvancedSetting, "mongo_db_major_version", clusterName, err))
}
Expand Down
7 changes: 5 additions & 2 deletions mongodbatlas/data_source_mongodbatlas_advanced_clusters.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,9 @@ func dataSourceMongoDBAtlasAdvancedClusters() *schema.Resource {
Computed: true,
},
"labels": {
Type: schema.TypeSet,
Computed: true,
Type: schema.TypeSet,
Computed: true,
Deprecated: fmt.Sprintf(DeprecationByDateWithReplacement, "November 2023", "tags"),
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"key": {
Expand All @@ -81,6 +82,7 @@ func dataSourceMongoDBAtlasAdvancedClusters() *schema.Resource {
},
},
},
"tags": &dsTagsSchema,
"mongo_db_major_version": {
Type: schema.TypeString,
Computed: true,
Expand Down Expand Up @@ -281,6 +283,7 @@ func flattenAdvancedClusters(ctx context.Context, conn *matlas.Client, clusters
"disk_size_gb": clusters[i].DiskSizeGB,
"encryption_at_rest_provider": clusters[i].EncryptionAtRestProvider,
"labels": flattenLabels(clusters[i].Labels),
"tags": flattenTags(&clusters[i].Tags),
"mongo_db_major_version": clusters[i].MongoDBMajorVersion,
"mongo_db_version": clusters[i].MongoDBVersion,
"name": clusters[i].Name,
Expand Down
10 changes: 8 additions & 2 deletions mongodbatlas/data_source_mongodbatlas_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,8 +292,9 @@ func dataSourceMongoDBAtlasCluster() *schema.Resource {
Computed: true,
},
"labels": {
Type: schema.TypeSet,
Computed: true,
Type: schema.TypeSet,
Computed: true,
Deprecated: fmt.Sprintf(DeprecationByDateWithReplacement, "November 2023", "tags"),
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"key": {
Expand All @@ -307,6 +308,7 @@ func dataSourceMongoDBAtlasCluster() *schema.Resource {
},
},
},
"tags": &dsTagsSchema,
"snapshot_backup_policy": computedCloudProviderSnapshotBackupPolicySchema(),
"container_id": {
Type: schema.TypeString,
Expand Down Expand Up @@ -450,6 +452,10 @@ func dataSourceMongoDBAtlasClusterRead(ctx context.Context, d *schema.ResourceDa
return diag.FromErr(fmt.Errorf(errorClusterSetting, "labels", clusterName, err))
}

if err := d.Set("tags", flattenTags(cluster.Tags)); err != nil {
return diag.FromErr(fmt.Errorf(errorClusterSetting, "tags", clusterName, err))
}

if err := d.Set("termination_protection_enabled", cluster.TerminationProtectionEnabled); err != nil {
return diag.FromErr(fmt.Errorf(errorClusterSetting, "termination_protection_enabled", clusterName, err))
}
Expand Down
7 changes: 5 additions & 2 deletions mongodbatlas/data_source_mongodbatlas_clusters.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,8 +295,9 @@ func dataSourceMongoDBAtlasClusters() *schema.Resource {
Computed: true,
},
"labels": {
Type: schema.TypeSet,
Computed: true,
Type: schema.TypeSet,
Computed: true,
Deprecated: fmt.Sprintf(DeprecationByDateWithReplacement, "November 2023", "tags"),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it might be useful to have constants with deprecation dates so it's easier to see next deprecation dates

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be helpful, but I believe configuring due date within jira tickets will be our best bet to make sure we are aware of removing deprecated fields.

Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"key": {
Expand All @@ -310,6 +311,7 @@ func dataSourceMongoDBAtlasClusters() *schema.Resource {
},
},
},
"tags": &dsTagsSchema,
"snapshot_backup_policy": computedCloudProviderSnapshotBackupPolicySchema(),
"container_id": {
Type: schema.TypeString,
Expand Down Expand Up @@ -410,6 +412,7 @@ func flattenClusters(ctx context.Context, d *schema.ResourceData, conn *matlas.C
"bi_connector_config": flattenBiConnectorConfig(clusters[i].BiConnector),
"replication_specs": flattenReplicationSpecs(clusters[i].ReplicationSpecs),
"labels": flattenLabels(clusters[i].Labels),
"tags": flattenTags(clusters[i].Tags),
"snapshot_backup_policy": snapshotBackupPolicy,
"termination_protection_enabled": clusters[i].TerminationProtectionEnabled,
"version_release_system": clusters[i].VersionReleaseSystem,
Expand Down
26 changes: 15 additions & 11 deletions mongodbatlas/data_source_mongodbatlas_serverless_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,49 +35,52 @@ func dataSourceMongoDBAtlasServerlessInstanceRead(ctx context.Context, d *schema
}

if err := d.Set("provider_settings_backing_provider_name", serverlessInstance.ProviderSettings.BackingProviderName); err != nil {
return diag.Errorf("error setting `provider_settings_backing_provider_name` for serverless instance (%s): %s", d.Id(), err)
return diag.Errorf(errorServerlessInstanceSetting, "provider_settings_backing_provider_name", d.Id(), err)
}

if err := d.Set("provider_settings_provider_name", serverlessInstance.ProviderSettings.ProviderName); err != nil {
return diag.Errorf("error setting `provider_settings_provider_name` for serverless instance (%s): %s", d.Id(), err)
return diag.Errorf(errorServerlessInstanceSetting, "provider_settings_provider_name", d.Id(), err)
}

if err := d.Set("provider_settings_region_name", serverlessInstance.ProviderSettings.RegionName); err != nil {
return diag.Errorf("error setting `provider_settings_region_name` for serverless instance (%s): %s", d.Id(), err)
return diag.Errorf(errorServerlessInstanceSetting, "provider_settings_region_name", d.Id(), err)
}

if err := d.Set("connection_strings_standard_srv", serverlessInstance.ConnectionStrings.StandardSrv); err != nil {
return diag.Errorf("error setting `connection_strings_standard_srv` for serverless instance (%s): %s", d.Id(), err)
return diag.Errorf(errorServerlessInstanceSetting, "connection_strings_standard_srv", d.Id(), err)
}

if len(serverlessInstance.ConnectionStrings.PrivateEndpoint) > 0 {
if err := d.Set("connection_strings_private_endpoint_srv", flattenSRVConnectionString(serverlessInstance.ConnectionStrings.PrivateEndpoint)); err != nil {
return diag.Errorf("error setting `connection_strings_private_endpoint_srv` for serverless instance (%s): %s", d.Id(), err)
return diag.Errorf(errorServerlessInstanceSetting, "connection_strings_private_endpoint_srv", d.Id(), err)
}
}

if err := d.Set("create_date", serverlessInstance.CreateDate); err != nil {
return diag.Errorf("error setting `create_date` for serverless instance (%s): %s", d.Id(), err)
return diag.Errorf(errorServerlessInstanceSetting, "create_date", d.Id(), err)
}

if err := d.Set("mongo_db_version", serverlessInstance.MongoDBVersion); err != nil {
return diag.Errorf("error setting `mongo_db_version` for serverless instance (%s): %s", d.Id(), err)
return diag.Errorf(errorServerlessInstanceSetting, "mongo_db_version", d.Id(), err)
}

if err := d.Set("links", flattenServerlessInstanceLinks(serverlessInstance.Links)); err != nil {
return diag.Errorf("error setting `links` for serverless instance (%s): %s", d.Id(), err)
return diag.Errorf(errorServerlessInstanceSetting, "links", d.Id(), err)
}

if err := d.Set("state_name", serverlessInstance.StateName); err != nil {
return diag.Errorf("error setting `state_name` for serverless instance (%s): %s", d.Id(), err)
return diag.Errorf(errorServerlessInstanceSetting, "state_name", d.Id(), err)
}

if err := d.Set("termination_protection_enabled", serverlessInstance.TerminationProtectionEnabled); err != nil {
return diag.Errorf("error setting `termination_protection_enabled` for serverless instance (%s): %s", d.Id(), err)
return diag.Errorf(errorServerlessInstanceSetting, "termination_protection_enabled", d.Id(), err)
}

if err := d.Set("continuous_backup_enabled", serverlessInstance.ServerlessBackupOptions.ServerlessContinuousBackupEnabled); err != nil {
return diag.Errorf("error setting `state_name` for serverless instance (%s): %s", d.Id(), err)
return diag.Errorf(errorServerlessInstanceSetting, "continuous_backup_enabled", d.Id(), err)
}
if err := d.Set("tags", flattenTags(serverlessInstance.Tags)); err != nil {
return diag.Errorf(errorClusterAdvancedSetting, "tags", d.Id(), err)
}

d.SetId(encodeStateID(map[string]string{
Expand Down Expand Up @@ -163,5 +166,6 @@ func returnServerlessInstanceDSSchema() map[string]*schema.Schema {
Optional: true,
Computed: true,
},
"tags": &dsTagsSchema,
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ func flattenServerlessInstances(serverlessInstances []*matlas.Cluster) []map[str
"state_name": serverlessInstances[i].StateName,
"termination_protection_enabled": serverlessInstances[i].TerminationProtectionEnabled,
"continuous_backup_enabled": serverlessInstances[i].ServerlessBackupOptions.ServerlessContinuousBackupEnabled,
"tags": flattenTags(serverlessInstances[i].Tags),
}
}

Expand Down
60 changes: 1 addition & 59 deletions mongodbatlas/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,12 @@ import (
"hash/crc32"
"log"
"os"
"reflect"
"regexp"
"sort"
"strconv"
"strings"
"time"

matlas "go.mongodb.org/atlas/mongodbatlas"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/credentials"
Expand All @@ -43,6 +40,7 @@ const (
DeprecationMessage = "this resource is deprecated and will be removed in %s, please transition to %s"
DeprecationMessageParameterToResource = "this parameter is deprecated and will be removed in %s, please transition to %s"
DeprecationByDateMessageParameter = "this parameter is deprecated and will be removed by %s"
DeprecationByDateWithReplacement = "this parameter is deprecated and will be removed by %s, please transition to %s"
AWS = "AWS"
AZURE = "AZURE"
GCP = "GCP"
Expand Down Expand Up @@ -512,62 +510,6 @@ func valRegion(reg interface{}, opt ...string) (string, error) {
return strings.ReplaceAll(region, "-", "_"), nil
}

func flattenLabels(l []matlas.Label) []map[string]interface{} {
labels := make([]map[string]interface{}, len(l))
for i, v := range l {
labels[i] = map[string]interface{}{
"key": v.Key,
"value": v.Value,
}
}

return labels
}

func expandLabelSliceFromSetSchema(d *schema.ResourceData) []matlas.Label {
list := d.Get("labels").(*schema.Set)
res := make([]matlas.Label, list.Len())

for i, val := range list.List() {
v := val.(map[string]interface{})
res[i] = matlas.Label{
Key: v["key"].(string),
Value: v["value"].(string),
}
}

return res
}

func containsLabelOrKey(list []matlas.Label, item matlas.Label) bool {
for _, v := range list {
if reflect.DeepEqual(v, item) || v.Key == item.Key {
return true
}
}

return false
}

func removeLabel(list []matlas.Label, item matlas.Label) []matlas.Label {
var pos int

for _, v := range list {
if reflect.DeepEqual(v, item) {
list = append(list[:pos], list[pos+1:]...)

if pos > 0 {
pos--
}

continue
}
pos++
}

return list
}

func expandStringList(list []interface{}) (res []string) {
for _, v := range list {
res = append(res, v.(string))
Expand Down
Loading
Loading