Enterprise security group
Enterprise security group management
Initialization
Confirm Endpoint
When confirming SDK usage and configuring endpoint, understand the related concepts of endpoint. Baidu AI Cloud currently supports multiple regions. Please refer toRegion Selection Guide. North China - Beijing, South China-Guangzhou, East China - Suzhou, Hong Kong, Central China-Wuhan (Financial) and North China - Baoding are supported currently. Corresponding endpoint details are as follows:
| Access region | Endpoint |
|---|---|
| North China-Beijing | bcc.bj.baidubce.com |
| South China-Guangzhou | bcc.gz.baidubce.com |
| East China-Suzhou | bcc.su.baidubce.com |
| Hong Kong | bcc.hkg.baidubce.com |
| Central China-Wuhan (Financial) | bcc.fwh.baidubce.com |
| North China-Baoding | bcc.bd.baidubce.com |
Retrieve access key
To use the security group of Baidu AI Cloud, you need a valid AK (Access Key ID) and SK (Secret Access Key) for signature certification. AK/SK are system-assigned strings used to identify users and perform signature certification for security group access. Your AK/SK information can be obtained and understood through the following steps: Register a Baidu AI Cloud account Create AK/SK
Create EsgClient
EsgClient acts as the client for the security group service, offering developers multiple methods to interact with the service.
Access EsgClient via AK/SK method
- Before creating EsgClient, a configuration file must be created to configure EsgClient. Below, this configuration file is named sg_sample_conf.py, with the following specific configuration information:
1#!/usr/bin/env python
2#coding=utf-8
3 #Import Python standard logging module
4import logging
5 #Import BCC configuration management module and security certification module from Python SDK
6from baidubce.bce_client_configuration import BceClientConfiguration
7from baidubce.auth.bce_credentials import BceCredentials
8import baidubce
9 #Set EsgClient Host, Access Key ID, and Secret Access Key
10esg_host = ""bcc.bj.baidubce.com"
11access_key_id = "AK"
12secret_access_key = "SK"
13 #Set log file handles and log levels
14logger = logging.getLogger('baidubce.http.bce_http_client')
15fh = logging.FileHandler("sample.log")
16fh.setLevel(logging.DEBUG)
17 #Set the order, structure and content of log file output
18formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
19fh.setFormatter(formatter)
20logger.setLevel(logging.DEBUG)
21logger.addHandler(fh)
22 #Create BceClientConfiguration instance
23config = BceClientConfiguration(credentials=BceCredentials(access_key_id, secret_access_key), endpoint = esg_host)
Note: For log files, logging has the following levels: DEBUG, INFOm WARNINGm ERROR and CRITICAL.
In the code above, access_key_id corresponds to “Access Key ID” in the console. secret_access_key corresponds to “Access Key Secret” in the console. For the method to retrieve them, refer to Guide - Manage ACCESSKEY.
The above method requires users to manually specify the security group service domain name by assigning it to the esg_host variable. If not specified, the endpoint parameter is not required to be passed, and the default is the Beijing Region http://bcc.bj.baidubce.com.
- After completing the configurations above, refer to the following code to create an EsgClient.
1#Import EsgClient configuration file
2import sg_sample_conf
3
4 #Import related Esg modules
5from baidubce import exception
6from baidubce.services import esg
7from baidubce.services.esg.esg_client import EsgClient
8from baidubce.services.esg.esg_model import EnterpriseSecurityGroupRuleModel
9
10 #Create BccClient
11esg_client = EsgClient(sg_sample_conf.config)
Create enterprise security group
Function declaration
1def create_enterprise_security_group(self, name, rules=None, desc=None, tags=None, client_token=None, config=None):
Parameter meaning
Refer to the OpenAPI documentation: https://cloud.baidu.com/doc/VPC/s/gl5gqhqtk
Response value
Operation succeeded:
1{
2 "enterpriseSecurityGroupId": "esg-nky7qeom"
3}
Operation failed:
Throw an exception. For the exception list, refer to: https://cloud.baidu.com/doc/VPC/s/sjwvyuhe7
Code example
For specific code examples, refer to example_create_enterprise_security_group.py
List enterprise security groups
Function declaration
1def list_enterprise_security_groups(self, instance_id=None, marker=None, max_keys=None, config=None):
Parameter meaning
Refer to the OpenAPI documentation: https://cloud.baidu.com/doc/VPC/s/Cl5jbuluf
Response value
Operation succeeded:
1{
2 "nextMarker": "",
3 "marker": "",
4 "maxKeys": 1000,
5 "enterpriseSecurityGroups": [
6 {
7 "desc": "",
8 "id": "esg-4NxWoxeq",
9 "name": "test enterprise sg",
10 "createdTime":"2019-09-24T08:25:59Z",
11 "rules": [
12 {
13 "destIp": "all",
14 "localIp": "all",
15 "direction": "egress",
16 "ethertype": "IPv4",
17 "portRange": "1-65535",
18 "sourcePortRange": "1-65535",
19 "action":"allow",
20 "priority":"1000",
21 "protocol": "all",
22 "remark": "Remarks",
23 "enterpriseSecurityGroupRuleId": "esgr-gkv8yupumvx2",
24 "createdTime": "2020-07-27T13:00:52Z",
25 "updatedTime": "2020-07-27T13:00:52Z"
26 }
27 ],
28 "tags":[
29 {
30 "tagKey": tagKey,
31 "tagValue": tagValue
32 }
33 ]
34 }
35 ],
36 "isTruncated": false
37}
Operation failed:
Throw an exception. For the exception list, refer to: https://cloud.baidu.com/doc/VPC/s/sjwvyuhe7
Code example
For specific code examples, refer to example_get_enterprise_security_group_list.py
Delete enterprise security group
Function declaration
1def delete_enterprise_security_group(self, enterprise_security_group_id, config=None):
Parameter meaning
Refer to the OpenAPI documentation: https://cloud.baidu.com/doc/VPC/s/yl5jcu51m
Response value
Operation succeeded:
1{}
Operation failed:
Throw an exception. For the exception list, refer to: https://cloud.baidu.com/doc/VPC/s/sjwvyuhe7
Code example
For specific code examples, refer to example_delete_enterprise_security_group.py
Authorize enterprise security group rules
Function declaration
1def authorize_enterprise_security_group_rule(self, enterprise_security_group_id, rules, client_token=None, config=None):
Parameter meaning
Refer to the OpenAPI documentation: https://cloud.baidu.com/doc/VPC/s/Kl5jd2uh0
Response value
Operation succeeded:
1{}
Operation failed:
Throw an exception. For the exception list, refer to: https://cloud.baidu.com/doc/VPC/s/sjwvyuhe7
Code example
For specific code examples, refer to example_authorize_enterprise_security_group_rule.py
Delete enterprise security group rules
Function declaration
1def delete_enterprise_security_group_rule(self, enterprise_security_group_rule_id, config=None):
Parameter meaning
Refer to the OpenAPI documentation: https://cloud.baidu.com/doc/VPC/s/jl5jdnohy
Response value
Operation succeeded:
1{}
Operation failed:
Throw an exception. For the exception list, refer to: https://cloud.baidu.com/doc/VPC/s/sjwvyuhe7
Code example
For specific code examples, refer to example_delete_enterprise_security_group.py
Update enterprise security group rules
Function declaration
1def update_enterprise_security_group_rule(self, enterprise_security_group_rule_id, remark=None,
2 protocol=None, portrange=None, source_ip=None,
3 dest_ip=None, action=None, local_ip=None,
4 priority=None, source_portrange=None,
5 config=None):
Parameter meaning
Refer to the OpenAPI documentation: https://cloud.baidu.com/doc/VPC/s/Hkmd2fk5t
Response value
Operation succeeded:
1{}
Operation failed:
Throw an exception. For the exception list, refer to: https://cloud.baidu.com/doc/VPC/s/sjwvyuhe7
Code example
For specific code examples, refer to example_update_enterprise_security_group_rule.py
