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

Is it possible to use ad-hoc functions to query endpoints by looping?

MarcioKaue
Helper

Hey guys,

We are testing the potential of using the new Dashboards with the use of Javascript, so you can concentrate the telemetry and governance data of my Managed Clusters in the SelfMonitoring environment.

We would like to have a Governance panel for tokens created on the Dynatrace platform and for that we perform the GET ALL in the respective API and then we need to perform a loop to obtain the details of each id returned in the vector. I would not like to use an external solution with python for this, is the ad-hoc function capable of doing this?

Our code example follows below, where an "HTTP Error 540: Unknown" error is displayed and no further details of the actual reason for the error are reported:

 

export default async function () {
  const environment = "{}/endpoint"
  const token = "{token}";
  const params = '/api/v2/apiTokens?pageSize=600&sort=-creationDate';

  const uri = environment + params;
  const response = await fetch(uri, {
  headers: {
    Accept: "application/json",
    Authorization: "Api-Token " + token
  }});

  const result = await response.json();
  const apiTokens = result.apiTokens;

  const secondAPIresults = [];

  await Promise.all(apiTokens.map(async (token) => {
    const uri2 = environment + '/api/v2/apiTokens/${token.id}';
    const response2 = await fetch(uri2, {
      headers: {
        Accept: "application/json",
        Authorization: "Api-Token " + token
      }});
      
    const data = await response2.json();
    const { name, lastUsedDate } = data;
    secondAPIResults.push({ name, lastUsedDate });
  }));
  
  return secondAPIResults;
}

 

2 REPLIES 2

educampver
Dynatrace Advisor
Dynatrace Advisor

Hi @MarcioKaue, the error 540 means there's an issue with the code. At the moment, you can debug these errors by catching them in your ad-hoc function and returning the error message as part of the response. Here's an example https://developer.dynatrace.com/develop/functions/error-handling/#custom-error-reporting.

At first glance, it looks like a typo. You're declaring a variable called secondAPIresultswith lowercase r, but then using secondAPIResults with uppercase R.

Hello @educampver , the variable actually had this incorrect character, but the code does not work with the suggested adjustment.

Thank you for coming back with the documentation, we will evaluate it in order to have a more detailed troubleshooting of the reason for the error.

Featured Posts