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 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547
|
# 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 openstackclient.identity import common as identity_common
from osc_lib.cli import parseractions
from osc_lib.command import command
from osc_lib import exceptions
from osc_lib import utils as osc_utils
from manilaclient import api_versions
from manilaclient.common._i18n import _
LOG = logging.getLogger(__name__)
class CreateShareGroup(command.ShowOne):
"""Create new share group."""
_description = _(
"Create new share group")
def get_parser(self, prog_name):
parser = super(CreateShareGroup, self).get_parser(prog_name)
parser.add_argument(
'--name',
metavar="<name>",
default=None,
help=_('Share group name')
)
parser.add_argument(
"--description",
metavar="<description>",
default=None,
help=_("Share group description."),
)
parser.add_argument(
"--share-types",
metavar="<share-types>",
nargs="+",
default=[],
help=_("Name or ID of share type(s)."),
)
parser.add_argument(
"--share-group-type",
metavar="<share-group-type>",
default=None,
help=_("Share group type name or ID of the share "
"group to be created."),
)
parser.add_argument(
"--share-network",
metavar="<share-network>",
default=False,
help=_("Specify share network name or id"),
)
parser.add_argument(
"--source-share-group-snapshot",
metavar="<source-share-group-snapshot>",
default=False,
help=_("Share group snapshot name or ID to create "
"the share group from."),
)
parser.add_argument(
"--availability-zone",
metavar='<availability-zone>',
default=None,
help=_("Optional availability zone in which group "
"should be created"),
)
parser.add_argument(
"--wait",
action='store_true',
default=False,
help=_('Wait for share group creation')
)
return parser
def take_action(self, parsed_args):
share_client = self.app.client_manager.share
share_types = []
for share_type in parsed_args.share_types:
share_types.append(
osc_utils.find_resource(
share_client.share_types,
share_type,
)
)
share_group_type = None
if parsed_args.share_group_type:
share_group_type = osc_utils.find_resource(
share_client.share_group_types,
parsed_args.share_group_type).id
share_network = None
if parsed_args.share_network:
share_network = osc_utils.find_resource(
share_client.share_networks,
parsed_args.share_network).id
source_share_group_snapshot = None
if parsed_args.source_share_group_snapshot:
source_share_group_snapshot = osc_utils.find_resource(
share_client.share_group_snapshots,
parsed_args.source_share_group_snapshot).id
body = {
'name': parsed_args.name,
'description': parsed_args.description,
'share_types': share_types,
'share_group_type': share_group_type,
'share_network': share_network,
'source_share_group_snapshot': source_share_group_snapshot,
'availability_zone': parsed_args.availability_zone
}
share_group = share_client.share_groups.create(**body)
if parsed_args.wait:
if not osc_utils.wait_for_status(
status_f=share_client.share_groups.get,
res_id=share_group.id,
success_status=['available']
):
LOG.error(_("ERROR: Share group is in error state."))
share_group = osc_utils.find_resource(
share_client.share_groups,
share_group.id)
printable_share_group = share_group._info
printable_share_group.pop('links', None)
if printable_share_group.get('share_types'):
if parsed_args.formatter == 'table':
printable_share_group['share_types'] = (
"\n".join(printable_share_group['share_types'])
)
return self.dict2columns(printable_share_group)
class DeleteShareGroup(command.Command):
"""Delete one or more share groups."""
_description = _("Delete one or more share groups")
def get_parser(self, prog_name):
parser = super(DeleteShareGroup, self).get_parser(prog_name)
parser.add_argument(
"share_group",
metavar="<share_group>",
nargs="+",
help=_("Name or ID of the share group(s) to delete")
)
parser.add_argument(
"--force",
action='store_true',
default=False,
help=_("Attempt to force delete the share group (Default=False) "
"(Admin only).")
)
parser.add_argument(
"--wait",
action='store_true',
default=False,
help=_("Wait for share group to delete")
)
return parser
def take_action(self, parsed_args):
share_client = self.app.client_manager.share
result = 0
for share_group in parsed_args.share_group:
try:
share_group_obj = osc_utils.find_resource(
share_client.share_groups,
share_group)
share_client.share_groups.delete(
share_group_obj,
force=parsed_args.force)
if parsed_args.wait:
if not osc_utils.wait_for_delete(
manager=share_client.share_groups,
res_id=share_group_obj.id):
result += 1
except Exception as e:
result += 1
LOG.error(_(
"Failed to delete share group with "
"name or ID '%(share_group)s': %(e)s"),
{'share_group': share_group, 'e': e})
if result > 0:
total = len(parsed_args.share_group)
msg = (_("%(result)s of %(total)s share groups failed "
"to delete.") % {'result': result, 'total': total})
raise exceptions.CommandError(msg)
class ListShareGroup(command.Lister):
"""List share groups."""
_description = _("List share groups")
def get_parser(self, prog_name):
parser = super(ListShareGroup, self).get_parser(prog_name)
parser.add_argument(
"--all-projects",
action='store_true',
default=False,
help=_("Display share groups from all projects (Admin only).")
)
parser.add_argument(
"--name",
metavar="<name>",
default=None,
help=_("Filter results by name.")
)
parser.add_argument(
"--description",
metavar="<description>",
default=None,
help=_("Filter results by description. Available "
"only for microversion >= 2.36.")
)
parser.add_argument(
"--status",
metavar="<status>",
default=None,
help=_("Filter results by status.")
)
parser.add_argument(
"--share-server",
metavar="<share-server-id>",
default=None,
help=_("Filter results by share server ID.")
)
parser.add_argument(
"--share-group-type",
metavar="<share-group-type>",
default=None,
help=_("Filter results by a share group type ID "
"or name that was used for share group "
"creation. ")
)
parser.add_argument(
"--snapshot",
metavar="<snapshot>",
default=None,
help=_("Filter results by share group snapshot "
"name or ID that was used to create the "
"share group. ")
)
parser.add_argument(
"--host",
metavar="<host>",
default=None,
help=_("Filter results by host.")
)
parser.add_argument(
"--share-network",
metavar="<share-network>",
default=None,
help=_("Filter results by share-network name or "
"ID. ")
)
parser.add_argument(
"--project",
metavar="<project>",
default=None,
help=_("Filter results by project name or ID. Useful with "
"set key '--all-projects'. ")
)
identity_common.add_project_domain_option_to_parser(parser)
parser.add_argument(
"--limit",
metavar="<limit>",
type=int,
default=None,
action=parseractions.NonNegativeAction,
help=_("Limit the number of share groups returned")
)
parser.add_argument(
"--marker",
metavar="<marker>",
help=_("The last share group ID of the previous page")
)
parser.add_argument(
'--sort',
metavar="<key>[:<direction>]",
default='name:asc',
help=_("Sort output by selected keys and directions(asc or desc) "
"(default: name:asc), multiple keys and directions can be "
"specified separated by comma")
)
parser.add_argument(
"--name~",
metavar="<name~>",
default=None,
help=_("Filter results matching a share group "
"name pattern. Available only for "
"microversion >= 2.36. ")
)
parser.add_argument(
"--description~",
metavar="<description~>",
default=None,
help=_("Filter results matching a share group "
"description pattern. Available only for "
"microversion >= 2.36. ")
)
return parser
def take_action(self, parsed_args):
share_client = self.app.client_manager.share
identity_client = self.app.client_manager.identity
share_server_id = None
if parsed_args.share_server:
share_server_id = osc_utils.find_resource(
share_client.share_servers,
parsed_args.share_server).id
share_group_type = None
if parsed_args.share_group_type:
share_group_type = osc_utils.find_resource(
share_client.share_group_types,
parsed_args.share_group_type).id
snapshot = None
if parsed_args.snapshot:
snapshot = apiutils.find_resource(
share_client.share_snapshots,
parsed_args.snapshot).id
share_network = None
if parsed_args.share_network:
share_network = osc_utils.find_resource(
share_client.share_networks,
parsed_args.share_network).id
project_id = None
if parsed_args.project:
project_id = identity_common.find_project(
identity_client,
parsed_args.project,
parsed_args.project_domain).id
columns = [
'ID',
'Name',
'Status',
'Description',
]
search_opts = {
'all_tenants': parsed_args.all_projects,
'name': parsed_args.name,
'status': parsed_args.status,
'share_server_id': share_server_id,
'share_group_type': share_group_type,
'snapshot': snapshot,
'host': parsed_args.host,
'share_network': share_network,
'project_id': project_id,
'limit': parsed_args.limit,
'offset': parsed_args.marker,
}
if share_client.api_version >= api_versions.APIVersion("2.36"):
search_opts['name~'] = getattr(parsed_args, 'name~')
search_opts['description~'] = getattr(parsed_args, 'description~')
search_opts['description'] = parsed_args.description
elif (parsed_args.description or getattr(parsed_args, 'name~') or
getattr(parsed_args, 'description~')):
raise exceptions.CommandError(
"Pattern based filtering (name~, description~ and description)"
" is only available with manila API version >= 2.36")
if parsed_args.all_projects:
columns.append('Project ID')
share_groups = share_client.share_groups.list(search_opts=search_opts)
data = (osc_utils.get_dict_properties(
share_group._info, columns) for share_group in share_groups)
return (columns, data)
class ShowShareGroup(command.ShowOne):
"""Show share group."""
_description = _("Show details about a share group")
def get_parser(self, prog_name):
parser = super(ShowShareGroup, self).get_parser(prog_name)
parser.add_argument(
"share_group",
metavar="<share-group>",
help=_("Name or ID of the share group.")
)
return parser
def take_action(self, parsed_args):
share_client = self.app.client_manager.share
share_group = osc_utils.find_resource(
share_client.share_groups,
parsed_args.share_group)
printable_share_group = share_group._info
printable_share_group.pop('links', None)
if printable_share_group.get('share_types'):
if parsed_args.formatter == 'table':
printable_share_group['share_types'] = "\n".join(
printable_share_group['share_types'])
return self.dict2columns(printable_share_group)
class SetShareGroup(command.Command):
"""Set share group."""
_description = _("Explicitly set share group status")
def get_parser(self, prog_name):
parser = super(SetShareGroup, self).get_parser(prog_name)
parser.add_argument(
'share_group',
metavar="<share-group>",
help=_('Name or ID of the share group to update.')
)
parser.add_argument(
'--name',
metavar="<name>",
default=None,
help=_('New name for the share group. (Default=None)')
)
parser.add_argument(
'--description',
metavar='<description>',
default=None,
help=_('Share group description. (Default=None)')
)
parser.add_argument(
'--status',
metavar='<status>',
default=None,
help=_('Explicitly update the status of a share group (Admin '
'only). Examples include: available, error, creating, '
'deleting, error_deleting.')
)
return parser
def take_action(self, parsed_args):
share_client = self.app.client_manager.share
result = 0
share_group = osc_utils.find_resource(
share_client.share_groups,
parsed_args.share_group)
kwargs = {}
if parsed_args.name is not None:
kwargs['name'] = parsed_args.name
if parsed_args.description is not None:
kwargs['description'] = parsed_args.description
if kwargs:
try:
share_client.share_groups.update(
share_group.id,
**kwargs
)
except Exception as e:
LOG.error(_("Failed to update share group name "
"or description: %s"), e)
result += 1
if parsed_args.status:
try:
share_group.reset_state(parsed_args.status)
except Exception as e:
LOG.error(_(
"Failed to set status for the share group: %s"), e)
result += 1
if result > 0:
raise exceptions.CommandError(_("One or more of the "
"set operations failed"))
class UnsetShareGroup(command.Command):
"""Unset a share group property."""
_description = _("Unset a share group property")
def get_parser(self, prog_name):
parser = super(UnsetShareGroup, self).get_parser(prog_name)
parser.add_argument(
'share_group',
metavar="<share-group>",
help=_("Name or ID of the share group "
"to set a property for.")
)
parser.add_argument(
"--name",
action='store_true',
help=_("Unset share group name."),
)
parser.add_argument(
"--description",
action='store_true',
help=_("Unset share group description."),
)
return parser
def take_action(self, parsed_args):
share_client = self.app.client_manager.share
share_group = osc_utils.find_resource(
share_client.share_groups,
parsed_args.share_group)
kwargs = {}
if parsed_args.name:
kwargs['name'] = None
if parsed_args.description:
kwargs['description'] = None
if kwargs:
try:
share_client.share_groups.update(
share_group,
**kwargs
)
except Exception as e:
raise exceptions.CommandError(_(
"Failed to unset share_group name "
"or description : %s" % e))
|