磁盘

直接创建磁盘

使用以下代码可以创建磁盘:

def create_volume_with_cds_size(self):

      client_token = generate_client_token()

      #设置CDS磁盘容量(GB)必须为大于0的整数,范围0~5120GB

      cds_size_in_gb = 5

      #设置付费方式

      billing = pre_paid_billing

      #批量创建的CDS磁盘的个数,必须为大于0的整数,单次创建不能超过5个。可选参数,缺省为1

      purchase_cpunt = 1

      #设置cds磁盘存储类别

      #std1:普通型

      #hp1:高性能型

      #cloud_hp1:SSD型

      #local:本地盘

      #sata:sata磁盘,创建DCC字实例专用

      #ssd:ssd磁盘,创建DCC子实例专用

      storage_type = 'hp1'

      #指定区域信息,默认为空由系统自动选择

      zone_name = 'cn-bj-a'

      self.assertEqual(

            type(self.client.create_volume_with_cds_size(cds_size_in_gb,

                                                                                     billing=billing,

                                                                                     purchase_count=purchase_count,

                                                                                     storage_type=storage_type,

                                                                                     zone_name=zone_name,

                                                                                     client_token=client_token),

            baidubce.bce_response.BceResponse)

从快照创建CDS磁盘

使用以下代码可以从快照创建磁盘:

def create_volume_with_snapshot_id(self):

      client_token = generate_client_token()

      #设置CDS磁盘容量(GB)必须为大于0的整数,范围0~5120GB

      snapshot_id = 'your-choose-snap-shot-id'

      #设置付费方式

      billing = pre_paid_billing

      #批量创建的CDS磁盘的个数,必须为大于0的整数,单次创建不能超过5个。可选参数,缺省为1

      purchase_cpunt = 1

      #设置cds磁盘存储类别

      #std1:普通型

      #hp1:高性能型

      #cloud_hp1:SSD型

      #local:本地盘

      #sata:sata磁盘,创建DCC字实例专用

      #ssd:ssd磁盘,创建DCC子实例专用

      storage_type = 'hp1'

      #指定区域信息,默认为空由系统自动选择

      zone_name = 'cn-bj-a'

      self.assertEqual(

            type(self.client.create_volume_with_snapshot_id(snapshot_id,

                                                                                           billing=billing,

                                                                                           purchase_count=purchase_count,

                                                                                           storage_type=storage_type,

                                                                                           zone_name=zone_name,

                                                                                           client_token=client_token),

            baidubce.bce_response.BceResponse)

查询磁盘列表

使用以下代码查询所有的磁盘列表:

def list_volumes(self):

      #查询某一实例挂载的磁盘列表

      self.client.list_volumes(instance_id = 'your-choose-instance-id')

      #查询某一区域下的磁盘列表

      self.client.list_volumes(zone_name = 'your-choose-zone-name')

      #批量获取列表的查询起始位置,是一个由系统产生的字符串

      self.client.list_volumes(marker = 'your-marker')

      #设置每页包含的最大数量,默认为1000

      self.client.list_volumes(max_keys = 100)

      print self.client.list_instances()

查询磁盘详情

使用以下代码可以根据磁盘ID查询单个磁盘的详细信息:

def get_volume(self):

      volume_id = 'your-choose-volume-id'

      self.assertEqual(

            type(self.client.get_volume(volume_id)), 

            baidubce.bce_response.BceResponse)

挂载CDS磁盘

使用以下代码将磁盘挂载在指定的虚机实例下,注意cds磁盘需要挂载在与其处在相同zone下的虚机实例上

只有磁盘状态为 Available 且实例状态为 Running 或 Stopped 时才允许挂载:

def attach_volume(self):

      #设置要挂载的磁盘ID

      volume_id = 'your-choose-volume-id'

      #设置要挂载的实例ID

      instance_id = 'your-choose-instance-id'  

      self.assertEqual(

            type(self.client.attach_volume(volume_id, 

                                                            instance_id)), 

            baidubce.bce_response.BceResponse)

卸载CDS磁盘

将指定的磁盘从实例中卸载

只有实例状态为 Running 或 Stopped 时,磁盘才可以执行此操作:

def detach_volume(self):

      #设置要卸载的磁盘ID

      volume_id = 'your-choose-volume-id'

      #设置所挂载的实例ID

      instance_id = 'your-choose-instance-id'  

      self.assertEqual(

            type(self.client.detach_volume(volume_id, 

                                                             instance_id)), 

            baidubce.bce_response.BceResponse)

释放CDS磁盘

用于释放未挂载的CDS磁盘,系统盘不能释放

磁盘释放后不可恢复。默认情况下,该磁盘的所有快照将保留,但会删除与磁盘的关联关系

只有磁盘状态为 Available 或 Expired 或 Error 时才可以执行此操作:

def release_volume(self):

      #设置要释放的磁盘ID

      volume_id = 'your-choose-volume-id'

      self.assertEqual(

            type(self.client.release_volume(volume_id)), 

            baidubce.bce_response.BceResponse)

磁盘扩容

磁盘只能进行扩容操作

只有Available状态的磁盘,才能进行扩容操作

只有磁盘状态为 Available 或 Expired 或 Error 时才可以执行此操作:

def resize_volume(self):

      #设置被扩容磁盘ID

      volume_id = 'your-choose-volume-id'

      #设置新的CDS磁盘容量,必须为大于0的整数,单位为GB,范围0~5120GB

      resize_cds_size_in_gb = 10

      client_token = generate_client_token()   

      self.assertEqual(

            type(self.client.resize_volume(volume_id,

                                                            resize_cds_size_in_gb,

                                                            client_token)), 

            baidubce.bce_response.BceResponse)

回滚磁盘

用指定磁盘自身的快照回滚磁盘内容

磁盘状态必须为 Available 才可以执行此操作

指定 snapshotId 必须是由 volumeId 创建的快照

若是回滚系统盘,实例状态必须为 Running 或 Stopped 才可以执行此操作

回滚系统盘快照,自本次快照以来的系统盘数据将全部丢失,不可恢复:

def rollback_volume(self): 

      #待回滚的磁盘ID

      #待系统盘则为实例ID

      volume_id = 'your-choose-volume-id'

      #用于回滚指定磁盘的快照ID,必须为 volumeId 所创建的快照

      snapshot_id = 'your-choose-snapshot-id'

      self.assertEqual(

            type(self.client.rollback_volume(volume_id,

                                                               snapshot_id)), 

            baidubce.bce_response.BceResponse)

磁盘续费

用以下代码可以实现磁盘续费

磁盘扩缩容期间不能进行续费操作:

def purchase_reserved_volume(self):

      #待续费磁盘ID

      volume_id = 'your-choose-volume-id'

      #设置付费方式

      billing = pre_paid_billing

      client_token = generate_client_token()   

      self.assertEqual(

            type(self.client.purchase_reserved_volume(volume_id,

                                                                                  billing,

                                                                                  client_token)), 

            baidubce.bce_response.BceResponse)

修改磁盘属性(新)

用以下代码可以修改磁盘属性:

def modify_volume_attribute(self):

      #待修改磁盘ID

      volume_id = 'your-choose-volume-id'

      #设置磁盘新名称

      cdsName = 'your-new-cds-name'

      self.assertEqual(

            type(self.client.modify_volume_Attribute(volume_id=volume_id,

                                                                             cdsName=cdsName)), 

            baidubce.bce_response.BceResponse)

修改磁盘计费方式(新)

用以下代码可以修改磁盘计费方式:

def modify_volume_charge_type(self):

      #待修改磁盘ID

      volume_id = 'your-choose-volume-id'

      #设置磁盘新付费方式,变更为预付费需指定付费时长

      pre_paid_billing = bcc_model.Billing('Prepaid', 2)

      billing = pre_paid_billing

      self.assertEqual(

            type(self.client.modify_volume_charge_type(volume_id=volume_id,

                                                                                   billing=billing)), 

            baidubce.bce_response.BceResponse)