File: capsules_shell.py

package info (click to toggle)
python-zunclient 5.3.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,056 kB
  • sloc: python: 10,127; sh: 110; makefile: 25
file content (111 lines) | stat: -rw-r--r-- 3,881 bytes parent folder | download | duplicates (4)
1
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#    Copyright 2017 Arm Limited.
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

import yaml

from oslo_serialization import jsonutils

from zunclient.common import cliutils as utils
from zunclient.common import template_utils
from zunclient.common import utils as zun_utils
from zunclient.i18n import _


def _show_capsule(capsule):
    zun_utils.format_container_addresses(capsule)
    utils.print_dict(capsule._info)


@utils.arg('-f', '--template-file', metavar='<file>',
           required=True, help=_('Path to the template.'))
def do_capsule_create(cs, args):
    """Create a capsule."""
    opts = {}
    if args.template_file:
        template = template_utils.get_template_contents(
            args.template_file)
        opts['template'] = template
        cs.capsules.create(**opts)
        print("Request to create capsule has been accepted.")


@utils.arg('--all-projects',
           action="store_true",
           default=False,
           help='List containers in all projects')
@utils.arg('--marker',
           metavar='<marker>',
           default=None,
           help='The last container UUID of the previous page; '
                'displays list of containers after "marker".')
@utils.arg('--limit',
           metavar='<limit>',
           type=int,
           help='Maximum number of containers to return')
@utils.arg('--sort-key',
           metavar='<sort-key>',
           help='Column to sort results by')
@utils.arg('--sort-dir',
           metavar='<sort-dir>',
           choices=['desc', 'asc'],
           help='Direction to sort. "asc" or "desc".')
def do_capsule_list(cs, args):
    """Print a list of available capsules."""
    opts = {}
    opts['all_projects'] = args.all_projects
    opts['marker'] = args.marker
    opts['limit'] = args.limit
    opts['sort_key'] = args.sort_key
    opts['sort_dir'] = args.sort_dir
    opts = zun_utils.remove_null_parms(**opts)
    capsules = cs.capsules.list(**opts)
    zun_utils.list_capsules(capsules)


@utils.arg('capsules',
           metavar='<capsule>',
           nargs='+',
           help='ID or name of the (capsule)s to delete.')
def do_capsule_delete(cs, args):
    """Delete specified capsules."""
    for capsule in args.capsules:
        try:
            cs.capsules.delete(capsule)
            print("Request to delete capsule %s has been accepted." %
                  capsule)
        except Exception as e:
            print("Delete for capsule %(capsule)s failed: %(e)s" %
                  {'capsule': capsule, 'e': e})


@utils.arg('capsule',
           metavar='<capsule>',
           help='ID or name of the capsule to show.')
@utils.arg('-f', '--format',
           metavar='<format>',
           action='store',
           choices=['json', 'yaml', 'table'],
           default='table',
           help='Print representation of the capsule. '
                'The choices of the output format is json,table,yaml. '
                'Defaults to table. ')
def do_capsule_describe(cs, args):
    """Show details of a capsule."""
    capsule = cs.capsules.describe(args.capsule)
    if args.format == 'json':
        print(jsonutils.dumps(capsule._info, indent=4, sort_keys=True))
    elif args.format == 'yaml':
        print(yaml.safe_dump(capsule._info, default_flow_style=False))
    elif args.format == 'table':
        _show_capsule(capsule)