Skip to content

Commit 529bbbb

Browse files
chore: Allow SA for mongodbatlas_roles_org_id datasource (#3764)
Co-authored-by: kanchana-mongodb <[email protected]>
1 parent bb93f96 commit 529bbbb

File tree

5 files changed

+28
-27
lines changed

5 files changed

+28
-27
lines changed

docs/data-sources/roles_org_id.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ subcategory: "Organizations"
44

55
# Data Source: mongodbatlas_roles_org_id
66

7-
`mongodbatlas_roles_org_id` describes a MongoDB Atlas Roles Org ID. This represents a Roles Org ID.
7+
`mongodbatlas_roles_org_id` allows to retrieve the Org ID of the authenticated user.
88

99
## Example Usage
1010

@@ -26,6 +26,6 @@ output "org_id" {
2626

2727
In addition to all arguments above, the following attributes are exported:
2828

29-
* `org_id` - The ID of the organization you want to retrieve associated to an API Key.
29+
* `org_id` - The ID of the organization you want to retrieve, which is associated with the Service Account or Programmatic API Key (PAK) of the authenticated user.
3030

3131
See [MongoDB Atlas API - Role Org ID](https://www.mongodb.com/docs/atlas/reference/api-resources-spec/#tag/Root/operation/getSystemStatus) Documentation for more information.

internal/common/constant/error_message.go

Lines changed: 0 additions & 5 deletions
This file was deleted.

internal/service/projectapikey/model_project_api_key.go

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

88
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
99
"github.com/mongodb/terraform-provider-mongodbatlas/internal/common/conversion"
10+
"github.com/mongodb/terraform-provider-mongodbatlas/internal/service/rolesorgid"
1011
"go.mongodb.org/atlas-sdk/v20250312008/admin"
1112
)
1213

@@ -67,16 +68,10 @@ func sameRoles(roles1, roles2 []string) bool {
6768

6869
// getKeyDetails returns nil error and nil details if not found as it's not considered an error
6970
func getKeyDetails(ctx context.Context, connV2 *admin.APIClient, apiKeyID string) (*admin.ApiKeyUserDetails, string, error) {
70-
resp, _, err := connV2.OrganizationsApi.ListOrgs(ctx).Execute()
71+
orgID, err := rolesorgid.GetCurrentOrgID(ctx, connV2)
7172
if err != nil {
7273
return nil, "", err
7374
}
74-
orgIDs := resp.GetResults()
75-
if len(orgIDs) == 0 {
76-
return nil, "", fmt.Errorf("no organizations found")
77-
}
78-
// At present a PAK or SA belongs to exactly one organization. If this changes in the future, this logic will need to be updated.
79-
orgID := orgIDs[0].GetId()
8075
key, _, err := connV2.ProgrammaticAPIKeysApi.GetOrgApiKey(ctx, orgID, apiKeyID).Execute()
8176
if err != nil {
8277
if admin.IsErrorCode(err, "API_KEY_NOT_FOUND") {

internal/service/rolesorgid/data_source_roles_org_id.go

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ package rolesorgid
22

33
import (
44
"context"
5-
"strings"
5+
"fmt"
66

77
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
88
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/id"
99
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
10-
"github.com/mongodb/terraform-provider-mongodbatlas/internal/common/constant"
1110
"github.com/mongodb/terraform-provider-mongodbatlas/internal/config"
11+
"go.mongodb.org/atlas-sdk/v20250312008/admin"
1212
)
1313

1414
func DataSource() *schema.Resource {
@@ -25,19 +25,28 @@ func DataSource() *schema.Resource {
2525

2626
func dataSourceRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
2727
connV2 := meta.(*config.MongoDBClient).AtlasV2
28-
apiKeyOrgList, _, err := connV2.RootApi.GetSystemStatus(ctx).Execute()
28+
orgID, err := GetCurrentOrgID(ctx, connV2)
2929
if err != nil {
30-
return diag.Errorf("error getting API Key's org assigned (%s): ", err)
30+
return diag.Errorf("error getting current organization ID: %v", err)
3131
}
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 {
35-
return diag.Errorf(constant.ErrorSettingAttribute, "org_id", err)
36-
}
37-
d.SetId(role.GetOrgId())
38-
return nil
39-
}
32+
if err := d.Set("org_id", orgID); err != nil {
33+
return diag.Errorf("error setting `org_id`: %v", err)
4034
}
4135
d.SetId(id.UniqueId())
4236
return nil
4337
}
38+
39+
// GetCurrentOrgID returns the current organization ID for the SA or Programmatic API key (PAK) of the authenticated user.
40+
func GetCurrentOrgID(ctx context.Context, connV2 *admin.APIClient) (string, error) {
41+
resp, _, err := connV2.OrganizationsApi.ListOrgs(ctx).Execute()
42+
if err != nil {
43+
return "", err
44+
}
45+
orgIDs := resp.GetResults()
46+
if len(orgIDs) == 0 {
47+
return "", fmt.Errorf("no organizations found")
48+
}
49+
50+
// At present a PAK or SA belongs to exactly one organization. If this changes in the future, this logic will need to be updated.
51+
return orgIDs[0].GetId(), nil
52+
}

internal/service/rolesorgid/data_source_roles_org_id_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package rolesorgid_test
22

33
import (
4+
"os"
45
"testing"
56

67
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
@@ -10,6 +11,7 @@ import (
1011
func TestAccConfigDSOrgID_basic(t *testing.T) {
1112
var (
1213
dataSourceName = "data.mongodbatlas_roles_org_id.test"
14+
orgID = os.Getenv("MONGODB_ATLAS_ORG_ID")
1315
)
1416

1517
resource.ParallelTest(t, resource.TestCase{
@@ -18,7 +20,7 @@ func TestAccConfigDSOrgID_basic(t *testing.T) {
1820
Steps: []resource.TestStep{
1921
{
2022
Config: configDS(),
21-
Check: resource.TestCheckResourceAttrSet(dataSourceName, "org_id"),
23+
Check: resource.TestCheckResourceAttr(dataSourceName, "org_id", orgID),
2224
},
2325
},
2426
})

0 commit comments

Comments
 (0)