Phase in new beta/vm view for creating vms + orders + bills
This commit is contained in:
		
					parent
					
						
							
								2cda6441f4
							
						
					
				
			
			
				commit
				
					
						eea654a9f8
					
				
			
		
					 5 changed files with 65 additions and 4 deletions
				
			
		| 
						 | 
				
			
			@ -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:
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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.
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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.
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue