Skip to content

Commit

Permalink
fix: gen_perms_apply_data拓扑资源按照资源类型聚合 (#90)
Browse files Browse the repository at this point in the history
# Reviewed, transaction id: 6369
  • Loading branch information
iSecloud authored Apr 19, 2024
1 parent fe97bf7 commit 943ab4e
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 69 deletions.
2 changes: 1 addition & 1 deletion iam/__version__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# -*- coding: utf-8 -*-

__version__ = "1.3.5"
__version__ = "1.3.6"
101 changes: 54 additions & 47 deletions iam/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific language governing permissions and limitations under the License.
"""


from collections import OrderedDict
import itertools
from collections import OrderedDict, defaultdict

from . import meta

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

# 2. aggregate resources by resource type, generate related_resource_type
related_resource_types = []
for system_id, resources_list in system_resources_list.items():
# get resource type from last resource in resources
a_resource = resources_list[0][-1]
resource_types = {
"system_id": system_id,
"system_name": meta.get_system_name(system_id),
"type": a_resource.type,
"type_name": meta.get_resource_name(system_id, a_resource.type),
}
instances = []

for resources in resources_list:
if not resources_list:
continue

# aggregate resources by resource type
resource_type__resources = defaultdict(list)
for resource in list(itertools.chain(*resources_list)):
resource_type__resources[resource.type].append(resource)

# get the list of resource instances of the same type
for __, resources in resource_type__resources.items():
a_resource = resources[0]
resource_types = {
"system_id": system_id,
"system_name": meta.get_system_name(system_id),
"type": a_resource.type,
"type_name": meta.get_resource_name(system_id, a_resource.type),
}
instances = []

# arrange instances according to topo level
for resource in resources:
inst_item = []
topo_path = None

if resource.attribute:
topo_path = resource.attribute.get("_bk_iam_path_")

if topo_path:
for part in topo_path[1:-1].split("/"):
# 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(",")
# 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:
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:
raise Exception("Invalid _bk_iam_path_: %s" % topo_path)

inst_item.append(
{
"type": rtype,
"type_name": meta.get_resource_name(rsystem, rtype),
"id": rid,
"name": part,
}
)

topo_path = []
# get the topo level of the resource
if resource.attribute and resource.attribute.get("_bk_iam_path_"):
bk_iam_path = resource.attribute["_bk_iam_path_"]
topo_path = bk_iam_path[1:-1].split("/")
# append paernt topo instance
for part in topo_path:
# 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(",")
# 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:
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:
raise Exception("Invalid _bk_iam_path_: %s" % topo_path)
inst_item.append(
{
"type": rtype,
"type_name": meta.get_resource_name(rsystem, rtype),
"id": rid,
"name": part,
}
)
# lastly, append self
inst_item.append(
{
"type": resource.type,
Expand All @@ -114,11 +122,10 @@ def gen_perms_apply_data(system, subject, action_to_resources_list):
"name": resource.attribute.get("name", "") if resource.attribute else "",
}
)

instances.append(inst_item)

resource_types["instances"] = instances
related_resource_types.append(resource_types)
resource_types["instances"] = instances
related_resource_types.append(resource_types)

action["related_resource_types"] = related_resource_types
actions.append(action)
Expand Down
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
@@ -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.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)
[![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)

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

Expand Down
2 changes: 1 addition & 1 deletion readme_en.md
Original file line number Diff line number Diff line change
@@ -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.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)
[![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)

## Overview

Expand Down
5 changes: 5 additions & 0 deletions release.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
版本日志
===============

# v1.3.5

- fix: gen_perms_apply_data拓扑资源按照资源类型聚合


# v1.3.5

- add: list_instance、search_instance 新增 ancestors 返回
Expand Down
70 changes: 51 additions & 19 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def get_resource_name(system, resource):
],
)

# assert data
# assert data
assert data == {
"system_id": "test_system",
"system_name": "test_system_name",
Expand All @@ -102,8 +102,8 @@ def get_resource_name(system, resource):
{
"system_id": "test_system",
"system_name": "test_system_name",
"type": "r3",
"type_name": "r3_type",
"type": "r1",
"type_name": "r1_type",
"instances": [
[
{
Expand All @@ -112,15 +112,31 @@ def get_resource_name(system, resource):
"id": "r1id",
"name": "r1n"
}
],
]
]
},
{
"system_id": "test_system",
"system_name": "test_system_name",
"type": "r2",
"type_name": "r2_type",
"instances": [
[
{
"type": "r2",
"type_name": "r2_type",
"id": "r2id",
"name": ""
}
],
]
]
},
{
"system_id": "test_system",
"system_name": "test_system_name",
"type": "r3",
"type_name": "r3_type",
"instances": [
[
{
"type": "r3",
Expand Down Expand Up @@ -161,8 +177,8 @@ def get_resource_name(system, resource):
{
"system_id": "test_system",
"system_name": "test_system_name",
"type": "r3",
"type_name": "r3_type",
"type": "r1",
"type_name": "r1_type",
"instances": [
[
{
Expand All @@ -172,22 +188,22 @@ def get_resource_name(system, resource):
"name": "r1n"
}
],
[
{
"type": "r3",
"type_name": "r3_type",
"id": "r3id",
"name": ""
}
],
[
{
"type": "r1",
"type_name": "r1_type",
"id": "r1id",
"name": "r1n"
}
],
]
]
},
{
"system_id": "test_system",
"system_name": "test_system_name",
"type": "r3",
"type_name": "r3_type",
"instances": [
[
{
"type": "r3",
Expand All @@ -198,9 +214,9 @@ def get_resource_name(system, resource):
],
[
{
"type": "r2",
"type_name": "r2_type",
"id": "r2id",
"type": "r3",
"type_name": "r3_type",
"id": "r3id",
"name": ""
}
],
Expand All @@ -214,6 +230,22 @@ def get_resource_name(system, resource):
]
]
},
{
"system_id": "test_system",
"system_name": "test_system_name",
"type": "r2",
"type_name": "r2_type",
"instances": [
[
{
"type": "r2",
"type_name": "r2_type",
"id": "r2id",
"name": ""
}
]
]
},
{
"system_id": "another_system",
"system_name": "another_system_name",
Expand Down

0 comments on commit 943ab4e

Please sign in to comment.