diff --git a/kong/__init__.py b/kong/__init__.py index 147c745..caefa5e 100644 --- a/kong/__init__.py +++ b/kong/__init__.py @@ -1,3 +1,3 @@ """Asynchronous Kong client""" -__version__ = '0.3.1' +__version__ = '0.3.2' diff --git a/kong/components.py b/kong/components.py index ca78659..889d5e7 100644 --- a/kong/components.py +++ b/kong/components.py @@ -103,6 +103,9 @@ def __init__(self, root, data): def __getitem__(self, item): return self.data[item] + def __contains__(self, item): + return item in self.data + @property def cli(self): return self.root.cli diff --git a/kong/plugins.py b/kong/plugins.py index d6d1404..9cfda46 100644 --- a/kong/plugins.py +++ b/kong/plugins.py @@ -14,10 +14,12 @@ def get_list(self, **params): url = '%s/%s' % (self.root.url, self.name) return self.execute(url, params=params, wrap=self.wrap_list) - async def apply_json(self, data): + async def apply_json(self, data, **kwargs): if not isinstance(data, list): data = [data] - plugins = await self.get_list() + plugins = await self.get_list(**kwargs) + if not kwargs: + plugins = [p for p in plugins if self.root_plugin(p)] plugins = dict(((p['name'], p) for p in plugins)) result = [] for entry in data: @@ -36,6 +38,9 @@ async def apply_json(self, data): await self.delete(entry['id']) return result + def root_plugin(self, plugin): + return 'service_id' not in plugin and 'route_id' not in plugin + async def preprocess_parameters(self, params): await anonymous(self.cli, params) preprocessor = PLUGIN_PREPROCESSORS.get(params.get('name')) @@ -62,6 +67,10 @@ async def create(self, skip_error=None, **params): wrap=self.wrap, skip_error=skip_error ) + async def apply_json(self, data, **kwargs): + kwargs['service_id'] = self.root.id + return await super().apply_json(data, **kwargs) + class RoutePlugins(PluginMixin, CrudComponent): """Plugins associated with a Route @@ -74,6 +83,10 @@ async def create(self, skip_error=None, **params): wrap=self.wrap, skip_error=skip_error ) + async def apply_json(self, data, **kwargs): + kwargs['route_id'] = self.root.id + return await super().apply_json(data, **kwargs) + class Plugin(KongEntity): pass