Revision 92d2d1ce snf-cyclades-app/synnefo/api/servers.py

b/snf-cyclades-app/synnefo/api/servers.py
43 43
from snf_django.lib.api import faults, utils
44 44

  
45 45
from synnefo.api import util
46
from synnefo.db import query as db_query
46 47
from synnefo.db.models import (VirtualMachine, VirtualMachineMetadata)
47 48
from synnefo.logic import servers, utils as logic_utils
48 49

  
......
108 109
    d = {'id': nic.id,
109 110
         'network_id': str(nic.network.id),
110 111
         'mac_address': nic.mac,
111
         'ipv4': nic.ipv4,
112
         'ipv6': nic.ipv6,
113
         'OS-EXT-IPS:type': nic.ip_type.lower()}
112
         'ipv4': '',
113
         'ipv6': ''}
114
    for ip in nic.ips.filter(deleted=False).select_related("subnet"):
115
        ip_type = "floating" if ip.floating_ip else "fixed"
116
        if ip.subnet.ipversion == 4:
117
            d["ipv4"] = ip.address
118
            d["OS-EXT-IPS:type"] = ip_type
119
        else:
120
            d["ipv6"] = ip.address
121
            d["OS-EXT-IPS:type"] = ip_type
114 122

  
115 123
    if nic.firewall_profile:
116 124
        d['firewallProfile'] = nic.firewall_profile
......
121 129
    addresses = {}
122 130
    for nic in attachments:
123 131
        net_nics = []
124
        net_nics.append({"version": 4,
125
                         "addr": nic["ipv4"],
126
                         "OS-EXT-IPS:type": nic["OS-EXT-IPS:type"]})
132
        if nic["ipv4"]:
133
            net_nics.append({"version": 4,
134
                             "addr": nic["ipv4"],
135
                             "OS-EXT-IPS:type": nic["OS-EXT-IPS:type"]})
127 136
        if nic["ipv6"]:
128 137
            net_nics.append({"version": 6,
129 138
                             "addr": nic["ipv6"],
......
180 189
def get_server_fqdn(vm):
181 190
    fqdn_setting = settings.CYCLADES_SERVERS_FQDN
182 191
    if fqdn_setting is None:
183
        public_nics = vm.nics.filter(network__public=True, state="ACTIVE")
184 192
        # Return the first public IPv4 address if exists
185
        ipv4_nics = public_nics.exclude(ipv4=None)
186
        if ipv4_nics:
187
            return ipv4_nics[0].ipv4
188
        # Else return the first public IPv6 address if exists
189
        ipv6_nics = public_nics.exclude(ipv6=None)
190
        if ipv6_nics:
191
            return ipv6_nics[0].ipv6
192
        return ""
193
        address = db_query.get_server_public_ip(server=vm, version=4)
194
        if address is None:
195
            # Else return the first public IPv6 address if exists
196
            address = db_query.get_server_public_ip(server=vm, version=6)
197
        if address is None:
198
            return ""
199
        else:
200
            return address
193 201
    elif isinstance(fqdn_setting, basestring):
194 202
        return fqdn_setting % {"id": vm.id}
195 203
    else:
......
214 222
    port_forwarding = {}
215 223
    for dport, to_dest in settings.CYCLADES_PORT_FORWARDING.items():
216 224
        if hasattr(to_dest, "__call__"):
217
            public_nics = vm.nics.filter(network__public=True, state="ACTIVE")\
218
                                 .exclude(ipv4=None).order_by('index')
219
            if public_nics:
220
                vm_ipv4 = public_nics[0].ipv4
221
            else:
222
                vm_ipv4 = None
223
            to_dest = to_dest(vm_ipv4, vm.id, fqdn, vm.userid)
225
            address = db_query.get_server_public_ip(server=vm, version=4)
226
            to_dest = to_dest(address, vm.id, fqdn, vm.userid)
224 227
        msg = ("Invalid setting: CYCLADES_PORT_FOWARDING."
225 228
               " Value must be a tuple of two elements (host, port).")
226 229
        if to_dest is None:

Also available in: Unified diff