
# 创建安全组规则 - CreateSecurityGroupRule
#### 功能介绍
创建安全组规则。
#### 调试
您可以在[API Explorer](https://apiexplorer.developer.huaweicloud.com/apiexplorer/doc?product=VPC&version=v2&api=CreateSecurityGroupRule)中调试该接口。
#### 授权信息
账号具备所有API的调用权限，如果使用账号下的IAM用户调用当前API，该IAM用户需具备调用API所需的权限。
- 如果使用角色与策略授权，具体权限要求请参见[权限和授权项](https://support.huaweicloud.com/api-vpc/vpc_permission_0000.html)。
- 如果使用身份策略授权，需具备如下身份策略权限。
  
  | 授权项                           | 访问级别  | 资源类型（\*为必须）          | 条件键                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           | 别名 | 依赖的授权项 |
  |:---|:---|:---|:---|:---|:---|
  | vpc:securityGroupRules:create | Write | securityGroupRule \* | - vpc:SecurityGroupRuleDirection  - vpc:SecurityGroupRuleProtocol  - vpc:SecurityGroupRuleAction  - vpc:SecurityGroupRulePort  - vpc:SecurityGroupRuleRemoteIpAddressGroup  - vpc:SecurityGroupRuleRemoteSecurityGroup  - vpc:SecurityGroupRuleRemoteIpPrefix   | -  | -      |
  | vpc:securityGroupRules:create | Write | securityGroup \*     | - [g:EnterpriseProjectId](https://support.huaweicloud.com/usermanual-iam5/iam_01_1287.html)  - vpc:SecurityGroupId  - [g:ResourceTag/\<tag-key\>](https://support.huaweicloud.com/usermanual-iam5/iam_01_1287.html)                                                                                                                                                                                                                                                                                                     | -  | -      |
     
  
 
#### URI
POST /v1/{project_id}/security-group-rules
#### 请求参数
表1请求参数 
| 名称                  | 是否必选 | 参数类型                                                              | 说明                                                    |
|:---|:---|:---|:---|
| security_group_rule | 是    | [security_group_rule] object | 安全组规则对象，请参见[表2]。 |
   
 表2security_group_rule字段说明 
| 名称                      | 是否必选 | 参数类型    | 说明                                                                                                                                                                                                                                                                                                                                                                                                                       |
|:---|:---|:---|:---|
| security_group_id       | 是    | String  | 所属安全组ID                                                                                                                                                                                                                                                                                                                                                                                                                  |
| description             | 否    | String  | - 功能说明：安全组规则描述  - 取值范围：0-255个字符，支持数字、字母、中文字符                                                                                                                                                                                                        |
| direction               | 是    | String  | - 功能说明：安全组规则出入控制方向  - 取值范围： - egress：出方向  - ingress：入方向                      |
| ethertype               | 否    | String  | - 功能说明：IP地址协议类型  - 取值范围：IPv4，IPv6  - 约束：不填默认值为IPv4                                                                                                                              |
| protocol                | 否    | String  | - 功能说明：协议类型  - 取值范围：icmp、tcp、udp、icmpv6或IP协议号（0\~255）(如gre协议号为47)  - 约束：为空表示支持所有协议。协议为icmpv6时，网络类型应该为IPv6；协议为icmp时，网络类型应该为IPv4                                                           |
| port_range_min          | 否    | Integer | - 功能说明：起始端口值  - 取值范围：1\~65535  - 约束：不能大于port_range_max的值，为空表示所有端口，如果协议是icmp类型，取值范围参照[安全组规则icmp协议名称对应关系表](https://support.huaweicloud.com/api-vpc/vpc_api_0009.html)。    |
| port_range_max          | 否    | Integer | - 功能说明：结束端口值  - 取值范围：1\~65535  - 约束：协议不为icmp时，取值不能小于port_range_min的值，为空表示所有端口，如果协议是icmp类型，取值范围参照[安全组规则icmp协议名称对应关系表](https://support.huaweicloud.com/api-vpc/vpc_api_0009.html)。   |
| remote_ip_prefix        | 否    | String  | - 功能说明：远端IP地址，当direction是egress时为虚拟机访问端的地址，当direction是ingress时为访问虚拟机的地址  - 取值范围：IP地址，或者cidr格式  - 约束：和remote_group_id，remote_address_group_id功能互斥。                                |
| remote_group_id         | 否    | String  | - 功能说明：对端安全组id  - 约束：和remote_ip_prefix，remote_address_group_id功能互斥。                                                                                                                                                                                   |
| remote_address_group_id | 否    | String  | - 功能说明：远端IP地址组ID。您可以登录管理控制台，在IP地址组页面查看该ID。  - 约束：和remote_ip_prefix，remote_group_id功能互斥。                                                                                                          |
   
#### 请求示例
- 创建一条入方向安全组规则，所在安全组id为a7734e61-b545-452d-a3cd-0189cbd9747a。
  ```
  POST https://{Endpoint}/v1/{project_id}/security-group-rules
  {
      "security_group_rule": {
          "direction": "ingress", 
          "port_range_min": 80, 
          "ethertype": "IPv4", 
          "port_range_max": 80, 
          "protocol": "tcp", 
          "remote_group_id": "85cc3048-abc3-43cc-89b3-377341426ac5", 
          "security_group_id": "a7734e61-b545-452d-a3cd-0189cbd9747a"
      }
  }
  ```
  ```
  POST https://{Endpoint}/v1/{project_id}/security-group-rules
  {
      "security_group_rule": {
          "direction": "ingress", 
          "port_range_min": 80, 
          "ethertype": "IPv6", 
          "port_range_max": 90, 
          "protocol": "tcp",  
          "security_group_id": "a7734e61-b545-452d-a3cd-0189cbd9747a"
      }
  }
  ```
  
 
#### 响应参数
表3响应参数 
| 名称                  | 参数类型                                                            | 说明                                                 |
|:---|:---|:---|
| security_group_rule | [security_group_rule] object | 安全组规则对象，请参见[表4] |
   
 表4security_group_rule对象 
| 名称                      | 参数类型    | 说明                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          |
|:---|:---|:---|
| id                      | String  | 安全组规则标识。                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    |
| description             | String  | - 功能说明：安全组规则描述。  - 取值范围：0-255个字符，支持数字、字母、中文字符。                                                                                                                                                                                                                                                      |
| security_group_id       | String  | 安全组标识。                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      |
| direction               | String  | - 功能说明：出入控制方向。  - 取值范围： - egress：出方向  - ingress：入方向     |
| ethertype               | String  | - 功能说明：IP协议类型。  - 取值范围：IPv4，IPv6                                                                                                                                                                                                                                                                  |
| protocol                | String  | - 功能说明：协议类型。  - 取值范围：icmp、tcp、udp、icmpv6、IP协议号（0\~255）(如gre协议号为47)。  - 约束：为空表示支持所有协议。                                                                                                         |
| port_range_min          | Integer | - 功能说明：起始端口值。  - 取值范围：1\~65535  - 约束：不能大于port_range_max的值，为空表示所有端口，如果协议是icmp类型，取值范围请参见[安全组规则icmp协议名称对应关系表](https://support.huaweicloud.com/api-vpc/vpc_api_0009.html)。                         |
| port_range_max          | Integer | - 功能说明：结束端口值。  - 取值范围：1\~65535  - 约束：协议不为icmp时，取值不能小于port_range_min的值，为空表示所有端口，如果协议是icmp类型，取值范围请参见[安全组规则icmp协议名称对应关系表](https://support.huaweicloud.com/api-vpc/vpc_api_0009.html)。                   |
| remote_ip_prefix        | String  | - 功能说明：远端IP地址，当direction是egress时为虚拟机访问端的地址，当direction是ingress时为访问虚拟机的地址。  - 取值范围：IP地址，或者cidr格式  - 约束：和remote_group_id，remote_address_group_id功能互斥。                                                |
| remote_group_id         | String  | - 功能说明：对端安全组id。  - 约束：和remote_ip_prefix，remote_address_group_id功能互斥。                                                                                                                                                                                                                            |
| remote_address_group_id | String  | - 功能说明：远端IP地址组ID。  - 约束：和remote_ip_prefix，remote_group_id功能互斥。                                                                                                                                                                                                                                        |
| tenant_id               | String  | - 功能说明：安全组规则所属项目ID。                                                                                                                                                                                                                                                                                                                                                                            |
   
#### 响应示例
```
{
    "security_group_rule": {
        "direction": "ingress",
        "ethertype": "IPv4",
        "id": "2bc0accf-312e-429a-956e-e4407625eb62",
        "description": "",
        "port_range_max": 80,
        "port_range_min": 80,
        "protocol": "tcp",
        "remote_group_id": "85cc3048-abc3-43cc-89b3-377341426ac5",
        "remote_ip_prefix": null,
        "security_group_id": "a7734e61-b545-452d-a3cd-0189cbd9747a",
        "tenant_id": "e4f50856753b4dc6afee5fa6b9b6c550",
        "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)。
