File: vnfpm_threshold.py

package info (click to toggle)
python-tackerclient 2.3.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,456 kB
  • sloc: python: 11,602; makefile: 23; sh: 3
file content (216 lines) | stat: -rw-r--r-- 7,787 bytes parent folder | download | duplicates (2)
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# Copyright (C) 2023 Fujitsu
# All Rights Reserved.
#
#    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 logging

from osc_lib.command import command
from osc_lib import utils

from tackerclient.common import exceptions
from tackerclient.i18n import _
from tackerclient.osc import sdk_utils
from tackerclient.osc import utils as tacker_osc_utils

LOG = logging.getLogger(__name__)

_ATTR_MAP = (
    ('id', 'ID', tacker_osc_utils.LIST_BOTH),
    ('objectType', 'Object Type', tacker_osc_utils.LIST_BOTH),
    ('_links', 'Links', tacker_osc_utils.LIST_BOTH)
)

_FORMATTERS = {
    'subObjectInstanceIds': tacker_osc_utils.FormatComplexDataColumn,
    'criteria': tacker_osc_utils.FormatComplexDataColumn,
    '_links': tacker_osc_utils.FormatComplexDataColumn
}

_MIXED_CASE_FIELDS = (
    'objectType', 'objectInstanceId', 'subObjectInstanceIds', 'callbackUri'
)

_MIXED_CASE_FIELDS_UPDATE = (
    'callbackUri'
)

_VNF_PM_THRESHOLD_ID = 'vnf_pm_threshold_id'


def _get_columns(vnf_pm_threshold, action=None):
    if action == 'update':
        column_map = {
            'callbackUri': 'Callback Uri'
        }
    else:
        column_map = {
            'id': 'ID',
            'objectType': 'Object Type',
            'objectInstanceId': 'Object Instance Id',
            'subObjectInstanceIds': 'Sub Object Instance Ids',
            'criteria': 'Criteria',
            'callbackUri': 'Callback Uri',
            '_links': 'Links'
        }
    return sdk_utils.get_osc_show_columns_for_sdk_resource(
        vnf_pm_threshold, column_map)


class CreateVnfPmThreshold(command.ShowOne):
    _description = _("Create a new VNF PM threshold")

    def get_parser(self, prog_name):
        LOG.debug('get_parser(%s)', prog_name)
        parser = super(CreateVnfPmThreshold, self).get_parser(prog_name)
        parser.add_argument(
            'request_file',
            metavar="<param-file>",
            help=_('Specify create VNF PM threshold request '
                   'parameters in a json file.'))
        return parser

    def take_action(self, parsed_args):
        client = self.app.client_manager.tackerclient
        vnf_pm_threshold = client.create_vnf_pm_threshold(
            tacker_osc_utils.jsonfile2body(parsed_args.request_file))
        display_columns, columns = _get_columns(vnf_pm_threshold)
        data = utils.get_item_properties(
            sdk_utils.DictModel(vnf_pm_threshold), columns,
            formatters=_FORMATTERS, mixed_case_fields=_MIXED_CASE_FIELDS)
        return (display_columns, data)


class ListVnfPmThreshold(command.Lister):
    _description = _("List VNF PM thresholds")

    def get_parser(self, prog_name):
        LOG.debug('get_parser(%s)', prog_name)
        parser = super(ListVnfPmThreshold, self).get_parser(prog_name)
        parser.add_argument(
            "--filter",
            metavar="<filter>",
            help=_("Attribute-based-filtering parameters"),
        )
        return parser

    def take_action(self, parsed_args):
        _params = {}

        if parsed_args.filter:
            _params['filter'] = parsed_args.filter

        client = self.app.client_manager.tackerclient
        data = client.list_vnf_pm_thresholds(**_params)
        headers, columns = tacker_osc_utils.get_column_definitions(
            _ATTR_MAP, long_listing=True)
        return (headers,
                (utils.get_dict_properties(
                    s, columns, formatters=_FORMATTERS,
                    mixed_case_fields=_MIXED_CASE_FIELDS,
                ) for s in data['vnf_pm_thresholds']))


class ShowVnfPmThreshold(command.ShowOne):
    _description = _("Display VNF PM threshold details")

    def get_parser(self, prog_name):
        LOG.debug('get_parser(%s)', prog_name)
        parser = super(ShowVnfPmThreshold, self).get_parser(prog_name)
        parser.add_argument(
            _VNF_PM_THRESHOLD_ID,
            metavar="<vnf-pm-threshold-id>",
            help=_("VNF PM threshold ID to display"))
        return parser

    def take_action(self, parsed_args):
        client = self.app.client_manager.tackerclient
        obj = client.show_vnf_pm_threshold(parsed_args.vnf_pm_threshold_id)
        display_columns, columns = _get_columns(obj)
        data = utils.get_item_properties(
            sdk_utils.DictModel(obj), columns,
            mixed_case_fields=_MIXED_CASE_FIELDS,
            formatters=_FORMATTERS)
        return (display_columns, data)


class UpdateVnfPmThreshold(command.ShowOne):
    _description = _("Update information about an individual VNF PM threshold")

    def get_parser(self, prog_name):
        LOG.debug('get_parser(%s)', prog_name)
        parser = super(UpdateVnfPmThreshold, self).get_parser(prog_name)
        parser.add_argument(
            _VNF_PM_THRESHOLD_ID,
            metavar="<vnf-pm-threshold-id>",
            help=_("VNF PM threshold ID to update.")
        )
        parser.add_argument(
            'request_file',
            metavar="<param-file>",
            help=_('Specify update PM threshold request '
                   'parameters in a json file.'))
        return parser

    def take_action(self, parsed_args):
        client = self.app.client_manager.tackerclient
        updated_values = client.update_vnf_pm_threshold(
            parsed_args.vnf_pm_threshold_id,
            tacker_osc_utils.jsonfile2body(parsed_args.request_file))
        display_columns, columns = _get_columns(updated_values, 'update')
        data = utils.get_item_properties(
            sdk_utils.DictModel(updated_values), columns,
            mixed_case_fields=_MIXED_CASE_FIELDS_UPDATE)
        return (display_columns, data)


class DeleteVnfPmThreshold(command.Command):
    _description = _("Delete VNF PM threshold")

    def get_parser(self, prog_name):
        LOG.debug('get_parser(%s)', prog_name)
        parser = super(DeleteVnfPmThreshold, self).get_parser(prog_name)
        parser.add_argument(
            _VNF_PM_THRESHOLD_ID,
            metavar="<vnf-pm-threshold-id>",
            nargs="+",
            help=_("VNF PM threshold ID(s) to delete"))
        return parser

    def take_action(self, parsed_args):
        error_count = 0
        client = self.app.client_manager.tackerclient
        vnf_pm_threshold_ids = parsed_args.vnf_pm_threshold_id

        for threshold_id in vnf_pm_threshold_ids:
            try:
                client.delete_vnf_pm_threshold(threshold_id)
            except Exception as e:
                error_count += 1
                LOG.error(_("Failed to delete VNF PM threshold with "
                            "ID '%(threshold_id)s': %(e)s"),
                          {'threshold_id': threshold_id, 'e': e})

        total = len(vnf_pm_threshold_ids)
        if error_count > 0:
            msg = (_("Failed to delete %(error_count)s of %(total)s "
                     "VNF PM thresholds.") %
                   {'error_count': error_count, 'total': total})
            raise exceptions.CommandError(message=msg)

        if total > 1:
            print(_('All specified VNF PM thresholds are deleted '
                    'successfully'))
            return
        print(_("VNF PM threshold '%s' deleted "
                "successfully") % vnf_pm_threshold_ids[0])