# Path: cloudify_resources/constants.py
# RESOURCES_LIST_PROPERTY = 'resource_config'
# 
# FREE_RESOURCES_LIST_PROPERTY = 'free_resources'
# 
# RESERVATIONS_PROPERTY = 'reservations'
# 
# SINGLE_RESERVATION_PROPERTY = 'reservation'


from cloudify.decorators import operation
from cloudify.exceptions import NonRecoverableError
from .constants import (
    RESOURCES_LIST_PROPERTY,
    FREE_RESOURCES_LIST_PROPERTY,
    RESERVATIONS_PROPERTY,
    SINGLE_RESERVATION_PROPERTY
)


@operation(resumable=True)
def delete_list_item(ctx, **kwargs):
    ctx.logger.debug('Removing list item: {}'.format(
        ctx.instance.runtime_properties.get(SINGLE_RESERVATION_PROPERTY)))
    ctx.instance.runtime_properties[SINGLE_RESERVATION_PROPERTY] = ''


@operation(resumable=True)
def delete_list(ctx, **kwargs):
    ctx.logger.debug('Removing resources list: {}'.format(
        ctx.instance.runtime_properties.get(RESOURCES_LIST_PROPERTY)))
    ctx.instance.runtime_properties[RESOURCES_LIST_PROPERTY] = []
    ctx.instance.runtime_properties[FREE_RESOURCES_LIST_PROPERTY] = []
    ctx.instance.runtime_properties[RESERVATIONS_PROPERTY] = {}


@operation(resumable=True)
def reserve_list_item(ctx, **kwargs):
    if not ctx.target.instance.runtime_properties.get(
            FREE_RESOURCES_LIST_PROPERTY, []):
        raise NonRecoverableError(
            'Reservation has failed, because there are no \
            available resources right now.')

    _refresh_source_and_target_runtime_props(ctx)

    free_resources = \
        ctx.target.instance.runtime_properties.get(

# Path: cloudify_resources/constants.py
# RESOURCES_LIST_PROPERTY = 'resource_config'
# 
# FREE_RESOURCES_LIST_PROPERTY = 'free_resources'
# 
# RESERVATIONS_PROPERTY = 'reservations'
# 
# SINGLE_RESERVATION_PROPERTY = 'reservation'


# Path: cloudify_resources/tasks.py
from cloudify.decorators import operation
from cloudify.exceptions import NonRecoverableError
from .constants import (
    RESOURCES_LIST_PROPERTY,
    FREE_RESOURCES_LIST_PROPERTY,
    RESERVATIONS_PROPERTY,
    SINGLE_RESERVATION_PROPERTY
)



@operation(resumable=True)
def delete_list_item(ctx, **kwargs):
    ctx.logger.debug('Removing list item: {}'.format(
        ctx.instance.runtime_properties.get(SINGLE_RESERVATION_PROPERTY)))
    ctx.instance.runtime_properties[SINGLE_RESERVATION_PROPERTY] = ''


@operation(resumable=True)
def delete_list(ctx, **kwargs):
    ctx.logger.debug('Removing resources list: {}'.format(
        ctx.instance.runtime_properties.get(RESOURCES_LIST_PROPERTY)))
    ctx.instance.runtime_properties[RESOURCES_LIST_PROPERTY] = []
    ctx.instance.runtime_properties[FREE_RESOURCES_LIST_PROPERTY] = []
    ctx.instance.runtime_properties[RESERVATIONS_PROPERTY] = {}


@operation(resumable=True)
def reserve_list_item(ctx, **kwargs):
    if not ctx.target.instance.runtime_properties.get(
            FREE_RESOURCES_LIST_PROPERTY, []):
        raise NonRecoverableError(
            'Reservation has failed, because there are no \
            available resources right now.')

    _refresh_source_and_target_runtime_props(ctx)

    free_resources = \
        ctx.target.instance.runtime_properties.get(

            FREE_RESOURCES_LIST_PROPERTY)