# 查看弹性伸缩策略-ShowAutoScalingPolicy
#### 功能介绍
查看指定集群的所有的弹性伸缩策略信息，包括自动扩缩容规则、触发条件和当前状态等。
#### 接口约束
无
#### 调用方法
请参见[如何调用API](https://support.huaweicloud.com/api-mrs/zh-cn_topic_0013582897.html)。
#### 授权信息
账号具备所有API的调用权限，如果使用账号下的IAM用户调用当前API，该IAM用户需具备调用API所需的权限。
- 如果使用角色与策略授权，具体权限要求请参见[权限和授权项](https://support.huaweicloud.com/api-mrs/mrs_02_0221.html)。
- 如果使用身份策略授权，需具备如下身份策略权限。
  
  | 授权项                      | 访问级别 | 资源类型（\*为必须） | 条件键                                                                                                                                                                                                                                                                                                                                                                                                                                | 别名                 | 依赖的授权项 |
  |:---|:---|:---|:---|:---|:---|
  | mrs:cluster:listPolicies | List | cluster \*  | - g:ResourceTag/\<tag-key\>   - g:EnterpriseProjectId    | mrs:cluster:policy | -      |
     
  
 
#### URI
GET /v2/{project_id}/autoscaling-policy/{cluster_id}
表1路径参数 
| 参数         | 是否必选 | 参数类型   | 描述                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                |
|:---|:---|:---|:---|
| project_id | 是    | String | **参数解释：** 项目编号。获取方法，请参见[获取项目ID](https://support.huaweicloud.com/api-mrs/mrs_02_0011.html)。 **约束限制：** 不涉及 **取值范围：** 只能由英文字母和数字组成，且长度为\[1-64\]个字符。 **默认取值：** 不涉及                                                                     |
| cluster_id | 是    | String | **参数解释：** 集群ID。获取方法，请参见[获取集群ID](https://support.huaweicloud.com/api-mrs/mrs_02_9001.html#mrs_02_9001__zh-cn_topic_0264280065_section177891315153619)。 **约束限制：** 不涉及 **取值范围：** 只能由英文字母、数字以及"_"和"-"组成，且长度为\[1-64\]个字符。 **默认取值：** 不涉及 |
   
#### 请求参数
无
#### 响应参数
**状态码：200**
表2响应Body参数 
| 参数       | 参数类型                                                                                         | 描述                                                                                                                                                           |
|:---|:---|:---|
| \[数组元素\] | Array of [AutoScalingPolicyV2] objects | **参数解释：** 弹性伸缩策略列表。 **取值范围：** 不涉及 |
   
 表3AutoScalingPolicyV2 
| 参数                  | 参数类型                                                                                   | 描述                                                                                                                                                                                                                            |
|:---|:---|:---|
| node_group_name     | String                                                                                 | **参数解释：** 节点组名称。 **取值范围：** 只能由小写字母（a-z）、大写字母（A-Z）、数字（0-9）以及下划线（_）组成，除系统默认节点组外，自定义节点组名称长度为3\~14个字符。 |
| resource_pool_name  | String                                                                                 | **参数解释：** 资源池名称。 **取值范围：** 只能由英文字母、数字以及"_"和"-"组成，长度为1\~50个字符。                                      |
| auto_scaling_policy | [AutoScalingPolicyInfo] object | **参数解释：** 弹性伸缩规则。 **取值范围：** 不涉及                                                                    |
   
 表4AutoScalingPolicyInfo 
| 参数                  | 参数类型                                                                             | 描述                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              |
|:---|:---|:---|
| auto_scaling_enable | Boolean                                                                          | **参数解释：** 当前自动伸缩规则是否开启。 **取值范围：** - true：开启自动伸缩规则   - false：不开启自动伸缩规则    |
| min_capacity        | Integer                                                                          | **参数解释：** 指定该节点组的最小保留节点数。 **取值范围：** 0-500                                                                                                                                                                                                                                                                                                                                                                                                            |
| max_capacity        | Integer                                                                          | **参数解释：** 指定该节点组的最大节点数。 **取值范围：** 0-500                                                                                                                                                                                                                                                                                                                                                                                                              |
| resources_plans     | Array of [ResourcesPlan] objects | **参数解释：** 资源计划列表，若该参数为空表示不启用资源计划。 当启用弹性伸缩时，资源计划与自动伸缩规则需至少配置其中一种，不能超过5条。 **取值范围：** 不涉及                                                                                                                                                                                                                                                                                                                          |
| rules               | Array of [Rule] objects                   | **参数解释：** 自动伸缩的规则列表。 当启用弹性伸缩时，资源计划与自动伸缩规则需至少配置其中一种，不能超过5条。 **取值范围：** 不涉及                                                                                                                                                                                                                                                                                                                                         |
| tags                | Array of [Tag] objects                     | **参数解释：** 弹性伸缩标签列表，不能超过20条。 **取值范围：** 不涉及                                                                                                                                                                                                                                                                                                                                                                                                            |
   
 表5ResourcesPlan 
| 参数             | 参数类型             | 描述                                                                                                                                                                                                                                                                                                                                                                             |
|:---|:---|:---|
| period_type    | String           | **参数解释：** 资源计划的周期类型，当前只允许以下类型：daily **取值范围：** - daily：按天计费    |
| start_time     | String           | **参数解释：** 资源计划的起始时间，格式为"hour:minute"，表示时间在0:00-23:59之间。 **取值范围：** 0:00-23:59之间                                                                                                                                                                      |
| end_time       | String           | **参数解释：** 资源计划的结束时间，格式与"start_time"相同。 不早于start_time表示的时间，且与start_time间隔不小于30分钟。 **取值范围：** 0:00\~23:59之间                                                                                                     |
| min_capacity   | Integer          | **参数解释：** 资源计划内该节点组的最小保留节点数。 **取值范围：** 0-500                                                                                                                                                                                                        |
| max_capacity   | Integer          | **参数解释：** 资源计划内该节点组的最大保留节点数。 **取值范围：** 0-500                                                                                                                                                                                                        |
| effective_days | Array of strings | **参数解释：** 资源计划的生效日期，为空时代表每日，另外也可为以下返回值： MONDAY（周一）、TUESDAY（周二）、WEDNESDAY（周三）、THURSDAY（周四）、FRIDAY（周五）、SATURDAY（周六）、SUNDAY（周日） **取值范围：** 不涉及                                                                      |
   
 表6Rule 
| 参数                 | 参数类型                                                       | 描述                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        |
|:---|:---|:---|
| name               | String                                                     | **参数解释：** 弹性伸缩规则的名称。 **取值范围：** 只能由英文字母、数字以及"_"和"-"组成，且长度为\[1-64\]个字符。 在一个节点组范围内，不允许重名。                                                                                                                                                                                                                                                                                                                |
| description        | String                                                     | **参数解释：** 弹性伸缩规则的说明。 **取值范围：** 长度为\[0-1024\]个字符。                                                                                                                                                                                                                                                                                                                                                                                               |
| adjustment_type    | String                                                     | **参数解释：** 弹性伸缩规则的调整类型。 **取值范围：** - scale_out：扩容   - scale_in：缩容    |
| cool_down_minutes  | Integer                                                    | **参数解释：** 触发弹性伸缩规则后，该集群处于冷却状态（不再执行弹性伸缩操作）的时长，单位为分钟。 **取值范围：** 0-10080。10080为一周的分钟数。                                                                                                                                                                                                                                                                                                                                                            |
| scaling_adjustment | Integer                                                    | **参数解释：** 单次调整集群节点的个数。 **取值范围：** 1-100                                                                                                                                                                                                                                                                                                                                                                                                         |
| trigger            | [Trigger] object | **参数解释：** 描述该规则触发条件。 **取值范围：** 不涉及                                                                                                                                                                                                                                                                                                                                                                                                             |
   
 表7Trigger 
| 参数                  | 参数类型    | 描述                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 |
|:---|:---|:---|
| metric_name         | String  | **参数解释：** 指标名称。该触发条件会依据该名称对应指标的值来进行判断。详细指标名称内容请参见["弹性伸缩指标列表"](https://support.huaweicloud.com/bestpractice-mrs/mrs_05_0132.html#mrs_05_0132__zh-cn_topic_0000001168444324_table15133845184415)。 **取值范围：** 取值范围请参见["弹性伸缩指标列表"](https://support.huaweicloud.com/bestpractice-mrs/mrs_05_0132.html#mrs_05_0132__zh-cn_topic_0000001168444324_table15133845184415)。                                                                                                                                                                                                                                                                                                                                                                                                                                                                         |
| metric_value        | String  | **参数解释：** 指标阈值。触发该条件的指标阈值，只允许输入整数或者带两位小数的数。 **取值范围：** 只允许输入整数或者带两位小数的数。                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 |
| comparison_operator | String  | **参数解释：** 指标判断逻辑运算符。 **取值范围：** - LT：小于   - GT：大于   - LTOE：小于等于   - GTOE：大于等于    |
| evaluation_periods  | Integer | **参数解释：** 判断连续满足指标阈值的周期数(一个周期为5分钟)。 **取值范围：** 1-288                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     |
   
 表8Tag 
| 参数    | 参数类型   | 描述                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  |
|:---|:---|:---|
| key   | String | **参数解释：** 标签的键。 **取值范围：** - 标签的key值可以包含任意语种字母、数字、空格和_.:=+-@，但首尾不能含有空格，不能以_sys_开头。   - 同一资源的key值不能重复。   - 最大长度128个unicode字符，不能为空字符串。    |
| value | String | **参数解释：** 标签的值。 **取值范围：** - 标签的value值可以包含任意语种字母、数字、空格和_.:=+-@，但首尾不能含有空格，不能以_sys_开头。   - 最大长度255个unicode字符，可以为空字符串。                                                                                                                                                                                                           |
   
**状态码：400**
表9响应Body参数 
| 参数         | 参数类型   | 描述                                                                                                                                                              |
|:---|:---|:---|
| error_code | String | **参数解释：** 错误码。 **取值范围：** 400：操作执行失败  |
| error_msg  | String | **参数解释：** 错误描述。 **取值范围：** 400：操作执行失败 |
   
#### 请求示例
无
#### 响应示例
**状态码：200**
查看弹性伸缩策略成功
```
[ {
  "node_group_name" : "task_node_group",
  "resource_pool_name" : "default",
  "auto_scaling_policy" : {
    "auto_scaling_enable" : true,
    "min_capacity" : 0,
    "max_capacity" : 1,
    "resources_plans" : [ {
      "period_type" : "daily",
      "effective_days" : [ "SUNDAY" ],
      "start_time" : "12:00",
      "end_time" : "13:00",
      "min_capacity" : 2,
      "max_capacity" : 3
    } ],
    "rules" : [ {
      "name" : "default-expand-1",
      "description" : "",
      "adjustment_type" : "scale_out",
      "cool_down_minutes" : 5,
      "scaling_adjustment" : 1,
      "trigger" : {
        "metric_name" : "YARNAppRunning",
        "metric_value" : "100",
        "comparison_operator" : "GTOE",
        "evaluation_periods" : 1
      }
    } ]
  }
} ]
```
#### SDK代码示例
SDK代码示例如下。
- [Java]
  ```
  package com.huaweicloud.sdk.test;
  import com.huaweicloud.sdk.core.auth.ICredential;
  import com.huaweicloud.sdk.core.auth.BasicCredentials;
  import com.huaweicloud.sdk.core.exception.ConnectionException;
  import com.huaweicloud.sdk.core.exception.RequestTimeoutException;
  import com.huaweicloud.sdk.core.exception.ServiceResponseException;
  import com.huaweicloud.sdk.mrs.v2.region.MrsRegion;
  import com.huaweicloud.sdk.mrs.v2.*;
  import com.huaweicloud.sdk.mrs.v2.model.*;
  public class ShowAutoScalingPolicySolution {
      public static void main(String[] args) {
          // The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security.
          // In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment
          String ak = System.getenv("CLOUD_SDK_AK");
          String sk = System.getenv("CLOUD_SDK_SK");
          String projectId = "{project_id}";
          ICredential auth = new BasicCredentials()
                  .withProjectId(projectId)
                  .withAk(ak)
                  .withSk(sk);
          MrsClient client = MrsClient.newBuilder()
                  .withCredential(auth)
                  .withRegion(MrsRegion.valueOf("<YOUR REGION>"))
                  .build();
          ShowAutoScalingPolicyRequest request = new ShowAutoScalingPolicyRequest();
          request.withClusterId("{cluster_id}");
          try {
              ShowAutoScalingPolicyResponse response = client.showAutoScalingPolicy(request);
              System.out.println(response.toString());
          } catch (ConnectionException e) {
              e.printStackTrace();
          } catch (RequestTimeoutException e) {
              e.printStackTrace();
          } catch (ServiceResponseException e) {
              e.printStackTrace();
              System.out.println(e.getHttpStatusCode());
              System.out.println(e.getRequestId());
              System.out.println(e.getErrorCode());
              System.out.println(e.getErrorMsg());
          }
      }
  }
  ```
- [Python]
  ```
  # coding: utf-8
  import os
  from huaweicloudsdkcore.auth.credentials import BasicCredentials
  from huaweicloudsdkmrs.v2.region.mrs_region import MrsRegion
  from huaweicloudsdkcore.exceptions import exceptions
  from huaweicloudsdkmrs.v2 import *
  if __name__ == "__main__":
      # The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security.
      # In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment
      ak = os.environ["CLOUD_SDK_AK"]
      sk = os.environ["CLOUD_SDK_SK"]
      projectId = "{project_id}"
      credentials = BasicCredentials(ak, sk, projectId)
      client = MrsClient.new_builder() \
          .with_credentials(credentials) \
          .with_region(MrsRegion.value_of("<YOUR REGION>")) \
          .build()
      try:
          request = ShowAutoScalingPolicyRequest()
          request.cluster_id = "{cluster_id}"
          response = client.show_auto_scaling_policy(request)
          print(response)
      except exceptions.ClientRequestException as e:
          print(e.status_code)
          print(e.request_id)
          print(e.error_code)
          print(e.error_msg)
  ```
- [Go]
  ```
  package main
  import (
  "fmt"
  "github.com/huaweicloud/huaweicloud-sdk-go-v3/core/auth/basic"
      mrs "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/mrs/v2"
  "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/mrs/v2/model"
      region "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/mrs/v2/region"
  )
  func main() {
      // The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security.
      // In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment
      ak := os.Getenv("CLOUD_SDK_AK")
      sk := os.Getenv("CLOUD_SDK_SK")
      projectId := "{project_id}"
      auth, err := basic.NewCredentialsBuilder().
          WithAk(ak).
          WithSk(sk).
          WithProjectId(projectId).
          SafeBuild()
      if err != nil {
          fmt.Println(err)
          return
      }
      hcClient, err := mrs.MrsClientBuilder().
           WithRegion(region.ValueOf("<YOUR REGION>")).
           WithCredential(auth).
           SafeBuild()
      if err != nil {
          fmt.Println(err)
          return
      }
      client := mrs.NewMrsClient(hcClient)
      request := &model.ShowAutoScalingPolicyRequest{}
  request.ClusterId = "{cluster_id}"
  response, err := client.ShowAutoScalingPolicy(request)
  if err == nil {
          fmt.Printf("%+v\n", response)
      } else {
          fmt.Println(err)
      }
  }
  ```
- [更多]
  更多编程语言的SDK代码示例，请参见[API Explorer](https://console.huaweicloud.com/apiexplorer/#/openapi/MRS/sdk?api=ShowAutoScalingPolicy&version=v2)的代码示例页签，可生成自动对应的SDK代码示例。
#### 状态码
| 状态码 | 描述         |
|:---|:---|
| 200 | 查看弹性伸缩策略成功 |
| 400 | 查看弹性伸缩策略失败 |
   
#### 错误码
请参见[错误码](https://support.huaweicloud.com/api-mrs/ErrorCode.html)。
