Exemplo n.º 1
0
def add_resource(resource_dict):
    name = resource_dict.get('name')
    service_type = resource_dict.get('service_type')
    service_origin = resource_dict.get('service_origin')
    if not name or not service_type or not service_origin:
        raise RegisterException("Malformed resource dict.")

    try:
        service = Service.objects.get(name=service_origin)
    except Service.DoesNotExist:
        m = "There is no service %s." % service_origin
        raise RegisterException(m)

    try:
        r = Resource.objects.select_for_update().get(name=name)
        exists = True
        if r.service_type != service_type and \
                different_component(service, r):
            m = ("There already exists a resource named %s with service "
                 "type %s." % (name, r.service_type))
            raise RegisterException(m)
        if r.service_origin != service_origin and \
                different_component(service, r):
            m = ("There already exists a resource named %s registered for "
                 "service %s." % (name, r.service_origin))
            raise RegisterException(m)
        r.service_origin = service_origin
        r.service_type = service_type
    except Resource.DoesNotExist:
        r = Resource(name=name,
                     uplimit=units.PRACTICALLY_INFINITE,
                     service_type=service_type,
                     service_origin=service_origin)
        exists = False
        for field in config_fields:
            value = resource_dict.get(field)
            if value is not None:
                setattr(r, field, value)

        r.project_default = 0 if r.api_visible else units.PRACTICALLY_INFINITE

    for field in main_fields:
        value = resource_dict.get(field)
        if value is not None:
            setattr(r, field, value)

    if r.ui_visible and not r.api_visible:
        m = "Flag 'ui_visible' should entail 'api_visible'."
        raise RegisterException(m)

    r.save()
    if not exists:
        quotas.qh_sync_new_resource(r)

    if exists:
        logger.info("Updated resource %s." % (name))
    else:
        logger.info("Added resource %s." % (name))
    return r, exists
Exemplo n.º 2
0
def add_resource(resource_dict):
    name = resource_dict.get('name')
    service_type = resource_dict.get('service_type')
    service_origin = resource_dict.get('service_origin')
    if not name or not service_type or not service_origin:
        raise RegisterException("Malformed resource dict.")

    try:
        service = Service.objects.get(name=service_origin)
    except Service.DoesNotExist:
        m = "There is no service %s." % service_origin
        raise RegisterException(m)

    try:
        r = Resource.objects.get_for_update(name=name)
        exists = True
        if r.service_type != service_type:
            m = ("There already exists a resource named %s with service "
                 "type %s." % (name, r.service_type))
            raise RegisterException(m)
        if r.service_origin != service_origin:
            m = ("There already exists a resource named %s registered for "
                 "service %s." % (name, r.service_origin))
            raise RegisterException(m)

    except Resource.DoesNotExist:
        r = Resource(name=name,
                     uplimit=0,
                     service_type=service_type,
                     service_origin=service_origin)
        exists = False

    for field in resource_fields:
        value = resource_dict.get(field)
        if value is not None:
            setattr(r, field, value)

    r.save()
    if not exists:
        qh_sync_new_resource(r, 0)

    if exists:
        logger.info("Updated resource %s." % (name))
    else:
        logger.info("Added resource %s." % (name))
    return r, exists
Exemplo n.º 3
0
def add_resource(resource_dict):
    name = resource_dict.get('name')
    service_type = resource_dict.get('service_type')
    service_origin = resource_dict.get('service_origin')
    if not name or not service_type or not service_origin:
        raise RegisterException("Malformed resource dict.")

    try:
        service = Service.objects.get(name=service_origin)
    except Service.DoesNotExist:
        m = "There is no service %s." % service_origin
        raise RegisterException(m)

    try:
        r = Resource.objects.get_for_update(name=name)
        exists = True
        if r.service_type != service_type:
            m = ("There already exists a resource named %s with service "
                 "type %s." % (name, r.service_type))
            raise RegisterException(m)
        if r.service_origin != service_origin:
            m = ("There already exists a resource named %s registered for "
                 "service %s." % (name, r.service_origin))
            raise RegisterException(m)

    except Resource.DoesNotExist:
        r = Resource(name=name,
                     uplimit=0,
                     service_type=service_type,
                     service_origin=service_origin)
        exists = False

    for field in resource_fields:
        value = resource_dict.get(field)
        if value is not None:
            setattr(r, field, value)

    r.save()
    if not exists:
        qh_sync_new_resource(r, 0)

    if exists:
        logger.info("Updated resource %s." % (name))
    else:
        logger.info("Added resource %s." % (name))
    return r, exists