diff --git a/uncloud_django_based/uncloud/uncloud/settings.py b/uncloud_django_based/uncloud/uncloud/settings.py index 527749d..884c370 100644 --- a/uncloud_django_based/uncloud/uncloud/settings.py +++ b/uncloud_django_based/uncloud/uncloud/settings.py @@ -172,10 +172,6 @@ OPENNEBULA_URL = 'https://opennebula.example.com:2634/RPC2' # user:pass for accessing opennebula OPENNEBULA_USER_PASS = 'user:password' -# See https://django-auth-ldap.readthedocs.io/en/latest/authentication.html -LDAP_ADMIN_DN="" -LDAP_ADMIN_PASSWORD="" -LDAP_SERVER_URI = "" # Stripe (Credit Card payments) STRIPE_KEY="" @@ -184,6 +180,7 @@ STRIPE_PUBLIC_KEY="" # The django secret key SECRET_KEY=get_random_secret_key() +ALLOWED_HOSTS = [] # Overwrite settings with local settings, if existing try: diff --git a/uncloud_django_based/uncloud/uncloud/urls.py b/uncloud_django_based/uncloud/uncloud/urls.py index 4d0ada1..5539846 100644 --- a/uncloud_django_based/uncloud/uncloud/urls.py +++ b/uncloud_django_based/uncloud/uncloud/urls.py @@ -31,12 +31,16 @@ from uncloud_service import views as serviceviews router = routers.DefaultRouter() +# Beta endpoints +router.register(r'beta/vm', vmviews.NicoVMProductViewSet, basename='nicovmproduct') + # VM router.register(r'vm/snapshot', vmviews.VMSnapshotProductViewSet, basename='vmsnapshotproduct') router.register(r'vm/diskimage', vmviews.VMDiskImageProductViewSet, basename='vmdiskimageproduct') router.register(r'vm/disk', vmviews.VMDiskProductViewSet, basename='vmdiskproduct') router.register(r'vm/vm', vmviews.VMProductViewSet, basename='vmproduct') + # creates VM from os image #router.register(r'vm/ipv6onlyvm', vmviews.VMProductViewSet, basename='vmproduct') # ... AND adds IPv4 mapping diff --git a/uncloud_django_based/uncloud/uncloud_vm/models.py b/uncloud_django_based/uncloud/uncloud_vm/models.py index 5dacdbe..39a5f40 100644 --- a/uncloud_django_based/uncloud/uncloud_vm/models.py +++ b/uncloud_django_based/uncloud/uncloud_vm/models.py @@ -69,6 +69,7 @@ class VMProduct(Product): cores = models.IntegerField() ram_in_gb = models.FloatField() + # Optional disk primary_disk = models.ForeignKey('VMDiskProduct', on_delete=models.CASCADE, null=True) # Default recurring price is PER_MONTH, see uncloud_pay.models.Product. diff --git a/uncloud_django_based/uncloud/uncloud_vm/serializers.py b/uncloud_django_based/uncloud/uncloud_vm/serializers.py index 92c7fe8..2c7137e 100644 --- a/uncloud_django_based/uncloud/uncloud_vm/serializers.py +++ b/uncloud_django_based/uncloud/uncloud_vm/serializers.py @@ -120,6 +120,25 @@ class OrderVMProductSerializer(VMProductSerializer): # Nico's playground. +class NicoVMProductSerializer(serializers.ModelSerializer): + primary_disk = CreateVMDiskProductSerializer() + snapshots = VMSnapshotProductSerializer(many=True, read_only=True) + disks = VMDiskProductSerializer(many=True, read_only=True) + + class Meta: + model = VMProduct + read_only_fields = ['uuid', 'order', 'owner', 'status', + 'vmhost', 'vmcluster', + 'extra_data' ] + fields = read_only_fields + [ 'name', + 'cores', + 'ram_in_gb', + 'primary_disk', + 'snapshots', + 'disks' ] + + + class DCLVMProductSerializer(serializers.HyperlinkedModelSerializer): """ Create an interface similar to standard DCL diff --git a/uncloud_django_based/uncloud/uncloud_vm/views.py b/uncloud_django_based/uncloud/uncloud_vm/views.py index 1dead62..39b7668 100644 --- a/uncloud_django_based/uncloud/uncloud_vm/views.py +++ b/uncloud_django_based/uncloud/uncloud_vm/views.py @@ -179,6 +179,46 @@ class VMProductViewSet(ProductViewSet): return Response(VMProductSerializer(vm, context={'request': request}).data) +class NicoVMProductViewSet(ProductViewSet): + permission_classes = [permissions.IsAuthenticated] + serializer_class = NicoVMProductSerializer + + def get_queryset(self): + obj = VMProduct.objects.filter(owner=self.request.user) + + return obj + + # Use a database transaction so that we do not get half-created structure + # if something goes wrong. + @transaction.atomic + def create(self, request): + # Extract serializer data. + serializer = self.get_serializer(data=request.data) + serializer.is_valid(raise_exception=True) + order_recurring_period = serializer.validated_data.pop("recurring_period") + order_billing_address = serializer.validated_data.pop("billing_address") + + # Create base order. + order = Order( + recurring_period=order_recurring_period, + billing_address=order_billing_address, + owner=request.user, + starting_date=timezone.now() + ) + order.save() + + # Create disk image. + disk = VMDiskProduct(owner=request.user, order=order, + **serializer.validated_data.pop("primary_disk")) + + # Create VM. + vm = serializer.save(owner=request.user, order=order, primary_disk=disk) + disk.vm = vm + disk.save() + + return Response(VMProductSerializer(vm, context={'request': request}).data) + + ### # Admin stuff.