Пример #1
0
    def _update_machine(
        self, request, provider_uuid, identity_uuid, machine_id
    ):
        # TODO: Determine who is allowed to edit machines besides
        # core_machine.owner
        user = request.user
        data = request.data
        try:
            esh_driver = prepare_driver(request, provider_uuid, identity_uuid)
        except ProviderNotActive as pna:
            return inactive_provider(pna)
        except Exception as e:
            return failure_response(status.HTTP_409_CONFLICT, e.message)

        if not esh_driver:
            return invalid_creds(provider_uuid, identity_uuid)
        esh_machine = esh_driver.get_machine(machine_id)
        core_machine = convert_esh_machine(
            esh_driver, esh_machine, provider_uuid, user
        )
        if not user.is_staff and user is not core_machine.application_version.application.created_by:
            logger.warn(
                '%s is Non-staff/non-owner trying to update a machine' %
                (user.username)
            )
            return failure_response(
                status.HTTP_401_UNAUTHORIZED,
                "Only Staff and the machine Owner "
                "are allowed to change machine info."
            )

        partial_update = True if request.method == 'PATCH' else False
        serializer = ProviderMachineSerializer(
            core_machine,
            request_user=request.user,
            data=data,
            partial=partial_update
        )
        if serializer.is_valid():
            logger.info('metadata = %s' % data)
            update_machine_metadata(esh_driver, esh_machine, data)
            machine = serializer.save()
            if 'created_by_identity' in request.data:
                identity = machine.created_by_identity
                update_application_owner(
                    core_machine.application_version.application, identity
                )
            logger.info(serializer.data)
            return Response(serializer.data)
        return failure_response(status.HTTP_400_BAD_REQUEST, serializer.errors)
Пример #2
0
def update_provider_machine_metadata(provider_machine_id, metadata={}):
    """
    Call appropriate method to update machine metadata
    -- This is a 'core-wrapper' to service-level object..
    """
    from service.machine import update_machine_metadata
    provider_machine = find_provider_machine(provider_machine_id)
    return update_machine_metadata(provider_machine, metadata)
Пример #3
0
def update_provider_machine_metadata(provider_machine_id, metadata={}):
    """
    Call appropriate method to update machine metadata
    -- This is a 'core-wrapper' to service-level object..
    """
    from service.machine import update_machine_metadata
    provider_machine = find_provider_machine(provider_machine_id)
    return update_machine_metadata(provider_machine, metadata)