Disk
Create disk directly
The following code can be used to create disk:
1from baidubce.auth.bce_credentials import BceCredentials
2from baidubce.bce_client_configuration import BceClientConfiguration
3from baidubce.services.bcc import bcc_client, bcc_model
4if __name__ == '__main__':
5# Set your AK, SK, and the region to be accessed
6 HOST = b'http://bcc.bj.baidubce.com'
7 AK = b'ak'
8 SK = b'sk'
9# Set default configuration
10 config = BceClientConfiguration(credentials=BceCredentials(AK, SK),
11 endpoint=HOST)
12# Create a BCC client
13 client = bcc_client.BccClient(config)
14# Tag list
15 tags = [bcc_model.TagModel("example", "createCds")]
16# Automatic snapshot policy
17 auto_snapshot_policy = bcc_model.AutoSnapshotPolicyModel(name="aspExample", # Automatic snapshot policy name
18 timePoints=[1, 2, 3], # Automatic snapshot creation time points, a list of parameters from 0 to 23
19 repeatWeekdays=[1, 5], # Automatic snapshot recurrence days, a list of parameters from 0 to 6
20 retentionDays=30) # Automatic snapshot retention time in days. -1: Permanent retention 1~65536: Specify the number of days for retention
21 resp = client.create_volume_with_cds_size(cds_size_in_gb=5,
22
23 charge_type='Postpaid', # Charge type
24 zone_name='cn-bj-a', # Availability zone name
25 instance_id='', # ID of instance to be attached after creation
26 encrypt_key='c6c0c0b3-****-****-****-ca6d5751504f', # Encrypted key ID
27 name='test-name', # Disk name
28 description='desc', # Disk description
29 renew_time_unit='month', # Unit of renewal duration, support: month/year
30 renew_time=2, # Renewal duration,
31 relation_tag=True, # Whether to uniformly apply tags to associated snapshot and snapshot chain
32 auto_snapshot_policy=auto_snapshot_policy, # Automatic snapshot policy
33 tags=tags) # Bind tag
34 print (resp)
Create CDS disk from snapshot
The following code can be used to create disk from snapshot:
1from baidubce.auth.bce_credentials import BceCredentials
2from baidubce.bce_client_configuration import BceClientConfiguration
3from baidubce.services.bcc import bcc_client, bcc_model
4if __name__ == '__main__':
5# Set your AK, SK, and the region to be accessed
6HOST = b'http://bcc.bj.baidubce.com'
7AK = b'ak'
8SK = b'sk'
9# Set default configuration
10config = BceClientConfiguration(credentials=BceCredentials(AK, SK),
11 endpoint=HOST)
12# Create a BCC client
13client = bcc_client.BccClient(config)
14 resp = client.create_volume_with_snapshot_id(snapshot_id="s-er2Q****", # Snapshot ID
15 charge_type='Postpaid', # Charge type
16 cluster_id="DC-jh5q****", # Dedicated cluster ID
17 purchase_count=2) # Purchase count
18print (resp)
Query the disk list
Query all disk lists with the following code:
1from baidubce.auth.bce_credentials import BceCredentials
2from baidubce.bce_client_configuration import BceClientConfiguration
3from baidubce.services.bcc import bcc_client
4if __name__ == '__main__':
5# Set your AK, SK, and the region to be accessed
6HOST = b'http://bcc.bj.baidubce.com'
7AK = b'ak'
8SK = b'sk'
9# Set default configuration
10config = BceClientConfiguration(credentials=BceCredentials(AK, SK),
11 endpoint=HOST)
12# Create a BCC client
13client = bcc_client.BccClient(config)
14 resp = client.list_volumes(instance_id="i-cVh8****", # Instance ID to which the disk is attached
15 zone_name="cn-bj-c", # Availability zone
16 marker="v-3uIJ****", # Starting volume ID
17 max_keys=10, # Maximum number of returned lists
18 cluster_id="DC-rWKx****") # CDS cluster ID
19print (resp)
Query disk details
The detailed information of a single disk can be queried by disk ID with the following code:
1from baidubce.auth.bce_credentials import BceCredentials
2from baidubce.bce_client_configuration import BceClientConfiguration
3from baidubce.services.bcc import bcc_client
4if __name__ == '__main__':
5# Set your AK, SK, and the region to be accessed
6 HOST = b'http://bcc.bj.baidubce.com'
7 AK = b'ak'
8 SK = b'sk'
9# Set default configuration
10 config = BceClientConfiguration(credentials=BceCredentials(AK, SK),
11 endpoint=HOST)
12# Create a BCC client
13 client = bcc_client.BccClient(config)
14 resp = client.get_volume(volume_id='v-3uIJ****') # Specified disk ID
15 print (resp)
Attach a CDS disk
Attach the disk to the specified BCC instance using the following code. Note that CDS disks must remain within the same availability zone as the BCC instance.
Disks can only be attached when their status is "Available" and the instance status is either "Running" or "Stopped."
1from baidubce.auth.bce_credentials import BceCredentials
2from baidubce.bce_client_configuration import BceClientConfiguration
3from baidubce.services.bcc import bcc_client, bcc_model
4if __name__ == '__main__':
5# Set your AK, SK, and the region to be accessed
6 HOST = 'http://bcc.bj.baidubce.com'
7 AK = 'ak'
8 SK = 'sk'
9# Set default configuration
10 config = BceClientConfiguration(credentials=BceCredentials(AK, SK),
11 endpoint=HOST)
12# Create a BCC client
13 client = bcc_client.BccClient(config)
14# Set ID of disk to be attached
15 volume_id = 'v-***'
16# Set ID of instance to be attached
17 instance_id = 'i-***'
18 resp = client.attach_volume(volume_id, instance_id)
19 print(resp)
Detach a CDS disk
Use the following code to detach the disk from the specified instance.
This operation is only applicable to disks when the instance status is "Running" or "Stopped.\
1from baidubce.auth.bce_credentials import BceCredentials
2from baidubce.bce_client_configuration import BceClientConfiguration
3from baidubce.services.bcc import bcc_client, bcc_model
4if __name__ == '__main__':
5# Set your AK, SK, and the region to be accessed
6 HOST = 'http://bcc.bj.baidubce.com'
7 AK = 'ak'
8 SK = 'sk'
9# Set default configuration
10 config = BceClientConfiguration(credentials=BceCredentials(AK, SK),
11 endpoint=HOST)
12# Create a BCC client
13 client = bcc_client.BccClient(config)
14# Set ID of disk to be detached
15 volume_id = 'v-***'
16# Set ID of instance to be attached
17 instance_id = 'i-***'
18 resp = client.detach_volume(volume_id, instance_id)
19 print(resp)
Release a CDS disk
This code is used to release unattached CDS disks; system disks cannot be released.
Disks cannot be recovered after they are released. By default, all snapshots will be retained, but they will no longer be associated with the disk.
This operation can only be performed when the disk status is Available, Expired, or Error:
1 def release_volume(self):
2 #Set the ID of disk to be released
3 volume_id = 'your-choose-volume-id'
4 self.assertEqual(
5 type(self.client.release_volume(volume_id)),
6 baidubce.bce_response.BceResponse)
Release CDS disk (new)
You can release a CDS disk with the following code:
- This API releases unattached CDS disks; system disks cannot be released.
- This operation can only be executed when the disk status is "Available," "Expired," or "Error." Otherwise, a 409 error will be returned.
- If no disk corresponding to the provided volumeId exists, a 404 error will be returned.
- Control whether to release the associated automatic or manual snapshot, or move it to the recycle bin.
1# !/usr/bin/env python
2# coding=utf-8
3from baidubce.services.bcc import bcc_client
4from baidubce.auth.bce_credentials import BceCredentials
5from baidubce.bce_client_configuration import BceClientConfiguration
6if __name__ == '__main__':
7# Set your AK, SK, and the region to be accessed
8 HOST = b'http://bcc.bj.baidubce.com'
9 AK = b'ak'
10 SK = b'sk'
11# Set default configuration
12 config = BceClientConfiguration(credentials=BceCredentials(AK, SK),
13 endpoint=HOST)
14# Create a BCC client
15 client = bcc_client.BccClient(config)
16
17 volume_id = "v-***"
18
19# Request to release CDS disk
20 response = client.release_volume_new(
21# Disk ID
22 volume_id=volume_id,
23# Whether to delete automatic snapshots associated with the disk
24 # auto_snapshot="on",
25# Whether to delete manual snapshots associated with the disk
26 # manual_snapshot="on",
27# Whether to move the disk to the recycle bin; if set to off, the disk will be released directly without entering the recycle bin
28 # recycle="off")
29 print(response)
Disk expansion
You can expand disk with the following code:
- Disks can only be expanded; reducing their size is not allowed.
- Only disks in the "Available" status can undergo an expansion operation.
- This operation is only permitted when the disk status is "Available," "Expired," or "Error."
1# !/usr/bin/env python
2# coding=utf-8
3from baidubce.services.bcc import bcc_client
4from baidubce.auth.bce_credentials import BceCredentials
5from baidubce.bce_client_configuration import BceClientConfiguration
6if __name__ == '__main__':
7# Set your AK, SK, and the region to be accessed
8 HOST = b'http://bcc.bj.baidubce.com'
9 AK = b'ak'
10 SK = b'sk'
11# Set default configuration
12 config = BceClientConfiguration(credentials=BceCredentials(AK, SK),
13 endpoint=HOST)
14# Create a BCC client
15 client = bcc_client.BccClient(config)
16
17 volume_id = "v-***"
18# Request disk expansion
19 response = client.resize_volume(
20# Disk ID
21 volume_id=volume_id,
22# Disk size after expansion, in GB
23 # new_cds_size=50,
24# Disk type after expansion. For possible values, refer to https://cloud.baidu.com/doc/BCC/s/6jwvyo0q2#storagetype
25 # new_volume_type="cloud_hp1")
26 print(response)
Roll back disk data
You can use a snapshot of the specified disk itself to roll back disk data with the following code:
- The disk must be in an Available state to proceed with this operation.
- The snapshot ID specified must correspond to a snapshot created from the volume ID.
- When rolling back a system disk, ensure the instance status is either Running or Stopped before initiating this operation.
- When rolling back a system disk snapshot, all data generated on the system disk after the snapshot was created will be completely lost and cannot be recovered.
1from baidubce.auth.bce_credentials import BceCredentials
2from baidubce.bce_client_configuration import BceClientConfiguration
3from baidubce.services.bcc import bcc_client, bcc_model
4if __name__ == '__main__':
5# Set your AK, SK, and the region to be accessed
6HOST = b'http://bcc.bj.baidubce.com'
7AK = b'ak'
8SK = b'sk'
9# Set default configuration
10config = BceClientConfiguration(credentials=BceCredentials(AK, SK),
11 endpoint=HOST)
12# Create a BCC client
13client = bcc_client.BccClient(config)
14volume_id = 'v-vG0z****'
15# Snapshot ID used to roll back the specified disk, which must be a snapshot created by the volumeId
16snapshot_id = 's-HkbJ****'
17resp = client.rollback_volume(volume_id, snapshot_id)
18print (resp)
Renew a disk
You can renew a disk with the following code:
Renewal operations cannot be carried out during disk scaling.
1from baidubce.auth.bce_credentials import BceCredentials
2from baidubce.bce_client_configuration import BceClientConfiguration
3from baidubce.services.bcc import bcc_client, bcc_model
4if __name__ == '__main__':
5# Set your AK, SK, and the region to be accessed
6 HOST = b'http://bcc.bj.baidubce.com'
7 AK = b'ak'
8 SK = b'sk'
9# Set default configuration
10 config = BceClientConfiguration(credentials=BceCredentials(AK, SK),
11 endpoint=HOST)
12# Create a BCC client
13 client = bcc_client.BccClient(config)
14# ID of disk to be renewed
15 volume_id = 'v-PGE6****'
16# Set renewal duration, currently only supporting month as the unit
17 billing = bcc_model.Billing('Prepaid', 2)
18 resp = client.purchase_reserved_volume(volume_id=volume_id, billing=billing)
19 print (resp)
