Skip to content

Commit

Permalink
Merge "Allow project switching for Designate API"
Browse files Browse the repository at this point in the history
  • Loading branch information
Zuul authored and openstack-gerrit committed May 28, 2024
2 parents 8091075 + e7bdb02 commit e3196ba
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 3 deletions.
20 changes: 20 additions & 0 deletions openstack/dns/v2/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,26 @@ def find(cls, session, name_or_id, ignore_missing=True, **params):
f"No {cls.__name__} found for {name_or_id}"
)

@classmethod
def list(
cls,
session,
project_id=None,
all_projects=None,
**params,
):
headers: ty.Union[ty.Dict[str, str] | None] = (
{} if project_id or all_projects else None
)

if headers is not None:
if project_id:
headers["x-auth-sudo-project-id"] = str(project_id)
if all_projects:
headers["x-auth-all-projects"] = str(all_projects)

return super().list(session=session, headers=headers, **params)

@classmethod
def _get_next_link(cls, uri, response, data, marker, limit, total_yielded):
next_link = None
Expand Down
2 changes: 1 addition & 1 deletion openstack/dns/v2/_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -561,7 +561,7 @@ def create_zone_transfer_accept(self, **attrs):

# ======== Zone Shares ========
def zone_shares(self, zone, **query):
"""Retrieve a generator of zone sharess
"""Retrieve a generator of zone shares
:param zone: The zone ID or a
:class:`~openstack.dns.v2.zone.Zone` instance
Expand Down
2 changes: 1 addition & 1 deletion openstack/proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -879,7 +879,7 @@ def should_skip_resource_cleanup(self, resource=None, skip_resources=None):

if resource_name in skip_resources:
self.log.debug(
f"Skipping resource {resource_name} " "in project cleanup"
f"Skipping resource {resource_name} in project cleanup"
)
return True

Expand Down
9 changes: 8 additions & 1 deletion openstack/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -1990,6 +1990,7 @@ def list(
allow_unknown_params=False,
*,
microversion=None,
headers=None,
**params,
):
"""This method is a generator which yields resource objects.
Expand All @@ -2010,6 +2011,8 @@ def list(
passing everything known to the server. ``False`` will result in
validation exception when unknown query parameters are passed.
:param str microversion: API version to override the negotiated one.
:param dict headers: Additional headers to inject into the HTTP
request.
:param dict params: These keyword arguments are passed through the
:meth:`~openstack.resource.QueryParamter._transpose` method
to find if any of them match expected query parameters to be sent
Expand Down Expand Up @@ -2079,14 +2082,18 @@ def _dict_filter(f, d):
return False
return True

headers_final = {"Accept": "application/json"}
if headers:
headers_final = {**headers_final, **headers}

# Track the total number of resources yielded so we can paginate
# swift objects
total_yielded = 0
while uri:
# Copy query_params due to weird mock unittest interactions
response = session.get(
uri,
headers={"Accept": "application/json"},
headers=headers_final,
params=query_params.copy(),
microversion=microversion,
)
Expand Down
33 changes: 33 additions & 0 deletions openstack/tests/unit/test_resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from keystoneauth1 import adapter
import requests

from openstack import dns
from openstack import exceptions
from openstack import format
from openstack import resource
Expand Down Expand Up @@ -2651,6 +2652,38 @@ class Test(self.test_class):
Test.base_path % {"something": uri_param},
)

def test_list_with_injected_headers(self):
mock_empty = mock.Mock()
mock_empty.status_code = 200
mock_empty.json.return_value = {"resources": []}

self.session.get.side_effect = [mock_empty]

_ = list(
self.test_class.list(self.session, headers={'X-Test': 'value'})
)

expected = {'Accept': 'application/json', 'X-Test': 'value'}
self.assertEqual(
expected, self.session.get.call_args.kwargs['headers']
)

@mock.patch.object(resource.Resource, 'list')
def test_list_dns_with_headers(self, mock_resource_list):
dns.v2._base.Resource.list(
self.session,
project_id='1234',
all_projects=True,
)

expected = {
'x-auth-sudo-project-id': '1234',
'x-auth-all-projects': 'True',
}
self.assertEqual(
expected, mock_resource_list.call_args.kwargs['headers']
)

def test_allow_invalid_list_params(self):
qp = "query param!"
qp_name = "query-param"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
features:
- |
Add functionality to list DNS resources for a certain project only,
or for all projects, using the new `project_id` and `all_projects`
parameters.

0 comments on commit e3196ba

Please sign in to comment.