Chilkat Online Tools

Objective-C / Anypoint Platform APIs / Create an alert

Back to Collection Items

#import <CkoHttp.h>
#import <CkoJsonObject.h>
#import <CkoHttpResponse.h>

// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.

CkoHttp *http = [[CkoHttp alloc] init];
BOOL success;

// Use this online tool to generate code from sample JSON: Generate Code to Create JSON

// The following JSON is sent in the request body.

// {
//   "name": "Platform API Postman Test 001",
//   "severity": "INFO",
//   "actions": [
//     {
//       "type": "email",
//       "content": "Hello,\nYou are receiving this alert because:\nThe application ${resource} is now in an ${state} state, based on the condition 'memory ${operator} ${value}%'.",
//       "subject": "${severity}: memory usage ${state}",
//       "userIds": [
//         "2909a63d-c57a-47b0-90f1-7ddfb815efba"
//       ]
//     }
//   ],
//   "condition": {
//     "operator": "GREATER_THAN",
//     "periodCount": 20,
//     "value": 80,
//     "resourceType": "cloudhub-application",
//     "type": "memory",
//     "periodMins": 1,
//     "resources": [
//       "*"
//     ]
//   }
// }

CkoJsonObject *json = [[CkoJsonObject alloc] init];
[json UpdateString: @"name" value: @"Platform API Postman Test 001"];
[json UpdateString: @"severity" value: @"INFO"];
[json UpdateString: @"actions[0].type" value: @"email"];
[json UpdateString: @"actions[0].content" value: @"Hello,\nYou are receiving this alert because:\nThe application ${resource} is now in an ${state} state, based on the condition 'memory ${operator} ${value}%'."];
[json UpdateString: @"actions[0].subject" value: @"${severity}: memory usage ${state}"];
[json UpdateString: @"actions[0].userIds[0]" value: @"2909a63d-c57a-47b0-90f1-7ddfb815efba"];
[json UpdateString: @"condition.operator" value: @"GREATER_THAN"];
[json UpdateInt: @"condition.periodCount" value: [NSNumber numberWithInt: 20]];
[json UpdateInt: @"condition.value" value: [NSNumber numberWithInt: 80]];
[json UpdateString: @"condition.resourceType" value: @"cloudhub-application"];
[json UpdateString: @"condition.type" value: @"memory"];
[json UpdateInt: @"condition.periodMins" value: [NSNumber numberWithInt: 1]];
[json UpdateString: @"condition.resources[0]" value: @"*"];

[http SetRequestHeader: @"X-ANYPNT-ENV-ID" value: @"{{environment_id}}"];
// Adds the "Authorization: Bearer {{token}}" header.
http.AuthToken = @"{{token}}";
[http SetRequestHeader: @"X-ANYPNT-ORG-ID" value: @"{{organization_id}}"];

CkoHttpResponse *resp = [http PostJson3: @"https://domain.com/armui/api/v1/alerts/cloudhub" contentType: @"application/json" json: json];
if (http.LastMethodSuccess == NO) {
    NSLog(@"%@",http.LastErrorText);
    return;
}

NSLog(@"%d",[resp.StatusCode intValue]);
NSLog(@"%@",resp.BodyStr);

Curl Command

curl -X POST
	-H "Authorization: Bearer {{token}}"
	-H "X-ANYPNT-ORG-ID: {{organization_id}}"
	-H "X-ANYPNT-ENV-ID: {{environment_id}}"
	-d '{
    "name": "Platform API Postman Test 001",
    "severity": "INFO",
    "actions": [
        {
            "type": "email",
            "content": "Hello,\nYou are receiving this alert because:\nThe application ${resource} is now in an ${state} state, based on the condition \'memory ${operator} ${value}%\'.",
            "subject": "${severity}: memory usage ${state}",
            "userIds": [
                "2909a63d-c57a-47b0-90f1-7ddfb815efba"
            ]
        }
    ],
    "condition": {
        "operator": "GREATER_THAN",
        "periodCount": 20,
        "value": 80,
        "resourceType": "cloudhub-application",
        "type": "memory",
        "periodMins": 1,
        "resources": [
            "*"
        ]
    }
}'
https://domain.com/armui/api/v1/alerts/cloudhub

Postman Collection Item JSON

{
  "name": "Create an alert",
  "request": {
    "method": "POST",
    "header": [
      {
        "key": "Authorization",
        "value": "Bearer {{token}}",
        "type": "text"
      },
      {
        "key": "X-ANYPNT-ORG-ID",
        "value": "{{organization_id}}",
        "type": "text"
      },
      {
        "key": "X-ANYPNT-ENV-ID",
        "value": "{{environment_id}}",
        "type": "text"
      }
    ],
    "body": {
      "mode": "raw",
      "raw": "{\r\n    \"name\": \"Platform API Postman Test 001\",\r\n    \"severity\": \"INFO\",\r\n    \"actions\": [\r\n        {\r\n            \"type\": \"email\",\r\n            \"content\": \"Hello,\\nYou are receiving this alert because:\\nThe application ${resource} is now in an ${state} state, based on the condition 'memory ${operator} ${value}%'.\",\r\n            \"subject\": \"${severity}: memory usage ${state}\",\r\n            \"userIds\": [\r\n                \"2909a63d-c57a-47b0-90f1-7ddfb815efba\"\r\n            ]\r\n        }\r\n    ],\r\n    \"condition\": {\r\n        \"operator\": \"GREATER_THAN\",\r\n        \"periodCount\": 20,\r\n        \"value\": 80,\r\n        \"resourceType\": \"cloudhub-application\",\r\n        \"type\": \"memory\",\r\n        \"periodMins\": 1,\r\n        \"resources\": [\r\n            \"*\"\r\n        ]\r\n    }\r\n}",
      "options": {
        "raw": {
          "language": "json"
        }
      }
    },
    "url": {
      "raw": "{{url}}/armui/api/v1/alerts/cloudhub",
      "host": [
        "{{url}}"
      ],
      "path": [
        "armui",
        "api",
        "v1",
        "alerts",
        "cloudhub"
      ]
    }
  },
  "response": [
  ]
}