|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "log" |
| 7 | + |
| 8 | + "github.com/selectel/iam-go" |
| 9 | + "github.com/selectel/iam-go/service/groups" |
| 10 | + "github.com/selectel/iam-go/service/roles" |
| 11 | + "github.com/selectel/iam-go/service/users" |
| 12 | +) |
| 13 | + |
| 14 | +var ( |
| 15 | + // KeystoneToken |
| 16 | + token = "gAAAAA..." |
| 17 | + deleteAfterRun = false |
| 18 | + |
| 19 | + // Prefix to be added to User-Agent. |
| 20 | + prefix = "iam-go" |
| 21 | + |
| 22 | + groupName = "test_group_name" |
| 23 | + description = "group_description" |
| 24 | + updatedGroupName = "new_test_group_name" |
| 25 | + updatedDescription = "new_group_description" |
| 26 | + email = "testmail@example.com" |
| 27 | +) |
| 28 | + |
| 29 | +func main() { |
| 30 | + // Create a new IAM client. |
| 31 | + iamClient, err := iam.New( |
| 32 | + iam.WithAuthOpts(&iam.AuthOpts{KeystoneToken: token}), |
| 33 | + iam.WithUserAgentPrefix(prefix), |
| 34 | + ) |
| 35 | + if err != nil { |
| 36 | + fmt.Println(err) |
| 37 | + return |
| 38 | + } |
| 39 | + |
| 40 | + usersAPI := iamClient.Users |
| 41 | + groupsAPI := iamClient.Groups |
| 42 | + ctx := context.Background() |
| 43 | + |
| 44 | + group, err := groupsAPI.Create(ctx, groups.CreateRequest{Name: groupName, Description: description}) |
| 45 | + if err != nil { |
| 46 | + fmt.Println(err) |
| 47 | + return |
| 48 | + } |
| 49 | + fmt.Printf("Step 1: Created Group Name: %s ID: %s\n", group.Name, group.ID) |
| 50 | + |
| 51 | + user, err := usersAPI.Create(ctx, users.CreateRequest{ |
| 52 | + AuthType: users.Local, |
| 53 | + Email: email, |
| 54 | + Federation: nil, |
| 55 | + Roles: []roles.Role{{Scope: roles.Account, RoleName: roles.Reader}}, |
| 56 | + GroupIDs: []string{group.ID}, |
| 57 | + }) |
| 58 | + if err != nil { |
| 59 | + fmt.Println(err) |
| 60 | + return |
| 61 | + } |
| 62 | + fmt.Printf("Step 2: Created User ID: %s Keystone ID: %s\n", user.ID, user.KeystoneID) |
| 63 | + |
| 64 | + err = groupsAPI.AssignRoles(ctx, group.ID, []roles.Role{{Scope: roles.Account, RoleName: roles.Member}}) |
| 65 | + if err != nil { |
| 66 | + log.Fatal(err) |
| 67 | + } |
| 68 | + fmt.Printf("Step 3: Assigned Role %s with scope %s to Group ID: %s\n", roles.Member, roles.Account, group.ID) |
| 69 | + |
| 70 | + group, err = groupsAPI.Update(ctx, group.ID, groups.ModifyRequest{Name: updatedGroupName, |
| 71 | + Description: &updatedDescription}) |
| 72 | + if err != nil { |
| 73 | + fmt.Println(err) |
| 74 | + return |
| 75 | + } |
| 76 | + fmt.Printf("Step 4: Group Name and Description updated to: %s and %s\n", group.Name, group.Description) |
| 77 | + |
| 78 | + if deleteAfterRun { |
| 79 | + fmt.Printf("Step 5: Deleting Group with ID: %s\n", group.ID) |
| 80 | + if err = groupsAPI.Delete(ctx, group.ID); err != nil { |
| 81 | + fmt.Println(err) |
| 82 | + } |
| 83 | + |
| 84 | + fmt.Printf("Step 6: Deleting User with ID: %s\n", user.ID) |
| 85 | + if err = usersAPI.Delete(ctx, user.ID); err != nil { |
| 86 | + fmt.Println(err) |
| 87 | + } |
| 88 | + } |
| 89 | +} |
0 commit comments