Refactored saving public key functionality to opennebula_api.models.save_public_key. Also, now creating hosting VMs loaded by default with ONEADMIN_USER_SSH_PUBLIC_KEY

This commit is contained in:
M.Ravi 2017-08-29 17:51:58 +02:00
parent 12255364b2
commit 34580b6514

View file

@ -27,8 +27,8 @@ from membership.models import CustomUser, StripeCustomer
from utils.stripe_utils import StripeUtils from utils.stripe_utils import StripeUtils
from utils.forms import BillingAddressForm, PasswordResetRequestForm, \ from utils.forms import BillingAddressForm, PasswordResetRequestForm, \
UserBillingAddressForm UserBillingAddressForm
from utils.views import PasswordResetViewMixin, PasswordResetConfirmViewMixin, \ from utils.views import PasswordResetViewMixin, \
LoginViewMixin PasswordResetConfirmViewMixin, LoginViewMixin
from utils.mailer import BaseEmail from utils.mailer import BaseEmail
from .models import HostingOrder, HostingBill, HostingPlan, UserHostingKey from .models import HostingOrder, HostingBill, HostingPlan, UserHostingKey
from .forms import HostingUserSignupForm, HostingUserLoginForm, \ from .forms import HostingUserSignupForm, HostingUserLoginForm, \
@ -43,8 +43,9 @@ import logging
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
CONNECTION_ERROR = "Your VMs cannot be displayed at the moment due to a backend \ CONNECTION_ERROR = "Your VMs cannot be displayed at the moment due to a \
connection error. please try again in a few minutes." backend connection error. please try again in a few \
minutes."
class DjangoHostingView(ProcessVMSelectionMixin, View): class DjangoHostingView(ProcessVMSelectionMixin, View):
@ -457,30 +458,14 @@ class SSHKeyCreateView(LoginRequiredMixin, FormView):
}) })
owner = self.request.user owner = self.request.user
# Get all VMs belonging to the user manager = OpenNebulaManager(
allorders = HostingOrder.objects.filter(customer__user=owner) email=owner.email,
if len(allorders) > 0: password=owner.password
logger.debug("The user {} has 1 or more VMs. We need to configure " )
"the ssh keys.".format(self.request.user.email)) public_key = form.cleaned_data['public_key']
hosts = [order.vm_id for order in allorders] if type(public_key) is bytes:
public_key = public_key.decode()
manager.save_public_key([public_key])
else:
logger.debug("The user {} has no VMs. We don't need to configure "
"the ssh keys.".format(self.request.user.email))
# manager = OpenNebulaManager()
#
# # Get user ssh key
# public_key = str(form.cleaned_data.get('public_key', ''))
# # Add ssh key to user
# try:
# manager.add_public_key(
# user=owner, public_key=public_key, merge=True)
# except ConnectionError:
# pass
# except WrongNameError:
# pass
return HttpResponseRedirect(self.success_url) return HttpResponseRedirect(self.success_url)
def post(self, request, *args, **kwargs): def post(self, request, *args, **kwargs):
@ -612,16 +597,12 @@ class PaymentVMView(LoginRequiredMixin, FormView):
'form': form 'form': form
}) })
return render(request, self.template_name, context) 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 # Create a vm using logged user
vm_id = manager.create_vm( vm_id = manager.create_vm(
template_id=vm_template_id, template_id=vm_template_id,
# XXX: Confi
specs=specs, specs=specs,
ssh_key=user_key.public_key, ssh_key=settings.ONEADMIN_USER_SSH_PUBLIC_KEY,
) )
# Create a Hosting Order # Create a Hosting Order