Exemplo n.º 1
0
 def detach_volume(self, volume_id, instance_id=None):
     if not instance_id:
         instance_id = self.get_related_vm().runtime_properties[EXTERNAL_ID]
     ctx.logger.info('Detaching volume {} from vm {}.'.format(volume_id, instance_id))
     VolumeHelper().detach_disks(instance_id, [volume_id])
     ctx.instance.runtime_properties['host_id'] = ''
     ctx.logger.info('Volume {} detached from instance {}.'.format(volume_id, instance_id))
Exemplo n.º 2
0
    def create(self, **kwargs):
        if self._is_create_system_disk():
            return
        volume_id = ctx.instance.runtime_properties.get(EXTERNAL_ID)
        if not volume_id:
            client_token = ctx.instance.runtime_properties.get('ClientToken', uuid.uuid4().hex)
            request_body = {
                "DiskType": self.resource_config.get('volume_type'),
                'DiskChargeType': self.resource_config.get('DiskChargeType', 'POSTPAID_BY_HOUR'),
                'Placement': self._prepare_placement(),
                'DiskName': self.resource_config.get('name') or ctx.instance.id,
                'DiskSize': self.resource_config.get('size'),
                'ClientToken': client_token
            }
            ctx.logger.info('Creating tencentcloud volume with parameters: {}.'.format(request_body))
            if not ctx.instance.runtime_properties.get('ClientToken'):
                ctx.instance.runtime_properties['ClientToken'] = client_token
            volume = VolumeHelper().create_disk(request_body)
            volume_id = volume['DiskId']
            ctx.instance.runtime_properties['disk_info'] = volume
            ctx.instance.runtime_properties['volume'] = {'size': volume['DiskSize']}
            ctx.instance.runtime_properties['volume_type'] = volume['DiskType']
            self.set_base_runtime_props(resource_id=volume_id, name=volume['DiskName'])

        self.attach_volume(volume_id)
        ctx.logger.info('Volume {} created.'.format(volume_id))
Exemplo n.º 3
0
    def attach_volume(self, volume_id, instance_id=None):
        vm_instance = self.get_related_vm()
        if not instance_id:
            instance_id = vm_instance.runtime_properties[EXTERNAL_ID]

        ctx.logger.info('Attaching volume {} to instance {}.'.format(volume_id, instance_id))
        VolumeHelper().attach_disks(instance_id, [volume_id])
        ctx.instance.runtime_properties['host_id'] = instance_id
        ctx.logger.info('Volume {} attached to instance {}.'.format(volume_id, instance_id))
Exemplo n.º 4
0
    def custom_resize(self, **kwargs):
        volume_id = ctx.instance.runtime_properties.get(EXTERNAL_ID)
        ctx.logger.info('Resizing volume {}.'.format(volume_id))

        size = validate_parameter('size', kwargs)
        VolumeHelper().resize_disk(volume_id, size)
        ctx.logger.info('Volume {} resized to {} GB.'.format(volume_id, size))
        ctx.instance.runtime_properties['volume'] = {'size': size}
        ctx.instance.update()
Exemplo n.º 5
0
 def create_snapshot(self, **kwargs):
     ctx.logger.info('Creating snapshot, parameters: {}.'.format(kwargs))
     name = validate_parameter('snapshotName', kwargs)
     instance_id = ctx.instance.runtime_properties.get(EXTERNAL_ID)
     snapshots = VolumeHelper().create_snapshot(name, instance_id)
     existed_snapshots = ctx.instance.runtime_properties.get(
         'snapshot_ids', [])
     ctx.instance.runtime_properties[
         'snapshot_ids'] = existed_snapshots + snapshots
     ctx.logger.info('Snapshot created.')
Exemplo n.º 6
0
 def delete(self, **kwargs):
     if ctx.node.properties['resource_config'].get('is_system_disk'):
         return
     volume_id = ctx.instance.runtime_properties.get(EXTERNAL_ID)
     if not volume_id:
         return
     ctx.logger.info('Deleting tencentcloud volume {}.'.format(volume_id))
     self.detach_volume(volume_id)
     VolumeHelper().delete_disks([volume_id])
     clear_runtime_properties()
     ctx.logger.info('Volume {} deleted.'.format(volume_id))
Exemplo n.º 7
0
    def delete_snapshots(self, snapshot_ids):
        if not snapshot_ids:
            return
        ctx.logger.info('Deleting snapshots: {}.'.format(snapshot_ids))
        VolumeHelper().delete_snapshots(snapshot_ids)
        saved_ids = ctx.instance.runtime_properties['snapshot_ids']
        remained_ids = [
            saved_id for saved_id in saved_ids if saved_id not in snapshot_ids
        ]
        ctx.instance.runtime_properties['snapshot_ids'] = remained_ids

        ctx.logger.info('Snapshots {} deleted.'.format(snapshot_ids))
Exemplo n.º 8
0
 def _is_create_system_disk(self):
     '''
     System_disk has been created where instance was created
     We Just need to update runtime_properties.
     '''
     if not convert2bool(ctx.node.properties['resource_config'].get('is_system_disk')):
         return False
     instance_id = ctx.instance.relationships[0].target.instance.runtime_properties.get('external_id')
     volume_info = json.loads(VolumeHelper().describe_system_disk(instance_id).to_json_string())
     volume_id = volume_info['DiskId']
     ctx.instance.runtime_properties['disk_info'] = volume_info
     ctx.instance.runtime_properties['volume'] = {'size': volume_info['DiskSize']}
     ctx.instance.runtime_properties['volume_type'] = volume_info['DiskType']
     ctx.instance.runtime_properties['host_id'] = instance_id
     self.set_base_runtime_props(resource_id=volume_id, name=volume_info['DiskName'])
     return True
Exemplo n.º 9
0
 def restore_snapshot(self, **kwargs):
     ctx.logger.info('Applying snapshot, parameters: {}.'.format(kwargs))
     name = validate_parameter('snapshotName', kwargs)
     instance_id = ctx.instance.runtime_properties.get(EXTERNAL_ID)
     VolumeHelper().apply_snapshot(name, instance_id)