1919from collections .abc import Collection , Iterable , Sequence
2020import datetime
2121import functools
22- from typing import Any , cast
22+ from typing import Any
2323
2424from cliff import columns as cliff_columns
25+ from openstack import utils as sdk_utils
2526from osc_lib import utils
2627
2728from openstackclient import command
2829from openstackclient .i18n import _
30+ from openstackclient .identity import common as identity_common
2931
3032
3133# TODO(stephenfin): This exists in a couple of places and should be moved to a
@@ -41,7 +43,9 @@ class ProjectColumn(cliff_columns.FormattableColumn[str]):
4143 project_cache)`` to use this.
4244 """
4345
44- def __init__ (self , value : str , project_cache : Any = None ) -> None :
46+ def __init__ (
47+ self , value : str , project_cache : dict [str , str ] | None = None
48+ ) -> None :
4549 super ().__init__ (value )
4650 self .project_cache = project_cache or {}
4751
@@ -50,8 +54,8 @@ def human_readable(self) -> str:
5054 if not project :
5155 return ''
5256
53- if project in self .project_cache . keys () :
54- return cast ( str , self .project_cache [project ]. name )
57+ if project in self .project_cache :
58+ return self .project_cache [project ]
5559
5660 return project
5761
@@ -66,7 +70,7 @@ def human_readable(self) -> str:
6670 return f"{ self ._value :.2f} "
6771
6872
69- def _formatters (project_cache : Any ) -> dict [str , Any ]:
73+ def _formatters (project_cache : dict [ str , str ] | None ) -> dict [str , Any ]:
7074 return {
7175 'project_id' : functools .partial (
7276 ProjectColumn , project_cache = project_cache
@@ -135,12 +139,16 @@ def take_action(
135139 def _format_project (project : str ) -> str :
136140 if not project :
137141 return ""
138- if project in project_cache . keys () :
139- return cast ( str , project_cache [project ]. name )
142+ if project in project_cache :
143+ return project_cache [project ]
140144 else :
141145 return project
142146
143147 compute_client = self .app .client_manager .compute
148+ identity_client = sdk_utils .ensure_service_version (
149+ self .app .client_manager .sdk_connection .identity , '3'
150+ )
151+
144152 columns = (
145153 "project_id" ,
146154 "server_usages" ,
@@ -182,8 +190,8 @@ def _format_project(project: str) -> str:
182190 # Cache the project list
183191 project_cache = {}
184192 try :
185- for p in self . app . client_manager . identity . projects . list ():
186- project_cache [p .id ] = p
193+ for p in identity_client . projects ():
194+ project_cache [p .id ] = p . name
187195 except Exception : # noqa: S110
188196 # Just forget it if there's any trouble
189197 pass
@@ -221,6 +229,7 @@ def get_parser(self, prog_name: str) -> argparse.ArgumentParser:
221229 default = None ,
222230 help = _ ("Name or ID of project to show usage for" ),
223231 )
232+ identity_common .add_project_domain_option_to_parser (parser )
224233 parser .add_argument (
225234 "--start" ,
226235 metavar = "<start>" ,
@@ -240,8 +249,11 @@ def get_parser(self, prog_name: str) -> argparse.ArgumentParser:
240249 def take_action (
241250 self , parsed_args : argparse .Namespace
242251 ) -> tuple [Sequence [str ], Iterable [Any ]]:
243- identity_client = self .app .client_manager .identity
244252 compute_client = self .app .client_manager .compute
253+ identity_client = sdk_utils .ensure_service_version (
254+ self .app .client_manager .sdk_connection .identity , '3'
255+ )
256+
245257 date_cli_format = "%Y-%m-%d"
246258 now = datetime .datetime .now (datetime .UTC ).replace (tzinfo = None )
247259
@@ -258,13 +270,16 @@ def take_action(
258270 end = now + datetime .timedelta (days = 1 )
259271
260272 if parsed_args .project :
261- project = utils . find_resource (
262- identity_client . projects ,
273+ project = identity_common . find_project_id_sdk (
274+ identity_client ,
263275 parsed_args .project ,
264- ).id
276+ parsed_args .project_domain ,
277+ )
265278 else :
266279 # Get the project from the current auth
267- project = self .app .client_manager .auth_ref .project_id
280+ _project_id = self .app .client_manager .auth_ref .project_id
281+ assert _project_id is not None # narrow type
282+ project = _project_id
268283
269284 usage = compute_client .get_usage (
270285 project = project ,
0 commit comments