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

Creating maintenance window using setting API

Naveen
Guide

Hi,

I am trying to create a maintenance window using settings API, but getting an error, could anyone please help?

Body:

 

 

[
  {
    "schemaId": "builtin:alerting.maintenance-window",
    "displayName": "Maintenance windows",
    "description": "Maintenance windows are typically planned, recurring periods of system downtime during which your DevOps team can perform preventative maintenance and system upgrades outside of peak traffic hours.",
    "scope": "environment",
    "value": {
      "enabled": true,
      "generalProperties": {
        "name": "Example Window",
        "description": "An example Maintenance window",
        "maintenanceType": "UNPLANNED",
        "suppression": "DONT_DETECT_PROBLEMS",
        "disableSyntheticMonitorExecution": true
      },
      "schedule": {
        "scheduleType": "ONCE",
        "OnceRecurrence": {
            "startTime": "13:45:00",
            "endTime": "14:45:00",
            "timeZone": "Europe/Vienna"
        }
      }
    }
  }
]

 

 

Error:

 

 

"code": 400,
"error": {
    "code": 400,
    "message": "Validation failed for 2 Validators.",
    "constraintViolations": [
        {
            "path": "builtin:alerting.maintenance-window/0/schedule/onceRecurrence",
            "message": "Given property 'onceRecurrence' with value: 'null' does not comply with required NonNull of schema",
            "parameterLocation": "PAYLOAD_BODY",
            "location": null
        },
        {
            "path": "builtin:alerting.maintenance-window/0/schedule/OnceRecurrence",
            "message": "Given property 'OnceRecurrence' with value: '{startTime=13:45:00, endTime=14:45:00, timeZone=Europe/Vienna}' was not found in the schema",
            "parameterLocation": "PAYLOAD_BODY",
            "location": null
        }
    ]
}

 

 

 

 

11 REPLIES 11

mgome
Advisor

You can generate a sample payload by creating a maintenance window in the console and using the API option under the "..." button in the upper right corner of the maintenance window configuration.  Open the "Add as new value" section and copy the result, which has the JSON payload as part of the curl command. 

When I dumped the JSON for one of my maintenance windows, the onceRecurrence looked like this:

"onceRecurrence": {
"startTime": "2020-09-26T22:50:00",
"endTime": "2020-09-27T00:00:00",
"timeZone": "America/Los_Angeles"
}


You're missing the date in your start and end times.

mgome_0-1684946866119.png

The local_date_time is of type java.time.LocalDateTime

https://docs.oracle.com/javase/8/docs/api/java/time/LocalDateTime.html

 

ChadTurner
DynaMight Legend
DynaMight Legend

I highly recommend making one via the UI, then do an API pull to grab the json for that Maintenance Window. Then once you do that you can break it down and templatize the MW. That's how we did it and inserted that Json into the CICD Pipeline. 

-Chad

Naveen
Guide

Thanks @ChadTurner and @mgome I have tried the method suggested by you but getting below error in the request
I am using powershell to get time and invoking API. 

 

Invoke-RestMethod : [{"code":400,"error":{"code":400,"message":"Validation failed for 6 Validators.","constraintViolations":[{"
path":"builtin:alerting.maintenance-window/0/schedule/onceRecurrence/startTime","message":"Given property 'startTime' with 
value: '2023-05-25 11.35.09' violates the following constraint: Must be of type local_date_time.","parameterLocation":"PAYLOAD_
BODY","location":null},{"path":"builtin:alerting.maintenance-window/0/schedule/onceRecurrence/endTime","message":"Given 
property 'endTime' with value: '2023-05-25 12.35.09' violates the following constraint: Must be of type local_date_time.","para
meterLocation":"PAYLOAD_BODY","location":null},{"path":"builtin:alerting.maintenance-window/0/generalProperties/disableSyntheti
cMonitorExecution","message":"Given property 'disableSyntheticMonitorExecution' with value: 'true' violates the following 
constraint: Must be of type boolean.","parameterLocation":"PAYLOAD_BODY","location":null},{"path":"builtin:alerting.maintenance
-window/0/filters/0/entityId","message":"Given property 'entityId' with value: 'null' violates the following constraint: 
Please use a format of TYPE-ID.","parameterLocation":"PAYLOAD_BODY","location":null},{"path":"builtin:alerting.maintenance-wind
ow/0/filters/0/entityId","message":"Given property 'entityId' with value: 'null' violates the following constraint: Invalid 
entity id..","parameterLocation":"PAYLOAD_BODY","location":null},{"path":"builtin:alerting.maintenance-window/0/enabled","messa

 

 

Could you please help, I have tried putting up different time format like 2023-05-25T15.30.54 or 1685009069

Naveen
Guide

Thanks @ChadTurner and @mgome I have tried posting it using the powershell but unable to format the starttime and endtime, could you please let me know what time format is it.

I have tried ([DateTimeOffset]$localTime).ToUnixTimeSeconds() or $localTime.ToString("yyyy-MM-ddTHH:mm:ss") but it's throwing an error.

The proper key is "onceRecurrence". You have "OnceRecurrence" with a capital "O" in your payload.

Hey @mgome still the same error

Invoke-RestMethod : [{"code":400,"error":{"code":400,"message":"Validation failed for 6
Validators.","constraintViolations":[{"path":"builtin:alerting.maintenance-window/0/schedule/onceRecurrence","message":"Given
property 'onceRecurrence' with value: 'null' does not comply with required NonNull of schema","parameterLocation":"PAYLOAD_BODY
","location":null},{"path":"builtin:alerting.maintenance-window/0/schedule/OnceRecurrence","message":"Given property
'OnceRecurrence' with value: '{endTime=2023-05-26T14.39.44, timeZone=Europe/Copenhagen, startTime=2023-05-26T13.39.44}' was
not found in the schema","parameterLocation":"PAYLOAD_BODY","location":null},{"path":"builtin:alerting.maintenance-window/0/gen
eralProperties/disableSyntheticMonitorExecution","message":"Given property 'disableSyntheticMonitorExecution' with value:
'true' violates the following constraint: Must be of type boolean.","parameterLocation":"PAYLOAD_BODY","location":null},{"path"
:"builtin:alerting.maintenance-window/0/filters/0/entityId","message":"Given property 'entityId' with value: 'null' violates
the following constraint: Please use a format of TYPE-ID.","parameterLocation":"PAYLOAD_BODY","location":null},{"path":"builtin
:alerting.maintenance-window/0/filters/0/entityId","message":"Given property 'entityId' with value: 'null' violates the

ChadTurner
DynaMight Legend
DynaMight Legend

@Naveen there are several Syntax errors in your Json. please use the following to post your MZs via the API. You will need to update the start/end time tho. 

[
{
  "scope": "environment",
  "schemaId": "builtin:alerting.maintenance-window",
  "schemaVersion": "2.14.2",
  "value": {
    "enabled": true,
    "generalProperties": {
      "name": "Maintenance windows",
      "description": "Maintenance windows are typically planned, recurring periods of system downtime during which your DevOps team can perform preventative maintenance and system upgrades outside of peak traffic hours.",
      "maintenanceType": "UNPLANNED",
      "suppression": "DONT_DETECT_PROBLEMS",
      "disableSyntheticMonitorExecution": true
    },
    "schedule": {
      "scheduleType": "ONCE",
      "onceRecurrence": {
        "startTime": "2023-07-25T13:45:00",
        "endTime": "2023-08-22T14:45:00",
        "timeZone": "Europe/Vienna"
      }
    },
    "filters": []
  }
}
]

 

So what were you missing? 

ChadTurner_0-1685102274275.png

1 - Schema Version was missing

2 - Conflicting names and descriptions 

3 - No Date with your time stamp for both start and end 

4 - Missing the filters segment

5 - Bracket/Braces were not correct. 

 

Please save this as a gold template for you to leverage with posting of MWs via the API 

-Chad

Hello,

 

I've tried to use this code to create a MW from Servicenow via REST and I am getting the following error:

 

*** Script: {"error":{"code":400,"message":"Could not map JSON at '' near line 1 column 1"}}

 

My code is the following:

 

var rm = new sn_ws.RESTMessageV2(); 
var jsonObj =   {
  "scope":"environment",
  "schemaId":"builtin:alerting.maintenance-window",
  "schemaVersion":"2.14.2",
  "value":{
     "enabled":true,
     "generalProperties":{
        "name":"TEST INTEGRATION",
        "description":"This a test for maintenance window via API",
        "maintenanceType":"PLANNED",
        "suppression":"DONT_DETECT_PROBLEMS",
        "disableSyntheticMonitorExecution":true
     },
     "schedule":{
        "scheduleType":"ONCE",
        "onceRecurrence":{
           "startTime":"2023-12-29T13:45:00",
           "endTime":"2023-12-30T14:45:00",
           "timeZone":"Europe/Vienna"
        }
     },
     "filters":[
        
     ]
  }
};
rm.setEndpoint('https://{environmentid}.live.dynatrace.com/api/v2/settings/objects'); 
rm.setHttpMethod('POST'); 
rm.setRequestHeader('Content-Type', 'application/json;charset=UTF-8');
rm.setRequestHeader('entities.write', 'application/json;charset=UTF-8');
rm.setRequestHeader('Accept', 'application/json; charset=utf-8');
rm.setRequestHeader('Authorization', 'Api-Token dt0c01.......');
rm.setRequestBody(JSON.stringify(jsonObj));


var response = rm.execute();
var responseBody = response.getBody(); 

gs.print(responseBody);

 

 

I dont understand why this is not being able to parse the JSON.

 

Thanks in advance.

 

Gabriel

You need to add square brackets at the start and end of your JSON or the API gets confused, so just add [ and ] at the start and end of your JSON body respectively.

Amazing Victor, thank you very much!

wipkumv3
Visitor

Hi,

Is there a way to fetch the entity ID against whom we need to create a maintenance window and use it in the same script, or how does it work to schedule MW for specific entity?

Featured Posts