Skip to content

Commit

Permalink
chore: Updates Atlas Go SDK to version 2024-08-05 (#2487)
Browse files Browse the repository at this point in the history
* automatic changes with renaming

* fix trivial compilation errors

* include 2024-05-30 version and adjust cloud-backup-schedule to use old SDK

* adjust global-cluster-config to use old API

* adjust advanced-cluster to use old API

* fix hcl config generation remove num_shards attribute
  • Loading branch information
AgustinBettati authored Aug 9, 2024
1 parent 2059bf6 commit 5f715ab
Show file tree
Hide file tree
Showing 148 changed files with 488 additions and 436 deletions.
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ require (
github.com/stretchr/testify v1.9.0
github.com/zclconf/go-cty v1.15.0
go.mongodb.org/atlas v0.36.0
go.mongodb.org/atlas-sdk/v20240530002 v20240530002.0.0
go.mongodb.org/atlas-sdk/v20240530005 v20240530005.0.0
go.mongodb.org/atlas-sdk/v20240805001 v20240805001.0.0
go.mongodb.org/realm v0.1.0
)

Expand Down
6 changes: 4 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -780,8 +780,10 @@ github.com/zclconf/go-cty-yaml v1.0.2/go.mod h1:IP3Ylp0wQpYm50IHK8OZWKMu6sPJIUgK
go.mongodb.org/atlas v0.12.0/go.mod h1:wVCnHcm/7/IfTjEB6K8K35PLG70yGz8BdkRwX0oK9/M=
go.mongodb.org/atlas v0.36.0 h1:m05S3AO7zkl+bcG1qaNsEKBnAqnKx2FDwLooHpIG3j4=
go.mongodb.org/atlas v0.36.0/go.mod h1:nfPldE9dSama6G2IbIzmEza02Ly7yFZjMMVscaM0uEc=
go.mongodb.org/atlas-sdk/v20240530002 v20240530002.0.0 h1:D+e3bpRwa9WH3HHs8bLjOdjTp1vdlp83ZYithzGbaQ8=
go.mongodb.org/atlas-sdk/v20240530002 v20240530002.0.0/go.mod h1:seuG5HpfG20/8FhJGyWi4yL7hqAcmq7pf/G0gipNOyM=
go.mongodb.org/atlas-sdk/v20240530005 v20240530005.0.0 h1:d/gbYJ+obR0EM/3DZf7+ZMi2QWISegm3mid7Or708cc=
go.mongodb.org/atlas-sdk/v20240530005 v20240530005.0.0/go.mod h1:O47ZrMMfcWb31wznNIq2PQkkdoFoK0ea2GlmRqGJC2s=
go.mongodb.org/atlas-sdk/v20240805001 v20240805001.0.0 h1:EwA2g7i4JYc0b/oE7zvvOH+POYVrHrWR7BONex3MFTA=
go.mongodb.org/atlas-sdk/v20240805001 v20240805001.0.0/go.mod h1:0aHEphVfsYbpg3CiEUcXeAU7OVoOFig1tltXdLjYiSQ=
go.mongodb.org/realm v0.1.0 h1:zJiXyLaZrznQ+Pz947ziSrDKUep39DO4SfA0Fzx8M4M=
go.mongodb.org/realm v0.1.0/go.mod h1:4Vj6iy+Puo1TDERcoh4XZ+pjtwbOzPpzqy3Cwe8ZmDM=
go.opencensus.io v0.21.0/go.mod h1:mSImk1erAIZhrmZN+AvHh14ztQfjbGwt4TtuofqLduU=
Expand Down
28 changes: 27 additions & 1 deletion internal/common/conversion/flatten_expand.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ package conversion
import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"

"go.mongodb.org/atlas-sdk/v20240530002/admin"
admin20240530 "go.mongodb.org/atlas-sdk/v20240530005/admin"
"go.mongodb.org/atlas-sdk/v20240805001/admin"
)

func FlattenLinks(links []admin.Link) []map[string]string {
Expand All @@ -28,6 +29,17 @@ func FlattenTags(tags []admin.ResourceTag) []map[string]string {
return ret
}

func FlattenTagsOldSDK(tags []admin20240530.ResourceTag) []map[string]string {
ret := make([]map[string]string, len(tags))
for i, tag := range tags {
ret[i] = map[string]string{
"key": tag.GetKey(),
"value": tag.GetValue(),
}
}
return ret
}

func ExpandTagsFromSetSchema(d *schema.ResourceData) *[]admin.ResourceTag {
list := d.Get("tags").(*schema.Set)
ret := make([]admin.ResourceTag, list.Len())
Expand All @@ -41,6 +53,20 @@ func ExpandTagsFromSetSchema(d *schema.ResourceData) *[]admin.ResourceTag {
return &ret
}

// this will be removed once ISS dev branch is merged
func ExpandTagsFromSetSchemaOldSDK(d *schema.ResourceData) *[]admin20240530.ResourceTag {
list := d.Get("tags").(*schema.Set)
ret := make([]admin20240530.ResourceTag, list.Len())
for i, item := range list.List() {
tag := item.(map[string]any)
ret[i] = admin20240530.ResourceTag{
Key: tag["key"].(string),
Value: tag["value"].(string),
}
}
return &ret
}

func ExpandStringList(list []any) (res []string) {
for _, v := range list {
res = append(res, v.(string))
Expand Down
38 changes: 31 additions & 7 deletions internal/config/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import (
"strings"
"time"

"go.mongodb.org/atlas-sdk/v20240530002/admin"
admin20240530 "go.mongodb.org/atlas-sdk/v20240530005/admin"
"go.mongodb.org/atlas-sdk/v20240805001/admin"
matlasClient "go.mongodb.org/atlas/mongodbatlas"
realmAuth "go.mongodb.org/realm/auth"
"go.mongodb.org/realm/realm"
Expand All @@ -28,9 +29,10 @@ const (

// MongoDBClient contains the mongodbatlas clients and configurations
type MongoDBClient struct {
Atlas *matlasClient.Client
AtlasV2 *admin.APIClient
Config *Config
Atlas *matlasClient.Client
AtlasV2 *admin.APIClient
AtlasV220240530 *admin20240530.APIClient // used in advanced_cluster and cloud_backup_schedule for avoiding breaking changes
Config *Config
}

// Config contains the configurations needed to use SDKs
Expand Down Expand Up @@ -103,10 +105,16 @@ func (c *Config) NewClient(ctx context.Context) (any, error) {
return nil, err
}

sdkV220240530Client, err := c.newSDKV220240530Client(client)
if err != nil {
return nil, err
}

clients := &MongoDBClient{
Atlas: atlasClient,
AtlasV2: sdkV2Client,
Config: c,
Atlas: atlasClient,
AtlasV2: sdkV2Client,
AtlasV220240530: sdkV220240530Client,
Config: c,
}

return clients, nil
Expand All @@ -128,6 +136,22 @@ func (c *Config) newSDKV2Client(client *http.Client) (*admin.APIClient, error) {
return sdkv2, nil
}

func (c *Config) newSDKV220240530Client(client *http.Client) (*admin20240530.APIClient, error) {
opts := []admin20240530.ClientModifier{
admin20240530.UseHTTPClient(client),
admin20240530.UseUserAgent(userAgent(c)),
admin20240530.UseBaseURL(c.BaseURL),
admin20240530.UseDebug(false)}

// Initialize the MongoDB Versioned Atlas Client.
sdkv2, err := admin20240530.NewClient(opts...)
if err != nil {
return nil, err
}

return sdkv2, nil
}

func (c *MongoDBClient) GetRealmClient(ctx context.Context) (*realm.Client, error) {
// Realm
if c.Config.PublicKey == "" && c.Config.PrivateKey == "" {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/mongodb/terraform-provider-mongodbatlas/internal/common/conversion"
"github.com/mongodb/terraform-provider-mongodbatlas/internal/config"

"go.mongodb.org/atlas-sdk/v20240530002/admin"
"go.mongodb.org/atlas-sdk/v20240805001/admin"
)

func PluralDataSource() *schema.Resource {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/mongodb/terraform-provider-mongodbatlas/internal/common/conversion"
"github.com/mongodb/terraform-provider-mongodbatlas/internal/config"
"go.mongodb.org/atlas-sdk/v20240530002/admin"
"go.mongodb.org/atlas-sdk/v20240805001/admin"
)

func Resource() *schema.Resource {
Expand Down
10 changes: 5 additions & 5 deletions internal/service/advancedcluster/data_source_advanced_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,11 +234,11 @@ func DataSource() *schema.Resource {
}

func dataSourceRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
connV2 := meta.(*config.MongoDBClient).AtlasV2
connV220240530 := meta.(*config.MongoDBClient).AtlasV220240530
projectID := d.Get("project_id").(string)
clusterName := d.Get("name").(string)

cluster, resp, err := connV2.ClustersApi.GetCluster(ctx, projectID, clusterName).Execute()
cluster, resp, err := connV220240530.ClustersApi.GetCluster(ctx, projectID, clusterName).Execute()
if err != nil {
if resp != nil && resp.StatusCode == http.StatusNotFound {
return nil
Expand Down Expand Up @@ -278,7 +278,7 @@ func dataSourceRead(ctx context.Context, d *schema.ResourceData, meta any) diag.
return diag.FromErr(fmt.Errorf(ErrorClusterAdvancedSetting, "labels", clusterName, err))
}

if err := d.Set("tags", conversion.FlattenTags(cluster.GetTags())); err != nil {
if err := d.Set("tags", conversion.FlattenTagsOldSDK(cluster.GetTags())); err != nil {
return diag.FromErr(fmt.Errorf(ErrorClusterAdvancedSetting, "tags", clusterName, err))
}

Expand All @@ -302,7 +302,7 @@ func dataSourceRead(ctx context.Context, d *schema.ResourceData, meta any) diag.
return diag.FromErr(fmt.Errorf(ErrorClusterAdvancedSetting, "pit_enabled", clusterName, err))
}

replicationSpecs, err := FlattenAdvancedReplicationSpecs(ctx, cluster.GetReplicationSpecs(), d.Get("replication_specs").([]any), d, connV2)
replicationSpecs, err := FlattenAdvancedReplicationSpecs(ctx, cluster.GetReplicationSpecs(), d.Get("replication_specs").([]any), d, connV220240530)
if err != nil {
return diag.FromErr(fmt.Errorf(ErrorClusterAdvancedSetting, "replication_specs", clusterName, err))
}
Expand All @@ -328,7 +328,7 @@ func dataSourceRead(ctx context.Context, d *schema.ResourceData, meta any) diag.
return diag.FromErr(fmt.Errorf(ErrorClusterAdvancedSetting, "global_cluster_self_managed_sharding", clusterName, err))
}

processArgs, _, err := connV2.ClustersApi.GetClusterAdvancedConfiguration(ctx, projectID, clusterName).Execute()
processArgs, _, err := connV220240530.ClustersApi.GetClusterAdvancedConfiguration(ctx, projectID, clusterName).Execute()
if err != nil {
return diag.FromErr(fmt.Errorf(ErrorAdvancedConfRead, clusterName, err))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ 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"
"go.mongodb.org/atlas-sdk/v20240530002/admin"
admin20240530 "go.mongodb.org/atlas-sdk/v20240530005/admin"
)

func PluralDataSource() *schema.Resource {
Expand Down Expand Up @@ -245,33 +245,33 @@ func PluralDataSource() *schema.Resource {
}

func dataSourcePluralRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
connV2 := meta.(*config.MongoDBClient).AtlasV2
connV220240530 := meta.(*config.MongoDBClient).AtlasV220240530
projectID := d.Get("project_id").(string)
d.SetId(id.UniqueId())

list, resp, err := connV2.ClustersApi.ListClusters(ctx, projectID).Execute()
list, resp, err := connV220240530.ClustersApi.ListClusters(ctx, projectID).Execute()
if err != nil {
if resp != nil && resp.StatusCode == http.StatusNotFound {
return nil
}
return diag.FromErr(fmt.Errorf("error reading advanced cluster list for project(%s): %s", projectID, err))
}
if err := d.Set("results", flattenAdvancedClusters(ctx, connV2, list.GetResults(), d)); err != nil {
if err := d.Set("results", flattenAdvancedClusters(ctx, connV220240530, list.GetResults(), d)); err != nil {
return diag.FromErr(fmt.Errorf(ErrorClusterAdvancedSetting, "results", d.Id(), err))
}

return nil
}

func flattenAdvancedClusters(ctx context.Context, connV2 *admin.APIClient, clusters []admin.AdvancedClusterDescription, d *schema.ResourceData) []map[string]any {
func flattenAdvancedClusters(ctx context.Context, connV220240530 *admin20240530.APIClient, clusters []admin20240530.AdvancedClusterDescription, d *schema.ResourceData) []map[string]any {
results := make([]map[string]any, 0, len(clusters))
for i := range clusters {
cluster := &clusters[i]
processArgs, _, err := connV2.ClustersApi.GetClusterAdvancedConfiguration(ctx, cluster.GetGroupId(), cluster.GetName()).Execute()
processArgs, _, err := connV220240530.ClustersApi.GetClusterAdvancedConfiguration(ctx, cluster.GetGroupId(), cluster.GetName()).Execute()
if err != nil {
log.Printf("[WARN] Error setting `advanced_configuration` for the cluster(%s): %s", cluster.GetId(), err)
}
replicationSpecs, err := FlattenAdvancedReplicationSpecs(ctx, cluster.GetReplicationSpecs(), nil, d, connV2)
replicationSpecs, err := FlattenAdvancedReplicationSpecs(ctx, cluster.GetReplicationSpecs(), nil, d, connV220240530)
if err != nil {
log.Printf("[WARN] Error setting `replication_specs` for the cluster(%s): %s", cluster.GetId(), err)
}
Expand All @@ -286,7 +286,7 @@ func flattenAdvancedClusters(ctx context.Context, connV2 *admin.APIClient, clust
"disk_size_gb": cluster.GetDiskSizeGB(),
"encryption_at_rest_provider": cluster.GetEncryptionAtRestProvider(),
"labels": flattenLabels(cluster.GetLabels()),
"tags": conversion.FlattenTags(cluster.GetTags()),
"tags": conversion.FlattenTagsOldSDK(cluster.GetTags()),
"mongo_db_major_version": cluster.GetMongoDBMajorVersion(),
"mongo_db_version": cluster.GetMongoDBVersion(),
"name": cluster.GetName(),
Expand Down
Loading

0 comments on commit 5f715ab

Please sign in to comment.