Esempio n. 1
0
 def create(self, request, role_guids=None):
     """
     Creates a Client
     """
     if 'role_guids' in request.DATA:
         del request.DATA['role_guids']
     serializer = FullSerializer(Client, instance=Client(), data=request.DATA)
     if serializer.is_valid():
         client = serializer.object
         if client.user is not None:
             if client.user_guid == request.client.user_guid or Toolbox.is_client_in_roles(request.client, ['manage']):
                 client.grant_type = 'CLIENT_CREDENTIALS'
                 client.client_secret = OAuth2Toolbox.create_hash(64)
                 serializer.save()
                 if not role_guids:
                     roles = [junction.role for junction in client.user.group.roles]
                 else:
                     possible_role_guids = [junction.role_guid for junction in client.user.group.roles]
                     roles = [Role(guid) for guid in role_guids if guid in possible_role_guids]
                 for role in roles:
                     roleclient = RoleClient()
                     roleclient.client = client
                     roleclient.role = role
                     roleclient.save()
                 return Response(serializer.data, status=status.HTTP_201_CREATED)
     return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Esempio n. 2
0
 def create(self, request):
     """
     Creates a Backend
     """
     serializer = FullSerializer(Backend, instance=Backend(), data=request.DATA)
     if serializer.is_valid():
         serializer.save()
         return Response(serializer.data, status=status.HTTP_201_CREATED)
     return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Esempio n. 3
0
 def partial_update(self, contents, pmachine, request):
     """
     Update a pMachine
     """
     contents = None if contents is None else contents.split(',')
     serializer = FullSerializer(PMachine, contents=contents, instance=pmachine, data=request.DATA)
     if serializer.is_valid():
         serializer.save()
         return Response(serializer.data, status=status.HTTP_202_ACCEPTED)
     else:
         return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Esempio n. 4
0
 def create(self, request):
     """
     Creates a Backend
     """
     serializer = FullSerializer(Backend, instance=Backend(), data=request.DATA)
     if serializer.is_valid():
         duplicate = BackendList.get_by_name(serializer.object.name)
         if duplicate is None:
             serializer.save()
             return Response(serializer.data, status=status.HTTP_201_CREATED)
     return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Esempio n. 5
0
    def partial_update(self, failuredomain, request, contents=None):
        """
        Update a Failure Domain
        """
        contents = None if contents is None else contents.split(',')
        serializer = FullSerializer(FailureDomain, contents=contents, instance=failuredomain, data=request.DATA)
        if serializer.is_valid():
            serializer.save()

            return Response(serializer.data, status=status.HTTP_202_ACCEPTED)
        else:
            return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Esempio n. 6
0
 def partial_update(self, contents, user, request):
     """
     Update a User
     """
     contents = None if contents is None else contents.split(',')
     serializer = FullSerializer(User, contents=contents, instance=user, data=request.DATA)
     if serializer.is_valid():
         if user.guid == request.client.user_guid:
             raise PermissionDenied('A user cannot update itself')
         serializer.save()
         return Response(serializer.data, status=status.HTTP_202_ACCEPTED)
     else:
         return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Esempio n. 7
0
 def create(self, request):
     """
     Creates a Backend
     """
     serializer = FullSerializer(Backend,
                                 instance=Backend(),
                                 data=request.DATA)
     if serializer.is_valid():
         duplicate = BackendList.get_by_name(serializer.object.name)
         if duplicate is None:
             serializer.save()
             return Response(serializer.data,
                             status=status.HTTP_201_CREATED)
     return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Esempio n. 8
0
 def partial_update(self, contents, pmachine, request):
     """
     Update a pMachine
     """
     contents = None if contents is None else contents.split(',')
     serializer = FullSerializer(PMachine,
                                 contents=contents,
                                 instance=pmachine,
                                 data=request.DATA)
     if serializer.is_valid():
         serializer.save()
         return Response(serializer.data, status=status.HTTP_202_ACCEPTED)
     else:
         return Response(serializer.errors,
                         status=status.HTTP_400_BAD_REQUEST)
Esempio n. 9
0
    def partial_update(self, failuredomain, request, contents=None):
        """
        Update a Failure Domain
        """
        contents = None if contents is None else contents.split(',')
        serializer = FullSerializer(FailureDomain,
                                    contents=contents,
                                    instance=failuredomain,
                                    data=request.DATA)
        if serializer.is_valid():
            serializer.save()

            return Response(serializer.data, status=status.HTTP_202_ACCEPTED)
        else:
            return Response(serializer.errors,
                            status=status.HTTP_400_BAD_REQUEST)
Esempio n. 10
0
 def partial_update(self, contents, user, request):
     """
     Update a User
     """
     contents = None if contents is None else contents.split(',')
     serializer = FullSerializer(User,
                                 contents=contents,
                                 instance=user,
                                 data=request.DATA)
     if serializer.is_valid():
         if user.guid == request.client.user_guid:
             raise PermissionDenied('A user cannot update itself')
         serializer.save()
         return Response(serializer.data, status=status.HTTP_202_ACCEPTED)
     else:
         return Response(serializer.errors,
                         status=status.HTTP_400_BAD_REQUEST)
Esempio n. 11
0
    def partial_update(self, storagerouter, request, contents=None):
        """
        Update a StorageRouter
        """
        contents = None if contents is None else contents.split(',')
        previous_primary = storagerouter.primary_failure_domain
        previous_secondary = storagerouter.secondary_failure_domain
        serializer = FullSerializer(StorageRouter, contents=contents, instance=storagerouter, data=request.DATA)
        if serializer.is_valid():
            primary = storagerouter.primary_failure_domain
            secondary = storagerouter.secondary_failure_domain
            if primary is None:
                raise NotAcceptable('A StorageRouter must have a primary FD configured')
            if secondary is not None:
                if primary.guid == secondary.guid:
                    raise NotAcceptable('A StorageRouter cannot have the same FD for both primary and secondary')
                if len(secondary.primary_storagerouters) == 0:
                    raise NotAcceptable('The secondary FD should be set as primary FD by at least one StorageRouter')
            if len(previous_primary.secondary_storagerouters) > 0 and len(previous_primary.primary_storagerouters) == 1 and \
                    previous_primary.primary_storagerouters[0].guid == storagerouter.guid and previous_primary.guid != primary.guid:
                raise NotAcceptable('Cannot change the primary FD as this StorageRouter is the only one serving it while it is used as secondary FD')
            serializer.save()
            if previous_primary != primary or previous_secondary != secondary:
                cache = VolatileFactory.get_client()
                key_mds = 'ovs_dedupe_fdchange_mds_{0}'.format(storagerouter.guid)
                key_dtl = 'ovs_dedupe_fdchange_dtl_{0}'.format(storagerouter.guid)
                task_mds_id = cache.get(key_mds)
                task_dtl_id = cache.get(key_dtl)
                if task_mds_id:
                    # Key exists, task was already scheduled. If task is already running, the revoke message will be ignored
                    revoke(task_mds_id)
                if task_dtl_id:
                    revoke(task_dtl_id)
                async_mds_result = MDSServiceController.mds_checkup.s().apply_async(countdown=60)
                async_dtl_result = VDiskController.dtl_checkup.s().apply_async(countdown=60)
                cache.set(key_mds, async_mds_result.id, 600)  # Store the task id
                cache.set(key_mds, async_dtl_result.id, 600)  # Store the task id

            return Response(serializer.data, status=status.HTTP_202_ACCEPTED)
        else:
            return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Esempio n. 12
0
    def partial_update(self, storagerouter, request, contents=None):
        """
        Update a StorageRouter
        """
        contents = None if contents is None else contents.split(',')
        previous_primary = storagerouter.primary_failure_domain
        previous_secondary = storagerouter.secondary_failure_domain
        serializer = FullSerializer(StorageRouter, contents=contents, instance=storagerouter, data=request.DATA)
        if serializer.is_valid():
            primary = storagerouter.primary_failure_domain
            secondary = storagerouter.secondary_failure_domain
            if primary is None:
                raise NotAcceptable('A StorageRouter must have a primary FD configured')
            if secondary is not None:
                if primary.guid == secondary.guid:
                    raise NotAcceptable('A StorageRouter cannot have the same FD for both primary and secondary')
                if len(secondary.primary_storagerouters) == 0:
                    raise NotAcceptable('The secondary FD should be set as primary FD by at least one StorageRouter')
            if len(previous_primary.secondary_storagerouters) > 0 and len(previous_primary.primary_storagerouters) == 1 and \
                    previous_primary.primary_storagerouters[0].guid == storagerouter.guid and previous_primary.guid != primary.guid:
                raise NotAcceptable('Cannot change the primary FD as this StorageRouter is the only one serving it while it is used as secondary FD')
            serializer.save()
            if previous_primary != primary or previous_secondary != secondary:
                cache = VolatileFactory.get_client()
                key_mds = 'ovs_dedupe_fdchange_mds_{0}'.format(storagerouter.guid)
                key_dtl = 'ovs_dedupe_fdchange_dtl_{0}'.format(storagerouter.guid)
                task_mds_id = cache.get(key_mds)
                task_dtl_id = cache.get(key_dtl)
                if task_mds_id:
                    # Key exists, task was already scheduled. If task is already running, the revoke message will be ignored
                    revoke(task_mds_id)
                if task_dtl_id:
                    revoke(task_dtl_id)
                async_mds_result = MDSServiceController.mds_checkup.s().apply_async(countdown=60)
                async_dtl_result = VDiskController.dtl_checkup.s().apply_async(countdown=60)
                cache.set(key_mds, async_mds_result.id, 600)  # Store the task id
                cache.set(key_mds, async_dtl_result.id, 600)  # Store the task id

            return Response(serializer.data, status=status.HTTP_202_ACCEPTED)
        else:
            return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Esempio n. 13
0
 def create(self, request, role_guids=None):
     """
     Creates a Client
     """
     if 'role_guids' in request.DATA:
         del request.DATA['role_guids']
     serializer = FullSerializer(Client,
                                 instance=Client(),
                                 data=request.DATA)
     if serializer.is_valid():
         client = serializer.object
         if client.user is not None:
             if client.user_guid == request.client.user_guid or Toolbox.is_client_in_roles(
                     request.client, ['manage']):
                 client.grant_type = 'CLIENT_CREDENTIALS'
                 client.client_secret = OAuth2Toolbox.create_hash(64)
                 serializer.save()
                 if not role_guids:
                     roles = [
                         junction.role
                         for junction in client.user.group.roles
                     ]
                 else:
                     possible_role_guids = [
                         junction.role_guid
                         for junction in client.user.group.roles
                     ]
                     roles = [
                         Role(guid) for guid in role_guids
                         if guid in possible_role_guids
                     ]
                 for role in roles:
                     roleclient = RoleClient()
                     roleclient.client = client
                     roleclient.role = role
                     roleclient.save()
                 return Response(serializer.data,
                                 status=status.HTTP_201_CREATED)
     return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)