cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Gather groups from user inside an APP

archem
Newcomer_

Hello team,

I'm developing an APP which manages documents, and creates dashboards as documents for the user.

Right now I also want the user to be able to assign/share those dashboards to different groups that the user can manage, but I'm unable to gather those groups.

I've seen that there's an API which lists all groups from an account UUID, but I've run into two issues:

  • Using an App Function, and calling the "GET all groups" from the Group Management API still asks me to authenticate, even if the fetch is being called inside the app function which should be already authenticated as it's running inside the tenant.
  • The "GET all groups" asks me for a UUID, which I don't know where to get inside of the app.

 

So overall the solution would be to gather the user UUID, gather all the groups the user can see or has permissions to manage, and create a document dashboard while also sharing/assigning it to those groups (dashboard document creation is already made)

How should I proceed?

Thanks in advance,

Manel.

3 REPLIES 3

david_lopes
Dynatrace Pro
Dynatrace Pro

I've had to do something similar in the past
The only way to get group information is by using the IAM APIs, they are not part of the platform

So you will need to:

1. Write a client to access the IAM data

Luckily there is a swagger documentation here: https://api.dynatrace.com/spec/

So you can easily create a client by running

npx swagger-typescript-api --modular --single-http-client --extract-response-body -p https://api.dynatrace.com/spec-json -o account-management



2. Write an app function to retrieve the list of groups

You will need to use an app function because you won't be able to access the account management API directly from the
browser

And you are correct, you WILL need to provide the oauth client details and account id

Here is a function that that app function can call to get the groups for a user:


/**
 * Get all groups for a user
 * @param {Iam} iam - The Iam object, used to make API calls
 * @param {string} email - The user email
 * @param {string} accountUuid - The account UUID
 * @returns {UserGroupsEntry} - A UserGroupsEntry object
 */
async function getUserGroups(
  iam: Iam,
  email: string,
  accountUuid: string
): Promise<UserGroupsEntry> {
  return await iam
    .usersControllerGetUserGroups(accountUuid, email)
    .then((response) => {
      return { email: email, result: ok(response.data) };
    })
    .catch((error: ApiError) => {
      let errorMessage = `Error retrieving data: ${error}`;
      if (error.error !== undefined && error.error.message !== undefined) {
        errorMessage = error.error.message;
      }
      return { email: email, result: err(new Error(`${errorMessage}`)) };
    });
}


You will need to ask for the account UUID, there is no way to retrieve this information from the app.
I suggest creating an app setting schema for your app, with the properties:

{
    "name": {
      "displayName": "Connection name",
      "description": "The name of these credentials",
      "documentation": "",
      "type": "text",
      "nullable": false,
      "default": "",
      "constraints": [
        {
          "type": "NOT_BLANK"
        }
      ]
    },
    "clientId": {
        "displayName": "Client ID",
        "description": "The [OAuth2 Client ID](https://www.dynatrace.com/support/help/shortlink/account-api-authentication#create-an-oauth2-client)",
        "type": "text",
        "nullable": false,
        "default": "",
        "constraints": [
            {
            "type": "NOT_BLANK"
            }
        ]
    },
    "clientSecret": {
        "displayName": "Client Secret",
        "description": "The [OAuth2 Client Secret](https://www.dynatrace.com/support/help/shortlink/account-api-authentication#create-an-oauth2-client)",
        "type": "secret",
        "nullable": false,
        "default": "",
        "constraints": [
            {
            "type": "NOT_BLANK"
            }
        ]
    },
    "accountUuid": {
        "displayName": "Account UUID",
        "documentation": "You can find the UUID on the `Account > Account management API` page, during creation of an OAuth2 client.",
        "type": "text",
        "nullable": false,
        "default": "",
        "constraints": [
            {
            "type": "PATTERN",
            "pattern": "^[a-f0-9]{8}-([a-f0-9]{4}-){3}[a-f0-9]{12}$"
            }
        ]
    }
}





I'm attaching the code for the app I created in case it helps.

Thanks David,

The app was a good starting point to start, although I did not need as much so I made it simpler as I just needed to gather groups.

Featured Posts