07 Oct 2024 04:24 PM - last edited on 08 Oct 2024 07:30 AM by MaciejNeumann
Hi I am creating a workflow to help manage our tenant. I want to find any Custom Logs at that environment scope and disable them if certain criteria are met.
I use the SDK to call get const res = await settingsObjectsClient.getSettingsObjects(params); successfully which gives me a list of custom logs and their objectIds
On my environment, I only have one test custom log source and it is disabled.
So using the I want to update the object IDs by enabling which I have chosen the method ettingsObjectsClient.putSettingsObjectByObjectId(config); but my code fails with some validation errors, as per below
Updating settings sandpit
config
{"schemaId":"builtin:logmonitoring.custom-log-source-settings","schemaVersion":"1.1","scope":"environment","objectId":"vu9U3hXa3xxxxxxxxxlYGG-71TeFdrerQ","updateToken":"vu9UxxxxxxrQ","body":{"value":{"enabled":true}}}
error raised. 400: Validation failed for 2 Validators.
2 questions
What is wrong with my code?
How can I find out what the validation error might be referring to?
import { settingsObjectsClient } from "@dynatrace-sdk/client-classic-environment-v2";
export default async function updateSettingsExample()
{
try{
console.log("Updating settings");
const config = {
schemaId: "builtin:logmonitoring.custom-log-source-settings",
schemaVersion: "1.1",
scope: "environment",
objectId: 'vu9U3hXa3q0AAAABADBxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxidWlsdGluOlYzYtYTEyMi1iZGU1MzliNzJlMGG-71TeFdrerQ',
updateToken: "vu9U3hxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxeFdjerQ",
body: {
value: {
enabled: true,
},
},
};
console.log("config");
console.log(JSON.stringify(config));
const data = await settingsObjectsClient.putSettingsObjectByObjectId(config);
console.log("result");
console.log(JSON.stringify(data));
}
catch (error)
{
console.log(`error raised. ${error}`);
}
}
08 Oct 2024 04:27 AM
Hey @HeadScratcher,
I have tested this in my own environment and it looks like you need to provide the entire value for the updated setting. For example, above you are only providing "enabled: true" when in actual fact you would also need to provide the rest of the log ingestion rule. You can find what needs to be included by using the API tab on the page as seen below.
My code looked like:
import { settingsObjectsClient } from "@dynatrace-sdk/client-classic-environment-v2";
export default async function updateSettingsExample()
{
const data =
await settingsObjectsClient.putSettingsObjectByObjectId({
objectId: "vu9U3hXa3q0AAAABADBidWlsdGluOmxvZ21vbml0b3JpbmcuY3VzdG9tLWxvZy1zb3VyY2Utc2V0dGluZ3MABnRlbmFudAAGdGVuYW50ACRkZTIxNmNiNi1kNmY1LTNhOWYtYmU2Ny03ZTQyMTYzNDc1MGG-71TeFdrerQ",
body: {
value: {
"enabled":false,"config-item-title":"Test","custom-log-source":{"type":"WINDOWS_EVENT_LOG","accept-binary":null,"values-and-enrichment":[{"path":"Test","enrichment":[]}]},"context":[]
}
},
});
}
Going back to your original requirement, you should have the value when you get the setting using "const res = await settingsObjectsClient.getSettingsObjects(params);" and then you should be able to modify what has returned and then just pass it back in "const data = await settingsObjectsClient.putSettingsObjectByObjectId(config);".
Hope this helps!
08 Oct 2024 11:37 AM
Thanks @Fin_Ubels I understand what you are saying that does make sense so I will try and get that working in the next couple of days.
Extra points for mentioning the API info. I always forget about that but it is hugely useful when using the API.
08 Oct 2024 04:56 PM
So @Fin_Ubels I eventually got this working as you suggested. It took me ages to get the JSON object copy just right. Am I right in thinking this can be developed in VS Code or some other humane IDE ?