root / snf-cyclades-app / synnefo / api / management / commands / network-list.py @ c6fe2f41
History | View | Annotate | Download (5.2 kB)
1 |
# Copyright 2012-2013 GRNET S.A. All rights reserved.
|
---|---|
2 |
#
|
3 |
# Redistribution and use in source and binary forms, with or
|
4 |
# without modification, are permitted provided that the following
|
5 |
# conditions are met:
|
6 |
#
|
7 |
# 1. Redistributions of source code must retain the above
|
8 |
# copyright notice, this list of conditions and the following
|
9 |
# disclaimer.
|
10 |
#
|
11 |
# 2. Redistributions in binary form must reproduce the above
|
12 |
# copyright notice, this list of conditions and the following
|
13 |
# disclaimer in the documentation and/or other materials
|
14 |
# provided with the distribution.
|
15 |
#
|
16 |
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
|
17 |
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
18 |
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
19 |
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
|
20 |
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
21 |
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
22 |
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
23 |
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
24 |
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
25 |
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
26 |
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
27 |
# POSSIBILITY OF SUCH DAMAGE.
|
28 |
#
|
29 |
# The views and conclusions contained in the software and
|
30 |
# documentation are those of the authors and should not be
|
31 |
# interpreted as representing official policies, either expressed
|
32 |
# or implied, of GRNET S.A.
|
33 |
|
34 |
from optparse import make_option |
35 |
|
36 |
from snf_django.management.commands import ListCommand |
37 |
from synnefo.db.models import Network |
38 |
from synnefo.settings import (CYCLADES_SERVICE_TOKEN as ASTAKOS_TOKEN, |
39 |
ASTAKOS_BASE_URL) |
40 |
|
41 |
from logging import getLogger |
42 |
log = getLogger(__name__) |
43 |
|
44 |
|
45 |
class Command(ListCommand): |
46 |
help = "List networks"
|
47 |
|
48 |
option_list = ListCommand.option_list + ( |
49 |
make_option( |
50 |
'--public',
|
51 |
action='store_true',
|
52 |
dest='public',
|
53 |
default=False,
|
54 |
help="List only public networks"),
|
55 |
make_option( |
56 |
'--ipv6',
|
57 |
action='store_true',
|
58 |
dest='ipv6',
|
59 |
default=False,
|
60 |
help="Include IPv6 information"),
|
61 |
) |
62 |
|
63 |
object_class = Network |
64 |
select_related = [] |
65 |
prefetch_related = ["subnets"]
|
66 |
deleted_field = "deleted"
|
67 |
user_uuid_field = "userid"
|
68 |
astakos_url = ASTAKOS_BASE_URL |
69 |
astakos_token = ASTAKOS_TOKEN |
70 |
|
71 |
def get_machines(network): |
72 |
return network.machines.filter(deleted=False).count() |
73 |
|
74 |
def get_backends(network): |
75 |
return network.backend_networks.values_list("backend_id", flat=True) |
76 |
|
77 |
def get_subnet_ipv4(network): |
78 |
return _get_subnet_field(network, "cidr", 4) |
79 |
|
80 |
def get_subnet_ipv6(network): |
81 |
return _get_subnet_field(network, "cidr", 6) |
82 |
|
83 |
def get_gateway_ipv4(network): |
84 |
return _get_subnet_field(network, "gateway", 4) |
85 |
|
86 |
def get_gateway_ipv6(network): |
87 |
return _get_subnet_field(network, "gateway", 6) |
88 |
|
89 |
def get_subnets(network): |
90 |
return network.subnets.values_list('id', flat=True) |
91 |
|
92 |
FIELDS = { |
93 |
"id": ("id", "The ID of the network"), |
94 |
"name": ("name", "The name of the network"), |
95 |
"user.uuid": ("userid", "The UUID of the network's owner"), |
96 |
"public": ("public", "Whether network is public or private"), |
97 |
"flavor": ("flavor", "The network's flavor"), |
98 |
"state": ("state", "The network's state"), |
99 |
"subnets": (get_subnets, "The IDs of the associated subnets"), |
100 |
"subnet.ipv4": (get_subnet_ipv4, "The IPv4 subnet of the network"), |
101 |
"gateway.ipv4": (get_gateway_ipv4, "The IPv4 gateway of the network"), |
102 |
"subnet.ipv6": (get_subnet_ipv6, "The IPv6 subnet of the network"), |
103 |
"gateway.ipv6": (get_gateway_ipv6, "The IPv6 gateway of the network"), |
104 |
"created": ("created", "The date the network was created"), |
105 |
"updated": ("updated", "The date the network was updated"), |
106 |
"deleted": ("deleted", "Whether the network is deleted or not"), |
107 |
"mode": ("mode", "The mode of the network"), |
108 |
"link": ("link", "The link of the network"), |
109 |
"mac_prefix": ("mac_prefix", "The network's MAC prefix"), |
110 |
"drained": ("drained", "Whether network is drained or not"), |
111 |
"vms": (get_machines, "Number of connected servers"), |
112 |
"backends": (get_backends, "IDs of Ganeti backends that the network is" |
113 |
" connected to"),
|
114 |
"floating_ip_pool": ("floating_ip_pool", |
115 |
"Whether the network is a floating IP pool"),
|
116 |
} |
117 |
|
118 |
fields = ["id", "name", "user.uuid", "state", "public", "subnet.ipv4", |
119 |
"gateway.ipv4", "link", "mac_prefix", "drained", |
120 |
"floating_ip_pool"]
|
121 |
|
122 |
def handle_args(self, *args, **options): |
123 |
if options["public"]: |
124 |
self.filters["public"] = True |
125 |
if options["ipv6"]: |
126 |
self.fields.extend(["subnet.ipv6", "gateway.ipv6"]) |
127 |
|
128 |
|
129 |
def _get_subnet_field(network, field, version=4): |
130 |
for subnet in network.subnets.all(): |
131 |
if subnet.ipversion == version:
|
132 |
return getattr(subnet, field) |
133 |
return None |