Statistics
| Branch: | Tag: | Revision:

root / snf-cyclades-app / synnefo / api / management / commands / network-list.py @ b0e7f310

History | View | Annotate | Download (4 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 synnefo.webproject.management.commands import ListCommand
37
from synnefo.db.models import Network
38

    
39
from logging import getLogger
40
log = getLogger(__name__)
41

    
42

    
43
class Command(ListCommand):
44
    option_list = ListCommand.option_list + (
45
        make_option(
46
            '--public',
47
            action='store_true',
48
            dest='public',
49
            default=False,
50
            help="List only public networks"),
51
        make_option(
52
            '--ipv6',
53
            action='store_true',
54
            dest='ipv6',
55
            default=False,
56
            help="Include IPv6 information"),
57
    )
58

    
59
    object_class = Network
60
    deleted_field = "deleted"
61
    user_uuid_field = "userid"
62

    
63
    def get_machines(network):
64
        return network.machines.filter(deleted=False).count()
65

    
66
    def get_backends(network):
67
        return network.backend_networks.values_list("backend_id", flat=True)
68

    
69
    FIELDS = {
70
        "id": ("id", "The ID of the network"),
71
        "name": ("name", "The name of the network"),
72
        "user.uuid": ("userid", "The UUID of the network's owner"),
73
        "public": ("public", "Whether network is public or private"),
74
        "flavor": ("flavor", "The network's flavor"),
75
        "state": ("state", "The network's state"),
76
        "dhcp": ("dhcp", "Whether network uses nfdhcpd or not"),
77
        "subnet.ipv4": ("subnet", "The IPv4 subnet of the network"),
78
        "gateway.ipv4": ("gateway", "The IPv4 gateway of the network"),
79
        "subnet.ipv6": ("subnet", "The IPv6 subnet of the network"),
80
        "gateway.ipv6": ("gateway", "The IPv6 gateway of the network"),
81
        "created": ("created", "The date the network was created"),
82
        "updated": ("created", "The date the network was updated"),
83
        "deleted": ("deleted", "Whether the network is deleted or not"),
84
        "mode": ("mode", "The mode of the network"),
85
        "link": ("link", "The link of the network"),
86
        "mac_prefix": ("mac_prefix", "The network's MAC prefix"),
87
        "vms": (get_machines, "Number of connected servers"),
88
        "backends": (get_backends, "IDs of Ganeti backends that the network is"
89
                                   " connected to"),
90
    }
91

    
92
    fields = ["id", "name", "user.uuid", "state", "public", "subnet.ipv4",
93
              "gateway.ipv4", "link", "mac_prefix"]
94

    
95
    def handle_args(self, *args, **options):
96
        if options["public"]:
97
            self.filters["public"] = True
98
        if options["ipv6"]:
99
            self.fields.extend(["subnet.ipv6", "gateway.ipv6"])