更新时间:2025-02-25 GMT+08:00

批量创建负载均衡器

功能介绍

批量创建独享型或者共享型负载均衡器。

  • 若要创建内网IPv4负载均衡器,则需要传入vip_subnet_cidr_id。

  • 若要创建公网IPv4负载均衡器,则需要传入publicip,以及传入vpc_id和vip_subnet_cidr_id这两个参数中的一个。

  • 若要绑定有已有公网IPv4地址,则需要传入publicip_ids,以及传入vpc_id和vip_subnet_cidr_id这两个参数中的一个。

  • 若要创建内网双栈负载均衡器,则需要传入ipv6_vip_virsubnet_id。

  • 若要创建公网双栈负载均衡器,则需要传入ipv6_vip_virsubnet_id和ipv6_bandwidth。

  • 若要创建网络型负载均衡器,则需要传入l4_flavor_id(网络型规格ID);若要创建应用型负载均衡器,则需要传入l7_flavor_id(应用型规格ID);若要创建网络型+应用型负载均衡器,则需要传入l4_flavor_id和l7_flavor_id。

  • 如果批量创建的负载均衡器数量大于1,则不支持绑定已有的公网IP,且不支持指定ipv4和ipv6地址。即number大于1时,不支持传入publicip_ids,vip_address和ipv6_vip_address字段。

  • 按需计费分为固定规格计费和弹性规格计费,根据创建时所选规格的类型决定计费方式。具体规格说明见创建LB请求参数l4_flavor_id和l7_flavor_id。

  • 异步接口,返回体中包含需要批量创建的负载均衡的ID列表和批量创建负载均衡器的job ID,可以通过job ID查询当前批量创建负载均衡器的进度。

  • 批量创建独享型和共享型实例时,请求体传参规则有所不同,具体见请求体说明中各个参数的解释。

接口约束

  • vpc_id 或 vip_subnet_cidr_id 或 ipv6_vip_virsubnet_id 不能同时为空。

  • number大于1时,不支持传入publicip_ids,vip_address和ipv6_vip_address字段。

  • ip_target_enable参数表示是否启用跨VPC后端转发能力,

    启用跨VPC后端转发的负载均衡器关联的member和VIP可以在不同VPC下,

    未启用跨VPC后端转发的负载均衡器关联的member和VIP必须在同一个VPC下。

  • admin_state_up参数的值必须是true。

  • provider参数的值必须是vlb。

  • elb_virsubnet_ids参数对应的网络类型支持双栈或者只支持ipv4。

    网络类型为只支持ipv4时,ipv6_vip_virsubnet_id参数需要为空。

  • 只能通过ELB Console控制台来解绑EIP,

    在ELB列表查询页面选择操作列中的更多按钮->解绑EIP。

  • 创建LB时不能同时设置publicip_ids和publicip,

    只能选择绑定已有公网IP或者选择新建公网IP,或者都不设置这两个字段。

  • 若要绑定已有带宽,则需要设置共享带宽ID,不支持绑定独享带宽。

    若需要新建独享带宽,则带宽信息中charge_mode、share_type和size字段是必须的。

  • 不支持绑定已有未使用的内网IPv4、内网IPv6或公网IPv6地址。

调用方法

请参见如何调用API

URI

POST /v3/{project_id}/elb/loadbalancers/batch-create

表1 路径参数

参数

是否必选

参数类型

描述

project_id

String

参数解释:负载均衡器的项目ID。

请求参数

表2 请求Header参数

参数

是否必选

参数类型

描述

X-Auth-Token

String

参数解释:IAM鉴权Token。

表3 请求Body参数

参数

是否必选

参数类型

描述

loadbalancer

BatchCreateLoadBalancerOption object

负载均衡器对象。

表4 BatchCreateLoadBalancerOption

参数

是否必选

参数类型

描述

id

String

负载均衡器ID(UUID)。不支持该字段,请勿使用。

ids

Array of strings

批创负载均衡器ID的列表(UUID)。不支持该字段,请勿使用。

project_id

String

负载均衡器所在的项目ID。

number

Integer

批量创建负载均衡器的数量

name

String

负载均衡器的名称。

使用说明:

  • 当批创负载均衡器数量为1时,负载均衡器的名字即为该传入的值。

  • 当批创负载均衡器数量大于1时,负载均衡器的名字在传入的name后加上4位后缀。

例如:批量创建3个负载均衡器,传入name为"elb-test",则三个负载均衡器名字分别为:

"elb-test-0001","elb-test-0002","elb-test-0003"

description

String

负载均衡器的描述。

vip_address

String

负载均衡器的IPv4虚拟IP。该地址必须包含在所在子网的IPv4网段内,且未被占用。

使用说明:

  • 传入vip_address时必须传入vip_subnet_cidr_id。

  • 不传入vip_address,但传入vip_subnet_cidr_id,则自动分配IPv4虚拟IP。

  • 不传入vip_address,且不传vip_subnet_cidr_id,则不分配虚拟IP,vip_address=null。

  • 当批量创建的number数量大于1时不支持传入该字段,返回400。

vip_subnet_cidr_id

String

负载均衡器所在子网的IPv4子网ID。若需要创建带IPv4虚拟IP的LB,该字段必须传入。

可以通过GET

https://{VPC_Endpoint}/v1/{project_id}/subnets

响应参数中的neutron_subnet_id得到。

使用说明:

  • vpc_id, vip_subnet_cidr_id, ipv6_vip_virsubnet_id不能同时为空,且需要在同一个vpc下。

  • 若同时传入vpc_id和vip_subnet_cidr_id,

    则vip_subnet_cidr_id对应的子网必须属于vpc_id对应的VPC。

ipv6_vip_virsubnet_id

String

双栈类型负载均衡器所在子网的IPv6网络ID。可以通过GET

https://{VPC_Endpoint}/v1/{project_id}/subnets 响应参数中的neutron_network_id得到。

使用说明:

  • vpc_id,vip_subnet_cidr_id,ipv6_vip_virsubnet_id不能同时为空,且需要在同一个vpc下。

  • 需要对应的子网开启IPv6。

  • 批量创建共享型实例时,该字段不允许指定。

provider

String

负载均衡器的生产者名称。固定为vlb。批量创建共享型实例时,该字段不允许指定。

l4_flavor_id

String

网络型规格ID。

使用说明:

  • 可以通过GET https://{ELB_Endpoint}/v3/{project_id}/elb/flavors?type=L4 响应参数中的id得到。

  • 当l4_flavor_id和l7_flavor_id都不传的时,会使用默认flavor

    (默认flavor根据不同局点有所不同,具体以实际值为准)。

  • 当传入的规格类型为L4,表示该实例为固定规格实例,按规格计费。

  • 当传入的规格类型为L4_elastic_max,表示该实例为弹性实例,按LCU计费。

  • 批量创建共享型实例时,该字段不允许指定。

l7_flavor_id

String

应用型规格ID。

使用说明:

  • 可以通过GET https://{ELB_Endpoint}/v3/{project_id}/elb/flavors?type=L7 响应参数中的id得到。

  • 当l4_flavor_id和l7_flavor_id都不传的时,会使用默认flavor

    (默认flavor根据不同局点有所不同,具体以实际值为准)。

  • 当传入的规格类型为L7,表示该实例为固定规格实例,按规格计费。

  • 当传入的规格类型为L7_elastic_max,表示该实例为弹性实例,按LCU计费。

  • 批量创建共享型实例时,该字段不允许指定。

guaranteed

Boolean

批量创建的是否是独享型负载均衡器。

取值:

  • true:独享型。

  • false:共享型。

默认:true。

vpc_id

String

负载均衡器所在的VPC ID。可以通过GET https://{VPC_Endpoint}/v1/{project_id}/vpcs

响应参数中的id得到。

使用说明:

  • vpc_id,vip_subnet_cidr_id,ipv6_vip_virsubnet_id不能同时为空,且需要在同一个vpc下。

  • 批量创建共享型实例时,该字段不允许指定。

availability_zone_list

Array of strings

可用区列表。可通过GET

https://{ELB_Endpoint}/v3/{project_id}/elb/availability-zones

接口来查询可用区集合列表。创建负载均衡器时,从查询结果选择某一个可用区集合,并从中选择一个或多个可用区。

使用说明:

  • 批量创建共享型实例时,该字段不允许指定。

enterprise_project_id

String

负载均衡器所属的企业项目ID。创建时传入""、"0"或不存在的企业项目ID,或者不传则资源属于default企业项目,默认返回"0"。

tags

Array of Tag objects

负载均衡的标签列表。示例:"tags":[{"key":"my_tag","value":"my_tag_value"}]

admin_state_up

Boolean

参数解释:负载均衡器的启用状态。

取值范围:

  • true :启用。

  • false:停用。

默认取值:true。

ipv6_bandwidth

BandwidthRef object

批量创建共享型实例的场景,该字段不允许指定。

bandwidth

BandwidthRef object

批量创建独享型实例的场景,该字段不允许指定。

publicip_ids

Array of strings

负载均衡器绑定的公网IP ID。只支持绑定数组中的第一个EIP,其他将被忽略。

当批量创建的number数量大于1时不支持传入该字段,返回400。

publicip

CreateLoadBalancerPublicIpOption object

参数解释:创建ELB时,新建公网IP请求参数

elb_virsubnet_ids

Array of strings

下联面子网的网络ID列表。可以通过GET https://{VPC_Endpoint}/v1/{project_id}/subnets

响应参数中的neutron_network_id得到。

若不指定该字段,则按如下规则选择下联面网络:

  1. 如果ELB实例开启ipv6,则选择ipv6_vip_virsubnet_id子网对应的网络ID。

  2. 如果ELB实例没有开启ipv6,开启ipv4,则选择vip_subnet_cidr_id子网对应的网络ID。

  3. 如果ELB实例没有选择私网,只开启公网,则会在当前负载均衡器所在的VPC中任意选一个子网,优选可用IP多的网络。

  4. 批量创建共享型实例的场景,该字段不允许指定。

若指定多个下联面子网,则按顺序优先使用第一个子网来为负载均衡器下联面端口分配ip地址。

下联面子网必须属于该LB所在的VPC。

ip_target_enable

Boolean

参数解释:是否启用跨VPC后端转发。

开启跨VPC后端转发后,后端服务器组不仅支持添加云上VPC内的服务器,还支持添加其他VPC、其他公有云、云下数据中心的服务器。

约束限制:

  • 开启后不能关闭。

  • 使用共享VPC的实例使用此特性时,需确保共享资源所有者已开通VPC对等连接,否则通信异常。

  • 仅独享型负载均衡器支持该特性。

取值范围:

  • true:开启。

  • false:不开启。

deletion_protection_enable

Boolean

是否开启删除保护。

批量创建共享型实例的场景,该字段不允许指定。

取值:false不开启,true开启。默认false不开启。

说明:

退场时需要先关闭所有资源的删除保护开关。

autoscaling

CreateLoadbalancerAutoscalingOption object

参数解释:弹性扩缩容配置信息。负载均衡器开启弹性扩缩容后,可根据负载情况自动调整负载均衡器的规格。

约束限制:

  • 仅当项目白名单放开后该字段才有效

  • 开启弹性扩缩容后,l4_flavor_id和l7_flavor_id表示该LB实例弹性规格的上限。

说明:

该字段已经废弃,但仍然保留兼容性支持,建议不要使用该字段。如果传入该字段,创建的弹性实例将会有保底规格并产生对应保底规格的费用。

waf_failure_action

String

WAF故障时的流量处理策略。discard:丢弃,forward: 转发到后端(默认)

使用说明:只有绑定了waf的LB实例,该字段才会生效。

protection_status

String

修改保护状态, 取值:

  • nonProtection: 不保护,默认值为nonProtection

  • consoleProtection: 控制台修改保护

protection_reason

String

参数解释:设置保护的原因。作为protection_status的转态设置的原因。

约束限制:仅当protection_status为consoleProtection时有效。

取值范围:除<和>外通用Unicode字符集字符,最大255个字符。

ipv6_vip_address

String

双栈类型负载均衡器的IPv6地址。

当批量创建的number数量大于1时不支持传入该字段,返回400。

批量创建共享型实例的场景,该字段不允许指定。

表5 Tag

参数

是否必选

参数类型

描述

key

String

参数解释:标签键

value

String

参数解释:标签值

表6 BandwidthRef

参数

是否必选

参数类型

描述

id

String

参数解释:共享带宽的ID。

表7 CreateLoadBalancerPublicIpOption

参数

是否必选

参数类型

描述

ip_version

Integer

参数解释:IP版本。

约束限制:取值只有4和6。4:IPv4, 6: IPv6。

取值范围:

  • 4:表示IPv4。

  • 6:表示IPv6。

默认取值:4

network_type

String

参数解释:弹性公网IP的网络类型,默认5_bgp,更多请参考弹性公网ip创建。

取值范围:5_bgp。

默认取值:5_bgp。

billing_info

String

参数解释:计费订单信息。

约束限制:

  • 空:按需计费。

不支持该字段,请勿使用。

description

String

参数解释:弹性公网IP的描述信息。

bandwidth

CreateLoadBalancerBandwidthOption object

参数解释:带宽信息

表8 CreateLoadBalancerBandwidthOption

参数

是否必选

参数类型

描述

name

String

参数解释:带宽名称。

约束限制:

  • 如果share_type是PER,该字段是必选。

  • 如果bandwidth对象的id有值,该字段被忽略。

取值范围:1-64个字符,支持数字、字母、中文、_(下划线)、-(中划线)、.(点)

size

Integer

参数解释:带宽大小。

约束限制:当id字段为null时,size是必须的。

注意,调整带宽时的最小单位会根据带宽范围不同存在差异。

  • 小于等于300Mbit/s: 默认最小单位为1Mbit/s。

  • 300Mbit/s~1000Mbit/s: 默认最小单位为50Mbit/s。

  • 大于1000Mbit/s: 默认最小单位为500Mbit/s。

取值范围:默认1Mbit/s~2000Mbit/s(具体范围以各区域配置为准,请参见控制台对应页面显示)。

charge_mode

String

参数解释:计费模式。bandwidth 按带宽计费;traffic 按流量计费。

约束限制:当id字段为null时,charge_mode是必须的。

取值范围:

  • bandwidth:按带宽计费。

  • traffic: 按流量计费。

share_type

String

参数解释:带宽类型。

约束限制:

  • 当id字段为null时,share_type是必须的。当id不为null时,该字段被忽略。

  • 该字段为WHOLE时,必须指定带宽ID。

  • IPv6的EIP不支持WHOLE类型带宽。

取值范围:

  • PER:独享带宽。

  • WHOLE:共享带宽。

billing_info

String

参数解释:资源计费信息。

约束限制:

不支持该字段,请勿使用。

id

String

参数解释:共享带宽ID。使用已存在的共享带宽。

约束限制:必须是已存在共享带宽ID。在预付费的情况下,不填该字段。该字段取空字符串时,会被忽略。

表9 CreateLoadbalancerAutoscalingOption

参数

是否必选

参数类型

描述

enable

Boolean

参数解释:负载均衡器实例弹性扩缩容开关。

取值范围:

  • true:开启。

  • false:关闭。

min_l7_flavor_id

String

参数解释:实例弹性扩缩容的最小七层规格ID。

约束限制: 该字段已经废弃,建议不要使用该字段。

响应参数

状态码:202

表10 响应Body参数

参数

参数类型

描述

loadbalancer_ids

Array of strings

批创负载均衡器ID(UUID)的列表。

job_id

String

批量创建负载均衡器的job ID。

request_id

String

请求ID。

注:自动生成 。

请求示例

  • 示例1 : 批量创建3个独享型按需私网IPv4负载均衡器

    POST https://{ELB_Endpoint}/v3/060576798a80d5762fafc01a9b5eedc7/elb/loadbalancers/batch-create
    
    {
      "loadbalancer" : {
        "name" : "loadbalancer",
        "description" : "simple batch create lb",
        "vip_subnet_cidr_id" : "1992ec06-f364-4ae3-b936-6a8cc24633b7",
        "admin_state_up" : true,
        "availability_zone_list" : [ "AZ1" ],
        "number" : 3
      }
    }
  • 示例1 : 批量创建3个共享型私网IPv4负载均衡器

    POST https://{ELB_Endpoint}/v3/060576798a80d5762fafc01a9b5eedc7/elb/loadbalancers/batch-create
    
    {
      "loadbalancer" : {
        "name" : "loadbalancer",
        "description" : "simple batch create lb",
        "availability_zone_list" : [ "AZ1" ],
        "vip_subnet_cidr_id" : "1992ec06-f364-4ae3-b936-6a8cc24633b7",
        "admin_state_up" : true,
        "guaranteed" : false,
        "number" : 3
      }
    }

响应示例

状态码:202

批量创建负载均衡器正常返回

{
  "job_id" : "060576798a80d5762fafc01a9b5eedc7",
  "loadbalancer_ids" : [ "de7946ba-3b77-4119-8338-acc25eb05611", "ba27f70b-b52f-4a77-9220-fd15726e50bd" ],
  "request_id" : "bf29597181cb81b30d19f1a0115a157d"
}

SDK代码示例

SDK代码示例如下。

  • 示例1 : 批量创建3个独享型按需私网IPv4负载均衡器

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    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.elb.v3.region.ElbRegion;
    import com.huaweicloud.sdk.elb.v3.*;
    import com.huaweicloud.sdk.elb.v3.model.*;
    
    import java.util.List;
    import java.util.ArrayList;
    
    public class BatchCreateLoadBalancersSolution {
    
        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);
    
            ElbClient client = ElbClient.newBuilder()
                    .withCredential(auth)
                    .withRegion(ElbRegion.valueOf("<YOUR REGION>"))
                    .build();
            BatchCreateLoadBalancersRequest request = new BatchCreateLoadBalancersRequest();
            BatchCreateLoadBalancersRequestBody body = new BatchCreateLoadBalancersRequestBody();
            List<String> listLoadbalancerAvailabilityZoneList = new ArrayList<>();
            listLoadbalancerAvailabilityZoneList.add("AZ1");
            BatchCreateLoadBalancerOption loadbalancerbody = new BatchCreateLoadBalancerOption();
            loadbalancerbody.withNumber(3)
                .withName("loadbalancer")
                .withDescription("simple batch create lb")
                .withVipSubnetCidrId("1992ec06-f364-4ae3-b936-6a8cc24633b7")
                .withAvailabilityZoneList(listLoadbalancerAvailabilityZoneList)
                .withAdminStateUp(true);
            body.withLoadbalancer(loadbalancerbody);
            request.withBody(body);
            try {
                BatchCreateLoadBalancersResponse response = client.batchCreateLoadBalancers(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());
            }
        }
    }
    
  • 示例1 : 批量创建3个共享型私网IPv4负载均衡器

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    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.elb.v3.region.ElbRegion;
    import com.huaweicloud.sdk.elb.v3.*;
    import com.huaweicloud.sdk.elb.v3.model.*;
    
    import java.util.List;
    import java.util.ArrayList;
    
    public class BatchCreateLoadBalancersSolution {
    
        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);
    
            ElbClient client = ElbClient.newBuilder()
                    .withCredential(auth)
                    .withRegion(ElbRegion.valueOf("<YOUR REGION>"))
                    .build();
            BatchCreateLoadBalancersRequest request = new BatchCreateLoadBalancersRequest();
            BatchCreateLoadBalancersRequestBody body = new BatchCreateLoadBalancersRequestBody();
            List<String> listLoadbalancerAvailabilityZoneList = new ArrayList<>();
            listLoadbalancerAvailabilityZoneList.add("AZ1");
            BatchCreateLoadBalancerOption loadbalancerbody = new BatchCreateLoadBalancerOption();
            loadbalancerbody.withNumber(3)
                .withName("loadbalancer")
                .withDescription("simple batch create lb")
                .withVipSubnetCidrId("1992ec06-f364-4ae3-b936-6a8cc24633b7")
                .withGuaranteed(false)
                .withAvailabilityZoneList(listLoadbalancerAvailabilityZoneList)
                .withAdminStateUp(true);
            body.withLoadbalancer(loadbalancerbody);
            request.withBody(body);
            try {
                BatchCreateLoadBalancersResponse response = client.batchCreateLoadBalancers(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());
            }
        }
    }
    
  • 示例1 : 批量创建3个独享型按需私网IPv4负载均衡器

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    # coding: utf-8
    
    import os
    from huaweicloudsdkcore.auth.credentials import BasicCredentials
    from huaweicloudsdkelb.v3.region.elb_region import ElbRegion
    from huaweicloudsdkcore.exceptions import exceptions
    from huaweicloudsdkelb.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 = ElbClient.new_builder() \
            .with_credentials(credentials) \
            .with_region(ElbRegion.value_of("<YOUR REGION>")) \
            .build()
    
        try:
            request = BatchCreateLoadBalancersRequest()
            listAvailabilityZoneListLoadbalancer = [
                "AZ1"
            ]
            loadbalancerbody = BatchCreateLoadBalancerOption(
                number=3,
                name="loadbalancer",
                description="simple batch create lb",
                vip_subnet_cidr_id="1992ec06-f364-4ae3-b936-6a8cc24633b7",
                availability_zone_list=listAvailabilityZoneListLoadbalancer,
                admin_state_up=True
            )
            request.body = BatchCreateLoadBalancersRequestBody(
                loadbalancer=loadbalancerbody
            )
            response = client.batch_create_load_balancers(request)
            print(response)
        except exceptions.ClientRequestException as e:
            print(e.status_code)
            print(e.request_id)
            print(e.error_code)
            print(e.error_msg)
    
  • 示例1 : 批量创建3个共享型私网IPv4负载均衡器

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    # coding: utf-8
    
    import os
    from huaweicloudsdkcore.auth.credentials import BasicCredentials
    from huaweicloudsdkelb.v3.region.elb_region import ElbRegion
    from huaweicloudsdkcore.exceptions import exceptions
    from huaweicloudsdkelb.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 = ElbClient.new_builder() \
            .with_credentials(credentials) \
            .with_region(ElbRegion.value_of("<YOUR REGION>")) \
            .build()
    
        try:
            request = BatchCreateLoadBalancersRequest()
            listAvailabilityZoneListLoadbalancer = [
                "AZ1"
            ]
            loadbalancerbody = BatchCreateLoadBalancerOption(
                number=3,
                name="loadbalancer",
                description="simple batch create lb",
                vip_subnet_cidr_id="1992ec06-f364-4ae3-b936-6a8cc24633b7",
                guaranteed=False,
                availability_zone_list=listAvailabilityZoneListLoadbalancer,
                admin_state_up=True
            )
            request.body = BatchCreateLoadBalancersRequestBody(
                loadbalancer=loadbalancerbody
            )
            response = client.batch_create_load_balancers(request)
            print(response)
        except exceptions.ClientRequestException as e:
            print(e.status_code)
            print(e.request_id)
            print(e.error_code)
            print(e.error_msg)
    
  • 示例1 : 批量创建3个独享型按需私网IPv4负载均衡器

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    package main
    
    import (
    	"fmt"
    	"github.com/huaweicloud/huaweicloud-sdk-go-v3/core/auth/basic"
        elb "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/elb/v3"
    	"github.com/huaweicloud/huaweicloud-sdk-go-v3/services/elb/v3/model"
        region "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/elb/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 := basic.NewCredentialsBuilder().
            WithAk(ak).
            WithSk(sk).
            WithProjectId(projectId).
            Build()
    
        client := elb.NewElbClient(
            elb.ElbClientBuilder().
                WithRegion(region.ValueOf("<YOUR REGION>")).
                WithCredential(auth).
                Build())
    
        request := &model.BatchCreateLoadBalancersRequest{}
    	var listAvailabilityZoneListLoadbalancer = []string{
            "AZ1",
        }
    	numberLoadbalancer:= int32(3)
    	nameLoadbalancer:= "loadbalancer"
    	descriptionLoadbalancer:= "simple batch create lb"
    	vipSubnetCidrIdLoadbalancer:= "1992ec06-f364-4ae3-b936-6a8cc24633b7"
    	adminStateUpLoadbalancer:= true
    	loadbalancerbody := &model.BatchCreateLoadBalancerOption{
    		Number: &numberLoadbalancer,
    		Name: &nameLoadbalancer,
    		Description: &descriptionLoadbalancer,
    		VipSubnetCidrId: &vipSubnetCidrIdLoadbalancer,
    		AvailabilityZoneList: listAvailabilityZoneListLoadbalancer,
    		AdminStateUp: &adminStateUpLoadbalancer,
    	}
    	request.Body = &model.BatchCreateLoadBalancersRequestBody{
    		Loadbalancer: loadbalancerbody,
    	}
    	response, err := client.BatchCreateLoadBalancers(request)
    	if err == nil {
            fmt.Printf("%+v\n", response)
        } else {
            fmt.Println(err)
        }
    }
    
  • 示例1 : 批量创建3个共享型私网IPv4负载均衡器

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    package main
    
    import (
    	"fmt"
    	"github.com/huaweicloud/huaweicloud-sdk-go-v3/core/auth/basic"
        elb "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/elb/v3"
    	"github.com/huaweicloud/huaweicloud-sdk-go-v3/services/elb/v3/model"
        region "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/elb/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 := basic.NewCredentialsBuilder().
            WithAk(ak).
            WithSk(sk).
            WithProjectId(projectId).
            Build()
    
        client := elb.NewElbClient(
            elb.ElbClientBuilder().
                WithRegion(region.ValueOf("<YOUR REGION>")).
                WithCredential(auth).
                Build())
    
        request := &model.BatchCreateLoadBalancersRequest{}
    	var listAvailabilityZoneListLoadbalancer = []string{
            "AZ1",
        }
    	numberLoadbalancer:= int32(3)
    	nameLoadbalancer:= "loadbalancer"
    	descriptionLoadbalancer:= "simple batch create lb"
    	vipSubnetCidrIdLoadbalancer:= "1992ec06-f364-4ae3-b936-6a8cc24633b7"
    	guaranteedLoadbalancer:= false
    	adminStateUpLoadbalancer:= true
    	loadbalancerbody := &model.BatchCreateLoadBalancerOption{
    		Number: &numberLoadbalancer,
    		Name: &nameLoadbalancer,
    		Description: &descriptionLoadbalancer,
    		VipSubnetCidrId: &vipSubnetCidrIdLoadbalancer,
    		Guaranteed: &guaranteedLoadbalancer,
    		AvailabilityZoneList: listAvailabilityZoneListLoadbalancer,
    		AdminStateUp: &adminStateUpLoadbalancer,
    	}
    	request.Body = &model.BatchCreateLoadBalancersRequestBody{
    		Loadbalancer: loadbalancerbody,
    	}
    	response, err := client.BatchCreateLoadBalancers(request)
    	if err == nil {
            fmt.Printf("%+v\n", response)
        } else {
            fmt.Println(err)
        }
    }
    

更多编程语言的SDK代码示例,请参见API Explorer的代码示例页签,可生成自动对应的SDK代码示例。

状态码

状态码

描述

202

批量创建负载均衡器正常返回

错误码

请参见错误码