Policy management API
Create Strategy
Create a policy with the following code
1name := "test_sdk_go_policy"
2args := &api.CreatePolicyArgs{
3 Name: name,
4 Description: "description",
5 Document: "{\"accessControlList\": [{\"region\":\"bj\",\"service\":\"bcc\"," +
6"\"resource\":[\"*\"],\"permission\":[\"*\"],\"effect\":\"Allow\"}]}",
7}
8result, err := client.CreatePolicy(args)
9if err != nil {
10 fmt.Println("Update policy failed", err)
11} else {
12 fmt.Println("Update policy success", result)
13}
Prompt:
- For detailed parameter configuration and constraints, refer to the IAM API documentationCreate Policy
Query policies
Query a policy with the following code
1 name := "test_sdk_go_policy"
2 policyType := "Custom"
3 result, err := client.GetPolicy(name, policyType)
4 if err != nil {
5 fmt.Println("Update policy failed", err)
6 } else {
7 fmt.Println("Update policy success", result)
8 }
Prompt:
- For detailed parameter configuration and constraints, refer to the IAM API documentationCreate Policy
Delete strategy
Delete a policy with the following code
1 name := "test_sdk_go_policy"
2 err = client.DeletePolicy(name)
3 if err != nil {
4 fmt.Println("List policy failed", err)
5 } else {
6 fmt.Println("List policy success", name)
7 }
Prompt:
- For detailed parameter configuration and constraints, refer to the IAM API documentationDelete Policy
List policies
List policies with the following code
1 name := "test_sdk_go_policy"
2 policyType := "Custom"
3 result, err := client.ListPolicy(name, policyType)
4 if err != nil {
5 fmt.Println("List policy failed", err)
6 } else {
7 fmt.Println("List policy success", result)
8 }
Prompt:
- For detailed parameter configuration and constraints, refer to the IAM API documentationList Policies
Associate user permissions
Associate user permissions with the following code
1 userName := "test_sdk_go_user"
2 policyName := "test_sdk_go_policy"
3 args := &api.AttachPolicyToUserArgs{
4 UserName: userName,
5 PolicyName: policyName,
6 }
7 err = client.AttachPolicyToUser(args)
8 if err != nil {
9 fmt.Println("Attach policy to user failed", err)
10 } else {
11 fmt.Println("Attach policy to user success", args)
12 }
Prompt:
- For detailed parameter configuration and constraints, refer to the IAM API documentationAssociate User Permissions
Revoke user permissions
Disassociate user permissions with the following code
1 userName := "test_sdk_go_user"
2 policyName := "test_sdk_go_policy"
3 args := &api.DetachPolicyFromUserArgs{
4 UserName: userName,
5 PolicyName: policyName,
6 }
7 err = client.DetachPolicyFromUser(args)
8 if err != nil {
9 fmt.Println("Detach policy to user failed", err)
10 } else {
11 fmt.Println("Detach policy to user success", args)
12 }
Prompt:
- For detailed parameter configuration and constraints, refer to the IAM API documentationDisassociate User Permissions
List user permissions
List user permissions with the following code
1 userName := "test_sdk_go_user"
2 result, err := client.ListUserAttachedPolicies(userName)
3 if err != nil {
4 fmt.Println("List user attached policy failed", err)
5 } else {
6 fmt.Println("List user attached policy success", result)
7 }
Prompt:
- For detailed parameter configuration and constraints, refer to the IAM API documentationList User Permissions
Associate group permissions
Associate group permissions with the following code
1 groupName := "test_sdk_go_group"
2 policyName := "test_sdk_go_policy"
3 args := &api.AttachPolicyToGroupArgs{
4 GroupName: groupName,
5 PolicyName: policyName,
6 }
7 err = client.AttachPolicyToGroup(args)
8 if err != nil {
9 fmt.Println("Attach policy to group failed", err)
10 } else {
11 fmt.Println("Attach policy to group success", args)
12 }
Prompt:
- For detailed parameter configuration and constraints, refer to the IAM API documentationAssociate Group Permissions
Revoke group permissions
Disassociate group permissions with the following code
1 groupName := "test_sdk_go_group"
2 policyName := "test_sdk_go_policy"
3 args := &api.DetachPolicyFromGroupArgs{
4 GroupName: groupName,
5 PolicyName: policyName,
6 }
7 err = client.DetachPolicyFromGroup(args)
8 if err != nil {
9 fmt.Println("Detach policy to group failed", err)
10 } else {
11 fmt.Println("Detach policy to group success", args)
12 }
Prompt:
- For detailed parameter configuration and constraints, refer to the IAM API documentationDisassociate Group Permissions
List group permissions
List group permissions with the following code
1 groupName := "test_sdk_go_group"
2 result, err := client.ListGroupAttachedPolicies(groupName)
3 if err != nil {
4 fmt.Println("List group attached policy failed", err)
5 } else {
6 fmt.Println("List group attached policy success", result)
7 }
Prompt:
- For detailed parameter configuration and constraints, refer to the IAM API documentationList Group Permissions
Associate role permissions
Associate role permissions with the following code
1 roleName := "test_sdk_go_group"
2 policyName := "test_sdk_go_policy"
3 args := &api.AttachPolicyToRoleArgs{
4 RoleName: roleName,
5 PolicyName: policyName,
6 }
7 err = client.AttachPolicyToRole(args)
8 if err != nil {
9 fmt.Println("Attach policy to role failed", err)
10 } else {
11 fmt.Println("Attach policy to role success", args)
12 }
Prompt:
- For detailed parameter configuration and constraints, refer to the IAM API documentationAssociate Role Permissions
Revoke role permissions
Associate role permissions with the following code
1 roleName := "test_sdk_go_group"
2 policyName := "test_sdk_go_policy"
3 args := &api.DetachPolicyToRoleArgs{
4 RoleName: roleName,
5 PolicyName: policyName,
6 }
7 err = client.DetachPolicyFromRole(args)
8 if err != nil {
9 fmt.Println("Detach policy to role failed", err)
10 } else {
11 fmt.Println("Detach policy to role success", args)
12 }
Prompt:
- For detailed parameter configuration and constraints, refer to the IAM API documentationDisassociate Role Permissions
List role permissions
List role permissions with the following code
1 roleName := "test_sdk_go_group"
2 result, err := client.ListRoleAttachedPolicies(roleName)
3 if err != nil {
4 fmt.Println("List role attached policy failed", err)
5 } else {
6 fmt.Println("List role attached policy success", result)
7 }
Prompt:
- For detailed parameter configuration and constraints, refer to the IAM API documentationList Role Permissions
List entities bound to permissions
List entities bound to permissions with the following code
1 policyId := "test_policy_id"
2 entities, err := client.ListPolicyAttachedEntities(policyId)
3 if err != nil {
4 fmt.Println("List policy attached entities failed", err)
5 } else {
6 fmt.Println("List policy attached entities success", entities)
7 }
Prompt:
- For detailed parameter configurations and constraints, refer to the IAM API documentation List All Permission-bound Entities
