|
| 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 | + |
| 14 | +"""Auto-allocated Topology Implementations""" |
| 15 | + |
| 16 | +import logging |
| 17 | + |
| 18 | +from osc_lib.command import command |
| 19 | +from osc_lib import utils |
| 20 | + |
| 21 | +from openstackclient.i18n import _ |
| 22 | +from openstackclient.identity import common as identity_common |
| 23 | +from openstackclient.network import sdk_utils |
| 24 | + |
| 25 | +LOG = logging.getLogger(__name__) |
| 26 | + |
| 27 | + |
| 28 | +def _get_columns(item): |
| 29 | + column_map = { |
| 30 | + 'tenant_id': 'project_id', |
| 31 | + } |
| 32 | + return sdk_utils.get_osc_show_columns_for_sdk_resource(item, column_map) |
| 33 | + |
| 34 | + |
| 35 | +def _format_check_resource_columns(): |
| 36 | + return ('dry_run',) |
| 37 | + |
| 38 | + |
| 39 | +def _format_check_resource(item): |
| 40 | + item_id = getattr(item, 'id', False) |
| 41 | + if item_id == 'dry-run=pass': |
| 42 | + item.check_resource = 'pass' |
| 43 | + return item |
| 44 | + |
| 45 | + |
| 46 | +def _get_attrs(client_manager, parsed_args): |
| 47 | + attrs = {} |
| 48 | + if parsed_args.project: |
| 49 | + identity_client = client_manager.identity |
| 50 | + project_id = identity_common.find_project( |
| 51 | + identity_client, |
| 52 | + parsed_args.project, |
| 53 | + parsed_args.project_domain, |
| 54 | + ).id |
| 55 | + attrs['tenant_id'] = project_id |
| 56 | + if parsed_args.check_resources: |
| 57 | + attrs['check_resources'] = True |
| 58 | + |
| 59 | + return attrs |
| 60 | + |
| 61 | + |
| 62 | +# TODO(ankur-gupta-f): Use the SDK resource mapped attribute names once the |
| 63 | +# OSC minimum requirements include SDK 1.0. |
| 64 | +class CreateAutoAllocatedTopology(command.ShowOne): |
| 65 | + _description = _("Create the auto allocated topology for project") |
| 66 | + |
| 67 | + def get_parser(self, prog_name): |
| 68 | + parser = super(CreateAutoAllocatedTopology, self).get_parser(prog_name) |
| 69 | + parser.add_argument( |
| 70 | + '--project', |
| 71 | + metavar='<project>', |
| 72 | + help=_("Return the auto allocated topology for a given project. " |
| 73 | + "Default is current project") |
| 74 | + ) |
| 75 | + identity_common.add_project_domain_option_to_parser(parser) |
| 76 | + parser.add_argument( |
| 77 | + '--check-resources', |
| 78 | + action='store_true', |
| 79 | + help=_("Validate the requirements for auto allocated topology. " |
| 80 | + "Does not return a topology.") |
| 81 | + ) |
| 82 | + parser.add_argument( |
| 83 | + '--or-show', |
| 84 | + action='store_true', |
| 85 | + default=True, |
| 86 | + help=_("If topology exists returns the topology's " |
| 87 | + "information (Default)") |
| 88 | + ) |
| 89 | + |
| 90 | + return parser |
| 91 | + |
| 92 | + def check_resource_topology(self, client, parsed_args): |
| 93 | + obj = client.validate_auto_allocated_topology(parsed_args.project) |
| 94 | + |
| 95 | + columns = _format_check_resource_columns() |
| 96 | + data = utils.get_item_properties(_format_check_resource(obj), |
| 97 | + columns, |
| 98 | + formatters={}) |
| 99 | + |
| 100 | + return (columns, data) |
| 101 | + |
| 102 | + def get_topology(self, client, parsed_args): |
| 103 | + obj = client.get_auto_allocated_topology(parsed_args.project) |
| 104 | + display_columns, columns = _get_columns(obj) |
| 105 | + data = utils.get_item_properties(obj, columns, formatters={}) |
| 106 | + return (display_columns, data) |
| 107 | + |
| 108 | + def take_action(self, parsed_args): |
| 109 | + client = self.app.client_manager.network |
| 110 | + if parsed_args.check_resources: |
| 111 | + columns, data = self.check_resource_topology(client, parsed_args) |
| 112 | + else: |
| 113 | + columns, data = self.get_topology(client, parsed_args) |
| 114 | + return (columns, data) |
| 115 | + |
| 116 | + |
| 117 | +# TODO(ankur-gupta-f): Use the SDK resource mapped attribute names once the |
| 118 | +# OSC minimum requirements include SDK 1.0. |
| 119 | +class DeleteAutoAllocatedTopology(command.Command): |
| 120 | + _description = _("Delete auto allocated topology for project") |
| 121 | + |
| 122 | + def get_parser(self, prog_name): |
| 123 | + parser = super(DeleteAutoAllocatedTopology, self).get_parser(prog_name) |
| 124 | + parser.add_argument( |
| 125 | + '--project', |
| 126 | + metavar='<project>', |
| 127 | + help=_('Delete auto allocated topology for a given project. ' |
| 128 | + 'Default is the current project') |
| 129 | + ) |
| 130 | + identity_common.add_project_domain_option_to_parser(parser) |
| 131 | + |
| 132 | + return parser |
| 133 | + |
| 134 | + def take_action(self, parsed_args): |
| 135 | + client = self.app.client_manager.network |
| 136 | + client.delete_auto_allocated_topology(parsed_args.project) |
0 commit comments