예제 #1
0
 def close_index(self, request, pk=None, project_pk=None):
     es_core = ElasticCore()
     index = Index.objects.get(pk=pk)
     es_core.close_index(index.name)
     index.is_open = False
     index.save()
     return Response({"message": f"Closed the index {index.name}"})
예제 #2
0
    def create(self, request, **kwargs):
        data = IndexSerializer(data=request.data)
        data.is_valid(raise_exception=True)

        es = ElasticCore()
        index = data.validated_data["name"]
        is_open = data.validated_data["is_open"]
        description = data.validated_data["description"]
        added_by = data.validated_data["added_by"]
        test = data.validated_data["test"]
        source = data.validated_data["source"]
        client = data.validated_data["client"]
        domain = data.validated_data["domain"]

        # Using get_or_create to avoid unique name constraints on creation.
        if es.check_if_indices_exist([index]):
            # Even if the index already exists, create the index object just in case
            index, is_created = Index.objects.get_or_create(name=index)

            if is_created:
                utc_time = es.get_index_creation_date(index)
                index.is_open = is_open
                index.description = description
                index.added_by = added_by
                index.test = test
                index.source = source
                index.client = client
                index.domain = domain
                index.created_at = utc_time
            index.save()
            raise ElasticIndexAlreadyExists()

        else:
            es.create_index(index=index)
            if not is_open:
                es.close_index(index)

            index, is_created = Index.objects.get_or_create(name=index)
            if is_created:
                utc_time = es.get_index_creation_date(index)
                index.is_open = is_open
                index.description = description
                index.added_by = added_by
                index.test = test
                index.source = source
                index.client = client
                index.domain = domain
                index.created_at = utc_time
            index.save()

            return Response(
                {"message": f"Added index {index} into Elasticsearch!"},
                status=status.HTTP_201_CREATED)