| 1 |
# Copyright 2016 NEC Corporation |
| 2 |
# |
| 3 |
# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 4 |
# not use this file except in compliance with the License. You may obtain |
| 5 |
# a copy of the License at |
| 6 |
# |
| 7 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
# |
| 9 |
# Unless required by applicable law or agreed to in writing, software |
| 10 |
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 11 |
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 |
# License for the specific language governing permissions and limitations |
| 13 |
# under the License. |
| 14 |
|
| 15 |
"""This module should contain OSC plugin generic methods. |
| 16 |
|
| 17 |
Methods in this module are candidates adopted to osc-lib. |
| 18 |
|
| 19 |
Stuffs specific to neutronclient OSC plugin should not be added |
| 20 |
to this module. They should go to neutronclient.osc.v2.utils. |
| 21 |
""" |
| 22 |
|
| 23 |
from keystoneclient import exceptions as identity_exc |
| 24 |
from keystoneclient.v3 import domains |
| 25 |
from keystoneclient.v3 import projects |
| 26 |
from osc_lib import utils |
| 27 |
|
| 28 |
from neutronclient._i18n import _ |
| 29 |
|
| 30 |
|
| 31 |
# TODO(amotoki): Use osc-lib version once osc-lib provides this. |
| 32 |
def add_project_owner_option_to_parser(parser): |
| 33 |
"""Register project and project domain options. |
| 34 |
|
| 35 |
:param parser: argparse.Argument parser object. |
| 36 |
|
| 37 |
""" |
| 38 |
parser.add_argument( |
| 39 |
'--project', |
| 40 |
metavar='<project>', |
| 41 |
help=_("Owner's project (name or ID)") |
| 42 |
) |
| 43 |
# Borrowed from openstackclient.identity.common |
| 44 |
# as it is not exposed officially. |
| 45 |
parser.add_argument( |
| 46 |
'--project-domain', |
| 47 |
metavar='<project-domain>', |
| 48 |
help=_('Domain the project belongs to (name or ID). ' |
| 49 |
'This can be used in case collisions between project names ' |
| 50 |
'exist.'), |
| 51 |
) |
| 52 |
|
| 53 |
|
| 54 |
# The following methods are borrowed from openstackclient.identity.common |
| 55 |
# as it is not exposed officially. |
| 56 |
# TODO(amotoki): Use osc-lib version once osc-lib provides this. |
| 57 |
|
| 58 |
|
| 59 |
def find_domain(identity_client, name_or_id): |
| 60 |
return _find_identity_resource(identity_client.domains, name_or_id, |
| 61 |
domains.Domain) |
| 62 |
|
| 63 |
|
| 64 |
def find_project(identity_client, name_or_id, domain_name_or_id=None): |
| 65 |
domain_id = _get_domain_id_if_requested(identity_client, domain_name_or_id) |
| 66 |
if not domain_id: |
| 67 |
return _find_identity_resource(identity_client.projects, name_or_id, |
| 68 |
projects.Project) |
| 69 |
else: |
| 70 |
return _find_identity_resource(identity_client.projects, name_or_id, |
| 71 |
projects.Project, domain_id=domain_id) |
| 72 |
|
| 73 |
|
| 74 |
def _get_domain_id_if_requested(identity_client, domain_name_or_id): |
| 75 |
if not domain_name_or_id: |
| 76 |
return None |
| 77 |
domain = find_domain(identity_client, domain_name_or_id) |
| 78 |
return domain.id |
| 79 |
|
| 80 |
|
| 81 |
def _find_identity_resource(identity_client_manager, name_or_id, |
| 82 |
resource_type, **kwargs): |
| 83 |
"""Find a specific identity resource. |
| 84 |
|
| 85 |
Using keystoneclient's manager, attempt to find a specific resource by its |
| 86 |
name or ID. If Forbidden to find the resource (a common case if the user |
| 87 |
does not have permission), then return the resource by creating a local |
| 88 |
instance of keystoneclient's Resource. |
| 89 |
|
| 90 |
The parameter identity_client_manager is a keystoneclient manager, |
| 91 |
for example: keystoneclient.v3.users or keystoneclient.v3.projects. |
| 92 |
|
| 93 |
The parameter resource_type is a keystoneclient resource, for example: |
| 94 |
keystoneclient.v3.users.User or keystoneclient.v3.projects.Project. |
| 95 |
|
| 96 |
:param identity_client_manager: the manager that contains the resource |
| 97 |
:type identity_client_manager: `keystoneclient.base.CrudManager` |
| 98 |
:param name_or_id: the resources's name or ID |
| 99 |
:type name_or_id: string |
| 100 |
:param resource_type: class that represents the resource type |
| 101 |
:type resource_type: `keystoneclient.base.Resource` |
| 102 |
|
| 103 |
:returns: the resource in question |
| 104 |
:rtype: `keystoneclient.base.Resource` |
| 105 |
|
| 106 |
""" |
| 107 |
|
| 108 |
try: |
| 109 |
identity_resource = utils.find_resource(identity_client_manager, |
| 110 |
name_or_id, **kwargs) |
| 111 |
if identity_resource is not None: |
| 112 |
return identity_resource |
| 113 |
except identity_exc.Forbidden: |
| 114 |
pass |
| 115 |
|
| 116 |
return resource_type(None, {'id': name_or_id, 'name': name_or_id}) |
| 117 |
|
| 118 |
|
| 119 |
# The above are borrowed from openstackclient.identity.common. |
| 120 |
# DO NOT ADD original methods in neutronclient repo to the above area. |
| 121 |
|
| 122 |
|
| 123 |
def _get_columns(item): |
| 124 |
column_map = {} |
| 125 |
hidden_columns = ['location', 'tenant_id'] |
| 126 |
return utils.get_osc_show_columns_for_sdk_resource( |
| 127 |
item, |
| 128 |
column_map, |
| 129 |
hidden_columns |
| 130 |
) |