Skip to content

Commit

Permalink
Allow to skip searching via all tenants in Openstack
Browse files Browse the repository at this point in the history
Set all_tenants=False to avoid the following error
novaclient.exceptions.Forbidden: Policy doesn't allow os_compute_api:servers:detail:get_all_tenants to be performed

Signed-off-by: Andrej Podhradsky <apodhrad@redhat.com>
  • Loading branch information
apodhrad committed Aug 23, 2023
1 parent 4b0d85b commit 1357cfe
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions wrapanapi/systems/openstack.py
Original file line number Diff line number Diff line change
Expand Up @@ -793,18 +793,18 @@ def _generic_paginator(self, f):
break
return lists

def list_vms(self, filter_tenants=True):
call = partial(self.api.servers.list, True, {'all_tenants': True})
def list_vms(self, filter_tenants=True, all_tenants=True):
call = partial(self.api.servers.list, True, {'all_tenants': all_tenants})
instances = self._generic_paginator(call)
if filter_tenants:
if filter_tenants and all_tenants:
# Filter instances based on their tenant ID
# needed for CFME 5.3 and higher
tenants = self._get_tenants()
ids = [tenant.id for tenant in tenants]
instances = [i for i in instances if i.tenant_id in ids]
return [OpenstackInstance(system=self, uuid=i.id, raw=i) for i in instances]

def find_vms(self, name=None, id=None, ip=None):
def find_vms(self, name=None, id=None, ip=None, all_tenants=True):
"""
Find VM based on name OR IP OR ID
Expand All @@ -826,7 +826,7 @@ def find_vms(self, name=None, id=None, ip=None):
if not any((name, ip, id)):
raise ValueError("Any of these parameters must be specified: name, ip, or id")
matches = []
instances = self.list_vms()
instances = self.list_vms(all_tenants=all_tenants)
for instance in instances:
# Use 'instance.raw' below so we don't refresh the properties, since we
# *just* pulled down this list of VMs and stored the raw data in list_vms()
Expand All @@ -840,7 +840,7 @@ def find_vms(self, name=None, id=None, ip=None):
matches.append(instance)
return matches

def get_vm(self, name=None, id=None, ip=None):
def get_vm(self, name=None, id=None, ip=None, all_tenants=True):
"""
Get a VM based on name, or ID, or IP
Expand All @@ -862,7 +862,7 @@ def get_vm(self, name=None, id=None, ip=None):
kwargs = {'name': name, 'id': id, 'ip': ip}
kwargs = {key: val for key, val in kwargs.items() if val is not None}

matches = self.find_vms(**kwargs)
matches = self.find_vms(**kwargs, all_tenants=all_tenants)
if not matches:
raise VMInstanceNotFound('match criteria: {}'.format(kwargs))
elif len(matches) > 1:
Expand Down

0 comments on commit 1357cfe

Please sign in to comment.