19 Jun 2024 01:06 AM - edited 19 Jun 2024 01:10 AM
Hi team,
I want to use loop task for a list of hosts to enable or disable monitoring in Dynatrace. I would like to have multiple hosts to be executed with single workflow for http_request.
[
{
"externalId": "string",
"schemaId": "builtin:host.monitoring",
"schemaVersion":"1.4",
"scope": "HOST-7AA30C2D50D93E12",
"value": {
"enabled": false
}
}
]
19 Jun 2024 05:41 PM
Hi @Smalhotra89
Yes this is possible with the loop task. Before you start you need to generate an "Access Token" with the scopes "Read settings" and "Write settings".
Then you can start creating the workflow. First you need to fetch all settings objects for the list of hosts you want to disable or enable with a http request task. The list of hosts is put in the URL and is comma separated. In the header section you add the Autorization details, please have a look at following screenshot
in the subsequent http request task you need to configure the loop task like this
The input for the tasks should be that
For this approach you have to create the token and manage it yourself.
But you could also do the same thing with a javascript task with custom javascript code, the benefit there would be that you don't need to create a token for changing the settings. Since the used sdks underneath managed this for you.
this is the snippet for the javascript task
// optional import of sdk modules
import { execution } from '@dynatrace-sdk/automation-utils';
import { actionExecution } from "@dynatrace-sdk/automation-utils";
import { SettingsObject, settingsObjectsClient } from "@dynatrace-sdk/client-classic-environment-v2";
export default async function ({ execution_id, action_execution_id }) {
// your code goes here
// e.g. get the current execution
const ex = await execution(execution_id);
const actionEx = await actionExecution(action_execution_id);
const scopes = actionEx.loopItem["host"];
//console.log(scopes);
const settingsObjects = await settingsObjectsClient.getSettingsObjects({
schemaIds: "builtin:host.monitoring",
scopes,
fields: "objectId,value,summary,searchSummary,created,modifid,createdBy,modifiedBy,author,updateToken,scope,modificationInfo,schemaId,schemaVersion,externalId"
});
//console.log(settingsObjects);
if (settingsObjects.items.length > 0){
const settingsObject = (settingsObjects.items[0]);
const settingsObjectResponse = await settingsObjectsClient.putSettingsObjectByObjectId({
objectId: settingsObject.objectId,
body: {
updateToken: settingsObject.updateToken,
value: {
"enabled": false
}
}
});
return { settingsObjectResponse };
}
}
Best,
Sini
20 Jun 2024 04:44 AM
Thanks Sini, javascript task works well.
I was unable to use the http request with loop task as it was unable to read allobjects.json.item in loop. I am not sure if I did something wrong but the first task to get all objects task ran through fine.
31 Aug 2024 02:48 AM
How do I iterate through all values from a fetch query using javascript?
31 Aug 2024 02:49 AM
here is the loop item
31 Aug 2024 03:20 AM
Thank You, I made it work for all tagged hosts.
15 Feb 2025 07:00 AM
I hope you guys are doing well.
I have replicated the above use case in my environment and it is not getting executed and requesting your help here.
Do I have to pass any bearer token ?
Thanks
17 Feb 2025 06:51 AM
Hi @DineshP,
In this case you have to use API token instead Bearer Token.
The APIs using in this workflow requeries API token.
Hope it helps.
Regards,
Elena.
17 Feb 2025 06:58 AM
Hi @erh_inetum ,
Thanks for the assistance, It would be great if you can help with some steps to proceed further to achieve the same.
Regards
DP
17 Feb 2025 07:30 AM
Hi,
You have to use this header:
headers: {
'Accept': 'application/json; charset=utf-8',
'Authorization': 'Api-Token dt0c01.XXXXXXXXXXXXXXXXX'
}
like in this screenshot
sent by @sinisa_zubic in the first reply of this post.
Hope it helps.
Regards,
Elena.