network-list.py 4.41 KB
Newer Older
1
# Copyright 2012-2013 GRNET S.A. All rights reserved.
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#
# Redistribution and use in source and binary forms, with or
# without modification, are permitted provided that the following
# conditions are met:
#
#   1. Redistributions of source code must retain the above
#      copyright notice, this list of conditions and the following
#      disclaimer.
#
#   2. Redistributions in binary form must reproduce the above
#      copyright notice, this list of conditions and the following
#      disclaimer in the documentation and/or other materials
#      provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
# The views and conclusions contained in the software and
# documentation are those of the authors and should not be
# interpreted as representing official policies, either expressed
# or implied, of GRNET S.A.

from optparse import make_option

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
42
from logging import getLogger
log = getLogger(__name__)
43

44

45
class Command(ListCommand):
46
47
    help = "List networks"

48
    option_list = ListCommand.option_list + (
49
50
        make_option(
            '--public',
51
52
53
54
            action='store_true',
            dest='public',
            default=False,
            help="List only public networks"),
55
        make_option(
56
            '--ipv6',
Christos Stavrakakis's avatar
Christos Stavrakakis committed
57
58
59
            action='store_true',
            dest='ipv6',
            default=False,
60
            help="Include IPv6 information"),
61
    )
62

63
64
65
    object_class = Network
    deleted_field = "deleted"
    user_uuid_field = "userid"
66
    astakos_url = ASTAKOS_BASE_URL
67
    astakos_token = ASTAKOS_TOKEN
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84

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

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

    FIELDS = {
        "id": ("id", "The ID of the network"),
        "name": ("name", "The name of the network"),
        "user.uuid": ("userid", "The UUID of the network's owner"),
        "public": ("public", "Whether network is public or private"),
        "flavor": ("flavor", "The network's flavor"),
        "state": ("state", "The network's state"),
        "dhcp": ("dhcp", "Whether network uses nfdhcpd or not"),
        "subnet.ipv4": ("subnet", "The IPv4 subnet of the network"),
        "gateway.ipv4": ("gateway", "The IPv4 gateway of the network"),
85
86
        "subnet.ipv6": ("subnet6", "The IPv6 subnet of the network"),
        "gateway.ipv6": ("gateway6", "The IPv6 gateway of the network"),
87
        "created": ("created", "The date the network was created"),
88
        "updated": ("updated", "The date the network was updated"),
89
90
91
92
        "deleted": ("deleted", "Whether the network is deleted or not"),
        "mode": ("mode", "The mode of the network"),
        "link": ("link", "The link of the network"),
        "mac_prefix": ("mac_prefix", "The network's MAC prefix"),
93
        "drained": ("drained", "Whether network is drained or not"),
94
95
96
        "vms": (get_machines, "Number of connected servers"),
        "backends": (get_backends, "IDs of Ganeti backends that the network is"
                                   " connected to"),
97
98
        "pool": ("floating_ip_pool", "Whether the network is a floating"
                                     " IP pool"),
99
100
101
    }

    fields = ["id", "name", "user.uuid", "state", "public", "subnet.ipv4",
102
              "gateway.ipv4", "link", "mac_prefix", "dhcp", "drained", "pool"]
103
104
105
106
107
108

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