Ejemplo n.º 1
0
    def put(self, request, database_name, format=None):
        data = request.DATA
        if not data:
            return Response("Invalid request", status=400)

        user = data.get('user')
        team = data.get('team')
        # data.get('plan')
        env = get_url_env(request)

        UserMiddleware.set_current_user(request.user)
        env = get_url_env(request)
        try:
            database = get_database(database_name, env)
        except IndexError as e:
            msg = "Database id provided does not exist {} in {}.".format(
                database_name, env)
            return log_and_response(msg=msg,
                                    e=e,
                                    http_status=status.HTTP_404_NOT_FOUND)

        try:
            dbaas_user = AccountUser.objects.get(email=user)
        except ObjectDoesNotExist as e:
            msg = "User does not exist."
            return log_and_response(
                msg=msg,
                e=e,
                http_status=status.HTTP_500_INTERNAL_SERVER_ERROR)
        except MultipleObjectsReturned as e:
            msg = "There are multiple user for {} email.".format(user)
            return log_and_response(
                msg=msg,
                e=e,
                http_status=status.HTTP_500_INTERNAL_SERVER_ERROR)

        try:
            dbaas_team = Team.objects.get(name=team)
        except ObjectDoesNotExist as e:
            msg = "Team does not exist."
            return log_and_response(
                msg=msg,
                e=e,
                http_status=status.HTTP_500_INTERNAL_SERVER_ERROR)

        try:
            dbaas_user.team_set.get(name=dbaas_team.name)
        except ObjectDoesNotExist as e:
            msg = "User {} is not on {} team.".format(dbaas_user,
                                                      dbaas_team.name)
            return log_and_response(
                msg=msg,
                e=e,
                http_status=status.HTTP_500_INTERNAL_SERVER_ERROR)

        database.team = dbaas_team
        database.save()

        return Response(status=status.HTTP_204_NO_CONTENT)
Ejemplo n.º 2
0
    def destroy(self, request, *args, **kwargs):
        instance = self.get_object()
        UserMiddleware.set_current_user(request.user)

        if instance.is_in_quarantine or instance.is_protected:
            return Response(status=status.HTTP_401_UNAUTHORIZED)

        instance.delete()
        return Response(status=status.HTTP_204_NO_CONTENT)
Ejemplo n.º 3
0
    def destroy(self, request, *args, **kwargs):
        instance = self.get_object()
        UserMiddleware.set_current_user(request.user)

        if instance.is_in_quarantine or instance.is_protected:
            return Response(status=status.HTTP_401_UNAUTHORIZED)

        instance.delete()
        return Response(status=status.HTTP_204_NO_CONTENT)
Ejemplo n.º 4
0
    def delete(self, request, database_name, format=None):
        UserMiddleware.set_current_user(request.user)
        env = get_url_env(request)
        try:
            database = get_database(database_name, env)
        except IndexError as e:
            msg = "Database id provided does not exist {} in {}.".format(
                database_name, env)
            return log_and_response(
                msg=msg, e=e, http_status=status.HTTP_500_INTERNAL_SERVER_ERROR)

        database.delete()
        return Response(status.HTTP_204_NO_CONTENT)
Ejemplo n.º 5
0
def database_pre_save(sender, **kwargs):
    database = kwargs.get('instance')
    if database.is_in_quarantine:
        if database.quarantine_dt is None:
            database.quarantine_dt = datetime.datetime.now().date()

        if not database.quarantine_user:
            from dbaas.middleware import UserMiddleware
            database.quarantine_user = UserMiddleware.current_user()
    else:
        database.quarantine_dt = None
        database.quarantine_user = None

    if database.id:
        saved_object = Database.objects.get(id=database.id)
        if database.name != saved_object.name:
            raise AttributeError(_("Attribute name cannot be edited"))
    else:
        # new database
        if database_name_evironment_constraint(database.name,
                                               database.environment.name):
            raise AttributeError(
                _('%s already exists in production!') % database.name)

        LOG.debug("slugfying database's name for %s" % database.name)
        database.name = slugify(database.name)
Ejemplo n.º 6
0
def database_pre_save(sender, **kwargs):
    from notification.tasks import TaskRegister

    database = kwargs.get('instance')
    if database.is_in_quarantine:
        if database.quarantine_dt is None:
            database.quarantine_dt = datetime.datetime.now().date()

        if not database.quarantine_user:
            from dbaas.middleware import UserMiddleware
            database.quarantine_user = UserMiddleware.current_user()
    else:
        database.quarantine_dt = None
        database.quarantine_user = None

    if database.id:
        saved_object = Database.objects.get(id=database.id)
        if database.name != saved_object.name:
            raise AttributeError(_("Attribute name cannot be edited"))

        if database.team and saved_object.team:
            if database.team.organization != saved_object.team.organization:
                TaskRegister.update_organization_name_monitoring(
                    database=database,
                    organization_name=database.team.organization.name)
                if saved_object.team.external:
                    TaskRegister.update_database_monitoring(
                        database=database,
                        hostgroup=(saved_object.team.organization
                                   .grafana_hostgroup),
                        action='remove')
                if database.team.external:
                    TaskRegister.update_database_monitoring(
                        database=database,
                        hostgroup=database.team.organization.grafana_hostgroup,
                        action='add')

    else:
        # new database
        if database_name_evironment_constraint(
           database.name, database.environment.name):
            raise AttributeError(
                _('%s already exists in production!') % database.name
            )

        LOG.debug("slugfying database's name for %s" % database.name)
        database.name = slugify(database.name)
Ejemplo n.º 7
0
def database_pre_save(sender, **kwargs):
    from notification.tasks import TaskRegister

    database = kwargs.get('instance')
    if database.is_in_quarantine:
        if database.quarantine_dt is None:
            database.quarantine_dt = datetime.datetime.now().date()

        if not database.quarantine_user:
            from dbaas.middleware import UserMiddleware
            database.quarantine_user = UserMiddleware.current_user()
    else:
        database.quarantine_dt = None
        database.quarantine_user = None

    if database.id:
        saved_object = Database.objects.get(id=database.id)
        if database.name != saved_object.name:
            raise AttributeError(_("Attribute name cannot be edited"))

        if database.team and saved_object.team:
            if database.team.organization != saved_object.team.organization:
                TaskRegister.update_organization_name_monitoring(
                    database=database,
                    organization_name=database.team.organization.name)
                if saved_object.team.external:
                    TaskRegister.update_database_monitoring(
                        database=database,
                        hostgroup=saved_object.team.organization.grafana_hostgroup,
                        action='remove')
                if database.team.external:
                    TaskRegister.update_database_monitoring(
                        database=database,
                        hostgroup=database.team.organization.grafana_hostgroup,
                        action='add')

    else:
        # new database
        if database_name_evironment_constraint(
           database.name, database.environment.name):
            raise AttributeError(
                _('%s already exists in production!') % database.name
            )

        LOG.debug("slugfying database's name for %s" % database.name)
        database.name = slugify(database.name)
Ejemplo n.º 8
0
    def put(self, request, database_name, format=None):
        data = request.DATA
        user = data['user']
        team = data['team']
        plan = data['plan']
        env = get_url_env(request)

        UserMiddleware.set_current_user(request.user)
        env = get_url_env(request)
        try:
            database = get_database(database_name, env)
        except IndexError as e:
            msg = "Database id provided does not exist {} in {}.".format(
                database_name, env)
            return log_and_response(
                msg=msg,
                e=e,
                http_status=status.HTTP_500_INTERNAL_SERVER_ERROR)

        try:
            dbaas_user = AccountUser.objects.get(email=user)
        except ObjectDoesNotExist as e:
            msg = "User does not exist."
            return log_and_response(
                msg=msg,
                e=e,
                http_status=status.HTTP_500_INTERNAL_SERVER_ERROR)
        except MultipleObjectsReturned as e:
            msg = "There are multiple user for {} email.".format(user)
            return log_and_response(
                msg=msg,
                e=e,
                http_status=status.HTTP_500_INTERNAL_SERVER_ERROR)

        try:
            dbaas_team = Team.objects.get(name=team)
        except ObjectDoesNotExist as e:
            msg = "Team does not exist."
            return log_and_response(
                msg=msg,
                e=e,
                http_status=status.HTTP_500_INTERNAL_SERVER_ERROR)

        try:
            dbaas_user.team_set.get(name=dbaas_team.name)
        except ObjectDoesNotExist as e:
            msg = "The user is not on {} team.".format(dbaas_team.name)
            return log_and_response(
                msg=msg,
                e=e,
                http_status=status.HTTP_500_INTERNAL_SERVER_ERROR)

        # LOG.info('Tsuru Service Remove plan {} | {}'.format(database.plan.tsuru_label, plan))
        #
        # if database.plan.tsuru_label != plan:
        #     msg = "Change plan is not allowed"
        #     return log_and_response(
        #         msg=msg, e=msg, http_status=status.HTTP_500_INTERNAL_SERVER_ERROR
        #     )

        database.team = dbaas_team
        database.save()

        return Response(status=status.HTTP_204_NO_CONTENT)