Chilkat Online Tools

C / Anypoint Platform APIs / Update alert by id

Back to Collection Items

#include <C_CkHttp.h>
#include <C_CkJsonObject.h>
#include <C_CkStringBuilder.h>
#include <C_CkHttpResponse.h>

void ChilkatSample(void)
    {
    HCkHttp http;
    BOOL success;
    HCkJsonObject json;
    HCkStringBuilder sbRequestBody;
    HCkHttpResponse resp;
    HCkStringBuilder sbResponseBody;
    HCkJsonObject jResp;
    int respStatusCode;
    const char *strVal;
    int intVal;
    int id;
    int timeCreated;
    int timeUpdated;
    const char *severity;
    const char *description;
    BOOL enabled;
    const char *ResourceType;
    const char *v_Event;
    int i;
    int count_i;

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

    http = CkHttp_Create();

    // 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": "Low CPU usage",
    //   "severity": "WARNING",
    //   "actions": [
    //     {
    //       "type": "email",
    //       "content": "Hello,\nYou are receiving this alert because:\nThe application ${resource} is now in an ${state} state, based on the condition 'CPU ${operator} ${value}%.",
    //       "subject": "${severity}: CPU usage ${state}",
    //       "emails": [
    //         "angel.alberici+testalerts@mulesoft.com",
    //         "angel.alberici@mulesoft.com"
    //       ]
    //     }
    //   ],
    //   "condition": {
    //     "operator": "LESS_THAN",
    //     "value": 50,
    //     "periodCount": 1,
    //     "resourceType": "cloudhub-application",
    //     "type": "cpu",
    //     "periodMins": 1,
    //     "resources": [
    //       "*"
    //     ]
    //   }
    // }

    json = CkJsonObject_Create();
    CkJsonObject_UpdateString(json,"name","Low CPU usage");
    CkJsonObject_UpdateString(json,"severity","WARNING");
    CkJsonObject_UpdateString(json,"actions[0].type","email");
    CkJsonObject_UpdateString(json,"actions[0].content","Hello,\nYou are receiving this alert because:\nThe application ${resource} is now in an ${state} state, based on the condition 'CPU ${operator} ${value}%.");
    CkJsonObject_UpdateString(json,"actions[0].subject","${severity}: CPU usage ${state}");
    CkJsonObject_UpdateString(json,"actions[0].emails[0]","angel.alberici+testalerts@mulesoft.com");
    CkJsonObject_UpdateString(json,"actions[0].emails[1]","angel.alberici@mulesoft.com");
    CkJsonObject_UpdateString(json,"condition.operator","LESS_THAN");
    CkJsonObject_UpdateInt(json,"condition.value",50);
    CkJsonObject_UpdateInt(json,"condition.periodCount",1);
    CkJsonObject_UpdateString(json,"condition.resourceType","cloudhub-application");
    CkJsonObject_UpdateString(json,"condition.type","cpu");
    CkJsonObject_UpdateInt(json,"condition.periodMins",1);
    CkJsonObject_UpdateString(json,"condition.resources[0]","*");

    CkHttp_SetRequestHeader(http,"X-ANYPNT-ENV-ID","{{environment_id}}");
    // Adds the "Authorization: Bearer {{token}}" header.
    CkHttp_putAuthToken(http,"{{token}}");
    CkHttp_SetRequestHeader(http,"X-ANYPNT-ORG-ID","{{organization_id}}");

    sbRequestBody = CkStringBuilder_Create();
    CkJsonObject_EmitSb(json,sbRequestBody);

    resp = CkHttp_PTextSb(http,"PUT","https://anypoint.mulesoft.com/armui/api/v1/alerts/cloudhub/02213013-10f2-4bbb-b399-718c0a543e18",sbRequestBody,"utf-8","application/json",FALSE,FALSE);
    if (CkHttp_getLastMethodSuccess(http) == FALSE) {
        printf("%s\n",CkHttp_lastErrorText(http));
        CkHttp_Dispose(http);
        CkJsonObject_Dispose(json);
        CkStringBuilder_Dispose(sbRequestBody);
        return;
    }

    sbResponseBody = CkStringBuilder_Create();
    CkHttpResponse_GetBodySb(resp,sbResponseBody);

    jResp = CkJsonObject_Create();
    CkJsonObject_LoadSb(jResp,sbResponseBody);
    CkJsonObject_putEmitCompact(jResp,FALSE);

    printf("Response Body:\n");
    printf("%s\n",CkJsonObject_emit(jResp));

    respStatusCode = CkHttpResponse_getStatusCode(resp);
    printf("Response Status Code = %d\n",respStatusCode);
    if (respStatusCode >= 400) {
        printf("Response Header:\n");
        printf("%s\n",CkHttpResponse_header(resp));
        printf("Failed.\n");
        CkHttpResponse_Dispose(resp);
        CkHttp_Dispose(http);
        CkJsonObject_Dispose(json);
        CkStringBuilder_Dispose(sbRequestBody);
        CkStringBuilder_Dispose(sbResponseBody);
        CkJsonObject_Dispose(jResp);
        return;
    }

    CkHttpResponse_Dispose(resp);

    // Sample JSON response:
    // (Sample code for parsing the JSON response is shown below)

    // {
    //   "id": 2312,
    //   "timeCreated": 342423432142342,
    //   "timeUpdated": 342423432154342,
    //   "severity": "CRITICAL",
    //   "description": "Notify when one server of cluster 45 went down",
    //   "enabled": true,
    //   "action": {
    //     "notification": {
    //       "recipients": [
    //         "alex.tiavo",
    //         "greg.kio"
    //       ]
    //     }
    //   },
    //   "condition": {
    //     "resourceType": "CLUSTER",
    //     "resources": [
    //       45
    //     ],
    //     "event": "DISCONNECTED"
    //   }
    // }

    // Sample code for parsing the JSON response...
    // Use this online tool to generate parsing code from sample JSON: Generate JSON Parsing Code

    // Chilkat functions returning "const char *" return a pointer to temporary internal memory owned and managed by Chilkat.

    id = CkJsonObject_IntOf(jResp,"id");
    timeCreated = CkJsonObject_IntOf(jResp,"timeCreated");
    timeUpdated = CkJsonObject_IntOf(jResp,"timeUpdated");
    severity = CkJsonObject_stringOf(jResp,"severity");
    description = CkJsonObject_stringOf(jResp,"description");
    enabled = CkJsonObject_BoolOf(jResp,"enabled");
    ResourceType = CkJsonObject_stringOf(jResp,"condition.resourceType");
    v_Event = CkJsonObject_stringOf(jResp,"condition.event");
    i = 0;
    count_i = CkJsonObject_SizeOfArray(jResp,"action.notification.recipients");
    while (i < count_i) {
        CkJsonObject_putI(jResp,i);
        strVal = CkJsonObject_stringOf(jResp,"action.notification.recipients[i]");
        i = i + 1;
    }

    i = 0;
    count_i = CkJsonObject_SizeOfArray(jResp,"condition.resources");
    while (i < count_i) {
        CkJsonObject_putI(jResp,i);
        intVal = CkJsonObject_IntOf(jResp,"condition.resources[i]");
        i = i + 1;
    }



    CkHttp_Dispose(http);
    CkJsonObject_Dispose(json);
    CkStringBuilder_Dispose(sbRequestBody);
    CkStringBuilder_Dispose(sbResponseBody);
    CkJsonObject_Dispose(jResp);

    }

Curl Command

curl -X PUT
	-H "X-ANYPNT-ENV-ID: {{environment_id}}"
	-H "X-ANYPNT-ORG-ID: {{organization_id}}"
	-H "Authorization: Bearer {{token}}"
	-d '{
    "name": "Low CPU usage",
    "severity": "WARNING",
    "actions": [
        {
            "type": "email",
            "content": "Hello,\nYou are receiving this alert because:\nThe application ${resource} is now in an ${state} state, based on the condition \'CPU ${operator} ${value}%.",
            "subject": "${severity}: CPU usage ${state}",
            "emails": [
                "angel.alberici+testalerts@mulesoft.com",
                "angel.alberici@mulesoft.com"
            ]
        }
    ],
    "condition": {
        "operator": "LESS_THAN",
        "value": 50,
        "periodCount": 1,
        "resourceType": "cloudhub-application",
        "type": "cpu",
        "periodMins": 1,
        "resources": [
            "*"
        ]
    }
}'
https://anypoint.mulesoft.com/armui/api/v1/alerts/cloudhub/02213013-10f2-4bbb-b399-718c0a543e18

Postman Collection Item JSON

{
  "name": "Update alert by id",
  "request": {
    "method": "PUT",
    "header": [
      {
        "description": "(Required) Identifier of the environment where the api user is currently working.",
        "key": "X-ANYPNT-ENV-ID",
        "value": "{{environment_id}}"
      },
      {
        "description": "(Required) Identifier of the sub org where the api user is currently working.",
        "key": "X-ANYPNT-ORG-ID",
        "value": "{{organization_id}}"
      },
      {
        "key": "Authorization",
        "value": "Bearer {{token}}",
        "type": "text"
      }
    ],
    "body": {
      "mode": "raw",
      "raw": "{\n    \"name\": \"Low CPU usage\",\n    \"severity\": \"WARNING\",\n    \"actions\": [\n        {\n            \"type\": \"email\",\n            \"content\": \"Hello,\\nYou are receiving this alert because:\\nThe application ${resource} is now in an ${state} state, based on the condition 'CPU ${operator} ${value}%.\",\n            \"subject\": \"${severity}: CPU usage ${state}\",\n            \"emails\": [\n                \"angel.alberici+testalerts@mulesoft.com\",\n                \"angel.alberici@mulesoft.com\"\n            ]\n        }\n    ],\n    \"condition\": {\n        \"operator\": \"LESS_THAN\",\n        \"value\": 50,\n        \"periodCount\": 1,\n        \"resourceType\": \"cloudhub-application\",\n        \"type\": \"cpu\",\n        \"periodMins\": 1,\n        \"resources\": [\n            \"*\"\n        ]\n    }\n}",
      "options": {
        "raw": {
          "language": "json"
        }
      }
    },
    "url": {
      "raw": "https://anypoint.mulesoft.com/armui/api/v1/alerts/cloudhub/02213013-10f2-4bbb-b399-718c0a543e18",
      "protocol": "https",
      "host": [
        "anypoint",
        "mulesoft",
        "com"
      ],
      "path": [
        "armui",
        "api",
        "v1",
        "alerts",
        "cloudhub",
        "02213013-10f2-4bbb-b399-718c0a543e18"
      ]
    }
  },
  "response": [
    {
      "name": "Untitled Example",
      "originalRequest": {
        "method": "PATCH",
        "header": [
          {
            "description": "(Required) Identifier of the environment where the api user is currently working.",
            "key": "X-ANYPNT-ENV-ID",
            "value": "pariatur magna in id esse"
          },
          {
            "description": "(Required) Identifier of the sub org where the api user is currently working.",
            "key": "X-ANYPNT-ORG-ID",
            "value": "pariatur magna in id esse"
          }
        ],
        "url": {
          "raw": "{{baseUrl}}/alerts/:alertId",
          "host": [
            "{{baseUrl}}"
          ],
          "path": [
            "alerts",
            ":alertId"
          ],
          "variable": [
            {
              "key": "alertId"
            }
          ]
        }
      },
      "status": "Accepted",
      "code": 202,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Content-Type",
          "value": "application/json"
        }
      ],
      "cookie": [
      ],
      "body": "{\n \"id\": 2312,\n \"timeCreated\": 342423432142342,\n \"timeUpdated\": 342423432154342,\n \"severity\": \"CRITICAL\",\n \"description\": \"Notify when one server of cluster 45 went down\",\n \"enabled\": true,\n \"action\": {\n  \"notification\": {\n   \"recipients\": [\n    \"alex.tiavo\",\n    \"greg.kio\"\n   ]\n  }\n },\n \"condition\": {\n  \"resourceType\": \"CLUSTER\",\n  \"resources\": [\n   45\n  ],\n  \"event\": \"DISCONNECTED\"\n }\n}"
    }
  ]
}