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

Test workflow started by API

henk_stobbe
DynaMight Leader
DynaMight Leader

Hello,

I needed a way to test my workflow that can be started by the API. Not wanting to make use one of the existing tools for triggering the workflow, I just created a simple Dashboard with variables  that fetches the OAUTH token for me, kicks off the workflow and shows the execution log!

 

henk_stobbe_0-1728508728011.png

Any var change, refreshes the dashboard. So simple but look so nice (-;

(want to look at the source, just contact me)

Have fun,

Henk

 

5 REPLIES 5

imsingh
Dynatrace Advisor
Dynatrace Advisor

Thanks for sharing it with the Community 🙂 

Hello, 

Forgot about this post, but while catching up found:

https://www.youtube.com/watch?v=IVFc0-Iil8w&t=879s

Not my original idea, but yeah you can look at it as a kind of poor man's app. 

KR Henk

Whether you're a beginner or an expert, have or don't have questions, we're excited to welcome you to this month's live Office Hours for App Development! During this session, you can expect to: Receive a summary of what's new in Dynatrace Apps Learn more about Dynatrace Apps See the first-hand ...

andreas_grabner
Dynatrace Guru
Dynatrace Guru

Hey @henk_stobbe - would you mind sharing this dashboard with me?

Contact our DevRel team through devrel@dynatrace.com

 🎄Weihnachtsgeschenk (-;🎄

Grüße Henk

 

henk_stobbe
DynaMight Leader
DynaMight Leader

export default async function () {
const sleep = (delay) => new Promise((resolve) => setTimeout(resolve, delay));

// Token request URL
const token_url = 'https://sso.dynatrace.com/sso/oauth2/token';

// First token credentials
const params1 = new URLSearchParams({
grant_type: 'client_credentials',
client_id: 'dt0s02.???',
client_secret: 'dt0s02.???',
resource: 'urn:dtaccount:???',
});

// Second token credentials
const params2 = new URLSearchParams({
grant_type: 'client_credentials',
client_id: 'dt0s02.???',
client_secret: 'dt0s02.???',
resource: 'urn:dtaccount:???',
});

// Common headers for token requests
const tokenHeaders = {
'Content-Type': 'application/x-www-form-urlencoded',
};

try {
// First token request
const response1 = await fetch(token_url, {
method: 'POST',
headers: tokenHeaders,
body: params1.toString(), // URL-encoded parameters
});

if (!response1.ok) {
const errorBody = await response1.text();
throw new Error(`Error fetching first token: ${response1.status} - ${errorBody}`);
}

const responseData1 = await response1.json();
const accessToken1 = responseData1.access_token;
console.log('First token received:', accessToken1);

// Use first token to call workflow API
const workflowUrl = 'https://???.apps.dynatrace.com/platform/automation/v1/workflows/???/run';
const workflowHeaders = {
Authorization: `Bearer ${accessToken1}`,
'Content-Type': 'application/json',
};

const body = {
input: {
request: $request,
endpoint: $endpoint,
cloudprovider: $cloudprovider,
environment: $environment,
body: $body,
}
};

const workflowResponse = await fetch(workflowUrl, {
method: 'POST',
headers: workflowHeaders,
body: JSON.stringify(body),
});

if (!workflowResponse.ok) {
const wfErrorBody = await workflowResponse.text();
throw new Error(`Error in Workflow API call: ${workflowResponse.status} - ${wfErrorBody}`);
}

const workflowData = await workflowResponse.json();
console.log('Workflow response:', workflowData);

// Second token request
const response2 = await fetch(token_url, {
method: 'POST',
headers: tokenHeaders,
body: params2.toString(), // URL-encoded parameters
});

if (!response2.ok) {
const errorBody = await response2.text();
throw new Error(`Error fetching second token: ${response2.status} - ${errorBody}`);
}

const responseData2 = await response2.json();
const accessToken2 = responseData2.access_token;
console.log('Second token received:', accessToken2);

// Log request URL
const logUrl = `https://???.apps.dynatrace.com/platform/automation/v1/executions/${workflowData.id}/tasks`;

// Sleep before fetching logs
await sleep(10000);

// Make the log request with the second token
const logHeaders = {
Authorization: `Bearer ${accessToken2}`,
'Content-Type': 'application/json',
};

const logResponse = await fetch(logUrl, {
method: 'GET',
headers: logHeaders,
});

if (!logResponse.ok) {
const logErrorBody = await logResponse.text();
throw new Error(`Error in Workflow LOG call: ${logResponse.status} - ${logErrorBody}`);
}

const logData = await logResponse.json();
console.log('Workflow log response:', logData);

return logData;

} catch (error) {
console.error(`Error occurred: ${error.message}`);
throw error;
}
}

Featured Posts