策略管理接口
更新时间:2024-04-26
创建策略
通过以下代码创建策略
name := "test_sdk_go_policy"
args := &api.CreatePolicyArgs{
Name: name,
Description: "description",
Document: "{\"accessControlList\": [{\"region\":\"bj\",\"service\":\"bcc\"," +
"\"resource\":[\"*\"],\"permission\":[\"*\"],\"effect\":\"Allow\"}]}",
}
result, err := client.CreatePolicy(args)
if err != nil {
fmt.Println("Update policy failed", err)
} else {
fmt.Println("Update policy success", result)
}
提示:
- 详细的参数配置及限制条件,可以参考IAM API 文档创建策略
查询策略
通过以下代码查询策略
name := "test_sdk_go_policy"
policyType := "Custom"
result, err := client.GetPolicy(name, policyType)
if err != nil {
fmt.Println("Update policy failed", err)
} else {
fmt.Println("Update policy success", result)
}
提示:
- 详细的参数配置及限制条件,可以参考IAM API 文档创建策略
删除策略
通过以下代码删除策略
name := "test_sdk_go_policy"
err = client.DeletePolicy(name)
if err != nil {
fmt.Println("List policy failed", err)
} else {
fmt.Println("List policy success", name)
}
提示:
- 详细的参数配置及限制条件,可以参考IAM API 文档删除策略
列举策略
通过以下代码列举策略
name := "test_sdk_go_policy"
policyType := "Custom"
result, err := client.ListPolicy(name, policyType)
if err != nil {
fmt.Println("List policy failed", err)
} else {
fmt.Println("List policy success", result)
}
提示:
- 详细的参数配置及限制条件,可以参考IAM API 文档列举策略
关联用户权限
通过以下代码关联用户权限
userName := "test_sdk_go_user"
policyName := "test_sdk_go_policy"
args := &api.AttachPolicyToUserArgs{
UserName: userName,
PolicyName: policyName,
}
err = client.AttachPolicyToUser(args)
if err != nil {
fmt.Println("Attach policy to user failed", err)
} else {
fmt.Println("Attach policy to user success", args)
}
提示:
- 详细的参数配置及限制条件,可以参考IAM API 文档关联用户权限
解除用户权限
通过以下代码解除用户权限
userName := "test_sdk_go_user"
policyName := "test_sdk_go_policy"
args := &api.DetachPolicyFromUserArgs{
UserName: userName,
PolicyName: policyName,
}
err = client.DetachPolicyFromUser(args)
if err != nil {
fmt.Println("Detach policy to user failed", err)
} else {
fmt.Println("Detach policy to user success", args)
}
提示:
- 详细的参数配置及限制条件,可以参考IAM API 文档解除用户权限
列举用户的权限
通过以下代码列举用户的权限
userName := "test_sdk_go_user"
result, err := client.ListUserAttachedPolicies(userName)
if err != nil {
fmt.Println("List user attached policy failed", err)
} else {
fmt.Println("List user attached policy success", result)
}
提示:
- 详细的参数配置及限制条件,可以参考IAM API 文档列举用户的权限
关联组权限
通过以下代码关联组权限
groupName := "test_sdk_go_group"
policyName := "test_sdk_go_policy"
args := &api.AttachPolicyToGroupArgs{
GroupName: groupName,
PolicyName: policyName,
}
err = client.AttachPolicyToGroup(args)
if err != nil {
fmt.Println("Attach policy to group failed", err)
} else {
fmt.Println("Attach policy to group success", args)
}
提示:
- 详细的参数配置及限制条件,可以参考IAM API 文档关联组权限
解除组权限
通过以下代码解除组权限
groupName := "test_sdk_go_group"
policyName := "test_sdk_go_policy"
args := &api.DetachPolicyFromGroupArgs{
GroupName: groupName,
PolicyName: policyName,
}
err = client.DetachPolicyFromGroup(args)
if err != nil {
fmt.Println("Detach policy to group failed", err)
} else {
fmt.Println("Detach policy to group success", args)
}
提示:
- 详细的参数配置及限制条件,可以参考IAM API 文档解除组权限
列举组权限
通过以下代码列举组权限
groupName := "test_sdk_go_group"
result, err := client.ListGroupAttachedPolicies(groupName)
if err != nil {
fmt.Println("List group attached policy failed", err)
} else {
fmt.Println("List group attached policy success", result)
}
提示:
- 详细的参数配置及限制条件,可以参考IAM API 文档列举组权限
关联角色权限
通过以下代码关联角色权限
roleName := "test_sdk_go_group"
policyName := "test_sdk_go_policy"
args := &api.AttachPolicyToRoleArgs{
RoleName: roleName,
PolicyName: policyName,
}
err = client.AttachPolicyToRole(args)
if err != nil {
fmt.Println("Attach policy to role failed", err)
} else {
fmt.Println("Attach policy to role success", args)
}
提示:
- 详细的参数配置及限制条件,可以参考IAM API 文档关联角色权限
解除角色权限
通过以下代码关联角色权限
roleName := "test_sdk_go_group"
policyName := "test_sdk_go_policy"
args := &api.DetachPolicyToRoleArgs{
RoleName: roleName,
PolicyName: policyName,
}
err = client.DetachPolicyFromRole(args)
if err != nil {
fmt.Println("Detach policy to role failed", err)
} else {
fmt.Println("Detach policy to role success", args)
}
提示:
- 详细的参数配置及限制条件,可以参考IAM API 文档解除角色权限
列举角色的权限
通过以下代码列举角色权限
roleName := "test_sdk_go_group"
result, err := client.ListRoleAttachedPolicies(roleName)
if err != nil {
fmt.Println("List role attached policy failed", err)
} else {
fmt.Println("List role attached policy success", result)
}
提示:
- 详细的参数配置及限制条件,可以参考IAM API 文档列举角色的权限
列举权限绑定的实体
通过以下代码列举权限绑定实体
policyId := "test_policy_id"
entities, err := client.ListPolicyAttachedEntities(policyId)
if err != nil {
fmt.Println("List policy attached entities failed", err)
} else {
fmt.Println("List policy attached entities success", entities)
}
提示:
- 详细的参数配置及限制条件,可以参考IAM API 文档列举权限授予的所有主体