Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

blue_krill 中 EnumField 是否可以增加一个字段 metadata,用来存储一些关联的额外数据 #100

Open
alex-smile opened this issue Jun 30, 2023 · 2 comments

Comments

@alex-smile
Copy link
Contributor

No description provided.

@alex-smile alex-smile changed the title blue_krill 中是否可以增加一个字段 metadata,用来存储一些关联的额外数据 blue_krill 中 EnumField 是否可以增加一个字段 metadata,用来存储一些关联的额外数据 Jul 1, 2023
@alex-smile
Copy link
Contributor Author

EnumField 定义:https://github.com/TencentBlueKing/bkpaas-python-sdk/blob/master/sdks/blue-krill/blue_krill/data_types/enum.py#L135

给 EnumField 添加一个 metadata 属性,支持定义一些扩展数据,方便程序统一管理配置。例如,定义权限中心的操作 ActionEnum 时,可以添加其允许操作的角色,关联的资源类型,在按角色授权时,可以统一根据这些数据生成权限中心需要的授权范围。

class ActionEnum:
    VIEW_FOO = EnumField("view_foo", label="", metadata={"role": ["operator"], "related_resource_type": "foo"})
     

class AuthorizationScopes:
    def get_scopes(self, role):
         field_members = ActionEnum.get_field_members()
         action_fields = [field for field in field_members.values() if role in field.metadata.get("role", [])]

         resource_type_to_actions = defaultdict(list)
         for field in action_fields:
              resource_type_to_actions[field.metadata["related_resource_type"]].append(field.real_value)

@piglei
Copy link
Collaborator

piglei commented Aug 17, 2023

EnumField 定义:https://github.com/TencentBlueKing/bkpaas-python-sdk/blob/master/sdks/blue-krill/blue_krill/data_types/enum.py#L135

给 EnumField 添加一个 metadata 属性,支持定义一些扩展数据,方便程序统一管理配置。例如,定义权限中心的操作 ActionEnum 时,可以添加其允许操作的角色,关联的资源类型,在按角色授权时,可以统一根据这些数据生成权限中心需要的授权范围。

这个需求,除了给枚举值增加 metadata 外,也有其他方式可以满足:

  1. 创建字典 action_perm_mdata: Dict[ActionEnum, Dict],来引用枚举类型
  2. 直接把携带 metadata 的结构化数据,作为值来定义枚举类型 VIEW_FOO = {"name": "view_foo", "role": ["operator"], "lated_resource_type": "foo"}

考虑到 metadata 本身含义较模糊,及当前需求有其他替代方案。暂时先挂起本 Issue。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants