策略管理接口
更新时间:2021-11-24
创建策略
创建权限策略,请参考如下代码:
def create_policy():
iam_client = IamClient(iam_sample_conf.config)
# 创建策略的请求为dict
# 设置策略名name
# 设置策略描述description
# 设置策略内容document,ACL格式序列化后得到的String
create_policy_request = {"name": "test_policy", "description": "create policy: test_policy_1",
"document": '{ "accessControlList": [ { "region": "bj", "resource": [ "*" ], "effect":'
'"Allow", "service": "bce:bos", "permission": [ "READ" ] } ] } '}
response = iam_client.create_policy(create_policy_request)
print(response)
查询策略
查询权限策略,请参考如下代码:
def get_policy():
iam_client = IamClient(iam_sample_conf.config)
# 策略名
policy_name = b"test_policy"
# 要查询的策略类型,为System时查询系统策略;为Custom时查询自定义策略
policy_type = b"Custom"
response = iam_client.get_policy(policy_name=policy_name, policy_type=policy_type)
print(response)
删除策略
删除权限策略,请参考如下代码:
def delete_policy():
iam_client = IamClient(iam_sample_conf.config)
# 策略名
policy_name = b"test_policy"
response = iam_client.delete_policy(policy_name=policy_name)
print(response)(policyName);
}
列举策略
列举权限策略,当policyType为System时,可列举系统内置的策略列表,请参考如下代码:
def list_policy():
iam_client = IamClient(iam_sample_conf.config)
# 要查询的策略类型,为System时查询系统策略;为Custom时查询自定义策略
policy_type = b"Custom"
# 关键字过滤
name_filter = b"test"
response = iam_client.list_policy(policy_type=policy_type, name_filter=name_filter)
print(response)
关联用户权限
为用户关联权限策略,请参考如下代码:
def attach_policy_to_user():
iam_client = IamClient(iam_sample_conf.config)
# 需要关联策略的用户
user_name = b"test_user"
# 关联的策略
policy_name = b"test_policy"
# 要查询的策略类型,为System时查询系统策略;为Custom时查询自定义策略
policy_type = b"Custom"
response = iam_client.attach_policy_to_user(user_name, policy_name, policy_type)
print(response)
解除用户权限
解除子用户关联的权限策略,请参考如下代码:
def detach_policy_from_user():
iam_client = IamClient(iam_sample_conf.config)
# 需要关联策略的用户
user_name = b"test_user"
# 关联的策略
policy_name = b"test_policy"
# 要查询的策略类型,为System时查询系统策略;为Custom时查询自定义策略
policy_type = b"Custom"
response = iam_client.detach_policy_from_user(user_name, policy_name, policy_type)
print(response)
列举用户的权限
列举用户关联的权限策略,请参考如下代码:
def list_policies_from_user():
iam_client = IamClient(iam_sample_conf.config)
# 用户名
user_name = b"test_user"
response = iam_client.list_policies_from_user(user_name)
print(response)
关联组权限
为用户组关联权限策略,请参考如下代码:
def attach_policy_to_group():
iam_client = IamClient(iam_sample_conf.config)
# 需要关联策略的用户组
group_name = b"test_group"
# 关联的策略
policy_name = b"test_policy"
# 要查询的策略类型,为System时查询系统策略;为Custom时查询自定义策略
policy_type = b"Custom"
response = iam_client.attach_policy_to_group(group_name, policy_name, policy_type)
print(response)
解除组权限
解除用户组关联的权限策略,请参考如下代码:
def detach_policy_from_group():
iam_client = IamClient(iam_sample_conf.config)
# 需要关联策略的用户组
group_name = b"test_group"
# 关联的策略
policy_name = b"test_policy"
# 要查询的策略类型,为System时查询系统策略;为Custom时查询自定义策略
policy_type = b"Custom"
response = iam_client.detach_policy_from_group(group_name, policy_name, policy_type)
print(response)
列举组关联的权限
列举组关联的权限策略,请参考如下代码:
def list_policies_from_group():
iam_client = IamClient(iam_sample_conf.config)
# 用户组名
group_name = b"test_group"
response = iam_client.list_policies_from_group(group_name)
print(response)
关联角色权限
为角色关联权限策略,请参考如下代码:
def attach_policy_to_role():
iam_client = IamClient(iam_sample_conf.config)
# 需要关联策略的角色
role_name = b"test_role"
# 关联的策略
policy_name = b"test_policy"
# 要查询的策略类型,为System时查询系统策略;为Custom时查询自定义策略
policy_type = b"Custom"
response = iam_client.attach_policy_to_role(role_name, policy_name, policy_type)
print(response)
解除角色权限
解除角色关联的权限策略,请参考如下代码:
def detach_policy_from_role():
iam_client = IamClient(iam_sample_conf.config)
# 需要关联策略的角色
role_name = b"test_role"
# 关联的策略
policy_name = b"test_policy"
# 要查询的策略类型,为System时查询系统策略;为Custom时查询自定义策略
policy_type = b"Custom"
response = iam_client.detach_policy_from_role(role_name, policy_name, policy_type)
print(response)
列举角色的权限
列举角色关联的权限策略,请参考如下代码:
def list_policies_from_role():
iam_client = IamClient(iam_sample_conf.config)
# 角色名
role_name = b"test_role"
response = iam_client.list_policies_from_role(role_name)
print(response)