|
| 1 | +# Licensed under the Apache License, Version 2.0 (the 'License'); you may |
| 2 | +# not use this file except in compliance with the License. You may obtain |
| 3 | +# a copy of the License at |
| 4 | +# |
| 5 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | +# |
| 7 | +# Unless required by applicable law or agreed to in writing, software |
| 8 | +# distributed under the License is distributed on an 'AS IS' BASIS, WITHOUT |
| 9 | +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 10 | +# License for the specific language governing permissions and limitations |
| 11 | +# under the License. |
| 12 | +# |
| 13 | +'''DCS Backup v1 action implementations''' |
| 14 | +import logging |
| 15 | + |
| 16 | +from osc_lib import utils |
| 17 | +from osc_lib.command import command |
| 18 | + |
| 19 | +from otcextensions.common import sdk_utils |
| 20 | + |
| 21 | +from otcextensions.i18n import _ |
| 22 | + |
| 23 | +LOG = logging.getLogger(__name__) |
| 24 | + |
| 25 | + |
| 26 | +def _get_columns(item): |
| 27 | + column_map = { |
| 28 | + } |
| 29 | + return sdk_utils.get_osc_show_columns_for_sdk_resource(item, column_map) |
| 30 | + |
| 31 | + |
| 32 | +class ListBackup(command.Lister): |
| 33 | + _description = _('List backups of a single DCS instance') |
| 34 | + columns = ('id', 'name', 'progress', 'status', 'error_code') |
| 35 | + |
| 36 | + def get_parser(self, prog_name): |
| 37 | + parser = super(ListBackup, self).get_parser(prog_name) |
| 38 | + parser.add_argument( |
| 39 | + 'instance', |
| 40 | + metavar='<instance>', |
| 41 | + help=_('Name or ID of the instance') |
| 42 | + ) |
| 43 | + parser.add_argument( |
| 44 | + '--limit', |
| 45 | + metavar='<limit>', |
| 46 | + type=int, |
| 47 | + help=_('Limit number of records to return.') |
| 48 | + ) |
| 49 | + parser.add_argument( |
| 50 | + '--start', |
| 51 | + metavar='<start>', |
| 52 | + type=int, |
| 53 | + help=_('Start number for querying.') |
| 54 | + ) |
| 55 | + parser.add_argument( |
| 56 | + '--from_time', |
| 57 | + metavar='<yyyyMMddHHmmss>', |
| 58 | + help=_('Start time of the period to be queried.') |
| 59 | + ) |
| 60 | + parser.add_argument( |
| 61 | + '--to_time', |
| 62 | + metavar='<yyyyMMddHHmmss>', |
| 63 | + help=_('End time of the period to be queried.') |
| 64 | + ) |
| 65 | + return parser |
| 66 | + |
| 67 | + def take_action(self, parsed_args): |
| 68 | + client = self.app.client_manager.dcs |
| 69 | + |
| 70 | + query = {} |
| 71 | + if parsed_args.limit: |
| 72 | + query['limit'] = parsed_args.limit |
| 73 | + if parsed_args.start: |
| 74 | + query['start'] = parsed_args.start |
| 75 | + if parsed_args.from_time: |
| 76 | + query['begin_time'] = parsed_args.from_time |
| 77 | + if parsed_args.to_time: |
| 78 | + query['end_time'] = parsed_args.to_time |
| 79 | + |
| 80 | + inst = client.find_instance(name_or_id=parsed_args.instance, |
| 81 | + ignore_missing=False) |
| 82 | + data = client.backups( |
| 83 | + instance={'id': inst.id}, |
| 84 | + **query |
| 85 | + ) |
| 86 | + |
| 87 | + table = (self.columns, |
| 88 | + (utils.get_item_properties( |
| 89 | + s, self.columns, |
| 90 | + ) for s in data)) |
| 91 | + return table |
| 92 | + |
| 93 | + |
| 94 | +class DeleteBackup(command.Command): |
| 95 | + _description = _('Delete DCS instance backup') |
| 96 | + |
| 97 | + def get_parser(self, prog_name): |
| 98 | + parser = super(DeleteBackup, self).get_parser(prog_name) |
| 99 | + parser.add_argument( |
| 100 | + 'instance', |
| 101 | + metavar='<instance>', |
| 102 | + help=_('ID of the instance.') |
| 103 | + ) |
| 104 | + parser.add_argument( |
| 105 | + 'backup', |
| 106 | + metavar='<backup>', |
| 107 | + nargs='+', |
| 108 | + help=_('ID of the instance backup to delete.') |
| 109 | + ) |
| 110 | + return parser |
| 111 | + |
| 112 | + def take_action(self, parsed_args): |
| 113 | + |
| 114 | + if parsed_args.instance: |
| 115 | + client = self.app.client_manager.dcs |
| 116 | + inst = client.find_instance(name_or_id=parsed_args.instance, |
| 117 | + ignore_missing=False) |
| 118 | + for backup in parsed_args.backup: |
| 119 | + client.delete_instance_backup( |
| 120 | + backup=backup, |
| 121 | + instance_id=inst.id |
| 122 | + ) |
| 123 | + |
| 124 | + |
| 125 | +class CreateBackup(command.ShowOne): |
| 126 | + _description = _('Create a backup of a DCS instance') |
| 127 | + |
| 128 | + def get_parser(self, prog_name): |
| 129 | + parser = super(CreateBackup, self).get_parser(prog_name) |
| 130 | + parser.add_argument( |
| 131 | + 'instance', |
| 132 | + metavar='<instance>', |
| 133 | + help=_('Name or ID of the DCS instance to take backup from.') |
| 134 | + ) |
| 135 | + parser.add_argument( |
| 136 | + '--description', |
| 137 | + metavar='<description>', |
| 138 | + help=_('Description of the backup.') |
| 139 | + ) |
| 140 | + return parser |
| 141 | + |
| 142 | + def take_action(self, parsed_args): |
| 143 | + |
| 144 | + client = self.app.client_manager.dcs |
| 145 | + |
| 146 | + attrs = {} |
| 147 | + |
| 148 | + if parsed_args.description: |
| 149 | + attrs['description'] = parsed_args.description |
| 150 | + |
| 151 | + inst = client.find_instance(name_or_id=parsed_args.instance, |
| 152 | + ignore_missing=False) |
| 153 | + |
| 154 | + obj = client.backup_instance( |
| 155 | + instance={'id': inst.id}, |
| 156 | + **attrs) |
| 157 | + |
| 158 | + display_columns, columns = _get_columns(obj) |
| 159 | + data = utils.get_item_properties(obj, columns) |
| 160 | + |
| 161 | + return (display_columns, data) |
0 commit comments