Updated on 2023-06-28 GMT+08:00

Creating a Policy

Function

This API is used to create a policy.

  • The value must be 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 with metric_operation set to <.
  • In an AS group, the metric_operation for a metric must be unique.
  • In a policy, the logic of 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

Request

Request headers

Table 1 describes the request headers.

Table 1 Request headers

Parameter

Description

Mandatory

Example

Deployment-Name

Application name.

Yes

-

Content-Type

Content type,

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

Yes

application/json;charset=utf-8

Cluster-Id

Cluster ID.

Yes

-

Namespace

Namespace.

Yes

-

X-Auth-Token

User token obtained from IAM.

Yes

-

Reserved-Info

Custom parameter of the product.

No

-

Request parameters

Table 2 describes the request parameters.

Table 2 Request parameters

Parameter

Mandatory

Type

Value Range

Description

name

Yes

String

The value must be 1 to 64 characters starting with a letter. Only digits, letters, underscores (_), and hyphens (-) are allowed.

Policy name.

policy_type

Yes

String

  • SCHEDULED
  • RECURRENCE
  • ALARM

Policy type.

rule

Yes

See Table 3.

-

Policy trigger rule.

Table 3 rule parameters

Parameter

Mandatory

Type

Value Range

Description

conditions

Yes

See Table 4.

Only one condition is allowed.

Condition contents.

When multiple alarm policies are used, their conditions cannot overlap. Example: You cannot set a metric greater than 10% in one condition and smaller than 20% in another condition.

actions

Yes

See Table 5.

Only one action is allowed.

Action executed after a specified policy is successfully matched.

Table 4 conditions parameters

Parameter

Mandatory

Type

Value Range

Description

launch_time

Yes

String

-

Trigger time, which must comply with ISO 8601 or UTC specifications.

  • If the value of policy_type is SCHEDULED, the time format is YYYY-MM-DDThh:mmZ.
  • If the value of policy_type is RECURRENCE, the time format is hh:mm.

recurrence_type

Yes

String

Value: Daily, Weekly, or Monthly.

  • Daily: Execution by day
  • Weekly: Execution by week
  • Monthly: Execution by month

Trigger period.

recurrence_value

Yes

String

  • When the value of recurrence_type is Daily, the value of this parameter is null.
  • When the value of recurrence_type is Weekly, this parameter indicates the Nth day within a week. Its value ranges from 0 to 6. 0 indicates Sunday, 1 indicates Monday, and the same rule applies to other values. To select multiple values, separate them using commas (,). Example: 0,2,4, which indicates Sunday, Tuesday, and Thursday, respectively.
  • When the value of recurrence_type is Monthly, this parameter indicates a day in a month. To select multiple values, separate them using commas (,). Example: 1,10,13,28.

Number of tasks executed when a periodic policy is triggered.

start_time

Yes

String

-

Time when periodic policy execution starts, which must comply with ISO 8601 or UTC specifications. Format: YYYY-MM-DDThh:mmZ.

end_time

Yes

String

-

Time when periodic policy execution stops, which must comply with ISO 8601 or UTC specifications. Format: YYYY-MM-DDThh:mmZ.

metric_namespace

Yes

String

  • PAAS.CONTAINER: application metric namespace.
  • PAAS.CUSTOMMETRICS: custom metric namespace.

Namespace.

metric_name

Yes

String

The value must be 1 to 255 characters long and meet the [a-zA-Z_:][a-zA-Z0-9_:]* expression. That is, the value must start with a letter, underscore (_), or colon (:). Only letters, digits, underscores, and colons are allowed.

Metric name.

metric_unit

Yes

String

-

Unit.

period

Yes

Integer

20, 60, 300, 900, 1800, or 3600

Statistical period (unit: s).

evaluation_periods

Yes

Integer

1, 2, 3, 4, or 5

Number of consecutive periods.

statistic

Yes

String

Currently, only average is supported.

Statistic.

metric_operation

Yes

String

> and <.

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

Metric operator.

metric_threshold

Yes

Float

[0, 2147483647]

Threshold criterion.

The following describes the meaning of each field in the conditions parameter.
"conditions": [{
   "metric_namespace" : "PAAS.CONTAINER", 
   "metric_name" : "cpuUsage", 
   "metric_unit" : "Percent", 
   "period" : 300, 
   "evaluation_periods" : 2, 
   "statistic" : "average", 
   "metric_operation" : ">", 
   "metric_threshold" : 70 
   }]

For cpuUsage (metric_name) in PAAS.CONTAINER (metric_namespace), when its average value (statistic) exceeds 70% (metric_threshold) for two (evaluation_periods) consecutive periods (with a duration of 300s), the policy is triggered.

Table 5 actions parameters

Parameter

Mandatory

Type

Value Range

Description

type

Yes

String

  • scale_out_k8s
  • scale_out_vm
  • scale_in_k8s
  • scale_in_vm
  • scale_set_k8s
  • scale_set_vm
  • scale_out_k8s and scale_out_vm indicate a scale-out.
  • scale_in_k8s and scale_in_vm indicate a scale-in.
  • scale_set_k8s and scale_set_vm indicate the number of application instances.
NOTE:

The value containing k8s indicates a container application and that containing vm indicates a process application.

parameters

Yes

See Table 6.

-

Number of scale-in or -out instances.

Table 6 parameters

Parameter

Mandatory

Type

Value Range

Description

scale_unit

Yes

Integer

Minimum number of instances to the maximum number of instances in a policy group.

Number of scale-in or -out instances.

Example request

  • Example of a scheduled 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 of a periodic 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
    {
        "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
                    }
                }
            ]
        }
    }

Response

Response parameters

Table 7 describes the response parameters.

Table 7 Response parameters

Parameter

Type

Description

errorCode

String

Error code.

errorMessage

String

Error details.

policy_id

String

Policy ID.

Example response

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

Error Code

Table 8 Error codes

Error Code

Message

Solution

SVCSTG.PE.4001101

Invalid parameter.

Check whether the parameter meets requirements.

SVCSTG.PE.4031012

Failed to verify the project ID.

Check whether the parameter meets requirements.

SVCSTG.PE.5001201

Failed to insert or update data in the background.

Contact technical support.