Skip to content

Commit 2bcc0c5

Browse files
authored
chore: Upgrades custom_db_role resource to auto-generated SDK (#1896)
* rename * enable skipped test * migration test * remove redundant checks * fix migration test * connV2 in tests * read * data source * import * delete * create & update
1 parent ded96f5 commit 2bcc0c5

7 files changed

+193
-214
lines changed

internal/service/customdbrole/data_source_custom_db_role.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212

1313
func DataSource() *schema.Resource {
1414
return &schema.Resource{
15-
ReadContext: dataSourceMongoDBAtlasCustomDBRoleRead,
15+
ReadContext: dataSourceRead,
1616
Schema: map[string]*schema.Schema{
1717
"project_id": {
1818
Type: schema.TypeString,
@@ -74,12 +74,12 @@ func DataSource() *schema.Resource {
7474
}
7575
}
7676

77-
func dataSourceMongoDBAtlasCustomDBRoleRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
78-
conn := meta.(*config.MongoDBClient).Atlas
77+
func dataSourceRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
78+
connV2 := meta.(*config.MongoDBClient).AtlasV2
7979
projectID := d.Get("project_id").(string)
8080
roleName := d.Get("role_name").(string)
8181

82-
customDBRole, _, err := conn.CustomDBRoles.Get(ctx, projectID, roleName)
82+
customDBRole, _, err := connV2.CustomDatabaseRolesApi.GetCustomDatabaseRole(ctx, projectID, roleName).Execute()
8383
if err != nil {
8484
return diag.FromErr(fmt.Errorf("error getting custom db role information: %s", err))
8585
}
@@ -88,11 +88,11 @@ func dataSourceMongoDBAtlasCustomDBRoleRead(ctx context.Context, d *schema.Resou
8888
return diag.FromErr(fmt.Errorf("error setting `role_name` for custom db role (%s): %s", d.Id(), err))
8989
}
9090

91-
if err := d.Set("actions", flattenActions(customDBRole.Actions)); err != nil {
91+
if err := d.Set("actions", flattenActions(customDBRole.GetActions())); err != nil {
9292
return diag.FromErr(fmt.Errorf("error setting `actions` for custom db role (%s): %s", d.Id(), err))
9393
}
9494

95-
if err := d.Set("inherited_roles", flattenInheritedRoles(customDBRole.InheritedRoles)); err != nil {
95+
if err := d.Set("inherited_roles", flattenInheritedRoles(customDBRole.GetInheritedRoles())); err != nil {
9696
return diag.FromErr(fmt.Errorf("error setting `inherited_roles` for custom db role (%s): %s", d.Id(), err))
9797
}
9898

internal/service/customdbrole/data_source_custom_db_role_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ func TestAccConfigDSCustomDBRole_basic(t *testing.T) {
2323
CheckDestroy: acc.CheckDestroyNetworkPeering,
2424
Steps: []resource.TestStep{
2525
{
26-
Config: testAccDSMongoDBAtlasCustomDBRoleConfig(orgID, projectName, roleName, "INSERT", fmt.Sprintf("test-acc-db_name-%s", acctest.RandString(5))),
26+
Config: configDS(orgID, projectName, roleName, "INSERT", fmt.Sprintf("test-acc-db_name-%s", acctest.RandString(5))),
2727
Check: resource.ComposeTestCheckFunc(
2828
// Test for Resource
29-
testAccCheckMongoDBAtlasCustomDBRolesExists(resourceName),
29+
checkExists(resourceName),
3030
resource.TestCheckResourceAttrSet(resourceName, "project_id"),
3131
resource.TestCheckResourceAttrSet(resourceName, "role_name"),
3232
resource.TestCheckResourceAttrSet(resourceName, "actions.0.action"),
@@ -44,7 +44,7 @@ func TestAccConfigDSCustomDBRole_basic(t *testing.T) {
4444
})
4545
}
4646

47-
func testAccDSMongoDBAtlasCustomDBRoleConfig(orgID, projectName, roleName, action, databaseName string) string {
47+
func configDS(orgID, projectName, roleName, action, databaseName string) string {
4848
return fmt.Sprintf(`
4949
resource "mongodbatlas_project" "test" {
5050
name = %[2]q

internal/service/customdbrole/data_source_custom_db_roles.go

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ import (
88
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/id"
99
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1010
"github.com/mongodb/terraform-provider-mongodbatlas/internal/config"
11-
matlas "go.mongodb.org/atlas/mongodbatlas"
11+
"go.mongodb.org/atlas-sdk/v20231115005/admin"
1212
)
1313

1414
func PluralDataSource() *schema.Resource {
1515
return &schema.Resource{
16-
ReadContext: dataSourceMongoDBAtlasCustomDBRolesRead,
16+
ReadContext: dataSourcePluralRead,
1717
Schema: map[string]*schema.Schema{
1818
"project_id": {
1919
Type: schema.TypeString,
@@ -83,16 +83,16 @@ func PluralDataSource() *schema.Resource {
8383
}
8484
}
8585

86-
func dataSourceMongoDBAtlasCustomDBRolesRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
87-
conn := meta.(*config.MongoDBClient).Atlas
86+
func dataSourcePluralRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
87+
connV2 := meta.(*config.MongoDBClient).AtlasV2
8888
projectID := d.Get("project_id").(string)
8989

90-
customDBRoles, _, err := conn.CustomDBRoles.List(ctx, projectID, nil)
90+
customDBRoles, _, err := connV2.CustomDatabaseRolesApi.ListCustomDatabaseRoles(ctx, projectID).Execute()
9191
if err != nil {
9292
return diag.FromErr(fmt.Errorf("error getting custom db roles information: %s", err))
9393
}
9494

95-
if err := d.Set("results", flattenCustomDBRoles(*customDBRoles)); err != nil {
95+
if err := d.Set("results", flattenCustomDBRoles(customDBRoles)); err != nil {
9696
return diag.FromErr(fmt.Errorf("error setting `results for custom db roles: %s", err))
9797
}
9898

@@ -101,20 +101,14 @@ func dataSourceMongoDBAtlasCustomDBRolesRead(ctx context.Context, d *schema.Reso
101101
return nil
102102
}
103103

104-
func flattenCustomDBRoles(customDBRoles []matlas.CustomDBRole) []map[string]any {
105-
var customDBRolesMap []map[string]any
106-
107-
if len(customDBRoles) > 0 {
108-
customDBRolesMap = make([]map[string]any, len(customDBRoles))
109-
110-
for k, customDBRole := range customDBRoles {
111-
customDBRolesMap[k] = map[string]any{
112-
"role_name": customDBRole.RoleName,
113-
"actions": flattenActions(customDBRole.Actions),
114-
"inherited_roles": flattenInheritedRoles(customDBRole.InheritedRoles),
115-
}
104+
func flattenCustomDBRoles(customDBRoles []admin.UserCustomDBRole) []map[string]any {
105+
customDBRolesMap := make([]map[string]any, len(customDBRoles))
106+
for k, customDBRole := range customDBRoles {
107+
customDBRolesMap[k] = map[string]any{
108+
"role_name": customDBRole.RoleName,
109+
"actions": flattenActions(customDBRole.GetActions()),
110+
"inherited_roles": flattenInheritedRoles(customDBRole.GetInheritedRoles()),
116111
}
117112
}
118-
119113
return customDBRolesMap
120114
}

internal/service/customdbrole/data_source_custom_db_roles_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ func TestAccConfigDSCustomDBRoles_basic(t *testing.T) {
2323
CheckDestroy: acc.CheckDestroyNetworkPeering,
2424
Steps: []resource.TestStep{
2525
{
26-
Config: testAccDSMongoDBAtlasCustomDBRolesConfig(orgID, projectName, roleName, "INSERT", fmt.Sprintf("test-acc-db_name-%s", acctest.RandString(5))),
26+
Config: configDSPlural(orgID, projectName, roleName, "INSERT", fmt.Sprintf("test-acc-db_name-%s", acctest.RandString(5))),
2727
Check: resource.ComposeTestCheckFunc(
2828
// Test for Resource
29-
testAccCheckMongoDBAtlasCustomDBRolesExists(resourceName),
29+
checkExists(resourceName),
3030
resource.TestCheckResourceAttrSet(resourceName, "project_id"),
3131
resource.TestCheckResourceAttrSet(resourceName, "role_name"),
3232
resource.TestCheckResourceAttrSet(resourceName, "actions.0.action"),
@@ -43,7 +43,7 @@ func TestAccConfigDSCustomDBRoles_basic(t *testing.T) {
4343
})
4444
}
4545

46-
func testAccDSMongoDBAtlasCustomDBRolesConfig(orgID, projectName, roleName, action, databaseName string) string {
46+
func configDSPlural(orgID, projectName, roleName, action, databaseName string) string {
4747
return fmt.Sprintf(`
4848
resource "mongodbatlas_project" "test" {
4949
name = %[2]q

0 commit comments

Comments
 (0)