From 0b2b870237b6a332239dcad73cd9cab2634cb44f Mon Sep 17 00:00:00 2001 From: Graham Hayes Date: Wed, 1 Jun 2016 16:51:34 +0100 Subject: [PATCH] Add Global Flags for filtering results In the HTTP Headers --all-projects sets "X-Auth-All-Projects:True" --edit-managed sets "X-Designate-Edit-Managed:True" --sudo-project-id sets "X-Auth-Sudo-Project-ID:" Change-Id: I4985a1a2cd75da4311e819afdbfdafcc8a023c56 --- designateclient/osc/plugin.py | 26 +++++++++++++++++++++- designateclient/v2/cli/recordsets.py | 11 ++++++++++ designateclient/v2/cli/zones.py | 2 ++ designateclient/v2/client.py | 33 +++++++++++++++++++++++++--- designateclient/v2/utils.py | 2 +- 5 files changed, 69 insertions(+), 5 deletions(-) diff --git a/designateclient/osc/plugin.py b/designateclient/osc/plugin.py index d57c54d3..90fded4e 100644 --- a/designateclient/osc/plugin.py +++ b/designateclient/osc/plugin.py @@ -32,8 +32,15 @@ def make_client(instance): API_NAME, instance._api_version[API_NAME], API_VERSIONS) kwargs = oscutils.build_kwargs_dict('endpoint_type', instance._interface) + + parsed_args = instance.get_configuration() + return cls(session=instance.session, - region_name=instance._region_name, **kwargs) + region_name=instance._region_name, + all_projects=parsed_args.get('all_projects', False), + edit_managed=parsed_args.get('edit_managed', False), + sudo_project_id=parsed_args.get('sudo_project_id', None), + **kwargs) def build_option_parser(parser): @@ -46,4 +53,21 @@ def build_option_parser(parser): DEFAULT_API_VERSION + ' (Env: OS_DNS_API_VERSION)') + parser.add_argument( + '--all-projects', + default=False, + action='store_true', + help='Show results from all projects. Default=False') + + parser.add_argument( + '--edit-managed', + default=False, + action='store_true', + help='Edit resources marked as managed. Default=False') + + parser.add_argument( + '--sudo-project-id', + default=None, + help='Project ID to impersonate for this command. Default=None') + return parser diff --git a/designateclient/v2/cli/recordsets.py b/designateclient/v2/cli/recordsets.py index 00d93486..281c03a5 100644 --- a/designateclient/v2/cli/recordsets.py +++ b/designateclient/v2/cli/recordsets.py @@ -33,6 +33,14 @@ def _format_recordset(recordset): return recordset +def _has_project_id(data): + if len(data) < 1: + return False + if 'project_id' in data[0]: + return True + return False + + class ListRecordSetsCommand(lister.Lister): """List recordsets""" @@ -88,6 +96,9 @@ class ListRecordSetsCommand(lister.Lister): data = get_all(client.recordsets.list, args=[parsed_args.zone_id], criterion=criterion) + if client.session.all_projects and _has_project_id(data): + cols.insert(1, 'project_id') + for i, rs in enumerate(data): data[i] = _format_recordset(rs) diff --git a/designateclient/v2/cli/zones.py b/designateclient/v2/cli/zones.py index 234d910e..86b3bfb8 100644 --- a/designateclient/v2/cli/zones.py +++ b/designateclient/v2/cli/zones.py @@ -85,6 +85,8 @@ class ListZonesCommand(lister.Lister): data = get_all(client.zones.list, criterion) cols = self.columns + if client.session.all_projects: + cols.insert(1, 'project_id') return cols, (utils.get_item_properties(s, cols) for s in data) diff --git a/designateclient/v2/client.py b/designateclient/v2/client.py index df8aebb2..c466ead5 100644 --- a/designateclient/v2/client.py +++ b/designateclient/v2/client.py @@ -40,6 +40,9 @@ class DesignateAdapter(adapter.LegacyJsonAdapter): """ def __init__(self, *args, **kwargs): self.timeout = kwargs.pop('timeout', None) + self.all_projects = kwargs.pop('all_projects', False) + self.edit_managed = kwargs.pop('edit_managed', False) + self.sudo_project_id = kwargs.pop('sudo_project_id', None) super(self.__class__, self).__init__(*args, **kwargs) def request(self, *args, **kwargs): @@ -48,7 +51,27 @@ class DesignateAdapter(adapter.LegacyJsonAdapter): if self.timeout is not None: kwargs.setdefault('timeout', self.timeout) - kwargs.setdefault('headers', {}).setdefault( + kwargs.setdefault('headers', {}) + + if self.all_projects: + kwargs['headers'].setdefault( + 'X-Auth-All-Projects', + self.all_projects + ) + + if self.edit_managed: + kwargs['headers'].setdefault( + 'X-Designate-Edit-Managed-Records', + self.edit_managed + ) + + if self.sudo_project_id is not None: + kwargs['headers'].setdefault( + 'X-Auth-Sudo-Project-ID', + self.sudo_project_id + ) + + kwargs['headers'].setdefault( 'Content-Type', 'application/json') response, body = super(self.__class__, self).request(*args, **kwargs) @@ -77,7 +100,8 @@ class Client(object): def __init__(self, region_name=None, endpoint_type='publicURL', extensions=None, service_type='dns', service_name=None, http_log_debug=False, session=None, auth=None, timeout=None, - endpoint_override=None): + endpoint_override=None, all_projects=False, + edit_managed=False, sudo_project_id=None): if session is None: raise ValueError("A session instance is required") @@ -90,7 +114,10 @@ class Client(object): user_agent='python-designateclient-%s' % version.version_info, version=('2'), endpoint_override=endpoint_override, - timeout=timeout + timeout=timeout, + all_projects=all_projects, + edit_managed=edit_managed, + sudo_project_id=sudo_project_id ) self.blacklists = BlacklistController(self) diff --git a/designateclient/v2/utils.py b/designateclient/v2/utils.py index 4a69a551..b56b4cee 100644 --- a/designateclient/v2/utils.py +++ b/designateclient/v2/utils.py @@ -77,4 +77,4 @@ def get_all(function, criterion=None, args=None): else: break - return returned_data \ No newline at end of file + return returned_data