Skip to content

Commit

Permalink
task: remove dependency on pointy (#1953)
Browse files Browse the repository at this point in the history
  • Loading branch information
gssbzn authored Feb 16, 2024
1 parent 8952761 commit 95098f6
Show file tree
Hide file tree
Showing 38 changed files with 352 additions and 381 deletions.
4 changes: 1 addition & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ require (
github.com/hashicorp/terraform-plugin-sdk/v2 v2.32.0
github.com/hashicorp/terraform-plugin-testing v1.6.0
github.com/mongodb-forks/digest v1.0.5
github.com/mwielbut/pointy v1.1.0
github.com/spf13/cast v1.6.0
github.com/stretchr/testify v1.8.4
github.com/zclconf/go-cty v1.14.2
Expand All @@ -32,8 +31,6 @@ require (
golang.org/x/exp v0.0.0-20230809150735-7b3493d9a819
)

require github.com/apparentlymart/go-textseg/v15 v15.0.0 // indirect

require (
cloud.google.com/go v0.110.10 // indirect
cloud.google.com/go/compute v1.23.3 // indirect
Expand All @@ -46,6 +43,7 @@ require (
github.com/ProtonMail/go-crypto v0.0.0-20230828082145-3c4c8a2d2371 // indirect
github.com/agext/levenshtein v1.2.3 // indirect
github.com/apparentlymart/go-cidr v1.1.0 // indirect
github.com/apparentlymart/go-textseg/v15 v15.0.0 // indirect
github.com/armon/go-radix v1.0.0 // indirect
github.com/bgentry/go-netrc v0.0.0-20140422174119-9fd32a8b3d3d // indirect
github.com/bgentry/speakeasy v0.1.0 // indirect
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -646,8 +646,6 @@ github.com/mongodb-forks/digest v1.0.5 h1:EJu3wtLZcA0HCvsZpX5yuD193/sW9tHiNvrEM5
github.com/mongodb-forks/digest v1.0.5/go.mod h1:rb+EX8zotClD5Dj4NdgxnJXG9nwrlx3NWKJ8xttz1Dg=
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq1c1nUAm88MOHcQC9l5mIlSMApZMrHA=
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ=
github.com/mwielbut/pointy v1.1.0 h1:U5/YEfoIkaGCHv0St3CgjduqXID4FNRoyZgLM1kY9vg=
github.com/mwielbut/pointy v1.1.0/go.mod h1:MvvO+uMFj9T5DMda33HlvogsFBX7pWWKAkFIn4teYwY=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
github.com/oklog/run v1.0.0 h1:Ru7dDtJNOyC66gQ5dQmaCa0qIsAUFY3sFpK1Xk8igrw=
github.com/oklog/run v1.0.0/go.mod h1:dlhp/R75TPv97u0XWUtDeV/lRKWPKSdTuV0TZvrmrQA=
Expand Down
2 changes: 1 addition & 1 deletion internal/common/conversion/type_conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func IntPtrToInt64Ptr(i *int) *int64 {

// IsStringPresent returns true if the string is non-empty.
func IsStringPresent(strPtr *string) bool {
return strPtr != nil && len(*strPtr) > 0
return strPtr != nil && *strPtr != ""
}

// MongoDBRegionToAWSRegion converts region in US_EAST_1-like format to us-east-1-like
Expand Down
3 changes: 1 addition & 2 deletions internal/provider/provider_sdk2.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ import (
"github.com/mongodb/terraform-provider-mongodbatlas/internal/service/teams"
"github.com/mongodb/terraform-provider-mongodbatlas/internal/service/thirdpartyintegration"
"github.com/mongodb/terraform-provider-mongodbatlas/internal/service/x509authenticationdatabaseuser"
"github.com/mwielbut/pointy"
)

var (
Expand Down Expand Up @@ -323,7 +322,7 @@ func providerConfigure(ctx context.Context, d *schema.ResourceData) (any, diag.D
func setDefaultsAndValidations(d *schema.ResourceData) diag.Diagnostics {
diagnostics := []diag.Diagnostic{}

mongodbgovCloud := pointy.Bool(d.Get("is_mongodbgov_cloud").(bool))
mongodbgovCloud := conversion.Pointer(d.Get("is_mongodbgov_cloud").(bool))
if *mongodbgovCloud {
if err := d.Set("base_url", MongodbGovCloudURL); err != nil {
return append(diagnostics, diag.FromErr(err)...)
Expand Down
18 changes: 9 additions & 9 deletions internal/service/advancedcluster/common_advanced_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/mwielbut/pointy"
"github.com/mongodb/terraform-provider-mongodbatlas/internal/common/conversion"
"github.com/spf13/cast"
matlas "go.mongodb.org/atlas/mongodbatlas"
)
Expand Down Expand Up @@ -326,48 +326,48 @@ func ExpandProcessArgs(d *schema.ResourceData, p map[string]any) *matlas.Process
}

if _, ok := d.GetOkExists("advanced_configuration.0.fail_index_key_too_long"); ok {
res.FailIndexKeyTooLong = pointy.Bool(cast.ToBool(p["fail_index_key_too_long"]))
res.FailIndexKeyTooLong = conversion.Pointer(cast.ToBool(p["fail_index_key_too_long"]))
}

if _, ok := d.GetOkExists("advanced_configuration.0.javascript_enabled"); ok {
res.JavascriptEnabled = pointy.Bool(cast.ToBool(p["javascript_enabled"]))
res.JavascriptEnabled = conversion.Pointer(cast.ToBool(p["javascript_enabled"]))
}

if _, ok := d.GetOkExists("advanced_configuration.0.minimum_enabled_tls_protocol"); ok {
res.MinimumEnabledTLSProtocol = cast.ToString(p["minimum_enabled_tls_protocol"])
}

if _, ok := d.GetOkExists("advanced_configuration.0.no_table_scan"); ok {
res.NoTableScan = pointy.Bool(cast.ToBool(p["no_table_scan"]))
res.NoTableScan = conversion.Pointer(cast.ToBool(p["no_table_scan"]))
}

if _, ok := d.GetOkExists("advanced_configuration.0.sample_size_bi_connector"); ok {
res.SampleSizeBIConnector = pointy.Int64(cast.ToInt64(p["sample_size_bi_connector"]))
res.SampleSizeBIConnector = conversion.Pointer(cast.ToInt64(p["sample_size_bi_connector"]))
}

if _, ok := d.GetOkExists("advanced_configuration.0.sample_refresh_interval_bi_connector"); ok {
res.SampleRefreshIntervalBIConnector = pointy.Int64(cast.ToInt64(p["sample_refresh_interval_bi_connector"]))
res.SampleRefreshIntervalBIConnector = conversion.Pointer(cast.ToInt64(p["sample_refresh_interval_bi_connector"]))
}

if _, ok := d.GetOkExists("advanced_configuration.0.oplog_size_mb"); ok {
if sizeMB := cast.ToInt64(p["oplog_size_mb"]); sizeMB != 0 {
res.OplogSizeMB = pointy.Int64(cast.ToInt64(p["oplog_size_mb"]))
res.OplogSizeMB = conversion.Pointer(cast.ToInt64(p["oplog_size_mb"]))
} else {
log.Printf(ErrorClusterSetting, `oplog_size_mb`, "", cast.ToString(sizeMB))
}
}

if _, ok := d.GetOkExists("advanced_configuration.0.oplog_min_retention_hours"); ok {
if minRetentionHours := cast.ToFloat64(p["oplog_min_retention_hours"]); minRetentionHours >= 0 {
res.OplogMinRetentionHours = pointy.Float64(cast.ToFloat64(p["oplog_min_retention_hours"]))
res.OplogMinRetentionHours = conversion.Pointer(cast.ToFloat64(p["oplog_min_retention_hours"]))
} else {
log.Printf(ErrorClusterSetting, `oplog_min_retention_hours`, "", cast.ToString(minRetentionHours))
}
}

if _, ok := d.GetOkExists("advanced_configuration.0.transaction_lifetime_limit_seconds"); ok {
if transactionLifetimeLimitSeconds := cast.ToInt64(p["transaction_lifetime_limit_seconds"]); transactionLifetimeLimitSeconds > 0 {
res.TransactionLifetimeLimitSeconds = pointy.Int64(cast.ToInt64(p["transaction_lifetime_limit_seconds"]))
res.TransactionLifetimeLimitSeconds = conversion.Pointer(cast.ToInt64(p["transaction_lifetime_limit_seconds"]))
} else {
log.Printf(ErrorClusterSetting, `transaction_lifetime_limit_seconds`, "", cast.ToString(transactionLifetimeLimitSeconds))
}
Expand Down
51 changes: 25 additions & 26 deletions internal/service/advancedcluster/resource_advanced_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"github.com/mongodb/terraform-provider-mongodbatlas/internal/common/constant"
"github.com/mongodb/terraform-provider-mongodbatlas/internal/common/conversion"
"github.com/mongodb/terraform-provider-mongodbatlas/internal/config"
"github.com/mwielbut/pointy"
"github.com/spf13/cast"
matlas "go.mongodb.org/atlas/mongodbatlas"
"golang.org/x/exp/slices"
Expand Down Expand Up @@ -370,7 +369,7 @@ func resourceMongoDBAtlasAdvancedClusterCreate(ctx context.Context, d *schema.Re
}

if v, ok := d.GetOk("backup_enabled"); ok {
request.BackupEnabled = pointy.Bool(v.(bool))
request.BackupEnabled = conversion.Pointer(v.(bool))
}
if _, ok := d.GetOk("bi_connector_config"); ok {
biConnector, err := ExpandBiConnectorConfig(d)
Expand All @@ -380,7 +379,7 @@ func resourceMongoDBAtlasAdvancedClusterCreate(ctx context.Context, d *schema.Re
request.BiConnector = biConnector
}
if v, ok := d.GetOk("disk_size_gb"); ok {
request.DiskSizeGB = pointy.Float64(v.(float64))
request.DiskSizeGB = conversion.Pointer(v.(float64))
}
if v, ok := d.GetOk("encryption_at_rest_provider"); ok {
request.EncryptionAtRestProvider = v.(string)
Expand All @@ -398,13 +397,13 @@ func resourceMongoDBAtlasAdvancedClusterCreate(ctx context.Context, d *schema.Re
request.MongoDBMajorVersion = FormatMongoDBMajorVersion(v.(string))
}
if v, ok := d.GetOk("pit_enabled"); ok {
request.PitEnabled = pointy.Bool(v.(bool))
request.PitEnabled = conversion.Pointer(v.(bool))
}
if v, ok := d.GetOk("root_cert_type"); ok {
request.RootCertType = v.(string)
}
if v, ok := d.GetOk("termination_protection_enabled"); ok {
request.TerminationProtectionEnabled = pointy.Bool(v.(bool))
request.TerminationProtectionEnabled = conversion.Pointer(v.(bool))
}
if v, ok := d.GetOk("version_release_system"); ok {
request.VersionReleaseSystem = v.(string)
Expand Down Expand Up @@ -458,7 +457,7 @@ func resourceMongoDBAtlasAdvancedClusterCreate(ctx context.Context, d *schema.Re
// To pause a cluster
if v := d.Get("paused").(bool); v {
request = &matlas.AdvancedCluster{
Paused: pointy.Bool(v),
Paused: conversion.Pointer(v),
}

_, _, err = updateAdvancedCluster(ctx, conn, request, projectID, d.Get("name").(string), timeout)
Expand Down Expand Up @@ -646,7 +645,7 @@ func resourceMongoDBAtlasAdvancedClusterUpdate(ctx context.Context, d *schema.Re
clusterChangeDetect := new(matlas.AdvancedCluster)

if d.HasChange("backup_enabled") {
cluster.BackupEnabled = pointy.Bool(d.Get("backup_enabled").(bool))
cluster.BackupEnabled = conversion.Pointer(d.Get("backup_enabled").(bool))
}

if d.HasChange("bi_connector_config") {
Expand All @@ -658,7 +657,7 @@ func resourceMongoDBAtlasAdvancedClusterUpdate(ctx context.Context, d *schema.Re
}

if d.HasChange("disk_size_gb") {
cluster.DiskSizeGB = pointy.Float64(d.Get("disk_size_gb").(float64))
cluster.DiskSizeGB = conversion.Pointer(d.Get("disk_size_gb").(float64))
}

if d.HasChange("encryption_at_rest_provider") {
Expand All @@ -682,7 +681,7 @@ func resourceMongoDBAtlasAdvancedClusterUpdate(ctx context.Context, d *schema.Re
}

if d.HasChange("pit_enabled") {
cluster.PitEnabled = pointy.Bool(d.Get("pit_enabled").(bool))
cluster.PitEnabled = conversion.Pointer(d.Get("pit_enabled").(bool))
}

if d.HasChange("replication_specs") {
Expand All @@ -694,7 +693,7 @@ func resourceMongoDBAtlasAdvancedClusterUpdate(ctx context.Context, d *schema.Re
}

if d.HasChange("termination_protection_enabled") {
cluster.TerminationProtectionEnabled = pointy.Bool(d.Get("termination_protection_enabled").(bool))
cluster.TerminationProtectionEnabled = conversion.Pointer(d.Get("termination_protection_enabled").(bool))
}

if d.HasChange("version_release_system") {
Expand All @@ -706,7 +705,7 @@ func resourceMongoDBAtlasAdvancedClusterUpdate(ctx context.Context, d *schema.Re
}

if d.HasChange("paused") && !d.Get("paused").(bool) {
cluster.Paused = pointy.Bool(d.Get("paused").(bool))
cluster.Paused = conversion.Pointer(d.Get("paused").(bool))
}

timeout := d.Timeout(schema.TimeoutUpdate)
Expand Down Expand Up @@ -743,7 +742,7 @@ func resourceMongoDBAtlasAdvancedClusterUpdate(ctx context.Context, d *schema.Re

if d.Get("paused").(bool) {
clusterRequest := &matlas.AdvancedCluster{
Paused: pointy.Bool(true),
Paused: conversion.Pointer(true),
}

_, _, err := updateAdvancedCluster(ctx, conn, clusterRequest, projectID, clusterName, timeout)
Expand All @@ -765,7 +764,7 @@ func resourceMongoDBAtlasAdvancedClusterDelete(ctx context.Context, d *schema.Re
var options *matlas.DeleteAdvanceClusterOptions
if v, ok := d.GetOkExists("retain_backups_enabled"); ok {
options = &matlas.DeleteAdvanceClusterOptions{
RetainBackups: pointy.Bool(v.(bool)),
RetainBackups: conversion.Pointer(v.(bool)),
}
}

Expand Down Expand Up @@ -886,7 +885,7 @@ func expandRegionConfig(tfMap map[string]any) *matlas.AdvancedRegionConfig {

providerName := tfMap["provider_name"].(string)
apiObject := &matlas.AdvancedRegionConfig{
Priority: pointy.Int(cast.ToInt(tfMap["priority"])),
Priority: conversion.Pointer(cast.ToInt(tfMap["priority"])),
ProviderName: providerName,
RegionName: tfMap["region_name"].(string),
}
Expand Down Expand Up @@ -946,7 +945,7 @@ func expandRegionConfigSpec(tfList []any, providerName string) *matlas.Specs {

if providerName == "AWS" {
if v, ok := tfMap["disk_iops"]; ok && v.(int) > 0 {
apiObject.DiskIOPS = pointy.Int64(cast.ToInt64(v.(int)))
apiObject.DiskIOPS = conversion.Pointer(cast.ToInt64(v.(int)))
}
if v, ok := tfMap["ebs_volume_type"]; ok {
apiObject.EbsVolumeType = v.(string)
Expand All @@ -956,7 +955,7 @@ func expandRegionConfigSpec(tfList []any, providerName string) *matlas.Specs {
apiObject.InstanceSize = v.(string)
}
if v, ok := tfMap["node_count"]; ok {
apiObject.NodeCount = pointy.Int(v.(int))
apiObject.NodeCount = conversion.Pointer(v.(int))
}

return apiObject
Expand All @@ -974,13 +973,13 @@ func expandRegionConfigAutoScaling(tfList []any) *matlas.AdvancedAutoScaling {
compute := &matlas.Compute{}

if v, ok := tfMap["disk_gb_enabled"]; ok {
diskGB.Enabled = pointy.Bool(v.(bool))
diskGB.Enabled = conversion.Pointer(v.(bool))
}
if v, ok := tfMap["compute_enabled"]; ok {
compute.Enabled = pointy.Bool(v.(bool))
compute.Enabled = conversion.Pointer(v.(bool))
}
if v, ok := tfMap["compute_scale_down_enabled"]; ok {
compute.ScaleDownEnabled = pointy.Bool(v.(bool))
compute.ScaleDownEnabled = conversion.Pointer(v.(bool))
}
if v, ok := tfMap["compute_min_instance_size"]; ok {
value := compute.ScaleDownEnabled
Expand Down Expand Up @@ -1011,19 +1010,19 @@ func flattenAdvancedReplicationSpec(ctx context.Context, apiObject *matlas.Advan
tfMap["num_shards"] = apiObject.NumShards
tfMap["id"] = apiObject.ID
if tfMapObject != nil {
object, containerIds, err := flattenAdvancedReplicationSpecRegionConfigs(ctx, apiObject.RegionConfigs, tfMapObject["region_configs"].([]any), d, conn)
object, containerIDs, err := flattenAdvancedReplicationSpecRegionConfigs(ctx, apiObject.RegionConfigs, tfMapObject["region_configs"].([]any), d, conn)
if err != nil {
return nil, err
}
tfMap["region_configs"] = object
tfMap["container_id"] = containerIds
tfMap["container_id"] = containerIDs
} else {
object, containerIds, err := flattenAdvancedReplicationSpecRegionConfigs(ctx, apiObject.RegionConfigs, nil, d, conn)
object, containerIDs, err := flattenAdvancedReplicationSpecRegionConfigs(ctx, apiObject.RegionConfigs, nil, d, conn)
if err != nil {
return nil, err
}
tfMap["region_configs"] = object
tfMap["container_id"] = containerIds
tfMap["container_id"] = containerIDs
}
tfMap["zone_name"] = apiObject.ZoneName

Expand Down Expand Up @@ -1149,7 +1148,7 @@ func flattenAdvancedReplicationSpecRegionConfigs(ctx context.Context, apiObjects
}

var tfList []map[string]any
containerIds := make(map[string]string)
containerIDs := make(map[string]string)

for i, apiObject := range apiObjects {
if apiObject == nil {
Expand All @@ -1171,12 +1170,12 @@ func flattenAdvancedReplicationSpecRegionConfigs(ctx context.Context, apiObjects
}
if result := getAdvancedClusterContainerID(containers, apiObject); result != "" {
// Will print as "providerName:regionName" = "containerId" in terraform show
containerIds[fmt.Sprintf("%s:%s", apiObject.ProviderName, apiObject.RegionName)] = result
containerIDs[fmt.Sprintf("%s:%s", apiObject.ProviderName, apiObject.RegionName)] = result
}
}
}

return tfList, containerIds, nil
return tfList, containerIDs, nil
}

func flattenAdvancedReplicationSpecRegionConfigSpec(apiObject *matlas.Specs, providerName string, tfMapObjects []any) []map[string]any {
Expand Down
Loading

0 comments on commit 95098f6

Please sign in to comment.