安全组

创建安全组

用于创建安全组以及安全组规则:

def create_security_group(self):

      client_token = generate_client_token()

      #设置安全组名称

      security_group_name = 'your_security_group_name' + client_token

      #设置安全组规则

      security_group_rule = bcc_model.SecurityGroupRuleModel('rule_' + client_token,# 设置备注

                                                              'ingress',# 设置入站/出站,取值ingress或egress,必选参数

                                                              portRange='1-65535',# 设置端口范围,默认空时为1-65535,可以指定80等单个端口

                                                              protocol='tcp',# 设置协议类型

                                                              sourceGroupId='',# 设置源安全组ID

                                                              sourceIp='')# 设置源IP地址,与sourceGroupId不能同时设定值

       security_group_rule_list = []

       security_group_rule_list.append(security_group_rule)

       #vpc的Id,空的时候,表示默认VPC的id

       security_group_rule_vpc_id = 'your-vpc-id'

       #设置安全组描述

       security_group_desc = 'your_security_group_desc'

       self.assertEqual(

            type(self.client.create_security_group(name=security_group_name,

                                                                         rules=security_group_rule_list,

                                                                         vpc_id=security_group_rule_vpc_id,

                                                                         desc=security_group_desc,

                                                                         client_token=client_token)), 

            baidubce.bce_response.BceResponse)

查询安全组列表

用于查询用户的所有安全组信息:

def list_security_groups(self):  

      #批量获取列表的查询的起始位置 

      marker = 'your-marker'

      #每页包含的最大数量

      maxKeys = 100

      #实例id,可查询实例关联的安全组列表 

      instance_id = 'your-instance-id'

      #vpcid,可查询实例关联的安全组列表 

      vpc_id = 'your-vpc-id'

      self.assertEqual(

           type(self.client.list_security_groups(instance_id=instance_id,

                                                                    vpc_id=vpc_id,

                                                                    marker=marker,

                                                                    max_keys=max_keys)), 

           baidubce.bce_response.BceResponse)

删除安全组

用于删除某个安全组:

def delete_security_group(self):

      #要删除的安全组ID

      security_group_id = 'your-security-group-id'

      self.assertEqual(

           type(self.client.delete_security_group(security_group_id)), 

           baidubce.bce_response.BceResponse)

授权安全组规则

用于在安全组中授权新的安全组规则:

def authorize_security_group_rule(self):

      client_token = generate_client_token()

      #设置安全组ID

      security_group_id = 'your-security-group-id'

      #设置安全组规则

      security_group_rule = bcc_model.SecurityGroupRuleModel(direction='ingress',# 设置入站/出站,取值ingress或egress,必选参数

                                                                                                      portRange='80-90',# 设置端口范围,默认空时为1-65535,可以指定80等单个端口

                                                                                                      protocol='tcp')# 设置协议类型

      self.assertEqual(

            type(self.client.authorize_security_group_rule(security_group_id=security_group_id,

                                                                                      rule=security_group_rule,

                                                                                      client_token=client_token)),

            baidubce.bce_response.BceResponse)

撤销安全组规则

用于撤销安全组中的规则:

def revoke_security_group_rule(self):

      client_token = generate_client_token()

      #待撤销安全组ID

      security_group_id = 'your-security-group-id'

      #待撤销安全组规则

      security_group_rule = bcc_model.SecurityGroupRuleModel(direction='ingress',# 设置入站/出站,取值ingress或egress,必选参数

                                                              portRange='80-90',# 设置端口范围,默认空时为1-65535,可以指定80等单个端口

                                                              protocol='tcp')# 设置协议类型

      self.assertEqual(

            type(self.client.revoke_security_group_rule(security_group_id=security_group_id,

                                                                                  security_group_rule,

                                                                                  client_token=client_token)),

            baidubce.bce_response.BceResponse)