Revision 5a673575 kamaki/cli/commands/errors.py

b/kamaki/cli/commands/errors.py
70 70
                        '  to check if token is valid: /astakos authenticate',
71 71
                        '  to set token: /config set [.server.]token <token>',
72 72
                        '  to get current token: /config get [server.]token'])
73
                elif ce.status in range(-12, 200) + [403, 500]:
73
                elif ce.status in range(-12, 200) + [403, 302, 500]:
74 74
                    raiseCLIError(ce, importance=3, details=[
75 75
                        'Check if service is up or set to url %s' % base_url,
76 76
                        '  to get url: /config get %s' % base_url,
77 77
                        '  to set url: /config set %s <URL>' % base_url])
78
                elif ce.status == 404\
79
                and 'kamakihttpresponse' in ('%s' % ce).lower():
80
                    client = getattr(self, 'client', None)
81
                    if not client:
82
                        raise
83
                    url = getattr(client, 'base_url', '<empty>')
84
                    raiseCLIError(ce,
85
                        'Invalid service url %s' % url,
86
                        details=[
87
                        'Please, check if service url is correctly set',
88
                        '* to get current url: /config get compute.url',
89
                        '* to set url: /config set compute.url <URL>'])
78 90
                raise
79 91
        return _raise
80 92

  
......
150 162
        '* get a list of flavor ids: /flavor list',
151 163
        '* details of flavor: /flavor info <flavor id>']
152 164

  
165
    about_network_id = [
166
        'How to pick a valid network id:',
167
        '* get a list of network ids: /network list',
168
        '* details of network: /network info <network id>']
169

  
153 170
    @classmethod
154 171
    def connection(this, foo):
155 172
        return generic._connection(foo, 'compute.url')
......
168 185
        return _raise
169 186

  
170 187
    @classmethod
188
    def network_id(this, foo):
189
        def _raise(self, *args, **kwargs):
190
            network_id = kwargs.get('network_id', None)
191
            try:
192
                network_id = int(network_id)
193
                return foo(self, *args, **kwargs)
194
            except ValueError as ve:
195
                raiseCLIError(ve, 'Invalid network id %s ' % network_id,
196
                    details='network id must be a positive integer',
197
                    importance=1)
198
            except ClientError as ce:
199
                if network_id and ce.status == 404 and\
200
                    'network' in ('%s' % ce).lower():
201
                        raiseCLIError(ce,
202
                            'No network with id %s found' % network_id,
203
                            details=this.about_network_id)
204
                raise
205
        return _raise
206

  
207
    @classmethod
208
    def network_max(this, foo):
209
        def _raise(self, *args, **kwargs):
210
            try:
211
                return foo(self, *args, **kwargs)
212
            except ClientError as ce:
213
                if ce.status == 413:
214
                    raiseCLIError(ce,
215
                        'Cannot create another network',
216
                        details=['Maximum number of networks reached',
217
                            '* to get a list of networks: /network list',
218
                            '* to delete a network: /network delete <net id>'])
219
                raise
220
        return _raise
221

  
222
    @classmethod
223
    def network_in_use(this, foo):
224
        def _raise(self, *args, **kwargs):
225
            network_id = kwargs.get('network_id', None)
226
            try:
227
                return foo(self, *args, **kwargs)
228
            except ClientError as ce:
229
                if network_id or ce.status == 421:
230
                    raiseCLIError(ce,
231
                        'Network with id %s is in use' % network_id,
232
                        details=[
233
                            'Disconnect all nics/VMs of this network first',
234
                            '* to get nics: /network info %s' % network_id,
235
                            '.  (under "attachments" section)',
236
                            '* to disconnect: /network disconnect <nic id>'])
237
                raise
238
        return _raise
239

  
240
    @classmethod
171 241
    def flavor_id(this, foo):
172 242
        def _raise(self, *args, **kwargs):
173 243
            flavor_id = kwargs.get('flavor_id', None)
......
180 250
                    importance=1)
181 251
            except ClientError as ce:
182 252
                if flavor_id and ce.status == 404 and\
183
                    'flavor not found' in ('%s' % ce).lower():
253
                    'flavor' in ('%s' % ce).lower():
184 254
                        raiseCLIError(ce,
185 255
                            'No flavor with id %s found' % flavor_id,
186 256
                            details=this.about_flavor_id)
......
188 258
        return _raise
189 259

  
190 260
    @classmethod
191
    def id(this, foo):
261
    def server_id(this, foo):
192 262
        def _raise(self, *args, **kwargs):
193 263
            server_id = kwargs.get('server_id', None)
194 264
            try:
......
196 266
                return foo(self, *args, **kwargs)
197 267
            except ValueError as ve:
198 268
                raiseCLIError(ve,
199
                    'Invalid VM id %s' % server_id,
269
                    'Invalid server(VM) id %s' % server_id,
200 270
                    details=['id must be a positive integer'],
201 271
                    importance=1)
202 272
            except ClientError as ce:
203
                if ce.status == 404 or\
204
                (ce.status == 400 and 'not found' in ('%s' % ce).lower()):
205
                    raiseCLIError(ce, 'VM with id %s not found' % server_id)
273
                err_msg = ('%s' % ce).lower()
274
                if (ce.status == 404 and 'server' in err_msg)\
275
                or (ce.status == 400 and 'not found' in err_msg):
276
                    raiseCLIError(ce,
277
                        'server(VM) with id %s not found' % server_id,
278
                        details=[
279
                            '* to get existing VM ids: /server list',
280
                            '* to get VM details: /server info <VM id>'])
281
                raise
282
        return _raise
283

  
284
    @classmethod
285
    def firewall(this, foo):
286
        def _raise(self, *args, **kwargs):
287
            profile = kwargs.get('profile', None)
288
            try:
289
                return foo(self, *args, **kwargs)
290
            except ClientError as ce:
291
                if ce.status == 400 and profile\
292
                and 'firewall' in ('%s' % ce).lower():
293
                    raiseCLIError(ce,
294
                        '%s is an invalid firewall profile term' % profile,
295
                        details=['Try one of the following:',
296
                            '* DISABLED: Shutdown firewall',
297
                            '* ENABLED: Firewall in normal mode',
298
                            '* PROTECTED: Firewall in secure mode'])
299
                raise
300
        return _raise
301

  
302
    @classmethod
303
    def nic_id(this, foo):
304
        def _raise(self, *args, **kwargs):
305
            try:
306
                return foo(self, *args, **kwargs)
307
            except ClientError as ce:
308
                nic_id = kwargs.get('nic_id', None)
309
                if nic_id and ce.status == 404\
310
                and 'network interface' in ('%s' % ce).lower():
311
                    server_id = kwargs.get('server_id', '<no server>')
312
                    err_msg = 'No nic %s on server(VM) with id %s' % (
313
                        nic_id,
314
                        server_id)
315
                    raiseCLIError(ce, err_msg, details=[
316
                        '* check server(VM) with id %s: /server info %s' % (
317
                            server_id,
318
                            server_id),
319
                        '* list nics for server(VM) with id %s:' % server_id,
320
                        '      /server addr %s' % server_id])
321
                raise
322
        return _raise
323

  
324
    @classmethod
325
    def nic_format(this, foo):
326
        def _raise(self, *args, **kwargs):
327
            try:
328
                return foo(self, *args, **kwargs)
329
            except IndexError as ie:
330
                nic_id = kwargs.get('nic_id', None)
331
                raiseCLIError(ie,
332
                    'Invalid format for network interface (nic) %s' % nic_id,
333
                    importance=1,
334
                    details=[
335
                        'nid_id format: nic-<server id>-<nic id>',
336
                        '* get nics of a network: /network info <net id>',
337
                        '    (listed the "attachments" section)'])
338
        return _raise
339

  
340
    @classmethod
341
    def metadata(this, foo):
342
        def _raise(self, *args, **kwargs):
343
            key = kwargs.get('key', None)
344
            try:
345
                foo(self, *args, **kwargs)
346
            except ClientError as ce:
347
                if key and ce.status == 404\
348
                    and 'metadata' in ('%s' % ce).lower():
349
                        raiseCLIError(ce, 'No VM metadata with key %s' % key)
206 350
                raise
207 351
        return _raise
208 352

  

Also available in: Unified diff