Chilkat Online Tools

autoit / Anypoint Platform APIs / Update alert by id

Back to Collection Items

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

$oHttp = ObjCreate("Chilkat.Http")
Local $bSuccess

; 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": [
;       "*"
;     ]
;   }
; }

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

$oHttp.SetRequestHeader "X-ANYPNT-ENV-ID","{{environment_id}}"
; Adds the "Authorization: Bearer {{token}}" header.
$oHttp.AuthToken = "{{token}}"
$oHttp.SetRequestHeader "X-ANYPNT-ORG-ID","{{organization_id}}"

$oSbRequestBody = ObjCreate("Chilkat.StringBuilder")
$oJson.EmitSb($oSbRequestBody)

Local $oResp = $oHttp.PTextSb("PUT","https://anypoint.mulesoft.com/armui/api/v1/alerts/cloudhub/02213013-10f2-4bbb-b399-718c0a543e18",$oSbRequestBody,"utf-8","application/json",False,False)
If ($oHttp.LastMethodSuccess = False) Then
    ConsoleWrite($oHttp.LastErrorText & @CRLF)
    Exit
EndIf

$oSbResponseBody = ObjCreate("Chilkat.StringBuilder")
$oResp.GetBodySb($oSbResponseBody)

$oJResp = ObjCreate("Chilkat.JsonObject")
$oJResp.LoadSb($oSbResponseBody)
$oJResp.EmitCompact = False

ConsoleWrite("Response Body:" & @CRLF)
ConsoleWrite($oJResp.Emit() & @CRLF)

Local $iRespStatusCode = $oResp.StatusCode
ConsoleWrite("Response Status Code = " & $iRespStatusCode & @CRLF)
If ($iRespStatusCode >= 400) Then
    ConsoleWrite("Response Header:" & @CRLF)
    ConsoleWrite($oResp.Header & @CRLF)
    ConsoleWrite("Failed." & @CRLF)

    Exit
EndIf

; 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

Local $strVal
Local $intVal

Local $id = $oJResp.IntOf("id")
Local $iTimeCreated = $oJResp.IntOf("timeCreated")
Local $iTimeUpdated = $oJResp.IntOf("timeUpdated")
Local $severity = $oJResp.StringOf("severity")
Local $sDescription = $oJResp.StringOf("description")
Local $bEnabled = $oJResp.BoolOf("enabled")
Local $sResourceType = $oJResp.StringOf("condition.resourceType")
Local $sV_Event = $oJResp.StringOf("condition.event")
Local $i = 0
Local $iCount_i = $oJResp.SizeOfArray("action.notification.recipients")
While $i < $iCount_i
    $oJResp.I = $i
    $strVal = $oJResp.StringOf("action.notification.recipients[i]")
    $i = $i + 1
Wend
$i = 0
$iCount_i = $oJResp.SizeOfArray("condition.resources")
While $i < $iCount_i
    $oJResp.I = $i
    $intVal = $oJResp.IntOf("condition.resources[i]")
    $i = $i + 1
Wend

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