# 开启数据库代理 - CreateGaussMySqlProxy
#### 功能介绍
开启数据库代理，只支持ELB模式。
#### 调用方法
请参见[如何调用API](https://support.huaweicloud.com/api-taurusdb/taurusdb_03_0000.html)。
#### 授权信息
账号具备所有API的调用权限，如果使用账号下的IAM用户调用当前API，该IAM用户需具备调用API所需的权限。
- 如果使用角色与策略授权，具体权限要求请参见[权限和授权项](https://support.huaweicloud.com/api-taurusdb/taurusdb_06_0000.html)。
- 如果使用身份策略授权，需具备如下身份策略权限。
  
  | 授权项                          | 访问级别  | 资源类型（\*为必须） | 条件键                                                                                                                                                                                                                                                                                                                                                                                                                                | 别名                                                                                                                                                                                                                           | 依赖的授权项 |
  |:---|:---|:---|:---|:---|:---|
  | gaussdbformysql:proxy:create | Write | instance \* | - g:EnterpriseProjectId   - g:ResourceTag/\<tag-key\>    | - gaussdb:instance:modifyProxy  - gaussdb:proxy:create   | -      |
     
  
 
#### URI
POST /v3/{project_id}/instances/{instance_id}/proxy
表1路径参数 
| 参数          | 是否必选 | 参数类型   | 描述                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              |
|:---|:---|:---|:---|
| project_id  | 是    | String | **参数解释**： 租户在某一Region下的project ID。 获取方法请参见[获取项目ID](https://support.huaweicloud.com/api-taurusdb/taurusdb_10_0004.html)。 **约束限制**： 不涉及。 **取值范围**： 只能由英文字母、数字组成，且长度为32个字符。 **默认取值**： 不涉及。 |
| instance_id | 是    | String | **参数解释**： 实例ID，此参数是实例的唯一标识。 **约束限制**： 不涉及。 **取值范围**： 只能由英文字母、数字组成，后缀为in07，长度为36个字符。 **默认取值**： 不涉及。                                                                                                                               |
   
#### 请求参数
表2请求Header参数 
| 参数           | 是否必选 | 参数类型   | 描述                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  |
|:---|:---|:---|:---|
| X-Auth-Token | 是    | String | **参数解释**： 用户Token。 通过调用IAM服务[获取用户Token](https://support.huaweicloud.com/api-iam/iam_30_0001.html)接口获取。 请求响应成功后在响应消息头中包含的"X-Subject-Token"的值即为Token值。 **约束限制**： 不涉及。 **取值范围**： 不涉及。 **默认取值**： 不涉及。                                                                                                                                                                                                                           |
| X-Language   | 否    | String | **参数解释**： 请求语言类型。 **约束限制**： 不涉及。 **取值范围**： - en-us：英文。   - zh-cn：中文。    **默认取值**： en-us。 |
   
表3请求Body参数 
| 参数                       | 是否必选 | 参数类型                                                                        | 描述                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          |
|:---|:---|:---|:---|
| flavor_ref               | 是    | String                                                                      | **参数解释** 数据库代理规格码。 **约束限制**： 不涉及。 **取值范围**： 获取方法请参见[查询数据库代理规格信息](https://support.huaweicloud.com/api-taurusdb/ShowGaussMySqlProxyFlavors.html)返回的spec_code字段。 **默认取值**： 不涉及。                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 |
| node_num                 | 是    | Integer                                                                     | **参数解释**： 数据库代理节点数。 **约束限制**： 不涉及。 **取值范围**： 2-16。 **默认取值**： 不涉及。                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            |
| proxy_name               | 否    | String                                                                      | **参数解释**： 数据库代理名称。 **约束限制**： 同一租户下，同类型的实例名可重名。 **取值范围**： 最小为4个字符，最大为64个字符且不超过64个字节（注意：一个中文字符占用3个字节），必须以字母或中文开头，区分大小写，可以包含字母、数字、中划线、下划线或中文，不能包含其他特殊字符。 **默认取值**： 不涉及。                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         |
| proxy_mode               | 否    | String                                                                      | **参数解释**： 数据库代理类型。 **约束限制**： 不涉及。 **取值范围**： - readwrite：读写。   - readonly：只读。    **默认取值**： readwrite。                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             |
| route_mode               | 否    | Integer                                                                     | **参数解释**： 数据库代理路由模式。 **约束限制**： 不涉及。 **取值范围**: - 0：表示权重负载模式。   - 1：表示负载均衡模式（数据库主节点不接受读请求）。   - 2：表示负载均衡模式（数据库主节点接受读请求）。   - 3：表示可用区就近负载模式（数据库主节点不接受读请求）。   - 4：表示可用区就近负载模式（数据库主节点接受读请求）。    **默认取值**： 0，权重负载模式。 |
| nodes_read_weight        | 否    | Array of [NodesWeight] objects | **参数解释**： 数据库节点的读权重设置。 **约束限制**： 在proxy_mode为readonly时，只能为只读节点选择权重。                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              |
| proxy_nodes_az_list      | 否    | Array of strings                                                            | **参数解释**： 数据库代理节点的可用区设置。 **约束限制**： 不传该字段，代理节点可用区将随机设置，优先与数据库节点可用区保持一致；传入该字段，代理节点将设置在指定可用区。                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       |
| subnet_id                | 否    | String                                                                      | **参数解释**： 子网的网络ID，获取方法如下： - 方法1：登录虚拟私有云服务的控制台界面，单击VPC下的子网，进入子网详情页面，查找网络ID。   - 方法2：通过虚拟私有云服务的API接口查询，具体操作可参考[查询子网列表](https://support.huaweicloud.com/api-vpc/vpc_subnet01_0003.html)。    **约束限制**： 当自定义数据库代理IP时，该字段必填。 **取值范围**： 不涉及。 **默认取值**： 不涉及。                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    |
| proxy_ip                 | 否    | String                                                                      | **参数解释**： 数据库代理IP，获取方法如下。 - 用户自定义代理IP时，由用户自定义传入。   - 用户不指定代理IP时，随机在指定子网下生成的IPV4地址。    **约束限制**： 指定子网下的可用IP，获取方法如下： 登录TaurusDB的控制台界面，单击实例名称，进入实例详情页面，在该页面单击数据库代理，新增代理，查看已使用IP地址，查找指定子网下未被使用的IP。 **取值范围**： 不涉及。 **默认取值**： 不涉及。                                                                                                                                                                                                                                                                                                                                                                                                                                                                   |
| new_node_auto_add_status | 否    | String                                                                      | **参数解释**: 是否开启新增节点自动加入该Proxy。 **约束限制**： 不涉及。 **取值范围**： - ON：开启。   - OFF：关闭。    **默认取值**： OFF。                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    |
| new_node_weight          | 否    | Integer                                                                     | **参数解释**： 新增节点的读权重。 **约束限制**： 在new_node_auto_add_status参数为ON时为必选字段。 **取值范围**： - 如果路由模式为0，新增节点自动加入为ON，取值为0\~1000。   - 如果路由模式不为0或新增节点自动加入为OFF，则可不输入读权重。    **默认取值**： 不涉及。                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          |
   
 表4NodesWeight 
| 参数     | 是否必选 | 参数类型    | 描述                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  |
|:---|:---|:---|:---|
| id     | 否    | String  | **参数解释**： 数据库节点ID。 **约束限制**： 节点ID需要属于对应的实例。 **取值范围**： 只能由英文字母、数字组成，前面为UUID，后缀为no07，长度为36个字符。 **默认取值**: 不涉及。                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          |
| weight | 否    | Integer | **参数解释**： 节点权重。 **约束限制**： 不涉及。 **取值范围**： - 路由模式为0：取值为0\~1000。   - 路由模式为1：数据库主节点取值为0，只读节点取值为0或1。   - 路由模式为2：数据库主节点取值为1，只读节点取值为0或1。    |
   
#### 响应参数
**状态码：201**
表5响应Body参数 
| 参数     | 参数类型   | 描述                                                                                                                                                        |
|:---|:---|:---|
| job_id | String | **参数解释**： 任务ID。 **取值范围**： 不涉及。 |
   
**状态码：400**
表6响应Body参数 
| 参数         | 参数类型   | 描述    |
|:---|:---|:---|
| error_code | String | 错误码。  |
| error_msg  | String | 错误消息。 |
   
**状态码：500**
表7响应Body参数 
| 参数         | 参数类型   | 描述    |
|:---|:---|:---|
| error_code | String | 错误码。  |
| error_msg  | String | 错误消息。 |
   
#### 请求示例
开启数据库代理，规格2U8GB，代理模式只读模式。
```
POST https://{endpoint}/v3/0483b6b16e954cb88930a360d2c4e663/instances/43e4feaab48f11e89039fa163ebaa7e4in07/proxy
{
  "flavor_ref" : "gaussdb.proxy.large.x86.2",
  "node_num" : 2,
  "proxy_name" : "gaussdb-proxy",
  "proxy_mode" : "readonly",
  "route_mode" : 0,
  "nodes_read_weight" : [ {
    "id" : "45021bf73a244312a3f2af95092feeecno07",
    "weight" : 50
  }, {
    "id" : "d78a65690cea4af5ad14585e110ff89bno07",
    "weight" : 400
  } ],
  "subnet_id" : "6991d82d-9166-4aff-a1fa-d2a8748b7084"
}
```
#### 响应示例
**状态码：201**
```
{
  "job_id" : "04efe8e2-9255-44ae-a98b-d87cae411890"
}
```
#### SDK代码示例
SDK代码示例如下。
- [Java]
  开启数据库代理，规格2U8GB，代理模式只读模式。
  ```
  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.gaussdb.v3.region.GaussDBRegion;
  import com.huaweicloud.sdk.gaussdb.v3.*;
  import com.huaweicloud.sdk.gaussdb.v3.model.*;
  import java.util.List;
  import java.util.ArrayList;
  public class CreateGaussMySqlProxySolution {
      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);
          GaussDBClient client = GaussDBClient.newBuilder()
                  .withCredential(auth)
                  .withRegion(GaussDBRegion.valueOf("<YOUR REGION>"))
                  .build();
          CreateGaussMySqlProxyRequest request = new CreateGaussMySqlProxyRequest();
          request.withInstanceId("{instance_id}");
          OpenMysqlProxyRequestBody body = new OpenMysqlProxyRequestBody();
          List<NodesWeight> listbodyNodesReadWeight = new ArrayList<>();
          listbodyNodesReadWeight.add(
              new NodesWeight()
                  .withId("45021bf73a244312a3f2af95092feeecno07")
                  .withWeight(50)
          );
          listbodyNodesReadWeight.add(
              new NodesWeight()
                  .withId("d78a65690cea4af5ad14585e110ff89bno07")
                  .withWeight(400)
          );
          body.withSubnetId("6991d82d-9166-4aff-a1fa-d2a8748b7084");
          body.withNodesReadWeight(listbodyNodesReadWeight);
          body.withRouteMode(0);
          body.withProxyMode(OpenMysqlProxyRequestBody.OpenMysqlProxyRequestBody.fromValue("readonly"));
          body.withProxyName("gaussdb-proxy");
          body.withNodeNum(2);
          body.withFlavorRef("gaussdb.proxy.large.x86.2");
          request.withBody(body);
          try {
              CreateGaussMySqlProxyResponse response = client.createGaussMySqlProxy(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]
  开启数据库代理，规格2U8GB，代理模式只读模式。
  ```
  # coding: utf-8
  import os
  from huaweicloudsdkcore.auth.credentials import BasicCredentials
  from huaweicloudsdkgaussdb.v3.region.gaussdb_region import GaussDBRegion
  from huaweicloudsdkcore.exceptions import exceptions
  from huaweicloudsdkgaussdb.v3 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 = GaussDBClient.new_builder() \
          .with_credentials(credentials) \
          .with_region(GaussDBRegion.value_of("<YOUR REGION>")) \
          .build()
      try:
          request = CreateGaussMySqlProxyRequest()
          request.instance_id = "{instance_id}"
          listNodesReadWeightbody = [
              NodesWeight(
                  id="45021bf73a244312a3f2af95092feeecno07",
                  weight=50
              ),
              NodesWeight(
                  id="d78a65690cea4af5ad14585e110ff89bno07",
                  weight=400
              )
          ]
          request.body = OpenMysqlProxyRequestBody(
              subnet_id="6991d82d-9166-4aff-a1fa-d2a8748b7084",
              nodes_read_weight=listNodesReadWeightbody,
              route_mode=0,
              proxy_mode="readonly",
              proxy_name="gaussdb-proxy",
              node_num=2,
              flavor_ref="gaussdb.proxy.large.x86.2"
          )
          response = client.create_gauss_my_sql_proxy(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]
  开启数据库代理，规格2U8GB，代理模式只读模式。
  ```
  package main
  import (
  "fmt"
  "github.com/huaweicloud/huaweicloud-sdk-go-v3/core/auth/basic"
      gaussdb "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/gaussdb/v3"
  "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/gaussdb/v3/model"
      region "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/gaussdb/v3/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 := gaussdb.GaussDBClientBuilder().
           WithRegion(region.ValueOf("<YOUR REGION>")).
           WithCredential(auth).
           SafeBuild()
      if err != nil {
          fmt.Println(err)
          return
      }
      client := gaussdb.NewGaussDBClient(hcClient)
      request := &model.CreateGaussMySqlProxyRequest{}
  request.InstanceId = "{instance_id}"
  idNodesReadWeight:= "45021bf73a244312a3f2af95092feeecno07"
  weightNodesReadWeight:= int32(50)
  idNodesReadWeight1:= "d78a65690cea4af5ad14585e110ff89bno07"
  weightNodesReadWeight1:= int32(400)
  var listNodesReadWeightbody = []model.NodesWeight{
          {
              Id: &idNodesReadWeight,
              Weight: &weightNodesReadWeight,
          },
          {
              Id: &idNodesReadWeight1,
              Weight: &weightNodesReadWeight1,
          },
      }
  subnetIdOpenMysqlProxyRequestBody:= "6991d82d-9166-4aff-a1fa-d2a8748b7084"
  routeModeOpenMysqlProxyRequestBody:= int32(0)
  proxyModeOpenMysqlProxyRequestBody:= model.GetOpenMysqlProxyRequestBodyProxyModeEnum().READONLY
  proxyNameOpenMysqlProxyRequestBody:= "gaussdb-proxy"
  request.Body = &model.OpenMysqlProxyRequestBody{
  SubnetId: &subnetIdOpenMysqlProxyRequestBody,
  NodesReadWeight: &listNodesReadWeightbody,
  RouteMode: &routeModeOpenMysqlProxyRequestBody,
  ProxyMode: &proxyModeOpenMysqlProxyRequestBody,
  ProxyName: &proxyNameOpenMysqlProxyRequestBody,
  NodeNum: int32(2),
  FlavorRef: "gaussdb.proxy.large.x86.2",
  }
  response, err := client.CreateGaussMySqlProxy(request)
  if err == nil {
          fmt.Printf("%+v\n", response)
      } else {
          fmt.Println(err)
      }
  }
  ```
- [更多]
  更多编程语言的SDK代码示例，请参见[API Explorer](https://console.huaweicloud.com/apiexplorer/#/openapi/GaussDB/sdk?api=CreateGaussMySqlProxy)的代码示例页签，可生成自动对应的SDK代码示例。
#### 状态码
| 状态码 | 描述            |
|:---|:---|
| 201 | Success.      |
| 400 | Client error. |
| 500 | Server error. |
   
#### 错误码
请参见[错误码](https://support.huaweicloud.com/api-taurusdb/ErrorCode.html)。
