summaryrefslogtreecommitdiff
path: root/saharaclient/osc/v2/node_group_templates.py
blob: c2aaf94b19de169da33079753d7d1f515e0a225c (plain)
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# Copyright (c) 2018 Red Hat Inc.
#
# 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 sys

from osc_lib import utils as osc_utils

from saharaclient.osc import utils
from saharaclient.osc.v1 import node_group_templates as ngt_v1

NGT_FIELDS = ['id', 'name', 'plugin_name', 'plugin_version', 'node_processes',
              'description', 'auto_security_group', 'security_groups',
              'availability_zone', 'flavor_id', 'floating_ip_pool',
              'volumes_per_node', 'volumes_size',
              'volume_type', 'volume_local_to_instance', 'volume_mount_prefix',
              'volumes_availability_zone', 'use_autoconfig',
              'is_proxy_gateway', 'is_default', 'is_protected', 'is_public',
              'boot_from_volume']


def _format_ngt_output(data):
    data['node_processes'] = osc_utils.format_list(data['node_processes'])
    if data['volumes_per_node'] == 0:
        del data['volume_local_to_instance']
        del data['volume_mount_prefix']
        del data['volume_type'],
        del data['volumes_availability_zone']
        del data['volumes_size']


class CreateNodeGroupTemplate(ngt_v1.CreateNodeGroupTemplate,
                              utils.NodeGroupTemplatesUtils):
    """Creates node group template"""

    def get_parser(self, prog_name):
        parser = super(CreateNodeGroupTemplate, self).get_parser(prog_name)

        parser.add_argument(
            '--boot-from-volume',
            action='store_true',
            default=False,
            help="Make the node group bootable from volume",
        )
        return parser

    def take_action(self, parsed_args):
        self.log.debug("take_action(%s)", parsed_args)
        client = self.app.client_manager.data_processing

        data = self._create_take_action(client, self.app, parsed_args)

        _format_ngt_output(data)
        data = utils.prepare_data(data, NGT_FIELDS)

        return self.dict2columns(data)


class ListNodeGroupTemplates(ngt_v1.ListNodeGroupTemplates,
                             utils.NodeGroupTemplatesUtils):
    """Lists node group templates"""

    def take_action(self, parsed_args):
        self.log.debug("take_action(%s)", parsed_args)
        client = self.app.client_manager.data_processing
        return self._list_take_action(client, self.app, parsed_args)


class ShowNodeGroupTemplate(ngt_v1.ShowNodeGroupTemplate,
                            utils.NodeGroupTemplatesUtils):
    """Display node group template details"""

    def take_action(self, parsed_args):
        self.log.debug("take_action(%s)", parsed_args)
        client = self.app.client_manager.data_processing

        data = utils.get_resource(
            client.node_group_templates,
            parsed_args.node_group_template).to_dict()

        _format_ngt_output(data)

        data = utils.prepare_data(data, NGT_FIELDS)

        return self.dict2columns(data)


class DeleteNodeGroupTemplate(ngt_v1.DeleteNodeGroupTemplate,
                              utils.NodeGroupTemplatesUtils):
    """Deletes node group template"""

    def take_action(self, parsed_args):
        self.log.debug("take_action(%s)", parsed_args)
        client = self.app.client_manager.data_processing
        for ngt in parsed_args.node_group_template:
            ngt_id = utils.get_resource_id(
                client.node_group_templates, ngt)
            client.node_group_templates.delete(ngt_id)
            sys.stdout.write(
                'Node group template "{ngt}" has been removed '
                'successfully.\n'.format(ngt=ngt))


class UpdateNodeGroupTemplate(ngt_v1.UpdateNodeGroupTemplate,
                              utils.NodeGroupTemplatesUtils):
    """Updates node group template"""

    def get_parser(self, prog_name):
        parser = super(UpdateNodeGroupTemplate, self).get_parser(prog_name)

        bootfromvolume = parser.add_mutually_exclusive_group()
        bootfromvolume.add_argument(
            '--boot-from-volume-enable',
            action='store_true',
            help='Makes node group bootable from volume.',
            dest='boot_from_volume'
        )
        bootfromvolume.add_argument(
            '--boot-from-volume-disable',
            action='store_false',
            help='Makes node group not bootable from volume.',
            dest='boot_from_volume'
        )
        parser.set_defaults(is_public=None, is_protected=None,
                            is_proxy_gateway=None, volume_locality=None,
                            use_auto_security_group=None, use_autoconfig=None,
                            boot_from_volume=None)
        return parser

    def take_action(self, parsed_args):
        self.log.debug("take_action(%s)", parsed_args)
        client = self.app.client_manager.data_processing

        data = self._update_take_action(client, self.app, parsed_args)

        _format_ngt_output(data)
        data = utils.prepare_data(data, NGT_FIELDS)

        return self.dict2columns(data)


class ImportNodeGroupTemplate(ngt_v1.ImportNodeGroupTemplate,
                              utils.NodeGroupTemplatesUtils):
    """Imports node group template"""

    def take_action(self, parsed_args):
        self.log.debug("take_action(%s)", parsed_args)
        client = self.app.client_manager.data_processing

        data = self._import_take_action(client, parsed_args)

        _format_ngt_output(data)
        data = utils.prepare_data(data, NGT_FIELDS)

        return self.dict2columns(data)


class ExportNodeGroupTemplate(ngt_v1.ExportNodeGroupTemplate,
                              utils.NodeGroupTemplatesUtils):
    """Export node group template to JSON"""

    def take_action(self, parsed_args):
        self.log.debug("take_action(%s)", parsed_args)
        client = self.app.client_manager.data_processing
        self._export_take_action(client, parsed_args)