
# 创建安全组规则
#### 功能介绍
创建安全组规则。默认创建允许策略，如需创建拒绝策略，请移步V3接口参考创建安全组规则的action参数说明。
#### 调试
您可以在[API Explorer](https://apiexplorer.developer.huaweicloud.com/apiexplorer/doc?product=VPC&version=v2&api=NeutronCreateSecurityGroupRule)中调试该接口。
#### URI
POST /v2.0/security-group-rules
#### 请求参数
表1请求参数 
| 参数名称                | 类型                                                                              | 必选 | 说明                                                                                |
|:---|:---|:---|:---|
| security_group_rule | [security_group_rule] object | 是  | security group rule对象。请参见[表2]。 |
   
 表2Security Group Rule对象 
| 参数名称                    | 是否必选 | 类型      | 说明                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        |
|:---|:---|:---|:---|
| description             | 否    | String  | 安全组规则描述。                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  |
| security_group_id       | 是    | String  | 所属安全组id。                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  |
| remote_group_id         | 否    | String  | - 功能说明：所属安全组的对端id。  - 使用说明：和remote_ip_prefix，remote_address_group_id参数互斥。                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              |
| direction               | 是    | String  | - 安全组规则方向。  - 取值范围：ingress表示入方向，egress表示出方向。                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         |
| remote_ip_prefix        | 否    | String  | - 功能说明：对端ip网段。  - 使用说明：和remote_group_id，remote_address_group_id参数互斥。                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              |
| protocol                | 否    | String  | - 功能说明：协议类型或直接指定IP协议号。  - 取值范围：tcp/udp/icmp或IP协议号。                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      |
| port_range_max          | 否    | Integer | - 功能说明：最大端口，当协议类型为ICMP时，该值表示ICMP的code。  - 取值范围：1-65535（当表示code时为0-255）。                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        |
| port_range_min          | 否    | Integer | - 功能说明：最小端口，当协议类型为ICMP时，该值表示ICMP的type。  - 约束： - protocol为tcp和udp时，port_range_max和port_range_min必须同时输入，且port_range_max应大于等于port_range_min。  - protocol为icmp时，指定ICMP code（port_range_max）时，必须同时指定ICMP type（port_range_min）    - 取值范围：1-65535（当表示code时为0-255）   |
| ethertype               | 否    | String  | - 功能说明：网络类型。  - 取值范围：IPv4/IPv6                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     |
| remote_address_group_id | 否    | String  | - 功能说明：远端IP地址组ID。您可以登录管理控制台，在IP地址组页面查看该ID。  - 约束：和remote_ip_prefix，remote_group_id功能互斥。                                                                                                                                                                                                                                                                                                                                                                                                                  |
   
#### 请求示例
创建出方向安全组规则，所在安全组id为5cb9c1ee-00e0-4d0f-9623-55463cd26ff8，协议为tcp，对端ip为10.10.0.0/24。
```
POST https://{Endpoint}/v2.0/security-group-rules
{
    "security_group_rule": {
        "security_group_id": "5cb9c1ee-00e0-4d0f-9623-55463cd26ff8", 
        "direction": "egress", 
        "protocol": "tcp", 
        "remote_ip_prefix": "10.10.0.0/24"
    }
}
```
#### 响应参数
表3响应参数 
| 参数名称                | 类型                                                                               | 说明                                                                                 |
|:---|:---|:---|
| security_group_rule | [security_group_rule] object | security group rule对象。请参见[表4]。 |
   
 表4Security Group Rule对象 
| 参数名称                    | 类型      | 说明                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    |
|:---|:---|:---|
| id                      | String  | - 安全组规则id。  - 使用说明：查询安全组规则非必选。                                                                                                                                                                                                                                                                                                                                                                                                                                         |
| description             | String  | 安全组规则描述。                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              |
| security_group_id       | String  | 所属安全组id。                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              |
| remote_group_id         | String  | 所属安全组的对端id。                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           |
| direction               | String  | 安全组规则方向。                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              |
| remote_ip_prefix        | String  | 对端ip网段。                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               |
| protocol                | String  | 协议类型或直接指定IP协议号。                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       |
| port_range_max          | Integer | - 功能说明：最大端口，当协议类型为ICMP时，该值表示ICMP的code。  - 取值范围：1-65535（当表示code时为0-255）。                                                                                                                                                                                                                                                                                                                                                                                        |
| port_range_min          | Integer | - 功能说明：最小端口，当协议类型为ICMP时，该值表示ICMP的type。  - 约束： - protocol为tcp和udp时，port_range_max和port_range_min必须同时输入，且port_range_max应大于等于port_range_min。  - protocol为icmp时，指定ICMP code（port_range_max）时，必须同时指定ICMP type（port_range_min）。     |
| ethertype               | String  | - 功能说明：IP地址协议类型。  - 取值范围：支持IPv4，IPv6                                                                                                                                                                                                                                                                                                                                                                                                                                    |
| tenant_id               | String  | 项目ID                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  |
| remote_address_group_id | String  | - 功能说明：远端IP地址组ID。  - 约束：和remote_ip_prefix，remote_group_id功能互斥。                                                                                                                                                                                                                                                                                                                                               |
| project_id              | String  | 项目ID，请参见[获取项目ID](https://support.huaweicloud.com/api-vpc/vpc_api_0011.html)。                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          |
| created_at              | String  | - 功能说明：安全组规则创建时间  - 取值范围：UTC时间格式，yyyy-MM-ddTHH:mm:ssZ                                                                                                                                                                                                                                                                                                                                                                                                        |
| updated_at              | String  | - 功能说明：安全组规则更新时间  - 取值范围：UTC时间格式，yyyy-MM-ddTHH:mm:ssZ                                                                                                                                                                                                                                                                                                                                                                                                                |
   
#### 响应示例
```
{
    "security_group_rule": {
        "remote_group_id": null, 
        "direction": "egress", 
        "remote_ip_prefix": "10.10.0.0/24", 
        "protocol": "tcp", 
        "tenant_id": "6fbe9263116a4b68818cf1edce16bc4f", 
        "port_range_max": null, 
        "security_group_id": "5cb9c1ee-00e0-4d0f-9623-55463cd26ff8", 
        "port_range_min": null, 
        "ethertype": "IPv4", 
        "description": null, 
        "id": "7c336b04-1603-4911-a6f4-f2af1d9a0488",
        "project_id": "6fbe9263116a4b68818cf1edce16bc4f", 
        "created_at": "2018-09-20T02:15:34",
        "updated_at": "2018-09-20T02:15:34",
        "remote_address_group_id": null
    }
}
```
#### 状态码
请参见[状态码](https://support.huaweicloud.com/api-vpc/vpc_api_0002.html)。
#### 错误码
请参见[错误码](https://support.huaweicloud.com/api-vpc/vpc_api_0003.html)。
