def patch(self, request, machine_request_id, action=None):
        """
        OPT2 for approval: sending a PATCH to the machine request with
          {"status":"approve/deny"}
        
        Modfiy attributes on a machine request
        """
        if not request.user.is_staff:
            raise NotAuthenticated("Must be a staff user to view requests "
                                   "directly")

        try:
            machine_request = CoreMachineRequest.objects.get(
                id=machine_request_id)
        except CoreMachineRequest.DoesNotExist:
            return Response('No machine request with id %s'
                            % machine_request_id,
                            status=status.HTTP_404_NOT_FOUND)

        data = request.DATA
        serializer = MachineRequestSerializer(machine_request, data=data,
                partial=True)
        if serializer.is_valid():
            #Only run task if status is 'approve'
            if machine_request.status == 'approve':
                start_machine_imaging(machine_request)
            machine_request.save()
        #Object may have changed
        serializer = MachineRequestSerializer(machine_request)
        return Response(serializer.data, status=status.HTTP_200_OK)
 def post(self, request, provider_id, identity_id):
     """
     Sends an e-mail to the admins to start
     the create_image process.
     """
     #request.DATA is r/o
     #Copy allows for editing
     data = copy.deepcopy(request.DATA)
     data.update({'owner': data.get('created_for', request.user.username)})
     if data.get('vis','public') != 'public':
         user_list  = re.split(', | |\n', data.get('shared_with',""))
         share_with_admins(user_list, data.get('provider'))
         share_with_self(user_list, request.user.username)
         user_list = [user for user in user_list if user] # Skips blanks
         data['shared_with'] = user_list
     logger.info(data)
     serializer = MachineRequestSerializer(data=data)
     if serializer.is_valid():
         #Add parent machine to request
         machine_request = serializer.object
         machine_request.parent_machine = machine_request.instance.provider_machine
         serializer.save()
         #Object now has an ID for links..
         machine_request_id = serializer.object.id
         active_provider = machine_request.active_provider()
         auto_approve = active_provider.has_trait("Auto-Imaging")
         requestImaging(request, machine_request_id,
                        auto_approve=auto_approve)
         if auto_approve:
             start_machine_imaging(machine_request)
         return Response(serializer.data,
                         status=status.HTTP_201_CREATED)
     else:
         return Response(serializer.errors,
                         status=status.HTTP_400_BAD_REQUEST)
 def post(self, request, provider_id, identity_id):
     """
     Sends an e-mail to the admins to start
     the create_image process.
     """
     #request.DATA is r/o
     #Copy allows for editing
     data = copy.deepcopy(request.DATA)
     data.update({'owner': data.get('created_for', request.user.username)})
     if data.get('vis', 'public') != 'public':
         user_list = re.split(', | |\n', data.get('shared_with', ""))
         share_with_admins(user_list, data.get('provider'))
         share_with_self(user_list, request.user.username)
         user_list = [user for user in user_list if user]  # Skips blanks
         data['shared_with'] = user_list
     logger.info(data)
     serializer = MachineRequestSerializer(data=data)
     if serializer.is_valid():
         #Add parent machine to request
         machine_request = serializer.object
         machine_request.parent_machine = machine_request.instance.provider_machine
         serializer.save()
         #Object now has an ID for links..
         machine_request_id = serializer.object.id
         active_provider = machine_request.active_provider()
         auto_approve = active_provider.has_trait("Auto-Imaging")
         requestImaging(request,
                        machine_request_id,
                        auto_approve=auto_approve)
         if auto_approve:
             start_machine_imaging(machine_request)
         return Response(serializer.data, status=status.HTTP_201_CREATED)
     else:
         return Response(serializer.errors,
                         status=status.HTTP_400_BAD_REQUEST)
    def patch(self, request, machine_request_id, action=None):
        """
        OPT2 for approval: sending a PATCH to the machine request with
          {"status":"approve/deny"}
        
        Modfiy attributes on a machine request
        """
        if not request.user.is_staff:
            raise NotAuthenticated("Must be a staff user to view requests "
                                   "directly")

        try:
            machine_request = CoreMachineRequest.objects.get(
                id=machine_request_id)
        except CoreMachineRequest.DoesNotExist:
            return Response('No machine request with id %s'
                            % machine_request_id,
                            status=status.HTTP_404_NOT_FOUND)

        data = request.DATA
        serializer = MachineRequestSerializer(machine_request, data=data,
                partial=True)
        if serializer.is_valid():
            #Only run task if status is 'approve'
            if machine_request.status == 'approve':
                start_machine_imaging(machine_request)
            machine_request.save()
        #Object may have changed
        serializer = MachineRequestSerializer(machine_request)
        return Response(serializer.data, status=status.HTTP_200_OK)
    def put(self, request, provider_id, identity_id, machine_request_id):
        """Authentication Required, update information on a pending request.
        """
        #Meta data changes in 'pending' are OK
        #Status change 'pending' --> 'cancel' are OK
        data = request.DATA
        try:
            machine_request = CoreMachineRequest.objects.get(
                id=machine_request_id)
        except CoreMachineRequest.DoesNotExist:
            return Response('No machine request with id %s' %
                            machine_request_id,
                            status=status.HTTP_404_NOT_FOUND)

        serializer = MachineRequestSerializer(machine_request,
                                              data=data,
                                              partial=True)
        if serializer.is_valid():
            #Only run task if status is 'approve'
            machine_request = serializer.object
            if machine_request.status == 'approve':
                start_machine_imaging(machine_request)
            serializer.save()
            return Response(serializer.data)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
 def post(self, request, provider_id, identity_id):
     """
     Sends an e-mail to the admins to start
     the create_image process.
     """
     # request.DATA is r/o
     # Copy allows for editing
     data = copy.deepcopy(request.DATA)
     data.update({"owner": data.get("created_for", request.user.username)})
     if data.get("vis", "public") != "public":
         user_list = re.split(", | |\n", data.get("shared_with", ""))
         share_with_admins(user_list, data.get("provider"))
         share_with_self(user_list, request.user.username)
         user_list = [user for user in user_list if user]  # Skips blanks
         data["shared_with"] = user_list
     logger.info(data)
     serializer = MachineRequestSerializer(data=data)
     if serializer.is_valid():
         # Add parent machine to request
         machine_request = serializer.object
         machine_request.parent_machine = machine_request.instance.provider_machine
         serializer.save()
         # Object now has an ID for links..
         machine_request_id = serializer.object.id
         requestImaging(request, machine_request_id)
         return Response(serializer.data, status=status.HTTP_201_CREATED)
     else:
         return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
    def patch(self, request, provider_id, identity_id, machine_request_id):
        """
        Meta data changes in 'pending' are OK
        Status change 'pending' --> 'cancel' are OK
        All other changes should FAIL
        """
        data = request.DATA
        try:
            machine_request = CoreMachineRequest.objects.get(
                id=machine_request_id)
        except CoreMachineRequest.DoesNotExist:
            return Response('No machine request with id %s' %
                            machine_request_id,
                            status=status.HTTP_404_NOT_FOUND)

        serializer = MachineRequestSerializer(machine_request,
                                              data=data,
                                              partial=True)
        if serializer.is_valid():
            machine_request = serializer.object
            if machine_request.status == 'approve':
                start_machine_imaging(machine_request)
            serializer.save()
            return Response(serializer.data)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
    def put(self, request, provider_uuid, identity_uuid, machine_request_id):
        """
        Authentication Required, update information on a pending request.
        """
        # Meta data changes in 'pending' are OK
        # Status change 'pending' --> 'cancel' are OK
        data = request.DATA
        try:
            machine_request = CoreMachineRequest.objects.get(
                id=machine_request_id)
        except CoreMachineRequest.DoesNotExist:
            return Response('No machine request with id %s'
                            % machine_request_id,
                            status=status.HTTP_404_NOT_FOUND)

        serializer = MachineRequestSerializer(machine_request,
                                              data=data, partial=True)
        if serializer.is_valid():
            # Only run task if status is 'approve'
            machine_request = serializer.object
            if machine_request.status == 'approve':
                start_machine_imaging(machine_request)
            serializer.save()
            return Response(serializer.data)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
 def post(self, request, provider_uuid, identity_uuid):
     """
     Sends an e-mail to the admins to start
     the create_image process.
     """
     #request.DATA is r/o
     #Copy allows for editing
     data = copy.deepcopy(request.DATA)
     data.update({'owner': data.get('created_for', request.user.username)})
     if data.get('vis','public') != 'public':
         user_list  = re.split(', | |\n', data.get('shared_with',""))
         share_with_admins(user_list, data.get('provider'))
         share_with_self(user_list, request.user.username)
         user_list = [user for user in user_list if user] # Skips blanks
         #TODO: Remove duplicates as well..
         data['shared_with'] = user_list
     logger.info(data)
     serializer = MachineRequestSerializer(data=data)
     if serializer.is_valid():
         #Add parent machine to request
         machine_request = serializer.object
         instance = machine_request.instance
         if instance.source.is_machine():
             machine_request.parent_machine = machine_request.instance\
                     .source.providermachine
         elif instance.source.is_volume():
             return failure_response(status.HTTP_400_BAD_REQUEST,
                     "Instance of booted volume can NOT be imaged."
                     "Contact your Administrator for more information.")
         else:
             return failure_response(status.HTTP_400_BAD_REQUEST,
                     "Instance source type cannot be determined."
                     "Contact your Administrator for more information.")
         #NOTE: THIS IS A HACK -- While we enforce all images to go to iPlant Cloud - Tucson.
         # THIS CODE SHOULD BE REMOVED 
         try:
             tucson_provider = Provider.objects.get(location='iPlant Cloud - Tucson')
             if machine_request.new_machine_provider.location != tucson_provider.location:
                 machine_request.new_machine_provider = tucson_provider
         except:
             pass
         serializer.save()
         #Object now has an ID for links..
         machine_request_id = serializer.object.id
         active_provider = machine_request.active_provider()
         auto_approve = active_provider.has_trait("Auto-Imaging")
         requestImaging(request, machine_request_id,
                        auto_approve=auto_approve)
         if auto_approve:
             start_machine_imaging(machine_request)
         return Response(serializer.data,
                         status=status.HTTP_201_CREATED)
     else:
         return Response(serializer.errors,
                         status=status.HTTP_400_BAD_REQUEST)
 def _create_image(self, request, provider_uuid, identity_uuid):
     _permission_to_act(identity_uuid, "Imaging")
     #request.DATA is r/o
     #Copy allows for editing
     data = copy.deepcopy(request.DATA)
     data.update({'owner': data.get('created_for', request.user.username)})
     if data.get('vis','public') != 'public':
         user_list  = re.split(', | |\n', data.get('shared_with',""))
         share_with_admins(user_list, data.get('provider'))
         share_with_self(user_list, request.user.username)
         user_list = [user for user in user_list if user] # Skips blanks
         #TODO: Remove duplicates as well..
         data['shared_with'] = user_list
     logger.info(data)
     serializer = MachineRequestSerializer(data=data)
     if serializer.is_valid():
         #Add parent machine to request
         machine_request = serializer.object
         instance = machine_request.instance
         if instance.source.is_machine():
             machine_request.parent_machine = machine_request.instance\
                     .source.providermachine
         elif instance.source.is_volume():
             raise Exception(
                     "Instance of booted volume can NOT be imaged."
                     "Contact your Administrator for more information.")
         else:
             raise Exception(
                     "Instance source type cannot be determined."
                     "Contact your Administrator for more information.")
         #NOTE: THIS IS A HACK -- While we enforce all images to go to iPlant Cloud - Tucson.
         # THIS CODE SHOULD BE REMOVED 
         try:
             tucson_provider = Provider.objects.get(location='iPlant Cloud - Tucson')
             if machine_request.new_machine_provider.location != tucson_provider.location:
                 machine_request.new_machine_provider = tucson_provider
         except:
             pass
         serializer.save()
         #Object now has an ID for links..
         machine_request_id = serializer.object.id
         active_provider = machine_request.active_provider()
         auto_approve = active_provider.auto_imaging
         requestImaging(request, machine_request_id,
                        auto_approve=auto_approve)
         if auto_approve:
             start_machine_imaging(machine_request)
         return Response(serializer.data,
                         status=status.HTTP_201_CREATED)
     else:
         return Response(serializer.errors,
                         status=status.HTTP_400_BAD_REQUEST)
    def patch(self,
              request,
              cloud_admin_uuid,
              machine_request_id,
              action=None):
        """
        OPT2 for approval: sending a PATCH to the machine request with
          {"status":"approve/deny"}

        Modfiy attributes on a machine request
        """
        user = request.user
        try:
            machine_request = CoreMachineRequest.objects.get(
                instance__source__provider__cloudadministrator__user=user,
                id=machine_request_id)
        except CoreMachineRequest.DoesNotExist:
            return Response('No machine request with id %s' %
                            machine_request_id,
                            status=status.HTTP_404_NOT_FOUND)

        data = request.DATA
        # Behavior will remove 'status' if its being updated.
        # Status should only be updated if denied or skipped.
        # Status of 'approve','continue' will use the machine_request.status
        # to allow restarting the request at the correct point in time.
        start_request = False
        if 'status' in data:
            _status = data['status'].lower()
            if machine_request.status == 'completed':
                return Response("Cannot update status of 'completed' request",
                                status=status.HTTP_409_conflict)
            elif _status in ['approve', 'continue']:
                data.pop('status')
                start_request = True
            elif _status not in ['deny', 'skip']:
                return Response("Bad Status Value: %s. "
                                "Available choices for a status update are: "
                                "approve, continue, deny, skip")
        serializer = MachineRequestSerializer(machine_request,
                                              data=data,
                                              partial=True)
        if not serializer.is_valid():
            return Response(serializer.errors,
                            status=status.HTTP_400_BAD_REQUEST)
        # Only run task if status is 'approve'
        machine_request = serializer.save()
        mr_data = serializer.data
        if start_request:
            start_machine_imaging(machine_request)
        return Response(mr_data, status=status.HTTP_200_OK)
    def patch(self, request,
              cloud_admin_uuid, machine_request_id, action=None):
        """
        OPT2 for approval: sending a PATCH to the machine request with
          {"status":"approve/deny"}

        Modfiy attributes on a machine request
        """
        user = request.user
        try:
            machine_request = CoreMachineRequest.objects.get(
                instance__source__provider__cloudadministrator__user=user,
                id=machine_request_id)
        except CoreMachineRequest.DoesNotExist:
            return Response('No machine request with id %s'
                            % machine_request_id,
                            status=status.HTTP_404_NOT_FOUND)

        data = request.DATA
        # Behavior will remove 'status' if its being updated.
        # Status should only be updated if denied or skipped.
        # Status of 'approve','continue' will use the machine_request.status
        # to allow restarting the request at the correct point in time.
        start_request = False
        if 'status' in data:
            _status = data['status'].lower()
            if machine_request.status == 'completed':
                return Response(
                    "Cannot update status of 'completed' request",
                    status=status.HTTP_409_conflict)
            elif _status in ['approve', 'continue']:
                data.pop('status')
                start_request = True
            elif _status not in ['deny', 'skip']:
                return Response(
                    "Bad Status Value: %s. "
                    "Available choices for a status update are: "
                    "approve, continue, deny, skip")
        serializer = MachineRequestSerializer(
            machine_request, data=data, partial=True)
        if not serializer.is_valid():
            return Response(serializer.errors,
                            status=status.HTTP_400_BAD_REQUEST)
        # Only run task if status is 'approve'
        machine_request = serializer.save()
        mr_data = serializer.data
        if start_request:
            start_machine_imaging(machine_request)
        return Response(mr_data, status=status.HTTP_200_OK)
    def patch(self, request, provider_id, identity_id, machine_request_id):
        """
        Meta data changes in 'pending' are OK
        Status change 'pending' --> 'cancel' are OK
        All other changes should FAIL
        """
        data = request.DATA
        try:
            machine_request = CoreMachineRequest.objects.get(id=machine_request_id)
        except CoreMachineRequest.DoesNotExist:
            return Response("No machine request with id %s" % machine_request_id, status=status.HTTP_404_NOT_FOUND)

        serializer = MachineRequestSerializer(machine_request, data=data, partial=True)
        if serializer.is_valid():
            machine_request = serializer.object
            if machine_request.status == "approve":
                start_machine_imaging(machine_request)
            serializer.save()
            return Response(serializer.data)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)