Skip to content

Commit

Permalink
chore: Upgrades roles_org_id resource to auto-generated SDK (#1906)
Browse files Browse the repository at this point in the history
* connv2 in datasource

* rename and small refactors

* simplify loop
  • Loading branch information
lantoli committed Feb 5, 2024
1 parent e322ab9 commit 026150c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 37 deletions.
26 changes: 8 additions & 18 deletions internal/service/rolesorgid/data_source_roles_org_id.go
Expand Up @@ -9,13 +9,11 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/mongodb/terraform-provider-mongodbatlas/internal/common/constant"
"github.com/mongodb/terraform-provider-mongodbatlas/internal/config"

matlas "go.mongodb.org/atlas/mongodbatlas"
)

func DataSource() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceMongoDBAtlasOrgIDRead,
ReadContext: dataSourceRead,
Schema: map[string]*schema.Schema{
"org_id": {
Type: schema.TypeString,
Expand All @@ -25,29 +23,21 @@ func DataSource() *schema.Resource {
}
}

func dataSourceMongoDBAtlasOrgIDRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
// Get client connection.
conn := meta.(*config.MongoDBClient).Atlas

var err error

options := &matlas.ListOptions{}
apiKeyOrgList, _, err := conn.Root.List(ctx, options)
func dataSourceRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
connV2 := meta.(*config.MongoDBClient).AtlasV2
apiKeyOrgList, _, err := connV2.RootApi.GetSystemStatus(ctx).Execute()
if err != nil {
return diag.Errorf("error getting API Key's org assigned (%s): ", err)
}

for idx, role := range apiKeyOrgList.APIKey.Roles {
if strings.HasPrefix(role.RoleName, "ORG_") {
if err := d.Set("org_id", apiKeyOrgList.APIKey.Roles[idx].OrgID); err != nil {
for _, role := range apiKeyOrgList.ApiKey.GetRoles() {
if strings.HasPrefix(role.GetRoleName(), "ORG_") {
if err := d.Set("org_id", role.GetOrgId()); err != nil {
return diag.Errorf(constant.ErrorSettingAttribute, "org_id", err)
}
d.SetId(apiKeyOrgList.APIKey.Roles[idx].OrgID)
d.SetId(role.GetOrgId())
return nil
}
}

d.SetId(id.UniqueId())

return nil
}
23 changes: 4 additions & 19 deletions internal/service/rolesorgid/data_source_roles_org_id_test.go
@@ -1,44 +1,29 @@
package rolesorgid_test

import (
"fmt"
"os"
"testing"

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

func TestAccConfigDSOrgID_basic(t *testing.T) {
var (
dataSourceName = "data.mongodbatlas_roles_org_id.test"
orgID = os.Getenv("MONGODB_ATLAS_ORG_ID")
name = fmt.Sprintf("test-acc-%s@mongodb.com", acctest.RandString(10))
initialRole = []string{"ORG_OWNER"}
)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acc.PreCheckBasic(t) },
ProtoV6ProviderFactories: acc.TestAccProviderV6Factories,
CheckDestroy: acc.CheckDestroyOrgInvitation,
Steps: []resource.TestStep{
{
Config: testAccDataSourceMongoDBAtlasOrgIDConfig(orgID, name, initialRole),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet(dataSourceName, "org_id"),
),
Config: configDS(),
Check: resource.TestCheckResourceAttrSet(dataSourceName, "org_id"),
},
},
})
}

func testAccDataSourceMongoDBAtlasOrgIDConfig(orgID, username string, roles []string) string {
return (`
data "mongodbatlas_roles_org_id" "test" {
}
output "org_id" {
value = data.mongodbatlas_roles_org_id.test.org_id
}`)
func configDS() string {
return `data "mongodbatlas_roles_org_id" "test" {}`
}

0 comments on commit 026150c

Please sign in to comment.