Chilkat Online Tools

PureBasic / Anypoint Platform APIs / Update alert by id

Back to Collection Items

IncludeFile "CkJsonObject.pb"
IncludeFile "CkStringBuilder.pb"
IncludeFile "CkHttp.pb"
IncludeFile "CkHttpResponse.pb"

Procedure ChilkatExample()

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

    http.i = CkHttp::ckCreate()
    If http.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success.i

    ; 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.i = CkJsonObject::ckCreate()
    If json.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

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

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

    sbRequestBody.i = CkStringBuilder::ckCreate()
    If sbRequestBody.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkJsonObject::ckEmitSb(json,sbRequestBody)

    resp.i = CkHttp::ckPTextSb(http,"PUT","https://anypoint.mulesoft.com/armui/api/v1/alerts/cloudhub/02213013-10f2-4bbb-b399-718c0a543e18",sbRequestBody,"utf-8","application/json",0,0)
    If CkHttp::ckLastMethodSuccess(http) = 0
        Debug CkHttp::ckLastErrorText(http)
        CkHttp::ckDispose(http)
        CkJsonObject::ckDispose(json)
        CkStringBuilder::ckDispose(sbRequestBody)
        ProcedureReturn
    EndIf

    sbResponseBody.i = CkStringBuilder::ckCreate()
    If sbResponseBody.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkHttpResponse::ckGetBodySb(resp,sbResponseBody)

    jResp.i = CkJsonObject::ckCreate()
    If jResp.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkJsonObject::ckLoadSb(jResp,sbResponseBody)
    CkJsonObject::setCkEmitCompact(jResp, 0)

    Debug "Response Body:"
    Debug CkJsonObject::ckEmit(jResp)

    respStatusCode.i = CkHttpResponse::ckStatusCode(resp)
    Debug "Response Status Code = " + Str(respStatusCode)
    If respStatusCode >= 400
        Debug "Response Header:"
        Debug CkHttpResponse::ckHeader(resp)
        Debug "Failed."
        CkHttpResponse::ckDispose(resp)

        CkHttp::ckDispose(http)
        CkJsonObject::ckDispose(json)
        CkStringBuilder::ckDispose(sbRequestBody)
        CkStringBuilder::ckDispose(sbResponseBody)
        CkJsonObject::ckDispose(jResp)
        ProcedureReturn
    EndIf

    CkHttpResponse::ckDispose(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

    strVal.s
    intVal.i

    id.i = CkJsonObject::ckIntOf(jResp,"id")
    timeCreated.i = CkJsonObject::ckIntOf(jResp,"timeCreated")
    timeUpdated.i = CkJsonObject::ckIntOf(jResp,"timeUpdated")
    severity.s = CkJsonObject::ckStringOf(jResp,"severity")
    description.s = CkJsonObject::ckStringOf(jResp,"description")
    enabled.i = CkJsonObject::ckBoolOf(jResp,"enabled")
    ResourceType.s = CkJsonObject::ckStringOf(jResp,"condition.resourceType")
    v_Event.s = CkJsonObject::ckStringOf(jResp,"condition.event")
    i.i = 0
    count_i.i = CkJsonObject::ckSizeOfArray(jResp,"action.notification.recipients")
    While i < count_i
        CkJsonObject::setCkI(jResp, i)
        strVal = CkJsonObject::ckStringOf(jResp,"action.notification.recipients[i]")
        i = i + 1
    Wend
    i = 0
    count_i = CkJsonObject::ckSizeOfArray(jResp,"condition.resources")
    While i < count_i
        CkJsonObject::setCkI(jResp, i)
        intVal = CkJsonObject::ckIntOf(jResp,"condition.resources[i]")
        i = i + 1
    Wend


    CkHttp::ckDispose(http)
    CkJsonObject::ckDispose(json)
    CkStringBuilder::ckDispose(sbRequestBody)
    CkStringBuilder::ckDispose(sbResponseBody)
    CkJsonObject::ckDispose(jResp)


    ProcedureReturn
EndProcedure

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}"
    }
  ]
}