From 088615f9d6e3a4098509c059aa25b37bce99d8f9 Mon Sep 17 00:00:00 2001 From: pablohashescobar Date: Mon, 30 Oct 2023 17:38:18 +0530 Subject: [PATCH] dev: instance licenses --- apiserver/plane/license/api/views/instance.py | 96 +++++++++++-------- .../0002_instance_last_checked_at.py | 20 ++++ 2 files changed, 76 insertions(+), 40 deletions(-) create mode 100644 apiserver/plane/license/migrations/0002_instance_last_checked_at.py diff --git a/apiserver/plane/license/api/views/instance.py b/apiserver/plane/license/api/views/instance.py index 22f68370a..08e41a9a5 100644 --- a/apiserver/plane/license/api/views/instance.py +++ b/apiserver/plane/license/api/views/instance.py @@ -37,56 +37,72 @@ class InstanceEndpoint(BaseAPIView): status=status.HTTP_400_BAD_REQUEST, ) - user = User.objects.filter(email=email).first() - if user is None: - user = User.objects.create( - email=email, - username=uuid.uuid4().hex, + instance = Instance.objects.first() + + if instance is None: + # Register the instance + user = User.objects.filter(email=email).first() + if user is None: + user = User.objects.create( + email=email, + username=uuid.uuid4().hex, + ) + user.set_password(password) + user.save() + else: + if not user.check_password(password): + return Response( + { + "error": "Sorry, we could not find a user with the provided credentials. Please try again." + }, + status=status.HTTP_401_UNAUTHORIZED, + ) + + LICENSE_ENGINE_BASE_URL = os.environ.get("LICENSE_ENGINE_BASE_URL", "") + + headers = {"Content-Type": "application/json"} + + payload = { + "email": email, + "version": data.get("version", 0.1), + "domain": str(request.headers.get("Host")), + } + + response = requests.post( + f"{LICENSE_ENGINE_BASE_URL}/api/instances", + headers=headers, + data=json.dumps(payload), ) - user.set_password(password) - user.save() - else: - if not user.check_password(password): + + if response.status_code == 201: + data = response.json() + instance = Instance.objects.create( + instance_id=data.get("id"), + license_key=data.get("license_key"), + api_key=data.get("api_key"), + version=data.get("version"), + email=data.get("email"), + user=user, + last_checked_at=timezone.now(), + ) return Response( { - "error": "Sorry, we could not find a user with the provided credentials. Please try again." + "id": str(instance.instance_id), + "message": "Instance registered succesfully", }, - status=status.HTTP_401_UNAUTHORIZED, + status=status.HTTP_200_OK, ) - LICENSE_ENGINE_BASE_URL = os.environ.get("LICENSE_ENGINE_BASE_URL", "") - - headers = {"Content-Type": "application/json"} - - payload = {"email": email, "version": data.get("version", 0.1)} - - response = requests.post( - f"{LICENSE_ENGINE_BASE_URL}/api/instances", - headers=headers, - data=json.dumps(payload), - ) - - if response.status_code == 201: - data = response.json() - instance = Instance.objects.create( - instance_id=data.get("id"), - license_key=data.get("license_key"), - api_key=data.get("api_key"), - version=data.get("version"), - email=data.get("email"), - user=user, - last_checked_at=timezone.now(), - ) return Response( - { - "id": str(instance.instance_id), - "message": "Instance registered succesfully", - }, - status=status.HTTP_200_OK, + {"error": "Unable to create instance"}, status=response.status_code ) return Response( - {"error": "Unable to create instance"}, status=response.status_code + { + "message": "Instance is already registered", + "instance_id": str(instance.id), + }, + status=status.HTTP_200_OK, ) def get(self, request): diff --git a/apiserver/plane/license/migrations/0002_instance_last_checked_at.py b/apiserver/plane/license/migrations/0002_instance_last_checked_at.py new file mode 100644 index 000000000..3ee6555d4 --- /dev/null +++ b/apiserver/plane/license/migrations/0002_instance_last_checked_at.py @@ -0,0 +1,20 @@ +# Generated by Django 4.2.3 on 2023-10-30 11:42 + +import datetime +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('license', '0001_initial'), + ] + + operations = [ + migrations.AddField( + model_name='instance', + name='last_checked_at', + field=models.DateTimeField(default=datetime.datetime(2023, 10, 30, 11, 42, 52, 273809, tzinfo=datetime.timezone.utc)), + preserve_default=False, + ), + ]