diff --git a/iam/__version__.py b/iam/__version__.py index 4c1d823..fdc6eda 100644 --- a/iam/__version__.py +++ b/iam/__version__.py @@ -1,3 +1,3 @@ # -*- coding: utf-8 -*- -__version__ = "1.3.4" +__version__ = "1.3.5" diff --git a/iam/contrib/django/dispatcher/dispatchers.py b/iam/contrib/django/dispatcher/dispatchers.py index 9c3aae1..fb6b195 100644 --- a/iam/contrib/django/dispatcher/dispatchers.py +++ b/iam/contrib/django/dispatcher/dispatchers.py @@ -151,7 +151,9 @@ def _dispatch_list_attr_value(self, request, data, request_id): def _dispatch_list_instance(self, request, data, request_id): options = self._get_options(request) - filter_obj = get_filter_obj(data.get("filter"), ["parent", "search", "action", "resource_type_chain"]) + filter_obj = get_filter_obj( + data.get("filter"), ["parent", "search", "action", "resource_type_chain", "ancestors"] + ) page_obj = get_page_obj(data.get("page")) provider = self._provider[data["type"]] @@ -198,7 +200,7 @@ def _dispatch_list_instance_by_policy(self, request, data, request_id): def _dispatch_search_instance(self, request, data, request_id): options = self._get_options(request) - filter_obj = get_filter_obj(data.get("filter"), ["parent", "action", "keyword"]) + filter_obj = get_filter_obj(data.get("filter"), ["parent", "action", "keyword", "ancestors"]) if filter_obj.keyword is None or len(filter_obj.keyword) < 2: raise KeywordTooShortException("the length of keyword should be greater than or equals to 2") diff --git a/iam/utils.py b/iam/utils.py index 7e8e45f..7eab38f 100644 --- a/iam/utils.py +++ b/iam/utils.py @@ -86,11 +86,12 @@ def gen_perms_apply_data(system, subject, action_to_resources_list): # NOTE: old _bk_iam_path_ is like /set,1/host,2/ # while the new _bk_iam_path_ is like /bk_cmdb,set,1/bk_cmdb,host,2/ node_parts = part.split(",") - rtype, rid = "", "" + # NOTE: topo resources should be considered to belong to different systems + rsystem, rtype, rid = system_id, "", "" if len(node_parts) == 2: rtype, rid = node_parts[0], node_parts[1] elif len(node_parts) == 3: - rtype, rid = node_parts[1], node_parts[2] + rsystem, rtype, rid = node_parts[0], node_parts[1], node_parts[2] # NOTE: currently, keep the name of /bk_cmdb,set,1/ same as /set,1/ part = ",".join(node_parts[1:]) else: @@ -99,7 +100,7 @@ def gen_perms_apply_data(system, subject, action_to_resources_list): inst_item.append( { "type": rtype, - "type_name": meta.get_resource_name(system_id, rtype), + "type_name": meta.get_resource_name(rsystem, rtype), "id": rid, "name": part, } diff --git a/readme.md b/readme.md index 58eccd0..454ab2c 100644 --- a/readme.md +++ b/readme.md @@ -1,7 +1,7 @@ ![](docs/resource/img/bk_iam_zh.png) --- -[![license](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat)](https://github.com/TencentBlueKing/iam-python-sdk/blob/master/LICENSE.txt) [![Release Version](https://img.shields.io/badge/release-1.3.4-brightgreen.svg)](https://github.com/TencentBlueKing/iam-python-sdk/releases) [![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)](https://github.com/TencentBlueKing/iam-python-sdk/pulls) [![BK Pipelines Status](https://api.bkdevops.qq.com/process/api/external/pipelines/projects/iam/p-5c359e750bb9457984ab84656651d843/badge?X-DEVOPS-PROJECT-ID=iam)](http://devops.oa.com/process/api-html/user/builds/projects/iam/pipelines/p-5c359e750bb9457984ab84656651d843/latestFinished?X-DEVOPS-PROJECT-ID=iam) +[![license](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat)](https://github.com/TencentBlueKing/iam-python-sdk/blob/master/LICENSE.txt) [![Release Version](https://img.shields.io/badge/release-1.3.5-brightgreen.svg)](https://github.com/TencentBlueKing/iam-python-sdk/releases) [![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)](https://github.com/TencentBlueKing/iam-python-sdk/pulls) [![BK Pipelines Status](https://api.bkdevops.qq.com/process/api/external/pipelines/projects/iam/p-5c359e750bb9457984ab84656651d843/badge?X-DEVOPS-PROJECT-ID=iam)](http://devops.oa.com/process/api-html/user/builds/projects/iam/pipelines/p-5c359e750bb9457984ab84656651d843/latestFinished?X-DEVOPS-PROJECT-ID=iam) [(English Documents Available)](readme_en.md) diff --git a/readme_en.md b/readme_en.md index 0fc83e1..2319785 100644 --- a/readme_en.md +++ b/readme_en.md @@ -1,7 +1,7 @@ ![](docs/resource/img/bk_iam_en.png) --- -[![license](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat)](https://github.com/TencentBlueKing/iam-python-sdk/blob/master/LICENSE.txt) [![Release Version](https://img.shields.io/badge/release-1.3.4-brightgreen.svg)](https://github.com/TencentBlueKing/iam-python-sdk/releases) [![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)](https://github.com/TencentBlueKing/iam-python-sdk/pulls) [![BK Pipelines Status](https://api.bkdevops.qq.com/process/api/external/pipelines/projects/iam/p-5c359e750bb9457984ab84656651d843/badge?X-DEVOPS-PROJECT-ID=iam)](http://devops.oa.com/process/api-html/user/builds/projects/iam/pipelines/p-5c359e750bb9457984ab84656651d843/latestFinished?X-DEVOPS-PROJECT-ID=iam) +[![license](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat)](https://github.com/TencentBlueKing/iam-python-sdk/blob/master/LICENSE.txt) [![Release Version](https://img.shields.io/badge/release-1.3.5-brightgreen.svg)](https://github.com/TencentBlueKing/iam-python-sdk/releases) [![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)](https://github.com/TencentBlueKing/iam-python-sdk/pulls) [![BK Pipelines Status](https://api.bkdevops.qq.com/process/api/external/pipelines/projects/iam/p-5c359e750bb9457984ab84656651d843/badge?X-DEVOPS-PROJECT-ID=iam)](http://devops.oa.com/process/api-html/user/builds/projects/iam/pipelines/p-5c359e750bb9457984ab84656651d843/latestFinished?X-DEVOPS-PROJECT-ID=iam) ## Overview diff --git a/release.md b/release.md index ae5e45a..bc1dda4 100644 --- a/release.md +++ b/release.md @@ -1,6 +1,11 @@ 版本日志 =============== +# v1.3.5 + +- add: list_instance、search_instance 新增 ancestors 返回 +- fix: gen_perms_apply_data拓扑资源需考虑system id + # v1.3.4 - IAMMigrator 将 bk_app_code/bk_app_secret 设置为类实例属性 diff --git a/tests/contrib/django/dispatcher/test_dispatchers.py b/tests/contrib/django/dispatcher/test_dispatchers.py index 2de3188..90dc994 100644 --- a/tests/contrib/django/dispatcher/test_dispatchers.py +++ b/tests/contrib/django/dispatcher/test_dispatchers.py @@ -119,7 +119,7 @@ def test_basic_resource_api_dispatcher__dispatch__json_load_fail(): assert resp["code"] == 400 assert resp["result"] is False assert resp["data"] is None - assert resp["message"] == "reqeust body is not a valid json" + assert resp["message"] == "request body is not a valid json" assert "X-Request-Id" in resp @@ -369,7 +369,13 @@ def fetch_resource_type_schema(self, **options): { "method": "list_instance", "type": "spy", - "filter": {"parent": "parent", "search": "search", "resource_type_chain": "resource_type_chain"}, + "filter": { + "parent": "parent", + "search": "search", + "ancestors": "ancestors", + "action": "action", + "resource_type_chain": "resource_type_chain" + }, "page": {"limit": "limit", "offset": "offset"}, } ) @@ -378,7 +384,13 @@ def fetch_resource_type_schema(self, **options): resp = dispatcher._dispatch(list_instance_req) provider.pre_list_instance.assert_called_once_with( - {"parent": "parent", "search": "search", "resource_type_chain": "resource_type_chain"}, + { + "parent": "parent", + "search": "search", + "ancestors": "ancestors", + "resource_type_chain": "resource_type_chain", + "action": "action", + }, {"limit": "limit", "offset": "offset"}, language="en", ) @@ -389,7 +401,13 @@ def fetch_resource_type_schema(self, **options): assert "message" in resp assert provider.list_instance_spy == { "options": {"language": "en"}, - "filter": {"parent": "parent", "search": "search", "resource_type_chain": "resource_type_chain"}, + "filter": { + "parent": "parent", + "search": "search", + "ancestors": "ancestors", + "resource_type_chain": "resource_type_chain", + "action": "action" + }, "page": {"limit": "limit", "offset": "offset"}, } @@ -399,7 +417,7 @@ def fetch_resource_type_schema(self, **options): { "method": "search_instance", "type": "spy", - "filter": {"parent": "parent", "keyword": "keyword"}, + "filter": {"parent": "parent", "keyword": "keyword", "action": "action", "ancestors": "ancestors"}, "page": {"limit": "limit", "offset": "offset"}, } ) @@ -408,7 +426,7 @@ def fetch_resource_type_schema(self, **options): resp = dispatcher._dispatch(search_instance_req) provider.pre_search_instance.assert_called_once_with( - {"parent": "parent", "keyword": "keyword"}, + {"parent": "parent", "keyword": "keyword", "action": "action", "ancestors": "ancestors"}, {"limit": "limit", "offset": "offset"}, language="en", ) @@ -419,7 +437,7 @@ def fetch_resource_type_schema(self, **options): assert "message" in resp assert provider.search_instance_spy == { "options": {"language": "en"}, - "filter": {"parent": "parent", "keyword": "keyword"}, + "filter": {"parent": "parent", "keyword": "keyword", "action": "action", "ancestors": "ancestors"}, "page": {"limit": "limit", "offset": "offset"}, } diff --git a/tests/test_utils.py b/tests/test_utils.py index 866e718..bea28de 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -65,6 +65,7 @@ def get_resource_name(system, resource): "module": "module_type", }, "another_system": {"r4": "r4_type"}, + "bk_cmdb": {"biz": "biz", "set": "set", "module": "module"} }[system][resource] with patch("iam.utils.meta.get_system_name", MagicMock(side_effect=get_system_name)): @@ -90,7 +91,6 @@ def get_resource_name(system, resource): ) # assert data - # TODO: fix dict compare assert data == { "system_id": "test_system", "system_name": "test_system_name", @@ -105,21 +105,55 @@ def get_resource_name(system, resource): "type": "r3", "type_name": "r3_type", "instances": [ - [{"type": "r1", "type_name": "r1_type", "id": "r1id", "name": "r1n"}], - [{"type": "r2", "type_name": "r2_type", "id": "r2id", "name": ""}], - [{"type": "r3", "type_name": "r3_type", "id": "r3id", "name": ""}], - ], + [ + { + "type": "r1", + "type_name": "r1_type", + "id": "r1id", + "name": "r1n" + } + ], + [ + { + "type": "r2", + "type_name": "r2_type", + "id": "r2id", + "name": "" + } + ], + [ + { + "type": "r3", + "type_name": "r3_type", + "id": "r3id", + "name": "" + } + ] + ] }, { "system_id": "another_system", "system_name": "another_system_name", "type": "r4", "type_name": "r4_type", - "instances": [[{"type": "r4", "type_name": "r4_type", "id": "r4id", "name": "r4n"}]], - }, - ], + "instances": [ + [ + { + "type": "r4", + "type_name": "r4_type", + "id": "r4id", + "name": "r4n" + } + ] + ] + } + ] + }, + { + "id": "action2", + "name": "action2_name", + "related_resource_types": [] }, - {"id": "action2", "name": "action2_name", "related_resource_types": []}, { "id": "action3", "name": "action3_name", @@ -130,13 +164,55 @@ def get_resource_name(system, resource): "type": "r3", "type_name": "r3_type", "instances": [ - [{"type": "r1", "type_name": "r1_type", "id": "r1id", "name": "r1n"}], - [{"type": "r3", "type_name": "r3_type", "id": "r3id", "name": ""}], - [{"type": "r1", "type_name": "r1_type", "id": "r1id", "name": "r1n"}], - [{"type": "r3", "type_name": "r3_type", "id": "r3id", "name": ""}], - [{"type": "r2", "type_name": "r2_type", "id": "r2id", "name": ""}], - [{"type": "r3", "type_name": "r3_type", "id": "r3id", "name": ""}], - ], + [ + { + "type": "r1", + "type_name": "r1_type", + "id": "r1id", + "name": "r1n" + } + ], + [ + { + "type": "r3", + "type_name": "r3_type", + "id": "r3id", + "name": "" + } + ], + [ + { + "type": "r1", + "type_name": "r1_type", + "id": "r1id", + "name": "r1n" + } + ], + [ + { + "type": "r3", + "type_name": "r3_type", + "id": "r3id", + "name": "" + } + ], + [ + { + "type": "r2", + "type_name": "r2_type", + "id": "r2id", + "name": "" + } + ], + [ + { + "type": "r3", + "type_name": "r3_type", + "id": "r3id", + "name": "" + } + ] + ] }, { "system_id": "another_system", @@ -144,12 +220,33 @@ def get_resource_name(system, resource): "type": "r4", "type_name": "r4_type", "instances": [ - [{"type": "r4", "type_name": "r4_type", "id": "r4id", "name": "r4n"}], - [{"type": "r4", "type_name": "r4_type", "id": "r4id", "name": "r4n"}], - [{"type": "r4", "type_name": "r4_type", "id": "r5id", "name": "r5n"}], - ], - }, - ], + [ + { + "type": "r4", + "type_name": "r4_type", + "id": "r4id", + "name": "r4n" + } + ], + [ + { + "type": "r4", + "type_name": "r4_type", + "id": "r4id", + "name": "r4n" + } + ], + [ + { + "type": "r4", + "type_name": "r4_type", + "id": "r5id", + "name": "r5n" + } + ] + ] + } + ] }, { "id": "action4", @@ -162,14 +259,34 @@ def get_resource_name(system, resource): "type_name": "r6_type", "instances": [ [ - {"type": "biz", "type_name": "biz_type", "id": "1", "name": "biz,1"}, - {"type": "set", "type_name": "set_type", "id": "2", "name": "set,2"}, - {"type": "module", "type_name": "module_type", "id": "3", "name": "module,3"}, - {"type": "r6", "type_name": "r6_type", "id": "r6id", "name": "r6n"}, + { + "type": "biz", + "type_name": "biz_type", + "id": "1", + "name": "biz,1" + }, + { + "type": "set", + "type_name": "set_type", + "id": "2", + "name": "set,2" + }, + { + "type": "module", + "type_name": "module_type", + "id": "3", + "name": "module,3" + }, + { + "type": "r6", + "type_name": "r6_type", + "id": "r6id", + "name": "r6n" + } ] - ], + ] } - ], + ] }, { "id": "action4", @@ -182,14 +299,34 @@ def get_resource_name(system, resource): "type_name": "r7_type", "instances": [ [ - {"type": "biz", "type_name": "biz_type", "id": "1", "name": "biz,1"}, - {"type": "set", "type_name": "set_type", "id": "2", "name": "set,2"}, - {"type": "module", "type_name": "module_type", "id": "3", "name": "module,3"}, - {"type": "r7", "type_name": "r7_type", "id": "r7id", "name": "r7n"}, + { + "type": "biz", + "type_name": "biz", + "id": "1", + "name": "biz,1" + }, + { + "type": "set", + "type_name": "set", + "id": "2", + "name": "set,2" + }, + { + "type": "module", + "type_name": "module", + "id": "3", + "name": "module,3" + }, + { + "type": "r7", + "type_name": "r7_type", + "id": "r7id", + "name": "r7n" + } ] - ], + ] } - ], - }, - ], + ] + } + ] }