09 Oct 2024 10:22 PM - last edited on 21 Oct 2024 10:03 AM by AgataWlodarczyk
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!
Any var change, refreshes the dashboard. So simple but look so nice (-;
(want to look at the source, just contact me)
Have fun,
Henk
Solved! Go to Solution.
21 Oct 2024 08:05 AM - last edited on 21 Oct 2024 10:04 AM by AgataWlodarczyk
Thanks for sharing it with the Community 🙂
12 Dec 2024 06:23 PM
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
12 Nov 2024 03:49 PM
Hey @henk_stobbe - would you mind sharing this dashboard with me?
12 Dec 2024 06:26 PM - edited 15 Dec 2024 10:13 AM
Grüße Henk
14 Dec 2024 07:33 AM
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;
}
}