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.