Statistics
| Branch: | Tag: | Revision:

root / kamaki / cli / commands / networking.py @ aef3fa1f

History | View | Annotate | Download (6.2 kB)

1
# Copyright 2011-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 base64 import b64encode
35
from os.path import exists, expanduser
36
from io import StringIO
37
from pydoc import pager
38

    
39
from kamaki.cli import command
40
from kamaki.cli.command_tree import CommandTree
41
from kamaki.cli.utils import remove_from_items, filter_dicts_by_dict
42
from kamaki.cli.errors import (
43
    raiseCLIError, CLISyntaxError, CLIBaseUrlError, CLIInvalidArgument)
44
from kamaki.clients.networking import NetworkingClient, ClientError
45
from kamaki.cli.argument import FlagArgument, ValueArgument, KeyValueArgument
46
from kamaki.cli.argument import ProgressBarArgument, DateArgument, IntArgument
47
from kamaki.cli.commands import _command_init, errors, addLogSettings
48
from kamaki.cli.commands import (
49
    _optional_output_cmd, _optional_json, _name_filter, _id_filter)
50

    
51

    
52
network_cmds = CommandTree('network', 'Networking API network commands')
53
port_cmds = CommandTree('port', 'Networking API network commands')
54
subnet_cmds = CommandTree('subnet', 'Networking API network commands')
55
_commands = [network_cmds, port_cmds, subnet_cmds]
56

    
57

    
58
about_authentication = '\nUser Authentication:\
59
    \n* to check authentication: /user authenticate\
60
    \n* to set authentication token: /config set cloud.<cloud>.token <token>'
61

    
62

    
63
class _init_networking(_command_init):
64
    @errors.generic.all
65
    @addLogSettings
66
    def _run(self, service='network'):
67
        if getattr(self, 'cloud', None):
68
            base_url = self._custom_url(service) or self._custom_url(
69
                'compute')
70
            if base_url:
71
                token = self._custom_token(service) or self._custom_token(
72
                    'compute') or self.config.get_cloud('token')
73
                self.client = NetworkingClient(
74
                  base_url=base_url, token=token)
75
                return
76
        else:
77
            self.cloud = 'default'
78
        if getattr(self, 'auth_base', False):
79
            cyclades_endpoints = self.auth_base.get_service_endpoints(
80
                self._custom_type('compute') or 'compute',
81
                self._custom_version('compute') or '')
82
            base_url = cyclades_endpoints['publicURL']
83
            token = self.auth_base.token
84
            self.client = NetworkingClient(base_url=base_url, token=token)
85
        else:
86
            raise CLIBaseUrlError(service='network')
87

    
88
    def main(self):
89
        self._run()
90

    
91

    
92
@command(network_cmds)
93
class network_list(_init_networking, _optional_json, _name_filter, _id_filter):
94
    """List networks
95
    Use filtering arguments (e.g., --name-like) to manage long server lists
96
    """
97

    
98
    arguments = dict(
99
        detail=FlagArgument('show detailed output', ('-l', '--details')),
100
        more=FlagArgument(
101
            'output results in pages (-n to set items per page, default 10)',
102
            '--more'),
103
    )
104

    
105
    @errors.generic.all
106
    @errors.cyclades.connection
107
    def _run(self):
108
        nets = self.client.list_networks()
109
        nets = self._filter_by_name(nets)
110
        nets = self._filter_by_id(nets)
111
        if not self['detail']:
112
            nets = [dict(id=net['id'], name=net['name']) for net in nets]
113
        kwargs = dict()
114
        if self['more']:
115
            kwargs['out'] = StringIO()
116
            kwargs['title'] = ()
117
        self._print(nets, **kwargs)
118
        if self['more']:
119
            pager(kwargs['out'].getvalue())
120

    
121
    def main(self):
122
        super(self.__class__, self)._run()
123
        self._run()
124

    
125

    
126
@command(network_cmds)
127
class network_info(_init_networking, _optional_json):
128
    """Get details about a network"""
129

    
130
    @errors.generic.all
131
    @errors.cyclades.connection
132
    @errors.cyclades.network_id
133
    def _run(self, network_id):
134
        net = self.client.get_network_details(network_id)
135
        self._print(net, self.print_dict)
136

    
137
    def main(self, network_id):
138
        super(self.__class__, self)._run()
139
        self._run(network_id=network_id)
140

    
141

    
142
@command(network_cmds)
143
class network_create(_init_networking, _optional_json):
144
    """Create a new network"""
145

    
146
    arguments = dict(shared=FlagArgument(
147
        'Network will be shared (special privileges required)', '--shared')
148
    )
149

    
150
    @errors.generic.all
151
    @errors.cyclades.connection
152
    def _run(self, name):
153
        #  admin_state_up is not used in Cyclades
154
        net = self.client.create_network(
155
            name, admin_state_up=True, shared=self['shared'])
156
        self._print(net, self.print_dict)
157

    
158
    def main(self, name):
159
        super(self.__class__, self)._run()
160
        self._run(name=name)
161

    
162

    
163
@command(network_cmds)
164
class network_delete(_init_networking, _optional_output_cmd):
165
    """Delete a network"""
166

    
167
    @errors.generic.all
168
    @errors.cyclades.connection
169
    def _run(self, network_id):
170
        r = self.client.delete_network(network_id)
171
        self._optional_output(r)
172

    
173
    def main(self, network_id):
174
        super(self.__class__, self)._run()
175
        self._run(network_id=network_id)