File: actions.py

package info (click to toggle)
python-freezerclient 6.1.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 676 kB
  • sloc: python: 4,550; makefile: 25; sh: 2
file content (185 lines) | stat: -rw-r--r-- 6,641 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
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
# (c) Copyright 2014-2016 Hewlett-Packard Development Company, L.P.
#
# 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 cliff import command
from cliff import lister
from cliff import show

from freezerclient import exceptions
from freezerclient import utils


logging = logging.getLogger(__name__)


class ActionShow(show.ShowOne):
    """Show a single action """
    def get_parser(self, prog_name):
        parser = super(ActionShow, self).get_parser(prog_name)
        parser.add_argument(dest='action_id',
                            help='ID of the action')
        return parser

    def take_action(self, parsed_args):
        action = self.app.client.actions.get(parsed_args.action_id)

        if not action:
            raise exceptions.ApiClientException('Action not found')

        column = (
            'Action ID',
            'Name',
            'Action',
            'Mode',
            'Path to Backup or Restore',
            'Storage',
            'Snapshot',
            'Container',
            'Log_file',
            'Remove older than',
            'Max retries interval',
            'Max retries',
            'User ID'
        )

        data = (
            action.get('action_id'),
            action.get('freezer_action', {}).get('backup_name', ''),
            action.get('freezer_action', {}).get('action', 'backup'),
            action.get('freezer_action', {}).get('mode', 'fs'),
            action.get('freezer_action', {}).get('path_to_backup', ''),
            action.get('freezer_action', {}).get('storage', 'swift'),
            action.get('freezer_action', {}).get('snapshot', 'False'),
            action.get('freezer_action', {}).get('container', ''),
            action.get('freezer_action', {}).get('log_file', ''),
            action.get('freezer_action', {}).get('remove_older_than', '365'),
            action.get('max_retries_interval', '6'),
            action.get('max_retries', '5'),
            action.get('user_id', ''),
        )

        return column, data


class ActionList(lister.Lister):
    """List all actions for your user"""
    def get_parser(self, prog_name):
        parser = super(ActionList, self).get_parser(prog_name)

        parser.add_argument(
            '--limit',
            dest='limit',
            default=100,
            help='Specify a limit for search query',
        )

        parser.add_argument(
            '--offset',
            dest='offset',
            default=0,
            help='',
        )

        parser.add_argument(
            '--search',
            dest='search',
            default='',
            help='Define a filter for the query',
        )
        return parser

    def take_action(self, parsed_args):
        search = utils.prepare_search(parsed_args.search)

        actions = self.app.client.actions.list(
            limit=parsed_args.limit,
            offset=parsed_args.offset,
            search=search
        )

        columns = ('Action ID', 'Name', 'Action',
                   'Path to Backup or Restore', 'Mode', 'Storage', 'snapshot')

        # Print empty table if no actions found
        if not actions:
            actions = [{}]
            data = ((action.get('action-id', ''),
                     action.get('freezer_action', {}).get('backup_name', ''),
                     action.get('freezer_action', {}).get('action', ''),
                     action.get('freezer_action', {}).get(
                         'path_to_backup', ''),
                     action.get('freezer_action', {}).get('mode', ''),
                     action.get('freezer_action', {}).get('storage', ''),
                     action.get('freezer_action', {}).get('snapshot', '')
                     ) for action in actions)
        else:
            data = ((action.get('action_id'),
                     action.get('freezer_action', {}).get('backup_name', ''),
                     action.get('freezer_action', {}).get('action', 'backup'),
                     action.get('freezer_action', {}).get(
                         'path_to_backup', ''),
                     action.get('freezer_action', {}).get('mode', 'fs'),
                     action.get('freezer_action', {}).get('storage', 'swift'),
                     action.get('freezer_action', {}).get('snapshot', 'False')
                     ) for action in actions)

        return columns, data


class ActionDelete(command.Command):
    """Delete an action from the api"""
    def get_parser(self, prog_name):
        parser = super(ActionDelete, self).get_parser(prog_name)
        parser.add_argument(dest='action_id',
                            help='ID of the action')
        return parser

    def take_action(self, parsed_args):
        self.app.client.actions.delete(parsed_args.action_id)
        logging.info('Action {0} deleted'.format(parsed_args.action_id))


class ActionCreate(command.Command):
    """Create an action from a file"""
    def get_parser(self, prog_name):
        parser = super(ActionCreate, self).get_parser(prog_name)
        parser.add_argument('--file',
                            dest='file',
                            required=True,
                            help='Path to json file with the action')
        return parser

    def take_action(self, parsed_args):
        action = utils.doc_from_json_file(parsed_args.file)
        action_id = self.app.client.actions.create(action)
        logging.info('Action {0} created'.format(action_id))


class ActionUpdate(command.Command):
    """Update an action from a file"""
    def get_parser(self, prog_name):
        parser = super(ActionUpdate, self).get_parser(prog_name)
        parser.add_argument(dest='action_id',
                            help='ID of the session')

        parser.add_argument(dest='file',
                            help='Path to json file with the action')
        return parser

    def take_action(self, parsed_args):
        action = utils.doc_from_json_file(parsed_args.file)
        self.app.client.actions.update(parsed_args.action_id, action)
        logging.info('Action {0} updated'.format(parsed_args.action_id))