Skip to content

Commit 026150c

Browse files
authored
chore: Upgrades roles_org_id resource to auto-generated SDK (#1906)
* connv2 in datasource * rename and small refactors * simplify loop
1 parent e322ab9 commit 026150c

File tree

2 files changed

+12
-37
lines changed

2 files changed

+12
-37
lines changed

internal/service/rolesorgid/data_source_roles_org_id.go

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,11 @@ import (
99
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1010
"github.com/mongodb/terraform-provider-mongodbatlas/internal/common/constant"
1111
"github.com/mongodb/terraform-provider-mongodbatlas/internal/config"
12-
13-
matlas "go.mongodb.org/atlas/mongodbatlas"
1412
)
1513

1614
func DataSource() *schema.Resource {
1715
return &schema.Resource{
18-
ReadContext: dataSourceMongoDBAtlasOrgIDRead,
16+
ReadContext: dataSourceRead,
1917
Schema: map[string]*schema.Schema{
2018
"org_id": {
2119
Type: schema.TypeString,
@@ -25,29 +23,21 @@ func DataSource() *schema.Resource {
2523
}
2624
}
2725

28-
func dataSourceMongoDBAtlasOrgIDRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
29-
// Get client connection.
30-
conn := meta.(*config.MongoDBClient).Atlas
31-
32-
var err error
33-
34-
options := &matlas.ListOptions{}
35-
apiKeyOrgList, _, err := conn.Root.List(ctx, options)
26+
func dataSourceRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
27+
connV2 := meta.(*config.MongoDBClient).AtlasV2
28+
apiKeyOrgList, _, err := connV2.RootApi.GetSystemStatus(ctx).Execute()
3629
if err != nil {
3730
return diag.Errorf("error getting API Key's org assigned (%s): ", err)
3831
}
39-
40-
for idx, role := range apiKeyOrgList.APIKey.Roles {
41-
if strings.HasPrefix(role.RoleName, "ORG_") {
42-
if err := d.Set("org_id", apiKeyOrgList.APIKey.Roles[idx].OrgID); err != nil {
32+
for _, role := range apiKeyOrgList.ApiKey.GetRoles() {
33+
if strings.HasPrefix(role.GetRoleName(), "ORG_") {
34+
if err := d.Set("org_id", role.GetOrgId()); err != nil {
4335
return diag.Errorf(constant.ErrorSettingAttribute, "org_id", err)
4436
}
45-
d.SetId(apiKeyOrgList.APIKey.Roles[idx].OrgID)
37+
d.SetId(role.GetOrgId())
4638
return nil
4739
}
4840
}
49-
5041
d.SetId(id.UniqueId())
51-
5242
return nil
5343
}
Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,29 @@
11
package rolesorgid_test
22

33
import (
4-
"fmt"
5-
"os"
64
"testing"
75

8-
"github.com/hashicorp/terraform-plugin-testing/helper/acctest"
96
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
107
"github.com/mongodb/terraform-provider-mongodbatlas/internal/testutil/acc"
118
)
129

1310
func TestAccConfigDSOrgID_basic(t *testing.T) {
1411
var (
1512
dataSourceName = "data.mongodbatlas_roles_org_id.test"
16-
orgID = os.Getenv("MONGODB_ATLAS_ORG_ID")
17-
name = fmt.Sprintf("test-acc-%[email protected]", acctest.RandString(10))
18-
initialRole = []string{"ORG_OWNER"}
1913
)
2014

2115
resource.ParallelTest(t, resource.TestCase{
2216
PreCheck: func() { acc.PreCheckBasic(t) },
2317
ProtoV6ProviderFactories: acc.TestAccProviderV6Factories,
24-
CheckDestroy: acc.CheckDestroyOrgInvitation,
2518
Steps: []resource.TestStep{
2619
{
27-
Config: testAccDataSourceMongoDBAtlasOrgIDConfig(orgID, name, initialRole),
28-
Check: resource.ComposeTestCheckFunc(
29-
resource.TestCheckResourceAttrSet(dataSourceName, "org_id"),
30-
),
20+
Config: configDS(),
21+
Check: resource.TestCheckResourceAttrSet(dataSourceName, "org_id"),
3122
},
3223
},
3324
})
3425
}
3526

36-
func testAccDataSourceMongoDBAtlasOrgIDConfig(orgID, username string, roles []string) string {
37-
return (`
38-
data "mongodbatlas_roles_org_id" "test" {
39-
}
40-
41-
output "org_id" {
42-
value = data.mongodbatlas_roles_org_id.test.org_id
43-
}`)
27+
func configDS() string {
28+
return `data "mongodbatlas_roles_org_id" "test" {}`
4429
}

0 commit comments

Comments
 (0)