Chilkat Online Tools

SQL Server / Anypoint Platform APIs / Update alert by id

Back to Collection Items

-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @iTmp0 int
    -- Important: Do not use nvarchar(max).  See the warning about using nvarchar(max).
    DECLARE @sTmp0 nvarchar(4000)
    -- This example assumes the Chilkat API to have been previously unlocked.
    -- See Global Unlock Sample for sample code.

    DECLARE @http int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.Http', @http OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    DECLARE @success int

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

    DECLARE @json int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.JsonObject', @json OUT

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

    EXEC sp_OAMethod @http, 'SetRequestHeader', NULL, 'X-ANYPNT-ENV-ID', '{{environment_id}}'
    -- Adds the "Authorization: Bearer {{token}}" header.
    EXEC sp_OASetProperty @http, 'AuthToken', '{{token}}'
    EXEC sp_OAMethod @http, 'SetRequestHeader', NULL, 'X-ANYPNT-ORG-ID', '{{organization_id}}'

    DECLARE @sbRequestBody int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.StringBuilder', @sbRequestBody OUT

    EXEC sp_OAMethod @json, 'EmitSb', @success OUT, @sbRequestBody

    DECLARE @resp int
    EXEC sp_OAMethod @http, 'PTextSb', @resp OUT, 'PUT', 'https://anypoint.mulesoft.com/armui/api/v1/alerts/cloudhub/02213013-10f2-4bbb-b399-718c0a543e18', @sbRequestBody, 'utf-8', 'application/json', 0, 0
    EXEC sp_OAGetProperty @http, 'LastMethodSuccess', @iTmp0 OUT
    IF @iTmp0 = 0
      BEGIN
        EXEC sp_OAGetProperty @http, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @json
        EXEC @hr = sp_OADestroy @sbRequestBody
        RETURN
      END

    DECLARE @sbResponseBody int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.StringBuilder', @sbResponseBody OUT

    EXEC sp_OAMethod @resp, 'GetBodySb', @success OUT, @sbResponseBody

    DECLARE @jResp int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.JsonObject', @jResp OUT

    EXEC sp_OAMethod @jResp, 'LoadSb', @success OUT, @sbResponseBody
    EXEC sp_OASetProperty @jResp, 'EmitCompact', 0


    PRINT 'Response Body:'
    EXEC sp_OAMethod @jResp, 'Emit', @sTmp0 OUT
    PRINT @sTmp0

    DECLARE @respStatusCode int
    EXEC sp_OAGetProperty @resp, 'StatusCode', @respStatusCode OUT

    PRINT 'Response Status Code = ' + @respStatusCode
    IF @respStatusCode >= 400
      BEGIN

        PRINT 'Response Header:'
        EXEC sp_OAGetProperty @resp, 'Header', @sTmp0 OUT
        PRINT @sTmp0

        PRINT 'Failed.'
        EXEC @hr = sp_OADestroy @resp

        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @json
        EXEC @hr = sp_OADestroy @sbRequestBody
        EXEC @hr = sp_OADestroy @sbResponseBody
        EXEC @hr = sp_OADestroy @jResp
        RETURN
      END
    EXEC @hr = sp_OADestroy @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

    DECLARE @strVal nvarchar(4000)

    DECLARE @intVal int

    DECLARE @id int
    EXEC sp_OAMethod @jResp, 'IntOf', @id OUT, 'id'
    DECLARE @timeCreated int
    EXEC sp_OAMethod @jResp, 'IntOf', @timeCreated OUT, 'timeCreated'
    DECLARE @timeUpdated int
    EXEC sp_OAMethod @jResp, 'IntOf', @timeUpdated OUT, 'timeUpdated'
    DECLARE @severity nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @severity OUT, 'severity'
    DECLARE @description nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @description OUT, 'description'
    DECLARE @enabled int
    EXEC sp_OAMethod @jResp, 'BoolOf', @enabled OUT, 'enabled'
    DECLARE @ResourceType nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @ResourceType OUT, 'condition.resourceType'
    DECLARE @v_Event nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @v_Event OUT, 'condition.event'
    DECLARE @i int
    SELECT @i = 0
    DECLARE @count_i int
    EXEC sp_OAMethod @jResp, 'SizeOfArray', @count_i OUT, 'action.notification.recipients'
    WHILE @i < @count_i
      BEGIN
        EXEC sp_OASetProperty @jResp, 'I', @i
        EXEC sp_OAMethod @jResp, 'StringOf', @strVal OUT, 'action.notification.recipients[i]'
        SELECT @i = @i + 1
      END
    SELECT @i = 0
    EXEC sp_OAMethod @jResp, 'SizeOfArray', @count_i OUT, 'condition.resources'
    WHILE @i < @count_i
      BEGIN
        EXEC sp_OASetProperty @jResp, 'I', @i
        EXEC sp_OAMethod @jResp, 'IntOf', @intVal OUT, 'condition.resources[i]'
        SELECT @i = @i + 1
      END

    EXEC @hr = sp_OADestroy @http
    EXEC @hr = sp_OADestroy @json
    EXEC @hr = sp_OADestroy @sbRequestBody
    EXEC @hr = sp_OADestroy @sbResponseBody
    EXEC @hr = sp_OADestroy @jResp


END
GO

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