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

api delete and create maintenance window

grant_drage
Newcomer

Hi,

I am writing a powershell script to delete and create maintenance windows. Firstly, if I try to create a maintenance window for the the API and theres already a window of the same name, does it update the existing window or does it just fail (i.e. do I need to delete the existing window first?).

Secondly, when trying to delete a window I am running the following command..

(Invoke-WebRequest -Headers @{"Authorization"="Api-Token $token"} "https://$id.live.dynatrace.com/api/v1/maintenance").Content | ConvertFrom-Json
id : TEST
type : Planned
description : test
suppressAlerts : False
suppressProblems : False
scope :
schedule : @{t......}


Invoke-WebRequest -Method Delete -Headers @{"Authorization"="Api-Token $token"}
"https://$id.live.dynatrace.com/api/v1/maintenance/TEST"

StatusCode : 204 StatusDescription : No Content Content : {} RawContent : HTTP/1.1 204 No Content Access-Control-Allow-Origin: * Access-Control-Allow-Methods: GET, POST, DELETE, PUT Access-Control-Allow-Headers: * X-RateLimit-Limit: 50 X-RateLimit-Remaining: 49 X-Rate... Headers : {[Access-Control-Allow-Origin, *], [Access-Control-Allow-Methods, GET, POST, DELETE, PUT], [Access-Control-Allow-Headers, *], [X-RateLimit-Limit, 50]...} RawContentLength : 0

I get the status 204 but the window does not get deleted. Can someone point out what I am doing wrong? Sure it is something obvious.

4 REPLIES 4

grant_drage
Newcomer

Not got the delete to work yet via the API. Have managed to get it to create a window using the API but only if there is no scope. what I am doing is using powershell to do an invoke webrequest to get all maintenance windows, then cycling through looking for one of a particular name. When it finds it, the script changes the start and end times, and does webrequest to post it back either with the same id or a new one (the name of the window). This works if I choose to efficitively amend a window that affects the 'entire environment' but I get the following error when trying to create a window with scope.

Invoke-WebRequest -Headers @{"Authorization"="Api-Token $token"} "https://$id.live.dynatrace.com/api/v1/maintenance" -ContentType 'application/json' -Method POST -Body ($window | ConvertTo-Json)

Invoke-WebRequest : {"error":{"code":400,"message":"failed processing (parsing, generating) JSON content"}}
At C:\Scripts\ruxit api.ps1:26 char:9
+ Invoke-WebRequest -Headers @{"Authorization"="Api-Token $token"} "https: ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand

grant_drage
Newcomer

Here is the JSON object I am trying to send....

{
"id": "UAT",
"type": "Planned",
"description": "Testing maintenance window",
"suppressAlerts": true,
"suppressProblems": true,
"scope": {
"entities": [
"SYNTHETIC_TEST-00000000000037DF"
],
"matches": [
"@{type=HOST; tags=System.Object[]}"
]
},
"schedule": {
"type": "Once",
"timezoneId": "Europe/London",
"maintenanceStart": "2017-11-16 11:41",
"maintenanceEnd": "2018-11-16 11:41"
}
}

grant_drage
Newcomer

Run the same script with no scope set, and it works fine...

{
"id": "TEST",
"type": "Planned",
"description": "test",
"suppressAlerts": false,
"suppressProblems": false,
"scope": null,
"schedule": {
"type": "Once",
"timezoneId": "Europe/London",
"maintenanceStart": "2017-11-16 11:45",
"maintenanceEnd": "2018-11-16 11:45"
}
}

StatusCode : 204
StatusDescription : No Content
Content : {}
RawContent : HTTP/1.1 204 No Content
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: GET, POST, DELETE, PUT
Access-Control-Allow-Headers: *
X-RateLimit-Limit: 50
X-RateLimit-Remaining: 49
X-Rate...
Headers : {[Access-Control-Allow-Origin, *], [Access-Control-Allow-Methods, GET, POST, DELETE, PUT], [Access-Control-Allow-Headers, *], [X-RateLimit-Limit, 50]...}
RawContentLength :

wolfgang_beer
Dynatrace Leader
Dynatrace Leader

Delete of maintenance window should of course work with a HTTP DELETE request to following path:

HTTP DELETE https://{your_domain}/api/v1/maintenance/theWindo...

Also an update of an existing config is possible, just use the same HTTP POST payload and change some infos.

HTTP POST https://{your_domain}/api/v1/maintenance

change the values within your maintenance window config object but keep the same "id" : "theWindowId" field.

The POST with your scope does not work because of wrong format. See my example below:

"scope" : {

"entities" : [ ],

"matches" : [

{
"type" : "HOST",
"tags" : [ {"key" : "myTag" }
]
},

{
"type" : "SERVICE",
"tags" : [ {"key" : "myTag" }
]
}

]

}

And please keep in mind that at least one service and one host must have that tag 'myTag' otherwise the call fails because there is no match.

Featured Posts