File: licensing.py

package info (click to toggle)
python-coriolisclient 1.0.9-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 596 kB
  • sloc: python: 5,614; makefile: 23; sh: 2
file content (174 lines) | stat: -rw-r--r-- 5,880 bytes parent folder | download
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
# Copyright (c) 2020 Cloudbase Solutions Srl
#
# 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 argparse

from cliff import command
from cliff import lister
from cliff import show

from coriolisclient.cli import formatter


class LicensingStatusFormatter(formatter.EntityFormatter):

    def __init__(self):
        self.columns = [
            "appliance_id",
            "earliest_licence_expiry_time",
            "latest_licence_expiry_time",
            "current_performed_migrations",
            "current_performed_replicas",
            "current_available_migrations",
            "current_available_replicas",
            "lifetime_performed_migrations",
            "lifetime_performed_replicas",
            "lifetime_available_migrations",
            "lifetime_available_replicas",
        ]

    def _get_formatted_data(self, obj):
        data = [obj.appliance_id,
                obj.earliest_licence_expiry_time,
                obj.latest_licence_expiry_time,
                obj.current_performed_migrations,
                obj.current_performed_replicas,
                obj.current_available_migrations,
                obj.current_available_replicas,
                obj.lifetime_performed_migrations,
                obj.lifetime_performed_replicas,
                obj.lifetime_available_migrations,
                obj.lifetime_available_replicas,
                ]

        return data


class LicenceFormatter(formatter.EntityFormatter):
    columns = ("ID",
               "Issue Date",
               "Migrations",
               "Replicas",
               "Period Start",
               "Period End",
               "Period Duration",
               "Licence Version",
               )

    def _get_sorted_list(self, obj_list):
        return sorted(obj_list, key=lambda o: o.period_end)

    def _get_formatted_data(self, obj):
        data = (obj.id,
                obj.issue_date,
                obj.migrations,
                obj.replicas,
                obj.period_start,
                obj.period_end,
                obj.period_duration,
                obj.licence_version,
                )

        return data


class LicensingApplianceStatus(show.ShowOne):
    """ Retrieves Licensing Status for the given appliance. """

    def get_parser(self, prog_name):
        parser = super().get_parser(prog_name)
        parser.add_argument('appliance_id', help="The appliance ID")
        return parser

    def take_action(self, args):
        lic_status = self.app.client_manager.coriolis.licensing.status(
            args.appliance_id)
        return LicensingStatusFormatter().get_formatted_entity(lic_status)


class LicenceRegister(show.ShowOne):
    """ Registers a Coriolis Licence """

    def get_parser(self, prog_name):
        parser = super().get_parser(prog_name)
        parser.add_argument('appliance_id', help="The appliance ID")
        licence_group = parser.add_mutually_exclusive_group(required=True)
        licence_group.add_argument('--licence-pem',
                                   help="PEM formatted licence")
        licence_group.add_argument('--licence-pem-file',
                                   type=argparse.FileType('r'),
                                   help='Relative/full path to a file '
                                        'containing the PEM formatted licence')
        return parser

    def take_action(self, args):
        pem = None
        raw_arg = getattr(args, 'licence_pem')
        file_arg = getattr(args, 'licence_pem_file')
        if raw_arg:
            pem = raw_arg
        elif file_arg:
            with file_arg as fin:
                pem = fin.read()

        if not pem:
            raise ValueError('No licence-pem[-file] parameter provided.')

        licence = self.app.client_manager.coriolis.licensing.register(
            args.appliance_id, pem)
        return LicenceFormatter().get_formatted_entity(licence)


class LicenceList(lister.Lister):
    """ Retrieves Licence list """

    def get_parser(self, prog_name):
        parser = super().get_parser(prog_name)
        parser.add_argument('appliance_id', help="The appliance ID")
        return parser

    def take_action(self, args):
        lic_list = self.app.client_manager.coriolis.licensing.list(
            args.appliance_id)
        return LicenceFormatter().list_objects(lic_list)


class LicenceShow(show.ShowOne):
    """ Retreieves information about licence """

    def get_parser(self, prog_name):
        parser = super().get_parser(prog_name)
        parser.add_argument('appliance_id', help='The appliance ID')
        parser.add_argument('licence_id', help='The ID of the licence')
        return parser

    def take_action(self, args):
        licence = self.app.client_manager.coriolis.licensing.show(
            args.appliance_id, args.licence_id)
        return LicenceFormatter().get_formatted_entity(licence)


class LicenceDelete(command.Command):
    """ Deletes a licence """

    def get_parser(self, prog_name):
        parser = super().get_parser(prog_name)
        parser.add_argument('appliance_id', help='The appliance ID')
        parser.add_argument('licence_id', help='The ID of the licence')
        return parser

    def take_action(self, args):
        self.app.client_manager.coriolis.licensing.delete(
            args.appliance_id, args.licence_id)