Chilkat Online Tools

SQL Server / Anypoint Platform APIs / Create an alert

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

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

    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'name', 'Platform API Postman Test 001'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'severity', 'INFO'
    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 ''memory ${operator} ${value}%''.'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'actions[0].subject', '${severity}: memory usage ${state}'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'actions[0].userIds[0]', '2909a63d-c57a-47b0-90f1-7ddfb815efba'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'condition.operator', 'GREATER_THAN'
    EXEC sp_OAMethod @json, 'UpdateInt', @success OUT, 'condition.periodCount', 20
    EXEC sp_OAMethod @json, 'UpdateInt', @success OUT, 'condition.value', 80
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'condition.resourceType', 'cloudhub-application'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'condition.type', 'memory'
    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 @resp int
    EXEC sp_OAMethod @http, 'PostJson3', @resp OUT, 'https://domain.com/armui/api/v1/alerts/cloudhub', 'application/json', @json
    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
        RETURN
      END

    EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
    PRINT @iTmp0
    EXEC sp_OAGetProperty @resp, 'BodyStr', @sTmp0 OUT
    PRINT @sTmp0
    EXEC @hr = sp_OADestroy @resp


    EXEC @hr = sp_OADestroy @http
    EXEC @hr = sp_OADestroy @json


END
GO

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