File: identity.py

package info (click to toggle)
python-osc-lib 4.2.0-4
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 820 kB
  • sloc: python: 6,505; makefile: 21; sh: 2
file content (85 lines) | stat: -rw-r--r-- 2,975 bytes parent folder | download
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
#   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 argparse

from openstack import connection
from openstack import exceptions
from openstack.identity.v3 import project

from osc_lib.i18n import _


def add_project_owner_option_to_parser(
    parser: argparse.ArgumentParser,
) -> None:
    """Register project and project domain options.

    :param parser: argparse.Argument parser object.
    """
    parser.add_argument(
        '--project',
        metavar='<project>',
        help=_("Owner's project (name or ID)"),
    )
    parser.add_argument(
        '--project-domain',
        metavar='<project-domain>',
        help=_(
            'Domain the project belongs to (name or ID). '
            'This can be used in case collisions between project names '
            'exist.'
        ),
    )


# TODO(stephenfin): This really doesn't belong here. This should be part of
# openstacksdk itself.
def find_project(
    sdk_connection: connection.Connection,
    name_or_id: str,
    domain_name_or_id: str | None = None,
) -> project.Project:
    """Find a project by its name name or ID.

    If Forbidden to find the resource (a common case if the user does not have
    permission), then return the resource by creating a local instance of
    openstack.identity.v3.Project resource.

    :param sdk_connection: Connection object of OpenStack SDK.
    :type sdk_connection: `openstack.connection.Connection`
    :param name_or_id: Name or ID of the project
    :type name_or_id: string
    :param domain_name_or_id: Domain name or ID of the project.
        This can be used when there are multiple projects with a same name.
    :returns: the project object found
    :rtype: `openstack.identity.v3.project.Project`
    """
    try:
        if domain_name_or_id:
            domain = sdk_connection.identity.find_domain(
                domain_name_or_id, ignore_missing=False
            )
            domain_id = domain.id
        else:
            domain_id = None
        return sdk_connection.identity.find_project(
            name_or_id, ignore_missing=False, domain_id=domain_id
        )
    # NOTE: OpenStack SDK raises HttpException for 403 response code.
    # There is no specific exception class at now, so we need to catch
    # HttpException and check the status code.
    except exceptions.HttpException as e:
        if e.status_code == 403:
            return project.Project(id=name_or_id, name=name_or_id)
        raise