Merge pull request #261 from Modulos/public_ip

Public ip
This commit is contained in:
Levi Velázquez 2017-05-23 11:10:08 -05:00 committed by GitHub
commit bed670c9e3
2 changed files with 42 additions and 6 deletions

View file

@ -41,11 +41,17 @@
<div class="col-md-12 inline-headers"> <div class="col-md-12 inline-headers">
<h3>{{virtual_machine.hosting_company_name}}</h3> <h3>{{virtual_machine.hosting_company_name}}</h3>
{% if virtual_machine.ip %} {% if virtual_machine.ipv6 %}
<div class="pull-right right-place"> <div class="pull-right right-place">
<button type="link" data-clipboard-text="{{virtual_machine.ip}}" id="copy_vm_id" class="to_copy btn btn-link" <button type="link"
data-clipboard-text="{{virtual_machine.ipv4}}" id="copy_vm_id" class="to_copy btn btn-link"
data-toggle="tooltip" data-placement="bottom" title="Copied" data-trigger="click"> data-toggle="tooltip" data-placement="bottom" title="Copied" data-trigger="click">
Ip: {{virtual_machine.ip}} <i class="fa fa-files-o" aria-hidden="true"></i> Ipv4: {{virtual_machine.ipv4}} <i class="fa fa-files-o" aria-hidden="true"></i>
</button>
<button type="link"
data-clipboard-text="{{virtual_machine.ipv6}}" id="copy_vm_id" class="to_copy btn btn-link"
data-toggle="tooltip" data-placement="bottom" title="Copied" data-trigger="click">
Ipv6: {{virtual_machine.ipv6}} <i class="fa fa-files-o" aria-hidden="true"></i>
</button> </button>
</div> </div>
{% else %} {% else %}

View file

@ -1,4 +1,5 @@
import oca import oca
import ipaddress
from rest_framework import serializers from rest_framework import serializers
@ -86,9 +87,8 @@ class VirtualMachineSerializer(serializers.Serializer):
disk_size = serializers.SerializerMethodField() disk_size = serializers.SerializerMethodField()
ip = serializers.CharField(read_only=True, ipv4 = serializers.SerializerMethodField()
source='user_template.ungleich_public_ip', ipv6 = serializers.SerializerMethodField()
default='-')
vm_id = serializers.IntegerField(read_only=True, source='id') vm_id = serializers.IntegerField(read_only=True, source='id')
state = serializers.CharField(read_only=True, source='str_state') state = serializers.CharField(read_only=True, source='str_state')
price = serializers.SerializerMethodField() price = serializers.SerializerMethodField()
@ -152,3 +152,33 @@ class VirtualMachineSerializer(serializers.Serializer):
template_id = obj.template.template_id template_id = obj.template.template_id
template = OpenNebulaManager().get_template(template_id) template = OpenNebulaManager().get_template(template_id)
return template.name return template.name
def get_ipv4(self, obj):
nic = obj.template.nics[0]
if 'vm-ipv6-nat64-ipv4' in nic.network and is_in_v4_range(nic.mac):
return str(v4_from_mac(nic.mac))
else:
return '-'
def get_ipv6(self, obj):
nic = obj.template.nics[0]
return nic.ip6_global
def hexstr2int(string):
return int(string.replace(':', ''), 16)
FIRST_MAC = hexstr2int('02:00:b3:39:79:4d')
FIRST_V4 = ipaddress.ip_address('185.203.112.2')
COUNT = 1000
def v4_from_mac(mac):
"""Calculates the IPv4 address from a MAC address.
mac: string (the colon-separated representation)
returns: ipaddress.ip_address object with the v4 address
"""
return FIRST_V4 + (hexstr2int(mac) - FIRST_MAC)
def is_in_v4_range(mac):
return FIRST_MAC <= hexstr2int(mac) < FIRST_MAC + 1000