
# 创建安全组规则 - CreateSecurityGroupRule
#### 功能介绍
根据用户的请求内容，创建安全组规则。
#### 调试
您可以在[API Explorer](https://console.huaweicloud.com/apiexplorer/#/openapi/IEC/doc?api=CreateSecurityGroupRule)中调试该接口，支持自动认证鉴权。API Explorer可以自动生成SDK代码示例，并提供SDK代码示例调试功能。
#### 授权信息
账号具备所有API的调用权限，如果使用账号下的IAM用户调用当前API，该IAM用户需具备调用API所需的权限，具体权限要求请参见[权限和授权项](https://support.huaweicloud.com/api-iec/iec_05_0600.html)。
#### URI
POST /v1/security-group-rules
#### 请求参数
表1请求Header参数 
| 参数           | 是否必选 | 参数类型   | 描述                                                                                                   |
|:---|:---|:---|:---|
| X-Auth-Token | 是    | String | 用户Token。 通过调用IAM服务获取用户Token接口获取（响应消息头中X-Subject-Token的值）。 |
   
表2请求Body参数 
| 参数                  | 是否必选 | 参数类型                                                                                                    | 描述           |
|:---|:---|:---|:---|
| security_group_rule | 是    | [CreateSecurityGroupRuleOption] object | 创建安全组规则参数对象。 |
   
 表3CreateSecurityGroupRuleOption 
| 参数                | 是否必选 | 参数类型    | 描述                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   |
|:---|:---|:---|:---|
| description       | 否    | String  | 安全组规则描述信息。                                                                                                                                                                                                                                                                                                                                                                                                                                                                           |
| security_group_id | 是    | String  | 安全组ID。                                                                                                                                                                                                                                                                                                                                                                                                                                                                               |
| direction         | 是    | String  | 出入控制方向。 取值范围： - egress：出方向   - ingress：入方向    |
| ethertype         | 否    | String  | IP协议类型。 取值范围：IPv4,IPv6                                                                                                                                                                                                                                                                                                                                                                                                                    |
| protocol          | 否    | String  | 协议类型。 取值范围：icmp、tcp、udp、icmpv6或IP协议号（0\~255） 约束：为空表示支持所有协议。协议为icmpv6时，网络类型应该为IPv6；协议为icmp时，网络类型应该为IPv4                                                                                                                                                                                                                                                                                         |
| port_range_min    | 否    | Integer | 起始端口值。 取值范围：1\~65535 约束：取值不能大于port_range_max的值，为空表示所有端口。                                                                                                                                                                                                                                                                                                                                       |
| port_range_max    | 否    | Integer | 结束端口值。 取值范围：1\~65535 约束：取值不能小于port_range_min的值，为空表示所有端口。                                                                                                                                                                                                                                                                                                                                       |
| remote_group_id   | 否    | String  | 对端安全组id。 约束：和remote_ip_prefix互斥                                                                                                                                                                                                                                                                                                                                                                                                           |
| remote_ip_prefix  | 否    | String  | 远端IP地址，当direction是egress时为虚拟机访问端的地址，当direction是ingress时为访问虚拟机的地址。 取值范围：IP地址，或者cidr格式 约束：和remote_group_id互斥                                                                                                                                                                                                                                                                                     |
| action            | 否    | String  | 安全组规则生效策略 取值范围：allow 允许，deny 拒绝 约束：默认值为allow                                                                                                                                                                                                                                                                                                                                                   |
| priority          | 否    | Integer | 规则在安全组中的优先级 取值范围：1\~100，1代表最高优先级 约束：默认值为1                                                                                                                                                                                                                                                                                                                                                      |
   
#### 响应参数
**状态码：200**
表4响应Body参数 
| 参数                  | 参数类型                                                                             | 描述       |
|:---|:---|:---|
| security_group_rule | [SecurityGroupRule] object | 安全组规则对象。 |
   
 表5SecurityGroupRule 
| 参数                | 参数类型    | 描述                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   |
|:---|:---|:---|
| id                | String  | 安全组规则的ID。                                                                                                                                                                                                                                                                                                                                                                                                                                                                            |
| description       | String  | 安全组规则描述信息。                                                                                                                                                                                                                                                                                                                                                                                                                                                                           |
| security_group_id | String  | 安全组ID。                                                                                                                                                                                                                                                                                                                                                                                                                                                                               |
| direction         | String  | 出入控制方向。 取值范围： - egress：出方向   - ingress：入方向    |
| ethertype         | String  | IP协议类型。 取值范围：IPv4，IPv6 约束：不填默认值为IPv4                                                                                                                                                                                                                                                                                                                                                           |
| protocol          | String  | 协议类型。 取值范围：icmp、tcp、udp、icmpv6等 约束：为空表示支持所有协议                                                                                                                                                                                                                                                                                                                                                  |
| port_range_min    | String  | 起始端口值。 取值范围：1\~65535 约束：取值不能大于port_range_max的值，为空表示所有端口                                                                                                                                                                                                                                                                                                                                        |
| port_range_max    | String  | 结束端口值。 取值范围：1\~65535 约束：取值不能小于port_range_min的值，为空表示所有端口。                                                                                                                                                                                                                                                                                                                                       |
| remote_group_id   | String  | 对端安全组ID。 约束：和remote_ip_prefix互斥 ，remote_group_id与remote_ip_prefix必须存在一个                                                                                                                                                                                                                                                                                                                                                                   |
| remote_ip_prefix  | String  | 远端IP地址，当direction是egress时为虚拟机访问端的地址，当direction是ingress时为访问虚拟机的地址。 取值范围：IP地址，或者cidr格式 约束：和remote_group_id互斥                                                                                                                                                                                                                                                                                     |
| action            | String  | 安全组规则生效策略 取值范围：allow 允许，deny 拒绝 约束：默认值为allow                                                                                                                                                                                                                                                                                                                                                   |
| priority          | Integer | 规则在安全组中的优先级 取值范围：1\~100，1代表最高优先级 约束：默认值为1                                                                                                                                                                                                                                                                                                                                                      |
   
#### 请求示例
创建一条入方向的安全组规则，所在安全组ID为44eee672-43c5-4820-8ab6-afc585972acf
```
POST https://{{localAddress}}/v1/security-group-rules
{
  "security_group_rule" : {
    "direction" : "ingress",
    "remote_group_id" : "44eee672-43c5-4820-8ab6-afc585972acf",
    "security_group_id" : "44eee672-43c5-4820-8ab6-afc585972acf"
  }
}
```
#### 响应示例
**状态码：200**
OK
```
{
  "security_group_rule" : {
    "id" : "d5002101-3d35-4bcd-b13b-56f123686c97",
    "description" : "",
    "security_group_id" : "44eee672-43c5-4820-8ab6-afc585972acf",
    "direction" : "ingress",
    "ethertype" : "",
    "protocol" : "",
    "port_range_min" : "",
    "port_range_max" : "",
    "remote_ip_prefix" : "",
    "remote_group_id" : "44eee672-43c5-4820-8ab6-afc585972acf"
  }
}
```
#### 状态码
| 状态码 | 描述 |
|:---|:---|
| 200 | OK |
   
#### 错误码
请参见[错误码](https://support.huaweicloud.com/api-iec/ErrorCode.html)。
