# 开启公网访问 - CreateBindPublic
#### 功能介绍
当Elasticsearch/OpenSearch集群开启了"安全模式"和"HTTPS访问"时，可以选择是否配置"公网访问"。配置公网访问后，用户可以获得一个公网访问的IP地址，通过这个IP地址可以在公网访问该安全集群。同时，还支持配置访问控制，设置允许公网访问集群的IP地址或网段。
Elasticsearch/OpenSearch集群的公网访问是通过共享型负载均衡器实现外网访问，共享型负载均衡器资源与其他实例共享资源，如果业务追求更优的负载均衡体验，推荐使用独享型负载均衡器接入集群，配置方案请参见配置Elasticsearch集群独享型负载均衡。
![](https://support.huaweicloud.com/api-css/public_sys-resources/note_3.0-zh-cn.png)
开启公网访问，不配置访问控制白名单时，允许所有IP访问，建议合理配置访问控制白名单。
#### 调用方法
请参见[如何调用API](https://support.huaweicloud.com/api-css/css_03_0077.html)。
#### 授权信息
账号具备所有API的调用权限，如果使用账号下的IAM用户调用当前API，该IAM用户需具备调用API所需的权限。
- 如果使用角色与策略授权，具体权限要求请参见[权限和授权项](https://support.huaweicloud.com/api-css/css_03_0096.html)。
- 如果使用身份策略授权，需具备如下身份策略权限。
  
  | 授权项                            | 访问级别  | 资源类型（\*为必须） | 条件键                                                                                                                                                                                                                                                                                                                                                                                        | 别名 | 依赖的授权项 |
  |:---|:---|:---|:---|:---|:---|
  | css:publicIPAddress:associates | Write | cluster \*  | - g:EnterpriseProjectId   - g:ResourceTag/\<tag-key\>    | -  | -      |
     
  
 
#### URI
POST /v1.0/{project_id}/clusters/{cluster_id}/public/open
表1路径参数 
| 参数         | 是否必选 | 参数类型   | 描述                                                                                                                                                                                                                                                                                                                                                                                                                          |
|:---|:---|:---|:---|
| project_id | 是    | String | **参数解释**： 项目ID。获取方法请参见[获取项目ID和名称](https://support.huaweicloud.com/api-css/css_03_0071.html)。 **约束限制**： 不涉及 **取值范围**： 账户的项目ID，长度为32个字符，由小写字母和数字组成。 **默认取值**： 不涉及 |
| cluster_id | 是    | String | **参数解释**： 指定开启公网访问的集群ID。获取方法请参见[获取集群ID](https://support.huaweicloud.com/api-css/css_03_0101.html)。 **约束限制**： 不涉及 **取值范围**： UUID格式，36个字符。 **默认取值**： 不涉及          |
   
#### 请求参数
表2请求Body参数 
| 参数          | 是否必选 | 参数类型                                                                   | 描述                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            |
|:---|:---|:---|:---|
| eip         | 是    | [BindPublicReqEip] object | **参数解释**： 公网弹性IP信息。 **约束限制**： 不涉及 **取值范围**： 不涉及 **默认取值**： 不涉及                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     |
| white_list  | 否    | String                                                                 | **参数解释**： 公网访问控制白名单，需要添加白名单的网段或IP，以英文逗号隔开，正确示例：192.168.1.1,10.0.0.0/24。 **约束限制**： 不支持：0.0.0.0、x.x.x.x/0、172.16.0.0-172.16.255.255、非标准格式（如192.168.1）或重复项。 不添加访问白名单，则表示所有公网IP地址都可以访问集群。此时会有安全风险，不推荐。 **取值范围**： IP地址或CIDR格式的网段，多个值以英文逗号分隔，不可重复 **默认取值**： 不涉及                                                                                                                                                                                                                                                                                                                     |
| is_auto_pay | 否    | Integer                                                                | **参数解释**： 是否自动支付。下单订购后，是否自动从客户的华为云账户中支付，而不需要客户手动支付。 **约束限制**： 按需集群无需设置此参数，设置后不生效；仅包周期集群带宽变更产生费用时该参数生效。 **取值范围**： - 1：自动支付。会自动选择折扣和优惠券，从客户的华为云账户中自动扣款。自动支付失败后会生成"待支付"的订单（该订单应付金额是优惠后的金额），等待客户手动完成支付，支付时可以修改系统自动选择的折扣和优惠券。   - 0：手动支付。需要客户手动支付订单，支付时，可以选择折扣和优惠券。    **默认取值**： 0 |
   
 表3BindPublicReqEip 
| 参数         | 是否必选 | 参数类型                                                                                     | 描述                                                                                                                                          |
|:---|:---|:---|:---|
| band_width | 是    | [BindPublicReqEipBandWidth] object | **参数解释**： 公网带宽信息。 **约束限制**： 不涉及 |
   
 表4BindPublicReqEipBandWidth 
| 参数   | 是否必选 | 参数类型    | 描述                                                                                                                                                                                                                                                                                                                                  |
|:---|:---|:---|:---|
| size | 是    | Integer | **参数解释**： 公网带宽大小，单位：Mbit/s。 **约束限制**： 不涉及 **取值范围**： 1-200 **默认取值**： 不涉及 |
   
#### 响应参数
**状态码：200**
表5响应Body参数 
| 参数     | 参数类型   | 描述                                                                                                                                                              |
|:---|:---|:---|
| action | String | **参数解释**： 开启公网访问操作行为。 **取值范围**： 固定为bindZone，表示绑定成功。 |
   
#### 请求示例
开启公网访问。
```
POST https://{Endpoint}/v1.0/{project_id}/clusters/4f3deec3-efa8-4598-bf91-560aad1377a3/public/open
{
  "eip" : {
    "band_width" : {
      "size" : 5
    }
  },
  "white_list" : "127.0.0.1"
}
```
#### 响应示例
**状态码：200**
```
{
  "action" : "bindZone"
}
```
#### 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.css.v1.region.CssRegion;
  import com.huaweicloud.sdk.css.v1.*;
  import com.huaweicloud.sdk.css.v1.model.*;
  public class CreateBindPublicSolution {
      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);
          CssClient client = CssClient.newBuilder()
                  .withCredential(auth)
                  .withRegion(CssRegion.valueOf("<YOUR REGION>"))
                  .build();
          CreateBindPublicRequest request = new CreateBindPublicRequest();
          request.withClusterId("{cluster_id}");
          BindPublicReq body = new BindPublicReq();
          BindPublicReqEipBandWidth bandWidthEip = new BindPublicReqEipBandWidth();
          bandWidthEip.withSize(5);
          BindPublicReqEip eipbody = new BindPublicReqEip();
          eipbody.withBandWidth(bandWidthEip);
          body.withWhiteList("127.0.0.1");
          body.withEip(eipbody);
          request.withBody(body);
          try {
              CreateBindPublicResponse response = client.createBindPublic(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 huaweicloudsdkcss.v1.region.css_region import CssRegion
  from huaweicloudsdkcore.exceptions import exceptions
  from huaweicloudsdkcss.v1 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 = CssClient.new_builder() \
          .with_credentials(credentials) \
          .with_region(CssRegion.value_of("<YOUR REGION>")) \
          .build()
      try:
          request = CreateBindPublicRequest()
          request.cluster_id = "{cluster_id}"
          bandWidthEip = BindPublicReqEipBandWidth(
              size=5
          )
          eipbody = BindPublicReqEip(
              band_width=bandWidthEip
          )
          request.body = BindPublicReq(
              white_list="127.0.0.1",
              eip=eipbody
          )
          response = client.create_bind_public(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"
      css "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/css/v1"
  "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/css/v1/model"
      region "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/css/v1/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 := css.CssClientBuilder().
           WithRegion(region.ValueOf("<YOUR REGION>")).
           WithCredential(auth).
           SafeBuild()
      if err != nil {
          fmt.Println(err)
          return
      }
      client := css.NewCssClient(hcClient)
      request := &model.CreateBindPublicRequest{}
  request.ClusterId = "{cluster_id}"
  bandWidthEip := &model.BindPublicReqEipBandWidth{
  Size: int32(5),
  }
  eipbody := &model.BindPublicReqEip{
  BandWidth: bandWidthEip,
  }
  whiteListBindPublicReq:= "127.0.0.1"
  request.Body = &model.BindPublicReq{
  WhiteList: &whiteListBindPublicReq,
  Eip: eipbody,
  }
  response, err := client.CreateBindPublic(request)
  if err == nil {
          fmt.Printf("%+v\n", response)
      } else {
          fmt.Println(err)
      }
  }
  ```
- [更多]
  更多编程语言的SDK代码示例，请参见[API Explorer](https://console.huaweicloud.com/apiexplorer/#/openapi/CSS/sdk?api=CreateBindPublic&version=v1)的代码示例页签，可生成自动对应的SDK代码示例。
#### 状态码
| 状态码 | 描述                                                                                             |
|:---|:---|
| 200 | 请求成功。                                                                                          |
| 400 | 非法请求。 建议直接修改该请求，不要重试该请求。                                   |
| 409 | 服务器在完成请求时发生冲突。 返回该状态码，表明客户端尝试创建的资源已经存在，或者由于冲突请求的更新操作不能被完成。 |
| 412 | 未满足前提条件，服务器未满足请求者在请求中设置的其中一个前提条件。                                                              |
   
#### 错误码
请参见[错误码](https://support.huaweicloud.com/api-css/css_03_0076.html)。
