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
4
5if __name__ == '__main__':
6# Set your AK, SK, and the region to be accessed
7 HOST = b'http://bcc.bj.baidubce.com'
8 AK = b'ak'
9 SK = b'sk'
10# Set default configuration
11 config = BceClientConfiguration(credentials=BceCredentials(AK, SK),
12 endpoint=HOST)
13# Create a BCC client
14 client = bcc_client.BccClient(config)
15
16
17# Tag list
18 tags = [bcc_model.TagModel("example", "createCds")]
19
20# Automatic snapshot policy
21 auto_snapshot_policy = bcc_model.AutoSnapshotPolicyModel(name="aspExample", # Automatic snapshot policy name
22 timePoints=[1, 2, 3], # Automatic snapshot creation time points, a list of parameters from 0 to 23
23 repeatWeekdays=[1, 5], # Automatic snapshot recurrence days, a list of parameters from 0 to 6
24 retentionDays=30) # Automatic snapshot retention time in days. -1: Permanent retention 1~65536: Specify the number of days for retention
25
26 resp = client.create_volume_with_cds_size(cds_size_in_gb=5,
27 charge_type='Postpaid',
28 zone_name='cn-bj-a', # Availability zone name
29 instance_id='', # ID of instance to be attached after creation
30 encrypt_key='c6c0c0b3-****-****-****-ca6d5751504f', # Encrypted key ID
31 name='test-name', # Disk name
32 description='desc', # Disk description
33 renew_time_unit='month', # Unit of renewal duration, support: month/year
34 renew_time=2, # Renewal duration,
35 relation_tag=True, # Whether to uniformly apply tags to associated snapshot and snapshot chain
36 auto_snapshot_policy=auto_snapshot_policy, # Automatic snapshot policy
37 tags=tags) # Bind tag
38 print (resp)
For the range of capacity configurable for each type of disk, see Disk Performance.
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
4
5if __name__ == '__main__':
6# Set your AK, SK, and the region to be accessed
7 HOST = b'http://bcc.bj.baidubce.com'
8 AK = b'ak'
9 SK = b'sk'
10# Set default configuration
11 config = BceClientConfiguration(credentials=BceCredentials(AK, SK),
12 endpoint=HOST)
13# Create a BCC client
14 client = bcc_client.BccClient(config)
15
16
17 resp = client.create_volume_with_snapshot_id(snapshot_id="s-er2Q****", # Snapshot ID
18 charge_type='Postpaid', # Payment method
19 cluster_id="DC-jh5q****", # Dedicated cluster ID
20 purchase_count=2) # Purchase count
21 print (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
4
5if __name__ == '__main__':
6# Set your AK, SK, and the region to be accessed
7 HOST = b'http://bcc.bj.baidubce.com'
8 AK = b'ak'
9 SK = b'sk'
10# Set default configuration
11 config = BceClientConfiguration(credentials=BceCredentials(AK, SK),
12 endpoint=HOST)
13# Create a BCC client
14 client = bcc_client.BccClient(config)
15
16 resp = client.list_volumes(instance_id="i-cVh8****", # Instance ID to which the disk is attached
17 zone_name="cn-bj-c", # Availability zone
18 marker="v-3uIJ****", # Starting volume ID
19 max_keys=10, # Maximum number of returned lists
20 cluster_id="DC-rWKx****") # CDS cluster ID
21 print (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
4
5if __name__ == '__main__':
6# Set your AK, SK, and the region to be accessed
7 HOST = b'http://bcc.bj.baidubce.com'
8 AK = b'ak'
9 SK = b'sk'
10# Set default configuration
11 config = BceClientConfiguration(credentials=BceCredentials(AK, SK),
12 endpoint=HOST)
13# Create a BCC client
14 client = bcc_client.BccClient(config)
15
16 resp = client.get_volume(volume_id='v-3uIJ****') # Specified disk ID
17 print (resp)
Attach a CDS disk
Use the following code to attach the disk to the specified BCC instance. Keep in mind that CDS disks can only be attached to BCC instances within the same availability zone.
Attachment is only allowed when the disk status is Available and 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 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 can only be performed on disk 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
15# Set ID of disk to be detached
16 volume_id = 'v-***'
17# Set ID of instance to be attached
18 instance_id = 'i-***'
19 resp = client.detach_volume(volume_id, instance_id)
20 print(resp)
Release a CDS disk
The following 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 is only permitted when the disk status is "Available," "Expired," or "Error."
def release_volume(self):
Plain Text1 #Set the ID of disk to be released 2 volume_id = 'your-choose-volume-id' 3 4 self.assertEqual( 5 6 type(self.client.release_volume(volume_id)), 7 8 baidubce.bce_response.BceResponse)
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."
- For the range of capacity configurable for each type of disk, see Disk Performance.
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 expand the disk, changing both disk type and size
20 response = client.resize_volume(volume_id=volume_id, # Disk ID
21 new_cds_size=50, # Disk size after expansion, in GB
22 new_volume_type="cloud_hp1") # Disk type after expansion. For possible values, refer to https://cloud.baidu.com/doc/BCC/s/6jwvyo0q2#storagetype
23 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 system disk data generated since this snapshot was created will be completely lost and irrecoverable:
from baidubce.auth.bce_credentials import BceCredentials from baidubce.bce_client_configuration import BceClientConfiguration from baidubce.services.bcc import bcc_client, bcc_model
if name == 'main':
Set your AK, SK, and the region to be accessed
Plain Text1 HOST = b'http://bcc.bj.baidubce.com' 2 AK = b'ak' 3 SK = b'sk'Set default configuration
Plain Text1 config = BceClientConfiguration(credentials=BceCredentials(AK, SK), 2 endpoint=HOST)Create a BCC client
Plain Text1 client = bcc_client.BccClient(config) 2 volume_id = 'v-vG0z****'Snapshot ID used to roll back the specified disk, which must be a snapshot created by the volumeId
Plain Text1 snapshot_id = 's-HkbJ****' 2 resp = client.rollback_volume(volume_id, snapshot_id) 3 print (resp)
Renew a disk
Renew a disk by executing the following code.
Renewal operations cannot be performed 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
4
5if __name__ == '__main__':
6# Set your AK, SK, and the region to be accessed
7 HOST = b'http://bcc.bj.baidubce.com'
8 AK = b'ak'
9 SK = b'sk'
10# Set default configuration
11 config = BceClientConfiguration(credentials=BceCredentials(AK, SK),
12 endpoint=HOST)
13# Create a BCC client
14 client = bcc_client.BccClient(config)
15# ID of disk to be renewed
16 volume_id = 'v-PGE6****'
17# Set renewal duration, currently only supporting month as the unit
18 billing = bcc_model.Billing('Prepaid', 2)
19 resp = client.purchase_reserved_volume(volume_id=volume_id, billing=billing)
20 print (resp)
Modify disk properties (new)
You can modify disk properties 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, bcc_model
4
5if __name__ == '__main__':
6# Set your AK, SK, and the region to be accessed
7 HOST = b'http://bcc.bj.baidubce.com'
8 AK = b'ak'
9 SK = b'sk'
10# Set default configuration
11 config = BceClientConfiguration(credentials=BceCredentials(AK, SK),
12 endpoint=HOST)
13# Create a BCC client
14 client = bcc_client.BccClient(config)
15# ID of disk to be modified
16 volume_id = 'v-vG0z****'
17# Set new disk name
18 cds_name = 'new-cds-name'
19 resp = client.modify_volume_Attribute(volume_id=volume_id, cds_name=cds_name)
20 print (resp)
Modify disk charge type (new)
You can change a pay-as-you-go (postpaid) disk to a subscription (prepaid) disk with the following code: # !/usr/bin/env python # coding=utf-8 from baidubce.services.bcc import bcc_client from baidubce.services.bcc import bcc_model from baidubce.auth.bce_credentials import BceCredentials from baidubce.bce_client_configuration import BceClientConfiguration
1if __name__ == '__main__':
2# Set your AK, SK, and the region to be accessed
3 HOST = b'http://bcc.bj.baidubce.com'
4 AK = b'ak'
5 SK = b'sk'
6# Set default configuration
7 config = BceClientConfiguration(credentials=BceCredentials(AK, SK),
8 endpoint=HOST)
9# Create a BCC client
10 client = bcc_client.BccClient(config)
11
12 volume_id = "v-***"
13 post_paid_billing = bcc_model.Billing(paymentTiming='Prepaid', # Prepay charge type. Specify paymentTiming to be Prepaid. Optional
14 reservationLength=3, # Prepaid duration in months with the value ranging from 1 to 9
15 reservationTimeUnit='Month') # Unit of prepaid duration, currently only supporting Month
16
17# Request to change the disk charge type from postpay to prepay
18 response = client.modify_volume_charge_type(volume_id=volume_id, # Disk ID
19 billing=post_paid_billing) # Charge type
20 print(response)
You can change the charge type of a subscription (prepaid) disk to a pay-as-you-go (postpaid) disk with the following code: # !/usr/bin/env python # coding=utf-8 from baidubce.services.bcc import bcc_client from baidubce.services.bcc import bcc_model from baidubce.auth.bce_credentials import BceCredentials from baidubce.bce_client_configuration import BceClientConfiguration
1if __name__ == '__main__':
2# Set your AK, SK, and the region to be accessed
3 HOST = b'http://bcc.bj.baidubce.com'
4 AK = b'ak'
5 SK = b'sk'
6# Set default configuration
7 config = BceClientConfiguration(credentials=BceCredentials(AK, SK),
8 endpoint=HOST)
9# Create a BCC client
10 client = bcc_client.BccClient(config)
11
12# Change the disk charge type from prepay to postpay
13 volume_id = "v-***"
14 post_paid_billing = bcc_model.Billing(paymentTiming='Postpaid', # Postpay charge type. Specify paymentTiming to be Postpaid. Optional
15 reservationLength=0) # For postpay, reservationLength must be set to 0
16
17# Request to change the disk charge type from prepay to postpay
18 response = client.modify_volume_charge_type(volume_id=volume_id, # Disk ID
19 billing=post_paid_billing) # Charge type
20 print(response)
Release CDS disk (new)
You can release a CDS disk with the following code
- This interface is used to release unattached CDS disks; system disks cannot be released
- This operation can only be performed when the disk status is Available, Expired, or Error; otherwise, a 409 error will be returned
- If the disk corresponding to the volumeId does not exist, a 404 error will be returned
- Control whether to release the associated automatic/manual snapshot or move it to 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(volume_id=volume_id, # Disk ID
21 auto_snapshot="on", # Whether to delete automatic snapshots associated with the disk
22 manual_snapshot="on", # Whether to delete manual snapshots associated with the disk
23 recycle="off") # Whether to move the disk to the recycle bin; if set to off, the disk will be released directly without entering the recycle bin
24 print(response)
Enable automatic renewal for disks
You can enable auto-renewal for disks with the following code:
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 to enable auto-renewal
19 response = client.auto_renew_cds_volume(volume_id=volume_id, # Disk ID
20 renew_time=3, # Renewal duration in months with the value ranging from 1 to 9
21 renew_time_unit="month") # Unit of renewal duration, currently only supporting month
22 print(response)
Disable automatic renewal for disks
You can disable auto-renewal for disks with the following code:
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 to disable auto-renewal
19 response = client.cancel_auto_renew_cds_volume(volume_id=volume_id) # Disk ID
20 print(response)
Query disk information in the availability zone
You can disk information in the availability zone with the following code:
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 azone_name = "cn-bj-c" # Availability zone name. When the passed azone_name is an empty string or an invalid azone, purchasable disk information for all availability zones will be returned.
18
19# Request to query purchasable disk information in the corresponding availability zone
20 response = client.get_available_disk_info(zone_name=azone_name)
21 print(response)
Bind a tag to a disk
You can bind a tag to a specified disk with the following code:
1# !/usr/bin/env python
2# coding=utf-8
3from baidubce.services.bcc import bcc_client
4from baidubce.services.bcc import bcc_model
5from baidubce.auth.bce_credentials import BceCredentials
6from baidubce.bce_client_configuration import BceClientConfiguration
7if __name__ == '__main__':
8# Set your AK, SK, and the region to be accessed
9 HOST = b'http://bcc.bj.baidubce.com'
10 AK = b'ak'
11 SK = b'sk'
12# Set default configuration
13 config = BceClientConfiguration(credentials=BceCredentials(AK, SK),
14 endpoint=HOST)
15# Create a BCC client
16 client = bcc_client.BccClient(config)
17
18 volume_id = "v-***" # Disk ID
19 tag1 = bcc_model.TagModel(tagKey='TagKey01', # Tag key
20 tagValue='TagValue01') # Tag value
21 tag2 = bcc_model.TagModel(tagKey='TagKey02', # Tag key
22 tagValue='TagValue02') # Tag value
23 tag_list = list() # Tag list
24 tag_list.append(tag1)
25 tag_list.append(tag2)
26
27# Request to bind a tag to the disk
28 response = client.tag_volume(volume_id=volume_id, # Disk ID
29 relation_tag=False, # Whether to bind the tag to the associated resource
30 tags=tag_list) # Tag list
31 print(response)
Unbind a tag from a disk
You can unbind a tag from a specified disk with the following code:
1# !/usr/bin/env python
2# coding=utf-8
3from baidubce.services.bcc import bcc_client
4from baidubce.services.bcc import bcc_model
5from baidubce.auth.bce_credentials import BceCredentials
6from baidubce.bce_client_configuration import BceClientConfiguration
7if __name__ == '__main__':
8# Set your AK, SK, and the region to be accessed
9 HOST = b'http://bcc.bj.baidubce.com'
10 AK = b'ak'
11 SK = b'sk'
12# Set default configuration
13 config = BceClientConfiguration(credentials=BceCredentials(AK, SK),
14 endpoint=HOST)
15# Create a BCC client
16 client = bcc_client.BccClient(config)
17
18 volume_id = "v-***" # Disk ID
19 tag1 = bcc_model.TagModel(tagKey='TagKey01', # Tag key
20 tagValue='TagValue01') # Tag value
21 tag2 = bcc_model.TagModel(tagKey='TagKey02', # Tag key
22 tagValue='TagValue02') # Tag value
23 tag_list = list() # Tag list
24 tag_list.append(tag1)
25 tag_list.append(tag2)
26
27# Request to unbind a tag from the disk
28 response = client.untag_volume(volume_id=volume_id, # Disk ID
29 relation_tag=False, # Whether to bind the tag to the associated resource
30 tags=tag_list) # Tag list
31 print(response)
