实例

创建实例

使用以下代码可以创建BCC实例,包括专属实例、普通型Ⅰ 型实例、普通型ⅠI 型实例、普通型ⅠII 型实例、GPU实例、计算优化I型实例、计算优化II型实例、存储优化I型实例:

def create_instance(self):

      #如果用户未指定client_token参数,用uuid4生成一个随机字符串给client_token

      generate_client_token = str(uuid.uuid4())

      client_token = generate_client_token()



      #输入你要创建instance使用的镜像ID

      image_id = 'your-choose-image-id'



      #设置你要创建的实例类型

      #各类型对应代码见下方『实例类型定义列表』

      instance_type = 'your-choose-instance-type'



      #选择付费方式:

      #付费方式为后付费

      post_paid_billing = bcc_model.Billing('Postpaid', 1)

      #付费方式为包年包月计费的预付费

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



      #创建cds_list所需参数:

      #云磁盘大小(普通云磁盘范围:5~32765,高性能云磁盘范围:5~32765,SSD云磁盘范围:50~32765

      cds_size_in_gb = 5



      #设置云磁盘类型

      #具体类型对应见代码段下方的链接

      storage_type = 'hp1'



      #根据CDS数据盘快照创建CDS盘数据时,输入用户选择的快照策略id

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



      #实例名称

      instance_name = 'your-choose-instance-name' + client_token



      #设置实例管理员密码(8-16位字符,英文,数字和符号必须同时存在,符号仅限!@#$%^*())

      admin_pass = 'your-admin-pass'



      #指定zone信息,默认为空,由系统自动选择,可通过调用查询可用区列表接口查询可用区列表

      #格式为:国家-区域-可用区,如'中国-北京-可用区A'就是'cn-bj-a'

      zone_name = 'country-region-zone'



      #指定subnet信息,为空时将使用默认子网

      subnet_id = 'your-choose-subnet-id'

      #指定security_group_id信息,为空时将使用默认安全组

      security_group_id = 'your-choose-security-group-id'



      #输入你选择的gpu_card

      #各gpu_card类型编码见下方『Gpu Type列表』

      gpuCard='your-choose-Gpu-Type-id'



      #输入你选择的fpga_card

      #目前fpga_card类型只有KU115

      fpgaCard='KU115'



      self.assertEqual(
            type(self.client.create_instance(1,# 设置创建BCC的核数

                                                               1,# 设置创建BCC的内存大小GB

                                                               image_id,# 设置创建BCC虚机使用的镜像

                                                               instance_type=instance_type,# 设置需要创建的BCC实例类型

                                                               billing=post_paid_billing,# 设置创建BCC的付费方式为后付费 

                                                               root_disk_size_in_gb=40,# 设置BCC系统盘大小

                                                               root_disk_storage_type='hp1',# 设置BCC系统盘系统盘介质,默认使用hp1型云磁盘,系统盘磁盘类型可参见StorageType

                                                               create_cds_list=bcc_model.CreateCdsModel,(cds_size_in_gb, storage_type, snap_shot_id)# 设置需要创建的CDS数据盘

                                                               network_capacity_in_mbps=1,# 设置创建BCC使用的网络带宽大小(必须在1~200之间)

                                                               purchase_count=1,# 批量创建BCC实例时使用,必须为大于0的整数

                                                               cardCount=1,# 创建实例所要携带的GPU卡或FPGA卡数量,仅在gpuCard或fpgaCard字段不为空时有效

                                                               name=instance_name,# 设置创建BCC的名称

                                                               admin_pass=admin_pass,# 设置实例管理员密码

                                                               zone_name=zone_name,# 设置创建BCC所在的zone

                                                               subnet_id=subnet_id,# 指定subnet信息,为空时将使用默认子网

                                                               security_group_id=security_group_id,# 指定securityGroup信息,为空时将使用默认安全组

                                                               gpuCard=gpuCard,#创建GPU实例时指定GPU类型

                                                               fpgaCard=fpgaCard,#创建FPGA实例时指定FPGA类型

                                                               client_token=client_token)),

            baidubce.bce_response.BceResponse)

类型编码请参考实例类型磁盘类型GPU类型

创建实例在专属服务器

使用以下代码可以在特定服务器中创建BCC实例:

def create_instance_from_dedicated_host(self):

      #如果用户未指定client_token参数,用uuid4生成一个随机字符串给client_token

      generate_client_token = str(uuid.uuid4())

      client_token = generate_client_token()

      #设置镜像id

      image_id = 'your-choose-image-id'

      #设置专属服务器id

      dedicated_host_id = 'your-choose-dedicated-host-id'

      #设置实例名

      instance_name = 'your-choose-instance-name' + client_token

      #设置实例管理员密码(8-16位字符,英文,数字和符号必须同时存在,符号仅限!@#$%^*())

      admin_pass = 'your-admin-pass'

      #指定subnet信息,为空时将使用默认子网

      subnet_id = 'your-choose-subnet-id'

      #指定security_group_id信息,为空时将使用默认安全组

      security_group_id = 'your-choose-security-group-id'

      #DCC专属服务器实例可以创建多块本地盘,需要指定磁盘类型及大小

      #其它类型只能用一块本地盘,使用默认类型,只需指定大小

      #设置创建本地磁盘大小

      ephemeral_disk = EphemeralDisk(6144)

      #设置本地盘类型为默认类型

      ephemeral_disks = [ephemeral_disk.__dict__, ephemeral_disk.__dict__]

      self.assertEqual(

            type(self.client.create_instance_from_dedicated_host(1, #设置创建BCC的CPU核数

                                                                                                  2, #设置创建BCC的内存大小GB

                                                                                                  image_id, #设置创建BCC的镜像

                                                                                                  dedicated_host_id, #设置创建BCC的专属服务器

                                                                                                  purchase_count=1,# 批量创建BCC实例时使用,必须为大于0的整数

                                                                                                  name=instance_name,# 设置创建BCC的名称

                                                                                                  admin_pass=admin_pass,# 设置实例管理员密码

                                                                                                  subnet_id=subnet_id,# 指定subnet信息,为空时将使用默认子网

                                                                                                  security_group_id=security_group_id,# 指定securityGroup信息,为空时将使用默认安全组

                                                                                                  client_token=client_token

                                                                                                  ephemeral_disks) #设置创建BCC的本地盘

            baidubce.bce_response.BceResponse)

查询实例列表

以下代码可以查询BCC虚机实例列表,支持通过内网ip、专属服务器id、可用区名称进行筛选:

def list_instances(self):

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

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

      #设置返回数据大小

      self.client.list_instances(max_keys=100)

      #通过internal Ip过滤虚机BCC列表

      self.client.list_instances(internal_ip='your-choose-internal-ip')

      #通过专属服务器DCC id过滤虚机BCC列表

      self.client.list_instances(dedicated_host_id='your-choose-dedicated-host-id')

      #通过zone name过滤虚机BCC列表

      self.client.list_instances(zone_name='country-region-zone')

      #执行查询虚机列表操作

      print self.client.list_instances()

查询实例详情

使用以下代码可以查询指定BCC虚机的详细信息:

def get_instance(self): 

      #设置你要操作的instance_id

      instance_id = 'your-choose-instance-id'

      self.assertEqual(

            type(self.client.get_instance(instance_id)), 

            baidubce.bce_response.BceResponse)

启动实例

使用以下代码可以启动指定BCC虚机实例:

def start_instance(self):

      #设置你要操作的instance_id

      instance_id = 'your-choose-instance-id'

      self.assertEqual(

            type(self.client.start_instance(instance_id)), 

            baidubce.bce_response.BceResponse)

停止实例

使用以下代码可以停止指定BCC虚机实例:

def stop_instance(self):

      #设置你要操作的instance_id
      instance_id = 'your-choose-instance-id'
      #设置关机是否计费,关机不计费为True,关机计费为False,默认为False。注意:只有白名单用户才可以实行关机不计费
      stop_with_no_charge = 'True'

      self.assertEqual(

            type(self.client.stop_instance(instance_id=instance_id,
                                           stopWithNoCharge=stop_with_no_charge,
                                           force_stop=force_stop)), 

            baidubce.bce_response.BceResponse)

重启实例

使用以下代码可以重启指定BCC虚机实例:

def reboot_instance(self):

      #设置你要操作的instance_id

      instance_id = 'your-choose-instance-id'

      self.assertEqual(

            type(self.client.reboot_instance(instance_id,

                                                               force_stop)),

            baidubce.bce_response.BceResponse)

修改实例密码

使用以下代码可以修改指定BCC虚机实例的管理员密码:

def modify_instance_password(self):

      #设置你要操作的instance_id

      instance_id = 'your-choose-instance-id'

      #设置新的密码

      admin_pass = 'your-new-admin-pass'

      self.assertEqual(

            type(self.client.modify_instance_password(instance_id,

                                                                                 admin_pass)), 

            baidubce.bce_response.BceResponse)

修改实例名称

使用以下代码可以修改指定BCC虚机实例的名称:

def modify_instance_attributes(self):

      #设置你要操作的instance_id

      instance_id = 'your-choose-instance-id'

      name = 'name_modify'    

      self.assertEqual(

            type(self.client.modify_instance_attributes(instance_id,

                                                                                name)), 

            baidubce.bce_response.BceResponse)

修改实例描述(新)

使用以下代码可以修改指定BCC虚机实例的描述:

def modify_instance_desc(self):

      #设置你要操作的instance_id

      instance_id = 'your-choose-instance-id'

      desc = 'your_new_description'    

      self.assertEqual(

            type(self.client.modify_instance_desc(instance_id,

                                                                        desc)), 

            baidubce.bce_response.BceResponse)

重装实例

使用以下代码可以使用镜像重建指定BCC虚机实例:

def rebuild_instance(self):

      #设置你要操作的instance_id

      instance_id = 'your-choose-instance-id'

      #设置使用的镜像id

      image_id = 'your-choose-image-id'

      #设置管理员密码

      admin_pass = 'your-new-admin-pass'

      self.assertEqual(

            type(self.client.rebuild_instance(instance_id,

                                                               image_id,

                                                               admin_pass)), 

            baidubce.bce_response.BceResponse)

释放实例

对于后付费Postpaid以及预付费Prepaid过期的BCC虚机实例,可以使用以下代码将其释放:

def release_instance(self):

      #设置你要操作的instance_id

      instance_id = 'your-choose-instance-id'

      self.assertEqual(

            type(self.client.release_instance(instance_id)), 

            baidubce.bce_response.BceResponse)

实例扩容

支持对BCC虚机的CPU以及内存进行扩容操作,具体可参考以下代码:

def resize_instance(self):

      #设置你要操作的instance_id

      instance_id = 'your-choose-instance-id'

      client_token = generate_client_token()

      self.assertEqual(

            type(self.client.resize_instance(instance_id,

                                                              1,#新CPU核数

                                                              2,#新内存大小

                                                              client_token)), 

            baidubce.bce_response.BceResponse)

将实例加入安全组

以下代码可以将指定BCC虚机实例加入到指定安全组内:

def bind_instance_to_security_group(self):

      #指定security_group_id信息

      security_group_id = 'your-choose-security-group-id'

      #设置你要操作的instance_id

      instance_id = 'your-choose-instance-id'

      self.assertEqual(

            type(self.client.bind_instance_to_security_group(instance_id,

                                                                                          security_group_id)), 

            baidubce.bce_response.BceResponse)

将放实例移出安全组

以下代码可以将指定实例移出指定安全组中:

def unbind_instance_from_security_group(self):

      #指定security_group_id信息

      security_group_id = 'your-choose-security-group-id'

      #设置你要操作的instance_id

      instance_id = 'your-choose-instance-id'

      self.assertEqual(

            type(self.client.unbind_instance_from_security_group(instance_id,

                                                                                                  security_group_id)), 

            baidubce.bce_response.BceResponse)

绑定实例到标签(新)

以下代码可以将指定BCC虚机实例绑定指定标签:

def bind_instance_to_tags(self):

      #输入标签信息,支持一次绑定多个标签

      instance_tag1 = bcc_model.TagModel(tagKey='TestKey01',



                                                                   tagValue='TestValue01')

      instance_tag2 = bcc_model.TagModel(tagKey='TestKey02',

                                                                   tagValue='TestValue02')

      instance_tag_list = []

      instance_tag_list.append(instance_tag1)

      instance_tag_list.append(instance_tag2)

      #设置你要操作的instance_id

      instance_id = 'your-choose-instance-id'

      self.assertEqual(

            type(self.client.bind_instance_to_tags(instance_id,

                                                                         tags=instance_tag_list)), 

            baidubce.bce_response.BceResponse)

解绑实例标签(新)

以下代码可以将指定BCC虚机实例解绑指定标签:

def unbind_instance_to_tags(self):

      #输入标签信息,支持一次解绑多个标签

      instance_tag1 = bcc_model.TagModel(tagKey='TestKey01',



                                                                   tagValue='TestValue01')

      instance_tag2 = bcc_model.TagModel(tagKey='TestKey02',

                                                                   tagValue='TestValue02')

      instance_tag_list = []

      instance_tag_list.append(instance_tag1)

      instance_tag_list.append(instance_tag2)

      #设置你要操作的instance_id

      instance_id = 'your-choose-instance-id'

      self.assertEqual(

            type(self.client.unbind_instance_to_tags(instance_id,

                                                                             tags=instance_tag_list)), 

            baidubce.bce_response.BceResponse)

查询实例地址

使用以下代码可以查询指定BCC虚机实例的VNC URL:

def get_instance_vnc(self):

      #设置你要操作的instance_id

      instance_id = 'your-choose-instance-id'

      self.assertEqual(

            type(self.client.get_instance_vnc(instance_id)), 

            baidubce.bce_response.BceResponse)

实例续费

对BCC虚机的续费操作,可以延长过期时长,以下代码可以对磁盘进行续费:

def purchase_reserved_instance(self):

      #设置你要操作的instance_id

      instance_id = 'your-choose-instance-id'

      billing = pre_paid_billing

      client_token = generate_client_token()

      self.assertEqual(

            type(self.client.purchase_reserved_instance(instance_id,

                                                                                   billing,

                                                                                   client_token)), 

           baidubce.bce_response.BceResponse)