From 966da065b4633d878418226c164133a2e1eb8f45 Mon Sep 17 00:00:00 2001 From: catborise Date: Thu, 14 May 2020 16:45:37 +0300 Subject: [PATCH] get host info from compute not instance --- datasource/urls.py | 3 +-- datasource/views.py | 15 +++++++-------- instances/templates/instance.html | 2 +- 3 files changed, 9 insertions(+), 11 deletions(-) diff --git a/datasource/urls.py b/datasource/urls.py index ec02ad3..2979a4e 100644 --- a/datasource/urls.py +++ b/datasource/urls.py @@ -8,6 +8,5 @@ urlpatterns = [ views.os_metadata_json, name='ds_openstack_metadata'), url(r'^openstack/(?P[\w\-\.]+)/user_data$', views.os_userdata, name='ds_openstack_userdata'), - url(r'^vdi/(?P[\w\-\.]+)/$', - views.get_vdi_url, name='vdi_url'), + url(r'^vdi/(?P[0-9]+)/(?P[\w\-\.]+)/$', views.get_vdi_url, name='vdi_url'), ] diff --git a/datasource/views.py b/datasource/views.py index 6f6078c..3688d65 100644 --- a/datasource/views.py +++ b/datasource/views.py @@ -1,10 +1,10 @@ import json import socket -from django.shortcuts import render +from django.shortcuts import render, get_object_or_404 from django.http import HttpResponse, Http404 from libvirt import libvirtError from accounts.models import UserInstance, UserSSHKey -from instances.models import Instance +from computes.models import Compute from vrtManager.instance import wvmInstance @@ -88,26 +88,25 @@ def get_hostname_by_ip(ip): return addrs[0] -def get_vdi_url(request, vname): +def get_vdi_url(request, compute_id, vname): """ :param request: :param vname: :return: """ - instance = Instance.objects.get(name=vname) - compute = instance.compute + compute = get_object_or_404(Compute, pk=compute_id) data = {} try: conn = wvmInstance(compute.hostname, compute.login, compute.password, compute.type, - instance.name) + vname) fqdn = get_hostname_by_ip(compute.hostname) - url = "{}://{}:{}".format(conn.get_console_type(), fqdn, conn.get_console_port()) + url = f"{conn.get_console_type()}://{fqdn}:{conn.get_console_port()}" response = url return HttpResponse(response) except libvirtError as lib_err: - err = "Error getting vdi url for {}".format(vname) + err = f"Error getting vdi url for {vname}" raise Http404(err) diff --git a/instances/templates/instance.html b/instances/templates/instance.html index a93dbf8..3c3a4b8 100644 --- a/instances/templates/instance.html +++ b/instances/templates/instance.html @@ -1817,7 +1817,7 @@ }); $(document).ready(function () { // set vdi url - $.get("{% url 'vdi_url' vname %}", function(data) { + $.get("{% url 'vdi_url' compute_id vname %}", function(data) { $("#vdi_url_input").attr("value", data); $("#vdi_url").attr("href", data); });