Revision 78c165cf snf-cyclades-app/synnefo/api/servers.py

b/snf-cyclades-app/synnefo/api/servers.py
104 104

  
105 105

  
106 106
def nic_to_dict(nic):
107
    ip_type = "floating" if nic.is_floating_ip else "fixed"
107 108
    d = {'id': util.construct_nic_id(nic),
108 109
         'network_id': str(nic.network.id),
109 110
         'mac_address': nic.mac,
110 111
         'ipv4': nic.ipv4 if nic.ipv4 else None,
111
         'ipv6': nic.ipv6 if nic.ipv6 else None}
112
         'ipv6': nic.ipv6 if nic.ipv6 else None,
113
         'OS-EXT-IPS:type': ip_type}
112 114

  
113 115
    if nic.firewall_profile:
114 116
        d['firewallProfile'] = nic.firewall_profile
115 117
    return d
116 118

  
117 119

  
118
def nics_to_addresses(nics):
120
def attachments_to_addresses(attachments):
119 121
    addresses = {}
120
    for nic in nics:
122
    for nic in attachments:
121 123
        net_nics = []
122 124
        net_nics.append({"version": 4,
123
                         "addr": nic.ipv4,
124
                         "OS-EXT-IPS:type": "fixed"})
125
        if nic.ipv6:
125
                         "addr": nic["ipv4"],
126
                         "OS-EXT-IPS:type": nic["OS-EXT-IPS:type"]})
127
        if nic["ipv6"]:
126 128
            net_nics.append({"version": 6,
127
                             "addr": nic.ipv6,
128
                             "OS-EXT-IPS:type": "fixed"})
129
        addresses[nic.network.id] = net_nics
129
                             "addr": nic["ipv6"],
130
                             "OS-EXT-IPS:type": nic["OS-EXT-IPS:type"]})
131
        addresses[nic["network_id"]] = net_nics
130 132
    return addresses
131 133

  
132 134

  
......
154 156
        vm_nics = vm.nics.filter(state="ACTIVE").order_by("index")
155 157
        attachments = map(nic_to_dict, vm_nics)
156 158
        d['attachments'] = attachments
157
        d['addresses'] = nics_to_addresses(vm_nics)
159
        d['addresses'] = attachments_to_addresses(attachments)
158 160

  
159 161
        # include the latest vm diagnostic, if set
160 162
        diagnostic = vm.get_last_diagnostic()
......
432 434
    log.debug('list_addresses %s', server_id)
433 435
    vm = util.get_vm(server_id, request.user_uniq)
434 436
    attachments = [nic_to_dict(nic) for nic in vm.nics.all()]
435
    addresses = nics_to_addresses(vm.nics.all())
437
    addresses = attachments_to_addresses(attachments)
436 438

  
437 439
    if request.serialization == 'xml':
438 440
        data = render_to_string('list_addresses.xml', {'addresses': addresses})
......
456 458
    machine = util.get_vm(server_id, request.user_uniq)
457 459
    network = util.get_network(network_id, request.user_uniq)
458 460
    nics = machine.nics.filter(network=network).all()
459
    addresses = nics_to_addresses(nics)
461
    addresses = attachments_to_addresses(map(nic_to_dict, nics))
460 462

  
461 463
    if request.serialization == 'xml':
462 464
        data = render_to_string('address.xml', {'addresses': addresses})

Also available in: Unified diff