| 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 |
import argparse |
| 15 |
|
| 16 |
from openstack import connection |
| 17 |
from openstack import exceptions |
| 18 |
from openstack.identity.v3 import project |
| 19 |
|
| 20 |
from osc_lib.i18n import _ |
| 21 |
|
| 22 |
|
| 23 |
def add_project_owner_option_to_parser( |
| 24 |
parser: argparse.ArgumentParser, |
| 25 |
) -> None: |
| 26 |
"""Register project and project domain options. |
| 27 |
|
| 28 |
:param parser: argparse.Argument parser object. |
| 29 |
""" |
| 30 |
parser.add_argument( |
| 31 |
'--project', |
| 32 |
metavar='<project>', |
| 33 |
help=_("Owner's project (name or ID)"), |
| 34 |
) |
| 35 |
parser.add_argument( |
| 36 |
'--project-domain', |
| 37 |
metavar='<project-domain>', |
| 38 |
help=_( |
| 39 |
'Domain the project belongs to (name or ID). ' |
| 40 |
'This can be used in case collisions between project names ' |
| 41 |
'exist.' |
| 42 |
), |
| 43 |
) |
| 44 |
|
| 45 |
|
| 46 |
# TODO(stephenfin): This really doesn't belong here. This should be part of |
| 47 |
# openstacksdk itself. |
| 48 |
def find_project( |
| 49 |
sdk_connection: connection.Connection, |
| 50 |
name_or_id: str, |
| 51 |
domain_name_or_id: str | None = None, |
| 52 |
) -> project.Project: |
| 53 |
"""Find a project by its name name or ID. |
| 54 |
|
| 55 |
If Forbidden to find the resource (a common case if the user does not have |
| 56 |
permission), then return the resource by creating a local instance of |
| 57 |
openstack.identity.v3.Project resource. |
| 58 |
|
| 59 |
:param sdk_connection: Connection object of OpenStack SDK. |
| 60 |
:type sdk_connection: `openstack.connection.Connection` |
| 61 |
:param name_or_id: Name or ID of the project |
| 62 |
:type name_or_id: string |
| 63 |
:param domain_name_or_id: Domain name or ID of the project. |
| 64 |
This can be used when there are multiple projects with a same name. |
| 65 |
:returns: the project object found |
| 66 |
:rtype: `openstack.identity.v3.project.Project` |
| 67 |
""" |
| 68 |
try: |
| 69 |
if domain_name_or_id: |
| 70 |
domain = sdk_connection.identity.find_domain( |
| 71 |
domain_name_or_id, ignore_missing=False |
| 72 |
) |
| 73 |
domain_id = domain.id |
| 74 |
else: |
| 75 |
domain_id = None |
| 76 |
|
| 77 |
return sdk_connection.identity.find_project( # type: ignore |
| 78 |
name_or_id, ignore_missing=False, domain_id=domain_id |
| 79 |
) |
| 80 |
# NOTE: OpenStack SDK raises HttpException for 403 response code. |
| 81 |
# There is no specific exception class at now, so we need to catch |
| 82 |
# HttpException and check the status code. |
| 83 |
except exceptions.HttpException as e: |
| 84 |
if e.status_code == 403: |
| 85 |
return project.Project(id=name_or_id, name=name_or_id) # type: ignore |
| 86 |
raise |