Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (6 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
#  Remove this when the endpoint issue is resolved
64
from kamaki.cli.commands.cyclades import _init_cyclades as _init_networking
65

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

    
91
#     def main(self):
92
#         self._run()
93

    
94

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

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

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

    
124
    def main(self):
125
        super(self.__class__, self)._run()
126
        self._run()
127

    
128

    
129
@command(network_cmds)
130
class network_info(_init_networking, _optional_json):
131
    """Get details about a network"""
132

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

    
140
    def main(self, network_id):
141
        super(self.__class__, self)._run()
142
        self._run(network_id=network_id)
143

    
144

    
145
@command(network_cmds)
146
class network_create(_init_networking, _optional_json):
147
    """Create a new network"""
148

    
149
    arguments = dict(shared=FlagArgument(
150
        'Network will be shared (special privileges required)', '--shared')
151
    )
152

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

    
161
    def main(self, name):
162
        super(self.__class__, self)._run()
163
        self._run(name=name)