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