Commit 5fc37cc6 by Bach Dániel

one: Added Instance.nat()

parent 2a0ffb7d
...@@ -429,7 +429,7 @@ class Instance(models.Model): ...@@ -429,7 +429,7 @@ class Instance(models.Model):
def get_port(self, use_ipv6=False): def get_port(self, use_ipv6=False):
"""Get public port number for default access method.""" """Get public port number for default access method."""
proto = self.template.access_type proto = self.template.access_type
if self.template.network.nat and not use_ipv6: if self.nat and not use_ipv6:
return {"rdp": 23000, "nx": 22000, "ssh": 22000}[proto] + int(self.ip.split('.')[2]) * 256 + int(self.ip.split('.')[3]) return {"rdp": 23000, "nx": 22000, "ssh": 22000}[proto] + int(self.ip.split('.')[2]) * 256 + int(self.ip.split('.')[3])
else: else:
return {"rdp": 3389, "nx": 22, "ssh": 22}[proto] return {"rdp": 3389, "nx": 22, "ssh": 22}[proto]
...@@ -483,6 +483,15 @@ class Instance(models.Model): ...@@ -483,6 +483,15 @@ class Instance(models.Model):
self.check_if_is_save_as_done() self.check_if_is_save_as_done()
return x return x
@property
def nat(self):
if self.firewall_host is not None:
return self.firewall_host.shared_ip
elif self.template is not None:
return self.template.network.nat
else:
return False
def get_age(self): def get_age(self):
"""Get age of VM in seconds.""" """Get age of VM in seconds."""
from datetime import datetime from datetime import datetime
......
...@@ -108,7 +108,6 @@ def vm_credentials(request, iid): ...@@ -108,7 +108,6 @@ def vm_credentials(request, iid):
vm.hostname_v4 = vm.get_connect_host(use_ipv6=False) vm.hostname_v4 = vm.get_connect_host(use_ipv6=False)
vm.hostname_v6 = vm.get_connect_host(use_ipv6=True) vm.hostname_v6 = vm.get_connect_host(use_ipv6=True)
vm.is_ipv6 = is_ipv6 vm.is_ipv6 = is_ipv6
vm.nat = vm.template.network.nat
vm.port_v4 = vm.get_port(use_ipv6=False) vm.port_v4 = vm.get_port(use_ipv6=False)
vm.port_v6 = vm.get_port(use_ipv6=True) vm.port_v6 = vm.get_port(use_ipv6=True)
return render_to_response('vm-credentials.html', RequestContext(request, { 'i' : vm })) return render_to_response('vm-credentials.html', RequestContext(request, { 'i' : vm }))
...@@ -372,7 +371,6 @@ def vm_show(request, iid): ...@@ -372,7 +371,6 @@ def vm_show(request, iid):
inst.is_ipv6 = is_ipv6 inst.is_ipv6 = is_ipv6
inst.hostname_v4 = inst.get_connect_host(use_ipv6=False) inst.hostname_v4 = inst.get_connect_host(use_ipv6=False)
inst.hostname_v6 = inst.get_connect_host(use_ipv6=True) inst.hostname_v6 = inst.get_connect_host(use_ipv6=True)
inst.nat = inst.template.network.nat
inst.port_v4 = inst.get_port(use_ipv6=False) inst.port_v4 = inst.get_port(use_ipv6=False)
inst.port_v6 = inst.get_port(use_ipv6=True) inst.port_v6 = inst.get_port(use_ipv6=True)
return render_to_response("show.html", RequestContext(request,{ return render_to_response("show.html", RequestContext(request,{
...@@ -425,7 +423,7 @@ class VmPortAddView(View): ...@@ -425,7 +423,7 @@ class VmPortAddView(View):
try: try:
port = int(request.POST['port']) port = int(request.POST['port'])
inst = get_object_or_404(Instance, id=iid, owner=request.user) inst = get_object_or_404(Instance, id=iid, owner=request.user)
if inst.firewall_host.shared_ip: if inst.nat:
inst.firewall_host.add_port(proto=request.POST['proto'], inst.firewall_host.add_port(proto=request.POST['proto'],
public=None, private=port) public=None, private=port)
else: else:
...@@ -434,7 +432,6 @@ class VmPortAddView(View): ...@@ -434,7 +432,6 @@ class VmPortAddView(View):
except: except:
messages.error(request, _(u"Adding port failed.")) messages.error(request, _(u"Adding port failed."))
raise
else: else:
messages.success(request, _(u"Port %d successfully added.") % port) messages.success(request, _(u"Port %d successfully added.") % port)
return redirect('/vm/show/%d/' % int(iid)) return redirect('/vm/show/%d/' % int(iid))
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment