Skip to content

Commit e58a5d3

Browse files
authored
uset ListOrgs in getKeyDetails (#3723)
1 parent e17889f commit e58a5d3

File tree

1 file changed

+13
-12
lines changed

1 file changed

+13
-12
lines changed

internal/service/projectapikey/model_project_api_key.go

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -67,23 +67,24 @@ func sameRoles(roles1, roles2 []string) bool {
6767

6868
// getKeyDetails returns nil error and nil details if not found as it's not considered an error
6969
func getKeyDetails(ctx context.Context, connV2 *admin.APIClient, apiKeyID string) (*admin.ApiKeyUserDetails, string, error) {
70-
root, _, err := connV2.RootApi.GetSystemStatus(ctx).Execute()
70+
resp, _, err := connV2.OrganizationsApi.ListOrgs(ctx).Execute()
7171
if err != nil {
7272
return nil, "", err
7373
}
74-
for _, role := range root.ApiKey.GetRoles() {
75-
if orgID := role.GetOrgId(); orgID != "" {
76-
key, _, err := connV2.ProgrammaticAPIKeysApi.GetOrgApiKey(ctx, orgID, apiKeyID).Execute()
77-
if err != nil {
78-
if admin.IsErrorCode(err, "API_KEY_NOT_FOUND") {
79-
return nil, orgID, nil
80-
}
81-
return nil, orgID, fmt.Errorf("error getting api key information: %s", err)
82-
}
83-
return key, orgID, nil
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()
80+
key, _, err := connV2.ProgrammaticAPIKeysApi.GetOrgApiKey(ctx, orgID, apiKeyID).Execute()
81+
if err != nil {
82+
if admin.IsErrorCode(err, "API_KEY_NOT_FOUND") {
83+
return nil, orgID, nil
8484
}
85+
return nil, orgID, fmt.Errorf("error getting api key information: %s", err)
8586
}
86-
return nil, "", nil
87+
return key, orgID, nil
8788
}
8889

8990
func validateUniqueProjectIDs(d *schema.ResourceData) error {

0 commit comments

Comments
 (0)