diff --git a/hosting/views.py b/hosting/views.py
index a694373a..3044590b 100644
--- a/hosting/views.py
+++ b/hosting/views.py
@@ -355,17 +355,6 @@ class GenerateVMSSHKeysView(LoginRequiredMixin, FormView):
return render(self.request, self.template_name, context)
def post(self, request, *args, **kwargs):
-
- # try:
- # UserHostingKey.objects.get(
- # user=self.request.user
- # )
- # return HttpResponseRedirect(reverse('hosting:key_pair'))
-
- # except UserHostingKey.DoesNotExist:
- # pass
-
-
form = self.get_form()
if form.is_valid():
return self.form_valid(form)
@@ -418,11 +407,7 @@ class PaymentVMView(LoginRequiredMixin, FormView):
return context
def get(self, request, *args, **kwargs):
- try:
- UserHostingKey.objects.get(
- user=self.request.user
- )
- except UserHostingKey.DoesNotExist:
+ if not UserHostingKey.objects.filter( user=self.request.user).exists():
messages.success(
request,
'In order to create a VM, you create/upload your SSH KEY first.'
@@ -484,14 +469,16 @@ class PaymentVMView(LoginRequiredMixin, FormView):
manager = OpenNebulaManager(email=owner.email,
password=owner.password)
# Get user ssh key
- try:
- user_key = UserHostingKey.objects.get(
- user=self.request.user
- )
-
- except UserHostingKey.DoesNotExist:
- pass
-
+ if not UserHostingKey.objects.filter( user=self.request.user).exists():
+ context.update({
+ 'sshError': 'error',
+ 'form': form
+ })
+ return render(request, self.template_name, context)
+ # For now just get first one
+ user_key = UserHostingKey.objects.filter(
+ user=self.request.user).first()
+
# Create a vm using logged user
vm_id = manager.create_vm(
template_id=vm_template_id,
@@ -636,11 +623,7 @@ class CreateVirtualMachinesView(LoginRequiredMixin, View):
def get(self, request, *args, **kwargs):
- try:
- UserHostingKey.objects.get(
- user=self.request.user
- )
- except UserHostingKey.DoesNotExist:
+ if not UserHostingKey.objects.filter( user=self.request.user).exists():
messages.success(
request,
'In order to create a VM, you need to create/upload your SSH KEY first.'
@@ -664,7 +647,7 @@ class CreateVirtualMachinesView(LoginRequiredMixin, View):
)
context = {
'error': 'connection'
- }
+ }
return render(request, self.template_name, context)
diff --git a/opennebula_api/models.py b/opennebula_api/models.py
index 4fa5d011..5d93c1bd 100644
--- a/opennebula_api/models.py
+++ b/opennebula_api/models.py
@@ -260,7 +260,6 @@ class OpenNebulaManager():
vd
{image_id}
-
""".format(size=1024 * int(specs['disk_size']),
image_id=image_id)
@@ -282,10 +281,18 @@ class OpenNebulaManager():
{image}
{image_uname}
-
""".format(size=1024 * int(specs['disk_size']),
image=image,
image_uname=image_uname)
+
+
+ if ssh_key:
+ vm_specs += """
+ {ssh}
+ YES
+
+
+ """.format(ssh=public_key)
vm_id = self.client.call(oca.VmTemplate.METHODS['instantiate'],
template.id,
'',
@@ -293,25 +300,6 @@ class OpenNebulaManager():
vm_specs,
False)
- self.oneadmin_client.call(
- 'vm.update',
- vm_id,
- """
- {ssh}
-
- """.format(ssh=ssh_key)
- )
- try:
- self.oneadmin_client.call(
- oca.VirtualMachine.METHODS['chown'],
- vm_id,
- self.opennebula_user.id,
- self.opennebula_user.group_ids[0]
- )
- except AttributeError:
- logger.info(
- 'Could not change owner for vm with id: {}.'.format(vm_id))
-
self.oneadmin_client.call(
oca.VirtualMachine.METHODS['action'],
'release',
diff --git a/opennebula_api/serializers.py b/opennebula_api/serializers.py
index 226d9f47..824ae7c6 100644
--- a/opennebula_api/serializers.py
+++ b/opennebula_api/serializers.py
@@ -15,7 +15,6 @@ class VirtualMachineTemplateSerializer(serializers.Serializer):
cores = serializers.SerializerMethodField()
disk_size = serializers.SerializerMethodField()
memory = serializers.SerializerMethodField()
- price = serializers.SerializerMethodField()
def get_cores(self, obj):
if hasattr(obj.template, 'vcpu'):
@@ -39,6 +38,8 @@ class VirtualMachineTemplateSerializer(serializers.Serializer):
def get_name(self, obj):
return obj.name.strip('public-')
+
+
class VirtualMachineSerializer(serializers.Serializer):
"""Serializer to map the virtual machine instance into JSON format."""