~ubuntu-branches/ubuntu/trusty/horizon/trusty

« back to all changes in this revision

Viewing changes to horizon/api/quantum.py

  • Committer: Package Import Robot
  • Author(s): Chuck Short
  • Date: 2012-03-09 11:50:22 UTC
  • mfrom: (1.1.9)
  • Revision ID: package-import@ubuntu.com-20120309115022-ymiww5i58rbg97my
Tags: 2012.1~rc1~20120308.1479-0ubuntu1
* New upstream version.
* debian/rules: Fix symlink when installing horizon.
  (LP: #947118)
* debian/control: Add python-django-nose as a dep. (LP: #944235)
* debian/control: Fix broken depends.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# vim: tabstop=4 shiftwidth=4 softtabstop=4
2
 
 
3
 
# Copyright 2012 United States Government as represented by the
4
 
# Administrator of the National Aeronautics and Space Administration.
5
 
# All Rights Reserved.
6
 
#
7
 
# Copyright 2012 Nebula, Inc.
8
 
#
9
 
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
10
 
#    not use this file except in compliance with the License. You may obtain
11
 
#    a copy of the License at
12
 
#
13
 
#         http://www.apache.org/licenses/LICENSE-2.0
14
 
#
15
 
#    Unless required by applicable law or agreed to in writing, software
16
 
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17
 
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
18
 
#    License for the specific language governing permissions and limitations
19
 
#    under the License.
20
 
 
21
 
 
22
 
from __future__ import absolute_import
23
 
 
24
 
import logging
25
 
import urlparse
26
 
 
27
 
from quantum import client as quantum_client
28
 
 
29
 
from horizon.api.base import url_for
30
 
from horizon.api import nova
31
 
 
32
 
 
33
 
LOG = logging.getLogger(__name__)
34
 
 
35
 
 
36
 
# FIXME(gabriel): Add object wrappers for Quantum client. The quantum client
37
 
#                 returns plain dicts (a la Glance) which we should wrap.
38
 
 
39
 
def quantum_api(request):
40
 
    tenant = request.user.tenant_id
41
 
    url = urlparse.urlparse(url_for(request, 'network'))
42
 
    return quantum_client.Client(url.hostname, url.port, False, tenant, 'json')
43
 
 
44
 
 
45
 
def quantum_list_networks(request):
46
 
    return quantum_api(request).list_networks()
47
 
 
48
 
 
49
 
def quantum_network_details(request, network_id):
50
 
    return quantum_api(request).show_network_details(network_id)
51
 
 
52
 
 
53
 
def quantum_list_ports(request, network_id):
54
 
    return quantum_api(request).list_ports(network_id)
55
 
 
56
 
 
57
 
def quantum_port_details(request, network_id, port_id):
58
 
    return quantum_api(request).show_port_details(network_id, port_id)
59
 
 
60
 
 
61
 
def quantum_create_network(request, data):
62
 
    return quantum_api(request).create_network(data)
63
 
 
64
 
 
65
 
def quantum_delete_network(request, network_id):
66
 
    return quantum_api(request).delete_network(network_id)
67
 
 
68
 
 
69
 
def quantum_update_network(request, network_id, data):
70
 
    return quantum_api(request).update_network(network_id, data)
71
 
 
72
 
 
73
 
def quantum_create_port(request, network_id):
74
 
    return quantum_api(request).create_port(network_id)
75
 
 
76
 
 
77
 
def quantum_delete_port(request, network_id, port_id):
78
 
    return quantum_api(request).delete_port(network_id, port_id)
79
 
 
80
 
 
81
 
def quantum_attach_port(request, network_id, port_id, data):
82
 
    return quantum_api(request).attach_resource(network_id, port_id, data)
83
 
 
84
 
 
85
 
def quantum_detach_port(request, network_id, port_id):
86
 
    return quantum_api(request).detach_resource(network_id, port_id)
87
 
 
88
 
 
89
 
def quantum_set_port_state(request, network_id, port_id, data):
90
 
    return quantum_api(request).update_port(network_id, port_id, data)
91
 
 
92
 
 
93
 
def quantum_port_attachment(request, network_id, port_id):
94
 
    return quantum_api(request).show_port_attachment(network_id, port_id)
95
 
 
96
 
 
97
 
def get_vif_ids(request):
98
 
    vifs = []
99
 
    attached_vifs = []
100
 
    # Get a list of all networks
101
 
    networks_list = quantum_api(request).list_networks()
102
 
    for network in networks_list['networks']:
103
 
        ports = quantum_api(request).list_ports(network['id'])
104
 
        # Get port attachments
105
 
        for port in ports['ports']:
106
 
            port_attachment = quantum_api(request).show_port_attachment(
107
 
                                                    network['id'],
108
 
                                                    port['id'])
109
 
            if port_attachment['attachment']:
110
 
                attached_vifs.append(
111
 
                    port_attachment['attachment']['id'].encode('ascii'))
112
 
    # Get all instances
113
 
    instances = nova.server_list(request)
114
 
    # Get virtual interface ids by instance
115
 
    for instance in instances:
116
 
        id = instance.id
117
 
        instance_vifs = nova.virtual_interfaces_list(request, id)
118
 
        for vif in instance_vifs:
119
 
            # Check if this VIF is already connected to any port
120
 
            if str(vif.id) in attached_vifs:
121
 
                vifs.append({
122
 
                    'id': vif.id,
123
 
                    'instance': instance.id,
124
 
                    'instance_name': instance.name,
125
 
                    'available': False})
126
 
            else:
127
 
                vifs.append({
128
 
                    'id': vif.id,
129
 
                    'instance': instance.id,
130
 
                    'instance_name': instance.name,
131
 
                    'available': True})
132
 
    return vifs