Skip to content

Commit

Permalink
refactor: upgrade SDK and shorten method names
Browse files Browse the repository at this point in the history
  • Loading branch information
EspenAlbert committed May 6, 2024
1 parent 5055793 commit 0cc3232
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ import (

func DataSource() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceMongoDBAtlasFederatedDatabaseQueryLimitRead,
Schema: schemaMongoDBAtlasFederatedDatabaseQueryLimitDataSource(),
ReadContext: dataSourceRead,
Schema: schemaDataSource(),
}
}

func schemaMongoDBAtlasFederatedDatabaseQueryLimitDataSource() map[string]*schema.Schema {
func schemaDataSource() map[string]*schema.Schema {
return map[string]*schema.Schema{
"project_id": {
Type: schema.TypeString,
Expand Down Expand Up @@ -58,14 +58,14 @@ func schemaMongoDBAtlasFederatedDatabaseQueryLimitDataSource() map[string]*schem
}
}

func dataSourceMongoDBAtlasFederatedDatabaseQueryLimitRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
conn := meta.(*config.MongoDBClient).Atlas
func dataSourceRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
conn := meta.(*config.MongoDBClient).AtlasV2

projectID := d.Get("project_id").(string)
tenantName := d.Get("tenant_name").(string)
limitName := d.Get("limit_name").(string)

queryLimit, _, err := conn.DataFederation.GetQueryLimit(ctx, projectID, tenantName, limitName)
queryLimit, _, err := conn.DataFederationApi.ReturnFederatedDatabaseQueryLimit(ctx, projectID, tenantName, limitName).Execute()

if err != nil {
return diag.FromErr(fmt.Errorf("couldn't import federated database query limit(%s) for project (%s), tenant (%s), error: %s", limitName, projectID, tenantName, err))
Expand All @@ -77,7 +77,7 @@ func dataSourceMongoDBAtlasFederatedDatabaseQueryLimitRead(ctx context.Context,

d.SetId(conversion.EncodeStateID(map[string]string{
"project_id": projectID,
"tenant_name": queryLimit.TenantName,
"tenant_name": queryLimit.GetTenantName(),
"limit_name": queryLimit.Name,
}))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/id"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/mongodb/terraform-provider-mongodbatlas/internal/common/conversion"
"github.com/mongodb/terraform-provider-mongodbatlas/internal/config"
matlas "go.mongodb.org/atlas/mongodbatlas"
"go.mongodb.org/atlas-sdk/v20231115012/admin"
)

func PluralDataSource() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceMongoDBAtlasFederatedDatabaseQueryLimitsRead,
ReadContext: dataSourcesRead,
Schema: map[string]*schema.Schema{
"project_id": {
Type: schema.TypeString,
Expand All @@ -27,25 +28,25 @@ func PluralDataSource() *schema.Resource {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: schemaMongoDBAtlasFederatedDatabaseQueryLimitDataSource(),
Schema: schemaDataSource(),
},
},
},
}
}

func dataSourceMongoDBAtlasFederatedDatabaseQueryLimitsRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
conn := meta.(*config.MongoDBClient).Atlas
func dataSourcesRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
conn := meta.(*config.MongoDBClient).AtlasV2

projectID := d.Get("project_id").(string)
tenantName := d.Get("tenant_name").(string)

queryLimits, _, err := conn.DataFederation.ListQueryLimits(ctx, projectID, tenantName)
queryLimits, _, err := conn.DataFederationApi.ReturnFederatedDatabaseQueryLimits(ctx, projectID, tenantName).Execute()
if err != nil {
return diag.FromErr(fmt.Errorf("error getting federated database query limits for project (%s), tenant (%s), error: %s", projectID, tenantName, err))
}

if results := flattenFederatedDatabaseQueryLimits(projectID, tenantName, queryLimits); results != nil {
if results := flattenFederatedDatabaseQueryLimits(projectID, queryLimits); results != nil {
if err := d.Set("results", results); err != nil {
return diag.FromErr(fmt.Errorf(errorFederatedDatabaseQueryLimit, "results", projectID, err))
}
Expand All @@ -56,7 +57,7 @@ func dataSourceMongoDBAtlasFederatedDatabaseQueryLimitsRead(ctx context.Context,
return nil
}

func flattenFederatedDatabaseQueryLimits(projectID, tenantName string, queryLimits []*matlas.DataFederationQueryLimit) []map[string]any {
func flattenFederatedDatabaseQueryLimits(projectID string, queryLimits []admin.DataFederationTenantQueryLimit) []map[string]any {
var federatedDatabaseQueryLimitMap []map[string]any
if len(queryLimits) == 0 {
return federatedDatabaseQueryLimitMap
Expand All @@ -66,14 +67,14 @@ func flattenFederatedDatabaseQueryLimits(projectID, tenantName string, queryLimi
for i := range queryLimits {
federatedDatabaseQueryLimitMap[i] = map[string]any{
"project_id": projectID,
"tenant_name": queryLimits[i].TenantName,
"tenant_name": queryLimits[i].GetTenantName(),
"limit_name": queryLimits[i].Name,
"overrun_policy": queryLimits[i].OverrunPolicy,
"overrun_policy": queryLimits[i].GetOverrunPolicy(),
"value": queryLimits[i].Value,
"current_usage": queryLimits[i].CurrentUsage,
"default_limit": queryLimits[i].DefaultLimit,
"last_modified_date": queryLimits[i].LastModifiedDate,
"maximum_limit": queryLimits[i].MaximumLimit,
"current_usage": queryLimits[i].GetCurrentUsage(),
"default_limit": queryLimits[i].GetDefaultLimit(),
"last_modified_date": conversion.TimeToString(queryLimits[i].GetLastModifiedDate()),
"maximum_limit": queryLimits[i].GetMaximumLimit(),
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/mongodb/terraform-provider-mongodbatlas/internal/common/conversion"
"github.com/mongodb/terraform-provider-mongodbatlas/internal/config"
matlas "go.mongodb.org/atlas/mongodbatlas"
"go.mongodb.org/atlas-sdk/v20231115012/admin"
)

const (
Expand All @@ -24,12 +24,12 @@ const (

func Resource() *schema.Resource {
return &schema.Resource{
CreateContext: resourceMongoDBFederatedDatabaseQueryLimitCreate,
ReadContext: resourceMongoDBFederatedDatabaseQueryLimitRead,
UpdateContext: resourceMongoDBFederatedDatabaseQueryLimitUpdate,
DeleteContext: resourceMongoDBFederatedDatabaseQueryLimitDelete,
CreateContext: resourceCreate,
ReadContext: resourceRead,
UpdateContext: resourceUpdate,
DeleteContext: resourceDelete,
Importer: &schema.ResourceImporter{
StateContext: resourceMongoDBAtlasFederatedDatabaseQueryLimitImportState,
StateContext: resourceImportState,
},
Schema: map[string]*schema.Schema{
"project_id": {
Expand Down Expand Up @@ -72,39 +72,42 @@ func Resource() *schema.Resource {
}
}

func resourceMongoDBFederatedDatabaseQueryLimitCreate(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
conn := meta.(*config.MongoDBClient).Atlas
func resourceCreate(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
return createOrUpdate(ctx, meta, d, errorFederatedDatabaseQueryLimitCreate)
}

func createOrUpdate(ctx context.Context, meta any, d *schema.ResourceData, errorTemplate string) diag.Diagnostics {
conn := meta.(*config.MongoDBClient).AtlasV2
projectID := d.Get(string("project_id")).(string)
tenantName := d.Get("tenant_name").(string)
limitName := d.Get("limit_name").(string)

requestBody := &matlas.DataFederationQueryLimit{
OverrunPolicy: d.Get("overrun_policy").(string),
requestBody := &admin.DataFederationTenantQueryLimit{
OverrunPolicy: conversion.StringPtr(d.Get("overrun_policy").(string)),
Value: int64(d.Get("value").(int)),
}

federatedDatabaseQueryLimit, _, err := conn.DataFederation.ConfigureQueryLimit(ctx, projectID, tenantName, limitName, requestBody)
federatedDatabaseQueryLimit, _, err := conn.DataFederationApi.CreateOneDataFederationQueryLimit(ctx, projectID, tenantName, limitName, requestBody).Execute()
if err != nil {
return diag.FromErr(fmt.Errorf(errorFederatedDatabaseQueryLimitCreate, limitName, err))
return diag.FromErr(fmt.Errorf(errorTemplate, limitName, err))
}
d.SetId(conversion.EncodeStateID(map[string]string{
"project_id": projectID,
"tenant_name": federatedDatabaseQueryLimit.TenantName,
"tenant_name": federatedDatabaseQueryLimit.GetTenantName(),
"limit_name": federatedDatabaseQueryLimit.Name,
}))

return resourceMongoDBFederatedDatabaseQueryLimitRead(ctx, d, meta)
return resourceRead(ctx, d, meta)
}

func resourceMongoDBFederatedDatabaseQueryLimitRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
conn := meta.(*config.MongoDBClient).Atlas
func resourceRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
conn := meta.(*config.MongoDBClient).AtlasV2
ids := conversion.DecodeStateID(d.Id())
projectID := ids["project_id"]
tenantName := ids["tenant_name"]
limitName := ids["limit_name"]

queryLimit, resp, err := conn.DataFederation.GetQueryLimit(ctx, projectID, tenantName, limitName)
queryLimit, resp, err := conn.DataFederationApi.ReturnFederatedDatabaseQueryLimit(ctx, projectID, tenantName, limitName).Execute()
if err != nil {
if resp != nil && resp.StatusCode == http.StatusNotFound {
d.SetId("")
Expand All @@ -119,50 +122,34 @@ func resourceMongoDBFederatedDatabaseQueryLimitRead(ctx context.Context, d *sche

d.SetId(conversion.EncodeStateID(map[string]string{
"project_id": projectID,
"tenant_name": queryLimit.TenantName,
"tenant_name": queryLimit.GetTenantName(),
"limit_name": queryLimit.Name,
}))

return nil
}

func resourceMongoDBFederatedDatabaseQueryLimitUpdate(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
conn := meta.(*config.MongoDBClient).Atlas

ids := conversion.DecodeStateID(d.Id())
projectID := ids["project_id"]
tenantName := ids["tenant_name"]
limitName := ids["limit_name"]

requestBody := &matlas.DataFederationQueryLimit{
OverrunPolicy: d.Get("overrun_policy").(string),
Value: int64(d.Get("value").(int)),
}

if _, _, err := conn.DataFederation.ConfigureQueryLimit(ctx, projectID, tenantName, limitName, requestBody); err != nil {
return diag.FromErr(fmt.Errorf(errorFederatedDatabaseQueryLimitUpdate, limitName, err))
}

return resourceMongoDBFederatedDatabaseQueryLimitRead(ctx, d, meta)
func resourceUpdate(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
return createOrUpdate(ctx, meta, d, errorFederatedDatabaseQueryLimitUpdate)
}

func resourceMongoDBFederatedDatabaseQueryLimitDelete(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
conn := meta.(*config.MongoDBClient).Atlas
func resourceDelete(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
conn := meta.(*config.MongoDBClient).AtlasV2

ids := conversion.DecodeStateID(d.Id())
projectID := ids["project_id"]
tenantName := ids["tenant_name"]
limitName := ids["limit_name"]

if _, err := conn.DataFederation.DeleteQueryLimit(ctx, projectID, tenantName, limitName); err != nil {
if _, _, err := conn.DataFederationApi.DeleteOneDataFederationInstanceQueryLimit(ctx, projectID, tenantName, limitName).Execute(); err != nil {
return diag.FromErr(fmt.Errorf(errorFederatedDatabaseQueryLimitDelete, limitName, err))
}

return nil
}

func resourceMongoDBAtlasFederatedDatabaseQueryLimitImportState(ctx context.Context, d *schema.ResourceData, meta any) ([]*schema.ResourceData, error) {
conn := meta.(*config.MongoDBClient).Atlas
func resourceImportState(ctx context.Context, d *schema.ResourceData, meta any) ([]*schema.ResourceData, error) {
conn := meta.(*config.MongoDBClient).AtlasV2
parts := strings.Split(d.Id(), "--")

var projectID, tenantName, limitName string
Expand All @@ -172,7 +159,7 @@ func resourceMongoDBAtlasFederatedDatabaseQueryLimitImportState(ctx context.Cont
}
projectID, tenantName, limitName = parts[0], parts[1], parts[2]

queryLimit, _, err := conn.DataFederation.GetQueryLimit(ctx, projectID, tenantName, limitName)
queryLimit, _, err := conn.DataFederationApi.ReturnFederatedDatabaseQueryLimit(ctx, projectID, tenantName, limitName).Execute()

if err != nil {
return nil, fmt.Errorf("couldn't import federated database query limit(%s) for project (%s), tenant (%s), error: %s", limitName, projectID, tenantName, err)
Expand All @@ -184,14 +171,14 @@ func resourceMongoDBAtlasFederatedDatabaseQueryLimitImportState(ctx context.Cont

d.SetId(conversion.EncodeStateID(map[string]string{
"project_id": projectID,
"tenant_name": queryLimit.TenantName,
"tenant_name": queryLimit.GetTenantName(),
"limit_name": queryLimit.Name,
}))

return []*schema.ResourceData{d}, nil
}

func setResourceFieldsFromFederatedDatabaseQueryLimit(d *schema.ResourceData, projectID string, queryLimit *matlas.DataFederationQueryLimit) error {
func setResourceFieldsFromFederatedDatabaseQueryLimit(d *schema.ResourceData, projectID string, queryLimit *admin.DataFederationTenantQueryLimit) error {
if err := d.Set("project_id", projectID); err != nil {
return fmt.Errorf(errorFederatedDatabaseQueryLimit, "project_id", d.Id(), err)
}
Expand All @@ -200,31 +187,31 @@ func setResourceFieldsFromFederatedDatabaseQueryLimit(d *schema.ResourceData, pr
return fmt.Errorf(errorFederatedDatabaseQueryLimit, "limit_name", d.Id(), err)
}

if err := d.Set("tenant_name", queryLimit.TenantName); err != nil {
if err := d.Set("tenant_name", queryLimit.GetTenantName()); err != nil {
return fmt.Errorf(errorFederatedDatabaseQueryLimit, "tenant_name", d.Id(), err)
}

if err := d.Set("overrun_policy", queryLimit.OverrunPolicy); err != nil {
if err := d.Set("overrun_policy", queryLimit.GetOverrunPolicy()); err != nil {
return fmt.Errorf(errorFederatedDatabaseQueryLimit, "overrun_policy", d.Id(), err)
}

if err := d.Set("value", queryLimit.Value); err != nil {
return fmt.Errorf(errorFederatedDatabaseQueryLimit, "value", d.Id(), err)
}

if err := d.Set("current_usage", queryLimit.CurrentUsage); err != nil {
if err := d.Set("current_usage", queryLimit.GetCurrentUsage()); err != nil {
return fmt.Errorf(errorFederatedDatabaseQueryLimit, "current_usage", d.Id(), err)
}

if err := d.Set("default_limit", queryLimit.DefaultLimit); err != nil {
if err := d.Set("default_limit", queryLimit.GetDefaultLimit()); err != nil {
return fmt.Errorf(errorFederatedDatabaseQueryLimit, "default_limit", d.Id(), err)
}

if err := d.Set("last_modified_date", queryLimit.LastModifiedDate); err != nil {
if err := d.Set("last_modified_date", conversion.TimeToString(queryLimit.GetLastModifiedDate())); err != nil {
return fmt.Errorf(errorFederatedDatabaseQueryLimit, "last_modified_date", d.Id(), err)
}

if err := d.Set("maximum_limit", queryLimit.MaximumLimit); err != nil {
if err := d.Set("maximum_limit", queryLimit.GetMaximumLimit()); err != nil {
return fmt.Errorf(errorFederatedDatabaseQueryLimit, "maximum_limit", d.Id(), err)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ func checkDestroy(s *terraform.State) error {
continue
}
ids := conversion.DecodeStateID(rs.Primary.ID)
_, _, err := acc.Conn().DataFederation.GetQueryLimit(context.Background(), ids["project_id"], ids["tenant_name"], ids["limit_name"])
_, _, err := acc.ConnV2().DataFederationApi.ReturnFederatedDatabaseQueryLimit(context.Background(), ids["project_id"], ids["tenant_name"], ids["limit_name"]).Execute()
if err == nil {
return fmt.Errorf("federated database query limit (%s) for project (%s) and tenant (%s)still exists", ids["project_id"], ids["tenant_name"], ids["limit_name"])
}
Expand Down

0 comments on commit 0cc3232

Please sign in to comment.