def detach_volume(ctx, **kwargs):
    '''Detaches a volume and deletes if expunge is requested.'''
    cloud_driver = get_cloud_driver(ctx)

    volume_id = ctx.target.instance.runtime_properties[CLOUDSTACK_ID_PROPERTY]
    volume = get_volume_by_id(cloud_driver, volume_id)

    if not volume:
        raise NonRecoverableError('Volume with id {0} not found'.format(
            volume_id))

    ctx.logger.info('Detaching volume {0}'.format(volume))
    cloud_driver.detach_volume(volume=volume)
def detach_volume(ctx, **kwargs):
    '''Detaches a volume and deletes if expunge is requested.'''
    cloud_driver = get_cloud_driver(ctx)

    volume_id = ctx.target.instance.runtime_properties[CLOUDSTACK_ID_PROPERTY]
    volume = get_volume_by_id(cloud_driver, volume_id)

    if not volume:
        raise NonRecoverableError('Volume with id {0} not found'.format(
            volume_id))

    ctx.logger.info('Detaching volume {0}'.format(volume))
    cloud_driver.detach_volume(volume=volume)
def attach_volume(ctx, **kwargs):
    """ Attach a volume to virtual machine.
    """

    cloud_driver = get_cloud_driver(ctx)

    server_id = ctx.source.instance.runtime_properties[CLOUDSTACK_ID_PROPERTY]
    volume_id = ctx.target.instance.runtime_properties.get(
        CLOUDSTACK_ID_PROPERTY, None)

    server = get_vm_by_id(ctx, cloud_driver, server_id)
    volume = get_volume_by_id(cloud_driver, volume_id)

    if server is None:
        raise NonRecoverableError(
            'Server with id {0} not found'.format(server_id))

    if volume is None:
        raise NonRecoverableError(
            'Volume with id {0} not found'.format(volume_id))

    cloud_driver.attach_volume(node=server, volume=volume)
def attach_volume(ctx, **kwargs):
    """ Attach a volume to virtual machine.
    """

    cloud_driver = get_cloud_driver(ctx)

    server_id = ctx.source.instance.runtime_properties[CLOUDSTACK_ID_PROPERTY]
    volume_id = ctx.target.instance.runtime_properties.get(
        CLOUDSTACK_ID_PROPERTY, None)

    server = get_vm_by_id(ctx, cloud_driver, server_id)
    volume = get_volume_by_id(cloud_driver, volume_id)

    if server is None:
        raise NonRecoverableError('Server with id {0} not found'
                                  .format(server_id))

    if volume is None:
        raise NonRecoverableError('Volume with id {0} not found'
                                  .format(volume_id))

    cloud_driver.attach_volume(node=server,
                               volume=volume)