コード例 #1
0
ファイル: machine.py プロジェクト: zhanghaihua/atmosphere
    def put(self, request, provider_id, identity_id, machine_id):
        """
        TODO: Determine who is allowed to edit machines besides
            core_machine.owner
        """
        user = request.user
        data = request.DATA
        esh_driver = prepare_driver(request, provider_id, identity_id)
        if not esh_driver:
            return invalid_creds(provider_id, identity_id)
        esh_machine = esh_driver.get_machine(machine_id)
        core_machine = convert_esh_machine(esh_driver, esh_machine,
                                           provider_id)

        if not user.is_staff\
           and user is not core_machine.application.created_by:
            logger.error('Non-staff/non-owner trying to update a machine')
            return failure_response(
                status.HTTP_401_UNAUTHORIZED,
                'Only Staff and the machine Owner ' +
                'are allowed to change machine info.')
        core_machine.application.update(data)
        serializer = ProviderMachineSerializer(core_machine,
                                               data=data,
                                               partial=True)
        if serializer.is_valid():
            logger.info('metadata = %s' % data)
            update_machine_metadata(esh_driver, esh_machine, data)
            serializer.save()
            logger.info(serializer.data)
            return Response(serializer.data)
        return failure_response(status.HTTP_400_BAD_REQUEST, serializer.errors)
コード例 #2
0
ファイル: machine.py プロジェクト: zhanghaihua/atmosphere
    def put(self, request, provider_id, identity_id, machine_id):
        """
        TODO: Determine who is allowed to edit machines besides
            core_machine.owner
        """
        user = request.user
        data = request.DATA
        esh_driver = prepare_driver(request, provider_id, identity_id)
        if not esh_driver:
            return invalid_creds(provider_id, identity_id)
        esh_machine = esh_driver.get_machine(machine_id)
        core_machine = convert_esh_machine(esh_driver, esh_machine, provider_id)

        if not user.is_staff\
           and user is not core_machine.application.created_by:
            logger.error('Non-staff/non-owner trying to update a machine')
            return failure_response(
                status.HTTP_401_UNAUTHORIZED,
                'Only Staff and the machine Owner '
                + 'are allowed to change machine info.')
        core_machine.application.update(data)
        serializer = ProviderMachineSerializer(core_machine,
                                               data=data, partial=True)
        if serializer.is_valid():
            logger.info('metadata = %s' % data)
            update_machine_metadata(esh_driver, esh_machine, data)
            serializer.save()
            logger.info(serializer.data)
            return Response(serializer.data)
        return failure_response(
            status.HTTP_400_BAD_REQUEST,
            serializer.errors)
コード例 #3
0
ファイル: machine.py プロジェクト: nickeddy/atmosphere
    def patch(self, request, provider_id, identity_id, machine_id):
        """
        TODO: Determine who is allowed to edit machines besides
            coreMachine.owner
        """
        user = request.user
        data = request.DATA
        esh_driver = prepare_driver(request, identity_id)
        esh_machine = esh_driver.get_machine(machine_id)
        coreMachine = convert_esh_machine(esh_driver, esh_machine, provider_id)

        if not user.is_staff and user is not coreMachine.application.created_by:
            logger.warn('%s is Non-staff/non-owner trying to update a machine'
                        % (user.username))
            errorObj = failureJSON([{
                'code': 401,
                'message':
                'Only Staff and the machine Owner '
                + 'are allowed to change machine info.'}])
            return Response(errorObj, status=status.HTTP_401_UNAUTHORIZED)

        coreMachine.application.update(request.DATA)
        serializer = ProviderMachineSerializer(coreMachine,
                                               data=data, partial=True)
        if serializer.is_valid():
            logger.info('metadata = %s' % data)
            update_machine_metadata(esh_driver, esh_machine, data)
            serializer.save()
            logger.info(serializer.data)
            return Response(serializer.data)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
コード例 #4
0
 def get(self, request, provider_uuid, identity_uuid):
     """
     Using provider and identity, getlist of machines
     TODO: Cache this request
     """
     try:
         request_user = request.user
         logger.debug("filtered_machine_list")
         filtered_machine_list = provider_filtered_machines(
             request, provider_uuid, identity_uuid, request_user)
         logger.debug(filtered_machine_list)
     except InvalidCredsError:
         return invalid_creds(provider_uuid, identity_uuid)
     except MalformedResponseError:
         return malformed_response(provider_uuid, identity_uuid)
     except Exception as e:
         logger.exception("Unexpected exception for user:%s" % request_user)
         return failure_response(status.HTTP_500_INTERNAL_SERVER_ERROR,
                                 e.message)
     logger.debug(filtered_machine_list)
     serialized_data = ProviderMachineSerializer(filtered_machine_list,
                                                 request_user=request.user,
                                                 many=True).data
     response = Response(serialized_data)
     return response
コード例 #5
0
    def get(self, request, machine_name):
        """
        """
        pms = ProviderMachine.objects.filter(application__name=machine_name)

        serialized_data = ProviderMachineSerializer(pms,
               request_user=None,
               many=True,
               ).data
        return Response(serialized_data)
コード例 #6
0
ファイル: machine.py プロジェクト: karbon62/atmosphere
    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
        esh_driver = prepare_driver(request, provider_uuid, identity_uuid)
        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.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)
            serializer.save()
            if 'created_by_identity' in request.DATA:
                identity = serializer.object.created_by_identity
                update_application_owner(core_machine.application, identity)
            logger.info(serializer.data)
            return Response(serializer.data)
        return failure_response(
            status.HTTP_400_BAD_REQUEST,
            serializer.errors)
コード例 #7
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
        esh_driver = prepare_driver(request, provider_uuid, identity_uuid)
        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.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)
            serializer.save()
            if 'created_by_identity' in request.DATA:
                identity = serializer.object.created_by_identity
                update_application_owner(core_machine.application, identity)
            logger.info(serializer.data)
            return Response(serializer.data)
        return failure_response(status.HTTP_400_BAD_REQUEST, serializer.errors)
コード例 #8
0
ファイル: machine.py プロジェクト: zhanghaihua/atmosphere
 def get(self, request, provider_id, identity_id):
     """
     Using provider and identity, getlist of machines
     TODO: Cache this request
     """
     try:
         request_user = request.user.username
         filtered_machine_list = provider_filtered_machines(
             request, provider_id, identity_id, request_user)
     except:
         return invalid_creds(provider_id, identity_id)
     serialized_data = ProviderMachineSerializer(filtered_machine_list,
                                                 many=True).data
     response = Response(serialized_data)
     return response
コード例 #9
0
ファイル: machine.py プロジェクト: zhanghaihua/atmosphere
 def get(self, request, provider_id, identity_id, machine_id):
     """
     Lookup the machine information
     (Lookup using the given provider/identity)
     Update on server (If applicable)
     """
     esh_driver = prepare_driver(request, provider_id, identity_id)
     if not esh_driver:
         return invalid_creds(provider_id, identity_id)
     #TODO: Need to determine that identity_id is ALLOWED to see machine_id.
     #     if not covered by calling as the users driver..
     esh_machine = esh_driver.get_machine(machine_id)
     core_machine = convert_esh_machine(esh_driver, esh_machine,
                                        provider_id)
     serialized_data = ProviderMachineSerializer(core_machine).data
     response = Response(serialized_data)
     return response
コード例 #10
0
 def get(self, request, provider_uuid, identity_uuid, machine_id):
     """
     Details view for specific machine
     (Lookup using the given provider/identity)
     """
     user = request.user
     esh_driver = prepare_driver(request, provider_uuid, identity_uuid)
     if not esh_driver:
         return invalid_creds(provider_uuid, identity_uuid)
     #TODO: Need to determine that identity_uuid is ALLOWED to see machine_id.
     #     if not covered by calling as the users driver..
     esh_machine = esh_driver.get_machine(machine_id)
     core_machine = convert_esh_machine(esh_driver, esh_machine,
                                        provider_uuid, user)
     serialized_data = ProviderMachineSerializer(
         core_machine, request_user=request.user).data
     response = Response(serialized_data)
     return response
コード例 #11
0
ファイル: machine.py プロジェクト: zhanghaihua/atmosphere
    def get(self, request, provider_id, identity_id):
        data = request.DATA
        user = User.objects.filter(username=request.user)

        if user and len(user) > 0:
            user = user[0]
        else:
            return failure_response(status.HTTP_401_UNAUTHORIZED,
                                    "User not found.")
        esh_driver = prepare_driver(request, provider_id, identity_id)
        if not esh_driver:
            return invalid_creds(provider_id, identity_id)
        # Historic Machines
        all_machines_list = all_filtered_machines()
        if all_machines_list:
            history_machine_list =\
                [m for m in all_machines_list if
                 m.application.created_by.username == user.username]
            #logger.warn(len(history_machine_list))
        else:
            history_machine_list = []

        page = request.QUERY_PARAMS.get('page')
        if page:
            paginator = Paginator(history_machine_list, 5)
            try:
                history_machine_page = paginator.page(page)
            except PageNotAnInteger:
                # If page is not an integer, deliver first page.
                history_machine_page = paginator.page(1)
            except EmptyPage:
                # Page is out of range.
                # deliver last page of results.
                history_machine_page = paginator.page(paginator.num_pages)
            serialized_data = \
                PaginatedProviderMachineSerializer(
                    history_machine_page).data
        else:
            serialized_data = ProviderMachineSerializer(
                history_machine_list).data

        response = Response(serialized_data)
        response['Cache-Control'] = 'no-cache'
        return response
コード例 #12
0
ファイル: machine.py プロジェクト: shelleyfeng/atmosphere
 def get(self, request, provider_id, identity_id):
     """
     Using provider and identity, getlist of machines
     TODO: Cache this request
     """
     try:
         request_user = request.user
         filtered_machine_list = provider_filtered_machines(
             request, provider_id, identity_id, request_user)
     except InvalidCredsError:
         return invalid_creds(provider_id, identity_id)
     except:
         logger.exception("Unexpected exception for user:%s" % request_user)
         return invalid_creds(provider_id, identity_id)
     serialized_data = ProviderMachineSerializer(filtered_machine_list,
                                                 request_user=request.user,
                                                 many=True).data
     response = Response(serialized_data)
     return response
コード例 #13
0
ファイル: machine.py プロジェクト: shelleyfeng/atmosphere
 def get(self, request, provider_id, identity_id):
     """
     """
     data = request.DATA
     user = get_first(User.objects.filter(username=request.user))
     if not user:
         return failure_response(status.HTTP_401_UNAUTHORIZED,
                                 "User not found.")
     query = request.QUERY_PARAMS.get('query')
     if not query:
         return failure_response(status.HTTP_400_BAD_REQUEST,
                                 "Query not provided.")
     identity = get_first(Identity.objects.filter(id=identity_id))
     if not identity:
         return failure_response(status.HTTP_400_BAD_REQUEST,
                                 'Identity not provided,')
     search_result = search([CoreSearchProvider], identity, query)
     page = request.QUERY_PARAMS.get('page')
     if page:
         paginator = Paginator(search_result, 20)
         try:
             search_page = paginator.page(page)
         except PageNotAnInteger:
             # If page is not an integer, deliver first page.
             search_page = paginator.page(1)
         except EmptyPage:
             # Page is out of range.
             # deliver last page of results.
             search_page = paginator.page(paginator.num_pages)
         serialized_data = \
             PaginatedProviderMachineSerializer(
                 search_page).data
     else:
         serialized_data = ProviderMachineSerializer(
             search_result, request_user=request.user).data
     response = Response(serialized_data)
     response['Cache-Control'] = 'no-cache'
     return response