Security group
Updated at:2025-10-20
Create a security group
Create security groups and security group rules with the following code:
Plain Text
1def create_security_group(self):
2
3 client_token = generate_client_token()
4
5 # Set security group name
6 security_group_name = 'your_security_group_name' + client_token
7
8 # Set security group rules
9 security_group_rule = bcc_model.SecurityGroupRuleModel('rule_' + client_token, # set remarks
10 'ingress',# set the ingress/egress, with values of ingress/egress, required parameter
11 portRange='1-65535', # set the port range, defaulting to 1-65535 when empty. A single port such as 80 can be specified
12 protocol='tcp', # set the protocol type
13 sourceGroupId='', # set the source security group ID
14 sourceIp='')# Set the Source IP Address. It cannot be set simultaneously with the value of sourceGroupId
15
16 security_group_rule_list = []
17
18 security_group_rule_list.append(security_group_rule)
19
20 # VPC ID. If empty, it indicates the default VPC ID
21 security_group_rule_vpc_id = 'your-vpc-id'
22
23 # Set security group description
24 security_group_desc = 'your_security_group_desc'
25
26 self.assertEqual(
27
28 type(self.client.create_security_group(name=security_group_name,
29 rules=security_group_rule_list,
30 vpc_id=security_group_rule_vpc_id,
31 desc=security_group_desc,
32 client_token=client_token)),
33
34 baidubce.bce_response.BceResponse)
Query the security group list
You can query all security group information of the user with the following code:
Plain Text
1def list_security_groups(self):
2
3 # Starting position of the batch list query
4 marker = 'your-marker'
5
6 # Maximum number of items included per page
7 maxKeys = 100
8
9 # Instance ID, which can be used to query the list of security groups associated with the instance
10 instance_id = 'your-instance-id'
11
12 #VPC ID, which can be used to query the list of security groups associated with the instance
13 vpc_id = 'your-vpc-id'
14
15 self.assertEqual(
16
17 type(self.client.list_security_groups(instance_id=instance_id,
18 vpc_id=vpc_id,
19 marker=marker,
20 max_keys=max_keys)),
21
22 baidubce.bce_response.BceResponse)
Delete a security group
You can delete a security group with the following code:
Plain Text
1def delete_security_group(self):
2
3 #ID of security group to be deleted
4 security_group_id = 'your-security-group-id'
5
6 self.assertEqual(
7
8 type(self.client.delete_security_group(security_group_id)),
9
10 baidubce.bce_response.BceResponse)
Authorize security group rules
You can authorize new security group rules in the security group with the following code:
Plain Text
1def authorize_security_group_rule(self):
2
3 client_token = generate_client_token()
4
5 # Set security group ID
6 security_group_id = 'your-security-group-id'
7
8 # Set security group rules
9 security_group_rule = bcc_model.SecurityGroupRuleModel(direction='ingress', # set the ingress/egress, with values of ingress/egress, required parameter
10 portRange='80-90',# set the port range, defaulting to 1-65535 when empty. A single port such as 80 can be specified
11 protocol='tcp') # set the protocol type
12
13 self.assertEqual(
14
15 type(self.client.authorize_security_group_rule(security_group_id=security_group_id,
16 rule=security_group_rule,
17 client_token=client_token)),
18
19 baidubce.bce_response.BceResponse)
Revoke security group rules
You can revoke rules in the security group with the following code:
Plain Text
1def revoke_security_group_rule(self):
2
3 client_token = generate_client_token()
4
5 #ID of security group to be revoked
6 security_group_id = 'your-security-group-id'
7
8 // Security group rules to be revoked
9 security_group_rule = bcc_model.SecurityGroupRuleModel(direction='ingress', # set the ingress/egress, with values of ingress/egress, required parameter
10 portRange='80-90',# set the port range, defaulting to 1-65535 when empty. A single port such as 80 can be specified
11 protocol='tcp') # set the protocol type
12
13 self.assertEqual(
14
15 type(self.client.revoke_security_group_rule(security_group_id=security_group_id,
16 security_group_rule,
17 client_token=client_token)),
18
19 baidubce.bce_response.BceResponse)
