def post(self, request, database_name, format=None):
        env = get_url_env(request)

        response = check_database_status(database_name, env)
        if type(response) != Database:
            return response

        database = response
        data = request.DATA
        LOG.debug("Request DATA {}".format(data))
        unit_host = data.get('unit-host') + '/32'
        created = False

        transaction.set_autocommit(False)
        database_bind = DatabaseBind(database= database,
            bind_address= unit_host, binds_requested=1)

        try:
            database_bind.save()
            created = True
        except IntegrityError, e:
            LOG.info("IntegrityError: {}".format(e))

            try:
                db_bind = DatabaseBind.objects.get(database= database, bind_address=unit_host)

                bind = DatabaseBind.objects.select_for_update().filter(id= db_bind.id)[0]
                if bind.bind_status != DESTROYING:
                    bind.binds_requested+=1
                    bind.save()
            except (IndexError, ObjectDoesNotExist), e:
                LOG.debug("DatabaseBind is under destruction! {}".format(e))
                msg = "DatabaseBind does not exist"
                return log_and_response(msg=msg, e=e,
                    http_status=status.HTTP_500_INTERNAL_SERVER_ERROR)
Exemple #2
0
    def post(self, request, database_name, format=None):
        env = get_url_env(request)

        database = check_database_status(database_name, env)
        if type(database) != Database:
            return database

        data = request.DATA
        LOG.debug("Request DATA {}".format(data))

        unit_network = check_acl_service_and_get_unit_network(database, data)
        if type(unit_network) == Response:
            return unit_network

        created = False
        transaction.set_autocommit(False)
        database_bind = DatabaseBind(
            database=database, bind_address=unit_network, binds_requested=1
        )

        try:
            database_bind.save()
            created = True
        except IntegrityError as e:
            LOG.info("IntegrityError: {}".format(e))

            try:
                db_bind = DatabaseBind.objects.get(database=database,
                                                   bind_address=unit_network)

                bind = DatabaseBind.objects.select_for_update().filter(
                    id=db_bind.id)[0]
                if bind.bind_status in [CREATED, CREATING]:
                    bind.binds_requested += 1
                    bind.save()
                else:
                    raise Exception("Binds are beeing destroyed!")
            except Exception as e:
                LOG.debug("DatabaseBind is under destruction! {}".format(e))
                msg = "We are destroying your binds to {}. Please wait.".format(
                    database_name
                )
                return log_and_response(
                    msg=msg, e=e,
                    http_status=status.HTTP_500_INTERNAL_SERVER_ERROR
                )

        finally:
            LOG.debug("Finishing transaction!")
            transaction.commit()
            transaction.set_autocommit(True)

        if created:
            bind_address_on_database.delay(
                database_bind=database_bind,
                user=request.user
            )

        return Response(None, status.HTTP_201_CREATED)
    def post(self, request, database_name, format=None):
        env = get_url_env(request)

        response = check_database_status(database_name, env)
        if type(response) != Database:
            return response

        database = response
        data = request.DATA
        LOG.debug("Request DATA {}".format(data))

        unit_network = get_network_from_ip(
            data.get('unit-host'), database.environment
        )

        created = False

        transaction.set_autocommit(False)
        database_bind = DatabaseBind(
            database=database, bind_address=unit_network, binds_requested=1
        )

        try:
            database_bind.save()
            created = True
        except IntegrityError as e:
            LOG.info("IntegrityError: {}".format(e))

            try:
                db_bind = DatabaseBind.objects.get(database=database,
                                                   bind_address=unit_network)

                bind = DatabaseBind.objects.select_for_update().filter(
                    id=db_bind.id)[0]
                if bind.bind_status in [CREATED, CREATING]:
                    bind.binds_requested += 1
                    bind.save()
                else:
                    raise Exception("Binds are beeing destroyed!")
            except (IndexError, ObjectDoesNotExist) as e:
                LOG.debug("DatabaseBind is under destruction! {}".format(e))
                msg = "We are destroying your binds to {}. Please wait.".format(database_name)
                return log_and_response(
                    msg=msg, e=e, http_status=status.HTTP_500_INTERNAL_SERVER_ERROR)

        finally:
            LOG.debug("Finishing transaction!")
            transaction.commit()
            transaction.set_autocommit(True)

        if created:
            bind_address_on_database.delay(database_bind=database_bind,
                                           user=request.user)

        return Response(None, status.HTTP_201_CREATED)
    def post(self, request, database_name, format=None):
        env = get_url_env(request)

        response = check_database_status(database_name, env)
        if type(response) != Database:
            return response

        database = response
        data = request.DATA
        LOG.debug("Request DATA {}".format(data))
        unit_host = data.get('unit-host') + '/32'
        created = False

        transaction.set_autocommit(False)
        database_bind = DatabaseBind(database=database,
                                     bind_address=unit_host,
                                     binds_requested=1)

        try:
            database_bind.save()
            created = True
        except IntegrityError, e:
            LOG.info("IntegrityError: {}".format(e))

            try:
                db_bind = DatabaseBind.objects.get(database=database,
                                                   bind_address=unit_host)

                bind = DatabaseBind.objects.select_for_update().filter(
                    id=db_bind.id)[0]
                if bind.bind_status != DESTROYING:
                    bind.binds_requested += 1
                    bind.save()
            except (IndexError, ObjectDoesNotExist), e:
                LOG.debug("DatabaseBind is under destruction! {}".format(e))
                msg = "DatabaseBind does not exist"
                return log_and_response(
                    msg=msg,
                    e=e,
                    http_status=status.HTTP_500_INTERNAL_SERVER_ERROR)