Updated on 2024-04-15 GMT+08:00

Creating a Policy

Function

This API is used to create a policy. The value must contain 1 to 64 characters starting with a letter. Only digits, letters, underscores (_), and hyphens (-) are allowed.In an AS group, for the same metric (metric_name), the value of metric_threshold with metric_operation set to > must be greater than that of metric_threshold with metric_operation set to <.In an AS group, you can create only one alarm policy with the same metric_operation for each metric.In a policy, conditions of metrics with the same metric_name cannot conflict.The year in the trigger time (launch_time) of a scheduled policy cannot be later than 2099.The year in the start time (start_time) and end time (end_time) of a periodic policy cannot be later than 2099.An AS group supports a maximum of 10 scheduled and periodic policies, and 10 alarm policies.In an AS group, alarm policies cannot affect each other.

URI

POST /v1/{project_id}/pe/policy

Table 1 Path Parameters

Parameter

Mandatory

Type

Description

project_id

Yes

String

Project ID.

Request Parameters

Table 2 Request header parameters

Parameter

Mandatory

Type

Description

resourcetype

Yes

String

Resource type. Example: node

Content-Type

Yes

String

Content type, which is application/json;charset=utf-8.

Enumeration values:

  • application/json;charset=utf-8

Cluster-Id

Yes

String

Cluster ID.

Namespace

Yes

String

Namespace.

X-Auth-Token

Yes

String

User token obtained from IAM.

Reserved-Info

No

String

Custom field.

Table 3 Request body parameters

Parameter

Mandatory

Type

Description

name

Yes

String

Policy name.

policy_type

Yes

String

Policy type. Value: SCHEDULED RECURRENCE ALARM

Enumeration values:

  • SCHEDULED

  • RECURRENCE

  • ALARM

group_id

Yes

String

Policy group ID.

rule

Yes

AlarmRule object

Policy trigger rule.

Table 4 AlarmRule

Parameter

Mandatory

Type

Description

actions

Yes

Array of Action objects

Action executed after a specified policy is matched.

conditions

Yes

Array of AlarmConditions objects

Condition contents. A rule can contain multiple conditions in AND relationships. One condition describes the matching method of one metric.

Table 5 Action

Parameter

Mandatory

Type

Description

type

Yes

String

scale_out_k8s and scale_out_vm indicate a scale-out. scale_in_k8s and scale_in_vm indicate a scale-in. Note: The value containing k8s indicates a scale-in or -out for containerized applications and that containing vm indicates a scale-in or -out for process applications.

Enumeration values:

  • scale_out_k8s

  • scale_out_vm

  • scale_in_k8s

  • scale_in_vm

parameters

Yes

Array of Parameter objects

Number of scale-in or -out instances.

Table 6 Parameter

Parameter

Mandatory

Type

Description

scale_unit

Yes

Integer

Number of scale-in or -out instances. The value is between the minimum number of instances to the maximum number of instances in a policy group.

Table 7 AlarmConditions

Parameter

Mandatory

Type

Description

metric_namespace

Yes

String

Metric namespace.

Enumeration values:

  • PAAS.CONTAINER

  • PAAS.CUSTOMMETRICS

metric_name

Yes

String

Metric name.

Enumeration values:

  • ^[a-zA-Z_:][a-zA-Z0-9_:]{0

  • 254}$

metric_unit

Yes

String

Unit. Note: The value is retrieved from an AMS API and varies with the metric name.

period

Yes

Integer

Statistical period (unit: s).

Enumeration values:

  • 20

  • 60

  • 300

  • 900

  • 1800

  • 3600

evaluation_periods

Yes

Integer

Number of consecutive periods.

Enumeration values:

  • 1

  • 2

  • 3

  • 4

  • 5

statistic

Yes

String

Statistic.

Enumeration values:

  • average

metric_operation

Yes

String

Metric operator. Option: > or <. For example, you can use > in a threshold criterion (when the value of a metric is greater than metric_thresholdUpdate) to trigger actions.

Enumeration values:

  • >

  • <

metric_threshold

Yes

Integer

Threshold condition.

Response Parameters

Status code: 200

Table 8 Response body parameters

Parameter

Type

Description

errorCode

String

Error code.

errorMessage

String

Details.

policy_id

String

Policy ID.

Example Requests

  • Example of a periodic policy

    POST https://{EndPoint}/v1/{project_id}/pe/policy
    
    {
      "name" : "policy_2",
      "policy_type" : "RECURRENCE",
      "rule" : {
        "conditions" : [ {
          "launch_time" : "13:45",
          "recurrence_type" : "Weekly",
          "recurrence_value" : "0,1,4",
          "start_time" : "2017-01-26T03:33Z",
          "end_time" : "2099-01-31T03:33Z"
        } ],
        "actions" : [ {
          "type" : "scale_set_k8s",
          "parameters" : {
            "scale_unit" : 1
          }
        } ]
      }
    }
  • Example of an alarm policy

    POST https://{EndPoint}/v1/{project_id}/pe/policy
    
    {
      "name" : "policy_1",
      "policy_type" : "ALARM",
      "rule" : {
        "conditions" : [ {
          "metric_namespace" : "PAAS.CONTAINER",
          "metric_name" : "cpuUsage",
          "metric_unit" : "Percent",
          "period" : 60,
          "evaluation_periods" : 1,
          "statistic" : "average",
          "metric_operation" : ">",
          "metric_threshold" : 70
        } ],
        "actions" : [ {
          "type" : "scale_out_k8s",
          "parameters" : {
            "scale_unit" : 1
          }
        } ]
      }
    }
  • Example of a scheduled policy

    POST https://{EndPoint}/v1/{project_id}/pe/policy
    
    {
      "name" : "policy1",
      "policy_type" : "SCHEDULED",
      "rule" : {
        "conditions" : [ {
          "launch_time" : "2017-03-04T03:37Z",
          "recurrence_type" : null,
          "recurrence_value" : null,
          "start_time" : null,
          "end_time" : null
        } ],
        "actions" : [ {
          "type" : "scale_set_k8s",
          "parameters" : {
            "scale_unit" : 1
          }
        } ]
      }
    }

Example Responses

Status code: 200

OK

The request is successful.

{
  "errorCode" : "SVCSTG.PE.0",
  "errorMessage" : "",
  "policy_id" : "1b9994f0-847a-45e4-aeee-e8b604dddb34"
}

Status Codes

Status Code

Description

200

OK

The request is successful.

400

Bad Request

Invalid request. The client should not repeat the request without modifications.

401

Unauthorized

The authorization information is incorrect or invalid.

403

ForbiddenThe request is rejected. The server has received the request and understood it, but the server refuses to respond to it. The client should not repeat the request without modifications.

500

Internal Server Error

The server is able to receive the request but unable to understand the request.

503

Service Unavailable

The requested service is invalid. The client should not repeat the request without modifications.

Error Codes

See Error Codes.