更新时间:2025-08-19 GMT+08:00
分享

创建网络ACL

功能介绍

创建网络ACL。

调用方法

请参见如何调用API

URI

POST /v3/{project_id}/vpc/firewalls

表1 路径参数

参数

是否必选

参数类型

描述

project_id

String

项目ID。

获取方式请参见获取项目ID

请求参数

表2 请求Body参数

参数

是否必选

参数类型

描述

firewall

CreateFirewallOption object

创建ACL的请求体。

dry_run

Boolean

  • 功能说明:是否只预检此次请求。

  • 取值范围:

    • true:发送检查请求,不会创建ACL。检查项包括是否填写了必需参数、请求格式、业务限制。如果检查不通过,则返回对应错误。如果检查通过,则返回响应码202。

    • false(默认值):发送正常请求,并直接创建ACL。

表3 CreateFirewallOption

参数

是否必选

参数类型

描述

name

String

  • 功能说明:ACL名称。

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

description

String

  • 功能说明:ACL描述信息。

  • 取值范围:0-255个字符。

  • 约束:不能包含“<”和“>”。

enterprise_project_id

String

  • 功能说明:ACL企业项目ID。

  • 取值范围:最大长度36字节,带“-”连字符的UUID格式,或者是字符串“0”。“0”表示默认企业项目。

tags

Array of ResourceTag objects

  • 功能描述:ACL资源标签。

admin_state_up

Boolean

  • 功能说明:ACL管理状态,默认值true。

  • 取值范围:true表示ACL开启;false表示ACL关闭。

表4 ResourceTag

参数

是否必选

参数类型

描述

key

String

  • 功能说明:标签键。

  • 约束:同一资源的key值不能重复。

  • 取值范围:1-128个字符。

最小长度:1

最大长度:128

value

String

  • 功能说明:标签值。

  • 取值范围:0-255个字符。

最大长度:255

响应参数

状态码:201

表5 响应Body参数

参数

参数类型

描述

firewall

FirewallDetail object

创建ACL响应体。

request_id

String

请求ID。

表6 FirewallDetail

参数

参数类型

描述

id

String

  • 功能说明:ACL唯一标识。

  • 取值范围:合法UUID的字符串。

name

String

  • 功能说明:ACL名称。

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

description

String

  • 功能说明:ACL描述信息。

  • 取值范围:0-255个字符。

  • 约束:不能包含“<”和“>”。

project_id

String

  • 功能说明:资源所属项目ID。

created_at

String

  • 功能说明:ACL创建时间

  • 取值范围:UTC时间格式,yyyy-MM-ddTHH:mm:ss,系统自动生成。

updated_at

String

  • 功能描述:ACL最近一次更新资源的时间。

  • 取值范围:UTC时间格式,yyyy-MM-ddTHH:mm:ss,系统自动生成。

admin_state_up

Boolean

  • 功能说明:ACL管理状态。

  • 取值范围:true表示ACL开启;false表示ACL关闭。

status

String

  • 功能说明:网络ACL的状态。

enterprise_project_id

String

  • 功能说明:ACL企业项目ID。

  • 取值范围:最大长度36字节,带“-”连字符的UUID格式,或者是字符串“0”。“0”表示默认企业项目。

tags

Array of ResourceTag objects

  • 功能描述:ACL资源标签。

associations

Array of FirewallAssociation objects

  • 功能说明:ACL绑定的子网列表。

ingress_rules

Array of FirewallRuleDetail objects

  • 功能说明:ACL入方向规则列表。

egress_rules

Array of FirewallRuleDetail objects

  • 功能说明:ACL出方向规则列表。

表7 ResourceTag

参数

参数类型

描述

key

String

  • 功能说明:标签键。

  • 约束:同一资源的key值不能重复。

  • 取值范围:1-128个字符。

最小长度:1

最大长度:128

value

String

  • 功能说明:标签值。

  • 取值范围:0-255个字符。

最大长度:255

表8 FirewallAssociation

参数

参数类型

描述

virsubnet_id

String

  • 功能说明:ACL绑定的子网ID。

表9 FirewallRuleDetail

参数

参数类型

描述

id

String

  • 功能说明:ACL规则唯一标识。

  • 取值范围:合法UUID的字符串。

name

String

  • 功能说明:ACL规则名称。

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

description

String

  • 功能说明:ACL规则描述信息。

  • 取值范围:0-255个字符。

  • 约束:不能包含“<”和“>”。

action

String

  • 功能说明:ACL规则对流量执行的操作放通或拒绝。

  • 取值范围:allow放通;deny拒绝。

project_id

String

  • 功能说明:资源所属项目ID。

protocol

String

  • 功能说明:ACL规则协议。

  • 取值范围:支持tcp, udp, icmp, icmpv6或者IP协议号(0-255)。

ip_version

Integer

  • 功能说明:ACL规则的ip版本。

  • 取值范围:4, 表示ipv4;6, 表示ipv6。

source_ip_address

String

  • 功能说明:ACL规则源IP地址或者CIDR。

  • 约束:source_ip_address和source_address_group_id不能同时设置。

destination_ip_address

String

  • 功能说明:ACL规则目的IP地址或者CIDR。

  • 约束:destination_ip_address和destination_address_group_id不能同时设置。

source_port

String

  • 功能说明:ACL规则的源端口。

  • 取值范围:支持端口号,一段端口范围,多个以逗号分隔。

  • 约束:支持的端口组的数量默认为20。

destination_port

String

  • 功能说明:ACL规则的目的端口。

  • 取值范围:支持端口号,一段端口范围,多个以逗号分隔。

  • 约束:支持的端口组的数量默认为20。

source_address_group_id

String

  • 功能说明:ACL规则的源地址组ID。

  • 约束:source_ip_address和source_address_group_id不能同时设置。

destination_address_group_id

String

  • 功能说明:ACL规则的目的地址组ID。

  • 约束:destination_ip_address和destination_address_group_id不能同时设置。

enabled

Boolean

  • 功能说明:是否启用ACL规则。

  • 取值范围:true,表示启用;false,表示停用。

  • 默认值:true。

请求示例

创建网络ACL,命名为network_acl_test1。

POST https://{Endpoint}/v3/{project_id}/vpc/firewalls

{
  "firewall" : {
    "name" : "network_acl_test1",
    "description" : "network_acl_test1",
    "enterprise_project_id" : "158ad39a-dab7-45a3-9b5a-2836b3cf93f9"
  }
}

响应示例

状态码:201

POST操作正常返回,更多状态码请参见状态码

{
  "firewall" : {
    "id" : "e9a7731d-5bd9-4250-a524-b9a076fd5629",
    "name" : "network_acl_test1",
    "description" : "network_acl_test1",
    "project_id" : "9476ea5a8a9849c38358e43c0c3a9e12",
    "created_at" : "2022-04-07T07:30:46.000+00:00",
    "updated_at" : "2022-04-07T07:30:46.000+00:00",
    "admin_state_up" : true,
    "enterprise_project_id" : "158ad39a-dab7-45a3-9b5a-2836b3cf93f9",
    "status" : "ACTIVE",
    "tags" : [ ],
    "ingress_rules" : [ ],
    "egress_rules" : [ ],
    "associations" : [ ]
  }
}

SDK代码示例

SDK代码示例如下。

创建网络ACL,命名为network_acl_test1。

 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
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.vpc.v3.region.VpcRegion;
import com.huaweicloud.sdk.vpc.v3.*;
import com.huaweicloud.sdk.vpc.v3.model.*;


public class CreateFirewallSolution {

    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);

        VpcClient client = VpcClient.newBuilder()
                .withCredential(auth)
                .withRegion(VpcRegion.valueOf("<YOUR REGION>"))
                .build();
        CreateFirewallRequest request = new CreateFirewallRequest();
        CreateFirewallRequestBody body = new CreateFirewallRequestBody();
        CreateFirewallOption firewallbody = new CreateFirewallOption();
        firewallbody.withName("network_acl_test1")
            .withDescription("network_acl_test1")
            .withEnterpriseProjectId("158ad39a-dab7-45a3-9b5a-2836b3cf93f9");
        body.withFirewall(firewallbody);
        request.withBody(body);
        try {
            CreateFirewallResponse response = client.createFirewall(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());
        }
    }
}

创建网络ACL,命名为network_acl_test1。

 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
# coding: utf-8

import os
from huaweicloudsdkcore.auth.credentials import BasicCredentials
from huaweicloudsdkvpc.v3.region.vpc_region import VpcRegion
from huaweicloudsdkcore.exceptions import exceptions
from huaweicloudsdkvpc.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 = VpcClient.new_builder() \
        .with_credentials(credentials) \
        .with_region(VpcRegion.value_of("<YOUR REGION>")) \
        .build()

    try:
        request = CreateFirewallRequest()
        firewallbody = CreateFirewallOption(
            name="network_acl_test1",
            description="network_acl_test1",
            enterprise_project_id="158ad39a-dab7-45a3-9b5a-2836b3cf93f9"
        )
        request.body = CreateFirewallRequestBody(
            firewall=firewallbody
        )
        response = client.create_firewall(request)
        print(response)
    except exceptions.ClientRequestException as e:
        print(e.status_code)
        print(e.request_id)
        print(e.error_code)
        print(e.error_msg)

创建网络ACL,命名为network_acl_test1。

 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
package main

import (
	"fmt"
	"github.com/huaweicloud/huaweicloud-sdk-go-v3/core/auth/basic"
    vpc "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/vpc/v3"
	"github.com/huaweicloud/huaweicloud-sdk-go-v3/services/vpc/v3/model"
    region "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/vpc/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 := vpc.NewVpcClient(
        vpc.VpcClientBuilder().
            WithRegion(region.ValueOf("<YOUR REGION>")).
            WithCredential(auth).
            Build())

    request := &model.CreateFirewallRequest{}
	descriptionFirewall:= "network_acl_test1"
	enterpriseProjectIdFirewall:= "158ad39a-dab7-45a3-9b5a-2836b3cf93f9"
	firewallbody := &model.CreateFirewallOption{
		Name: "network_acl_test1",
		Description: &descriptionFirewall,
		EnterpriseProjectId: &enterpriseProjectIdFirewall,
	}
	request.Body = &model.CreateFirewallRequestBody{
		Firewall: firewallbody,
	}
	response, err := client.CreateFirewall(request)
	if err == nil {
        fmt.Printf("%+v\n", response)
    } else {
        fmt.Println(err)
    }
}

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

状态码

状态码

描述

201

POST操作正常返回,更多状态码请参见状态码

错误码

请参见错误码

相关文档