Fix 'openstack keypair list --project <project>'
The --project option of 'openstack keypair list' is supposed to filter keypairs by a project but has not been working and instead returns keypairs from all projects. The reason appears to be because it uses a request for a user list filtered by project but tenant_id/project_id is not a valid filter for GET /users. This fixes the issue by requesting role assignments for the specified project and then requesting keypairs for users with a role in the project. This change depends on a recent openstacksdk bug fix change Ic552dee83d56278d2b866de0cb365a0c394fe26a which fixed the user_id query parameter for the compute /os-keypairs APIs. The bug fix was released in openstacksdk 4.4.0. Closes-Bug: #2096947 Change-Id: Ibb5757766e3040e58d64388b95678fab9b2b6f23
This commit is contained in:
@@ -300,6 +300,7 @@ class ListKeypair(command.Lister):
|
||||
def take_action(self, parsed_args):
|
||||
compute_client = self.app.client_manager.compute
|
||||
identity_client = self.app.client_manager.identity
|
||||
identity_sdk_client = self.app.client_manager.sdk_connection.identity
|
||||
|
||||
kwargs = {}
|
||||
|
||||
@@ -345,11 +346,17 @@ class ListKeypair(command.Lister):
|
||||
parsed_args.project,
|
||||
parsed_args.project_domain,
|
||||
).id
|
||||
users = identity_client.users.list(tenant_id=project)
|
||||
assignments = identity_sdk_client.role_assignments(
|
||||
scope_project_id=project
|
||||
)
|
||||
user_ids = set()
|
||||
for assignment in assignments:
|
||||
if assignment.user:
|
||||
user_ids.add(assignment.user['id'])
|
||||
|
||||
data = []
|
||||
for user in users:
|
||||
kwargs['user_id'] = user.id
|
||||
for user_id in user_ids:
|
||||
kwargs['user_id'] = user_id
|
||||
data.extend(compute_client.keypairs(**kwargs))
|
||||
elif parsed_args.user:
|
||||
if not sdk_utils.supports_microversion(compute_client, '2.10'):
|
||||
|
||||
Reference in New Issue
Block a user