Skip to content

Commit 943ab4e

Browse files
authored
fix: gen_perms_apply_data拓扑资源按照资源类型聚合 (#90)
# Reviewed, transaction id: 6369
1 parent fe97bf7 commit 943ab4e

File tree

6 files changed

+113
-69
lines changed

6 files changed

+113
-69
lines changed

iam/__version__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
# -*- coding: utf-8 -*-
22

3-
__version__ = "1.3.5"
3+
__version__ = "1.3.6"

iam/utils.py

Lines changed: 54 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@
99
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
1010
specific language governing permissions and limitations under the License.
1111
"""
12-
13-
14-
from collections import OrderedDict
12+
import itertools
13+
from collections import OrderedDict, defaultdict
1514

1615
from . import meta
1716

@@ -61,51 +60,60 @@ def gen_perms_apply_data(system, subject, action_to_resources_list):
6160
for system_id, resources in system_resources.items():
6261
system_resources_list.setdefault(system_id, []).append(resources)
6362

63+
# 2. aggregate resources by resource type, generate related_resource_type
6464
related_resource_types = []
6565
for system_id, resources_list in system_resources_list.items():
66-
# get resource type from last resource in resources
67-
a_resource = resources_list[0][-1]
68-
resource_types = {
69-
"system_id": system_id,
70-
"system_name": meta.get_system_name(system_id),
71-
"type": a_resource.type,
72-
"type_name": meta.get_resource_name(system_id, a_resource.type),
73-
}
74-
instances = []
75-
76-
for resources in resources_list:
66+
if not resources_list:
67+
continue
68+
69+
# aggregate resources by resource type
70+
resource_type__resources = defaultdict(list)
71+
for resource in list(itertools.chain(*resources_list)):
72+
resource_type__resources[resource.type].append(resource)
73+
74+
# get the list of resource instances of the same type
75+
for __, resources in resource_type__resources.items():
76+
a_resource = resources[0]
77+
resource_types = {
78+
"system_id": system_id,
79+
"system_name": meta.get_system_name(system_id),
80+
"type": a_resource.type,
81+
"type_name": meta.get_resource_name(system_id, a_resource.type),
82+
}
83+
instances = []
84+
85+
# arrange instances according to topo level
7786
for resource in resources:
7887
inst_item = []
79-
topo_path = None
80-
81-
if resource.attribute:
82-
topo_path = resource.attribute.get("_bk_iam_path_")
83-
84-
if topo_path:
85-
for part in topo_path[1:-1].split("/"):
86-
# NOTE: old _bk_iam_path_ is like /set,1/host,2/
87-
# while the new _bk_iam_path_ is like /bk_cmdb,set,1/bk_cmdb,host,2/
88-
node_parts = part.split(",")
89-
# NOTE: topo resources should be considered to belong to different systems
90-
rsystem, rtype, rid = system_id, "", ""
91-
if len(node_parts) == 2:
92-
rtype, rid = node_parts[0], node_parts[1]
93-
elif len(node_parts) == 3:
94-
rsystem, rtype, rid = node_parts[0], node_parts[1], node_parts[2]
95-
# NOTE: currently, keep the name of /bk_cmdb,set,1/ same as /set,1/
96-
part = ",".join(node_parts[1:])
97-
else:
98-
raise Exception("Invalid _bk_iam_path_: %s" % topo_path)
99-
100-
inst_item.append(
101-
{
102-
"type": rtype,
103-
"type_name": meta.get_resource_name(rsystem, rtype),
104-
"id": rid,
105-
"name": part,
106-
}
107-
)
108-
88+
topo_path = []
89+
# get the topo level of the resource
90+
if resource.attribute and resource.attribute.get("_bk_iam_path_"):
91+
bk_iam_path = resource.attribute["_bk_iam_path_"]
92+
topo_path = bk_iam_path[1:-1].split("/")
93+
# append paernt topo instance
94+
for part in topo_path:
95+
# NOTE: old _bk_iam_path_ is like /set,1/host,2/
96+
# while the new _bk_iam_path_ is like /bk_cmdb,set,1/bk_cmdb,host,2/
97+
node_parts = part.split(",")
98+
# NOTE: topo resources should be considered to belong to different systems
99+
rsystem, rtype, rid = system_id, "", ""
100+
if len(node_parts) == 2:
101+
rtype, rid = node_parts[0], node_parts[1]
102+
elif len(node_parts) == 3:
103+
rsystem, rtype, rid = node_parts[0], node_parts[1], node_parts[2]
104+
# NOTE: currently, keep the name of /bk_cmdb,set,1/ same as /set,1/
105+
part = ",".join(node_parts[1:])
106+
else:
107+
raise Exception("Invalid _bk_iam_path_: %s" % topo_path)
108+
inst_item.append(
109+
{
110+
"type": rtype,
111+
"type_name": meta.get_resource_name(rsystem, rtype),
112+
"id": rid,
113+
"name": part,
114+
}
115+
)
116+
# lastly, append self
109117
inst_item.append(
110118
{
111119
"type": resource.type,
@@ -114,11 +122,10 @@ def gen_perms_apply_data(system, subject, action_to_resources_list):
114122
"name": resource.attribute.get("name", "") if resource.attribute else "",
115123
}
116124
)
117-
118125
instances.append(inst_item)
119126

120-
resource_types["instances"] = instances
121-
related_resource_types.append(resource_types)
127+
resource_types["instances"] = instances
128+
related_resource_types.append(resource_types)
122129

123130
action["related_resource_types"] = related_resource_types
124131
actions.append(action)

readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
![](docs/resource/img/bk_iam_zh.png)
22
---
33

4-
[![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)
4+
[![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.6-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)
55

66
[(English Documents Available)](readme_en.md)
77

readme_en.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
![](docs/resource/img/bk_iam_en.png)
22
---
33

4-
[![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)
4+
[![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.6-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)
55

66
## Overview
77

release.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
版本日志
22
===============
33

4+
# v1.3.5
5+
6+
- fix: gen_perms_apply_data拓扑资源按照资源类型聚合
7+
8+
49
# v1.3.5
510

611
- add: list_instance、search_instance 新增 ancestors 返回

tests/test_utils.py

Lines changed: 51 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ def get_resource_name(system, resource):
9090
],
9191
)
9292

93-
# assert data
93+
# assert data
9494
assert data == {
9595
"system_id": "test_system",
9696
"system_name": "test_system_name",
@@ -102,8 +102,8 @@ def get_resource_name(system, resource):
102102
{
103103
"system_id": "test_system",
104104
"system_name": "test_system_name",
105-
"type": "r3",
106-
"type_name": "r3_type",
105+
"type": "r1",
106+
"type_name": "r1_type",
107107
"instances": [
108108
[
109109
{
@@ -112,15 +112,31 @@ def get_resource_name(system, resource):
112112
"id": "r1id",
113113
"name": "r1n"
114114
}
115-
],
115+
]
116+
]
117+
},
118+
{
119+
"system_id": "test_system",
120+
"system_name": "test_system_name",
121+
"type": "r2",
122+
"type_name": "r2_type",
123+
"instances": [
116124
[
117125
{
118126
"type": "r2",
119127
"type_name": "r2_type",
120128
"id": "r2id",
121129
"name": ""
122130
}
123-
],
131+
]
132+
]
133+
},
134+
{
135+
"system_id": "test_system",
136+
"system_name": "test_system_name",
137+
"type": "r3",
138+
"type_name": "r3_type",
139+
"instances": [
124140
[
125141
{
126142
"type": "r3",
@@ -161,8 +177,8 @@ def get_resource_name(system, resource):
161177
{
162178
"system_id": "test_system",
163179
"system_name": "test_system_name",
164-
"type": "r3",
165-
"type_name": "r3_type",
180+
"type": "r1",
181+
"type_name": "r1_type",
166182
"instances": [
167183
[
168184
{
@@ -172,22 +188,22 @@ def get_resource_name(system, resource):
172188
"name": "r1n"
173189
}
174190
],
175-
[
176-
{
177-
"type": "r3",
178-
"type_name": "r3_type",
179-
"id": "r3id",
180-
"name": ""
181-
}
182-
],
183191
[
184192
{
185193
"type": "r1",
186194
"type_name": "r1_type",
187195
"id": "r1id",
188196
"name": "r1n"
189197
}
190-
],
198+
]
199+
]
200+
},
201+
{
202+
"system_id": "test_system",
203+
"system_name": "test_system_name",
204+
"type": "r3",
205+
"type_name": "r3_type",
206+
"instances": [
191207
[
192208
{
193209
"type": "r3",
@@ -198,9 +214,9 @@ def get_resource_name(system, resource):
198214
],
199215
[
200216
{
201-
"type": "r2",
202-
"type_name": "r2_type",
203-
"id": "r2id",
217+
"type": "r3",
218+
"type_name": "r3_type",
219+
"id": "r3id",
204220
"name": ""
205221
}
206222
],
@@ -214,6 +230,22 @@ def get_resource_name(system, resource):
214230
]
215231
]
216232
},
233+
{
234+
"system_id": "test_system",
235+
"system_name": "test_system_name",
236+
"type": "r2",
237+
"type_name": "r2_type",
238+
"instances": [
239+
[
240+
{
241+
"type": "r2",
242+
"type_name": "r2_type",
243+
"id": "r2id",
244+
"name": ""
245+
}
246+
]
247+
]
248+
},
217249
{
218250
"system_id": "another_system",
219251
"system_name": "another_system_name",

0 commit comments

Comments
 (0)