Skip to content

Commit 030042b

Browse files
author
saydamir
authored
Add methods for Groups (#6)
Added methods for managing Groups, their roles and users in them. The Users and Service Users structs are extended with Groups. The **changes are backwards incompatible** because now different structs are used for different kinds of requests.
1 parent 1f3033a commit 030042b

File tree

29 files changed

+1444
-83
lines changed

29 files changed

+1444
-83
lines changed

.golangci.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,13 @@ linters-settings:
7676

7777
nolintlint:
7878
allow-leading-space: false
79+
80+
revive:
81+
rules:
82+
- name: var-naming
83+
arguments:
84+
# they outplayed themselves, and "IDS" actually means "allow 'Ids' in var name"
85+
- [ "IDS" ] # AllowList"
7986

8087
tagliatelle:
8188
case:

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ You can use this library to work with the following objects of the Selectel IAM
2121

2222
* [users](https://pkg.go.dev/github.com/selectel/iam-go/service/users)
2323
* [serviceusers](https://pkg.go.dev/github.com/selectel/iam-go/service/serviceusers)
24+
* [groups](https://pkg.go.dev/github.com/selectel/iam-go/service/groups)
2425
* [s3credentials](https://pkg.go.dev/github.com/selectel/iam-go/service/s3credentials)
2526

2627
### Installation

examples/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
This directory contains examples that cover various use cases and functionality for iam-go.
44

55
### Concepts
6+
- [**Create Group with role and User**](./group-with-user): Create a new Group with role, create and add a User and delete them.
67
- [**Create & Delete S3 Credentials**](./s3credentials-create-delete): Create a new S3 Credentials for an existing Service User (ID is needed) and delete it.
78
- [**Create, Update & Delete Service User**](./serviceuser-create-update-delete): Create a new Service User, then update it's data and delete it.
89
- [**Transfer role from one User to another**](./transfer-role): Find a billing User from all and transfer it's role to another User (ID is needed).

examples/group-with-user/README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Create Group with Role & add User
2+
3+
This example program demonstrates how to manage creating and deleting Group with Roles and Users.
4+
5+
The part of deleting is disabled by `deleteAfterRun` variable.
6+
7+
As an example, the Member Role will be assigned for a new Group.
8+
9+
## Running this example
10+
11+
Running this file will execute the following operations:
12+
13+
1. **Create Group:** Create is used to create a new Group.
14+
2. **Create User:** Create is used to create a new User.
15+
3. **Assign Role:** Assign a role to the created Group.
16+
4. **Update Group:** Updates the Group Name and Description.
17+
5. **(Delete Group):** _(disabled by default)_ Delete a just-created Group on a previous step.
18+
6. **(Delete User):** _(disabled by default)_ Delete a just-created User on a previous step.
19+
20+
You should see an output like the following:
21+
```
22+
Step 1: Created Group Name: test_group_name ID: 1a2b3c...
23+
Step 2: Created User ID: 12345_3... Keystone ID: 1a2b3c...
24+
Step 3: Assigned Role member with scope account to Group ID: 1a2b3c...
25+
Step 4: Group Name and Description updated to: new_test_group_name and new_group_description
26+
Step 5: Deleting Group with ID: 1a2b3c...
27+
Step 6: Deleting User with ID: 12345_3...
28+
```

examples/group-with-user/main.go

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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+
}

examples/s3credentials-create-delete/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22

33
This example program demonstrates how to manage creating and deleting S3 Credentials for a Service User.
44

5-
The part of deleting a just-created credentials is commented.
5+
The part of deleting a just-created credentials is disabled by `deleteAfterRun` variable.
66

77
## Running this example
88

99
Running this file will execute the following operations:
1010

1111
1. **Create:** Create is used to create a new S3 Credentials. It is implied, that the Service User ID is known.
12-
2. **(Delete):** _(commented by default)_ Delete deletes a just-created credentials on a previous step.
12+
2. **(Delete):** _(disabled by default)_ Delete deletes a just-created credentials on a previous step.
1313

1414
You should see an output like the following (with both operations enabled):
1515

examples/s3credentials-create-delete/main.go

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,25 @@ import (
77
iam "github.com/selectel/iam-go"
88
)
99

10-
func main() {
10+
var (
1111
// KeystoneToken
12-
token := "gAAAAA..."
12+
token = "gAAAAA..."
13+
deleteAfterRun = false
1314

1415
// Prefix to be added to User-Agent.
15-
prefix := "iam-go"
16+
prefix = "iam-go"
1617

1718
// ID of the User to create S3 Credentials for.
18-
userID := "a1b2c3..."
19+
userID = "a1b2c3..."
1920

2021
// Name of the S3 Credentials to create.
21-
name := "my-s3-credentials"
22+
name = "my-s3-credentials"
2223

2324
// Project ID to create the S3 Credentials for.
24-
projectID := "a1b2c3..."
25+
projectID = "a1b2c3..."
26+
)
2527

28+
func main() {
2629
// Create a new IAM client.
2730
iamClient, err := iam.New(
2831
iam.WithAuthOpts(&iam.AuthOpts{KeystoneToken: token}),
@@ -56,16 +59,15 @@ func main() {
5659
fmt.Printf("Step 1: Created credentials Secret Key: %s Access Key: %s\n", credentials.SecretKey,
5760
credentials.AccessKey)
5861

59-
// // Delete an existing S3 Credentials.
60-
// err = s3CredAPI.Delete(ctx, &s3credentials.DeleteInput{
61-
// UserID: userID,
62-
// AccessKey: credentials.AccessKey,
63-
// })
62+
if deleteAfterRun {
63+
// Delete an existing S3 Credentials.
64+
err = s3CredAPI.Delete(ctx, userID, credentials.AccessKey)
6465

65-
// // Handle the error.
66-
// if err != nil {
67-
// fmt.Println(err)
68-
// }
66+
// Handle the error.
67+
if err != nil {
68+
fmt.Println(err)
69+
}
6970

70-
// fmt.Printf("Step 2: Deleted credentials")
71+
fmt.Printf("Step 2: Deleted credentials")
72+
}
7173
}

examples/serviceuser-create-update-delete/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
This example program demonstrates how to manage creating, updating and deleting Service User.
44

5-
The part of deleting a just-created Service User is commented.
5+
The part of deleting a just-created Service User is disabled by `deleteAfterRun` variable.
66

77
As an example, the Billing Role will be assigned for a new Service User and in update method this Service User will be set to _Disabled_.
88

@@ -11,8 +11,8 @@ As an example, the Billing Role will be assigned for a new Service User and in u
1111
Running this file will execute the following operations:
1212

1313
1. **Create:** Create is used to create a new Service User.
14-
2. **Update** Update sets _Enabled_ property of the just-created Service User to _false_
15-
3. **(Delete):** _(commented by default)_ Delete deletes a just-created Service User.
14+
2. **Update:** Update sets _Enabled_ property of the just-created Service User to _false_
15+
3. **(Delete):** _(disabled by default)_ Delete deletes a just-created Service User.
1616

1717
You should see an output like the following (with all operations enabled):
1818

examples/serviceuser-create-update-delete/main.go

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,25 @@ import (
99
"github.com/selectel/iam-go/service/serviceusers"
1010
)
1111

12-
func main() {
12+
var (
1313
// KeystoneToken
14-
token := "gAAAAA..."
14+
token = "gAAAAA..."
15+
deleteAfterRun = false
1516

1617
// Prefix to be added to User-Agent.
17-
prefix := "iam-go"
18-
18+
prefix = "iam-go"
1919
// Name of the Service User to create.
20-
name := "service-user"
21-
20+
name = "service-user"
2221
// Password of the Service User to create.
23-
password := "Qazwsxedc123"
22+
password = "Qazwsxedc123"
23+
)
2424

25+
func main() {
2526
// Create a new IAM client.
2627
iamClient, err := iam.New(
2728
iam.WithAuthOpts(&iam.AuthOpts{KeystoneToken: token}),
2829
iam.WithUserAgentPrefix(prefix),
2930
)
30-
// Handle the error.
3131
if err != nil {
3232
fmt.Println(err)
3333
return
@@ -46,7 +46,6 @@ func main() {
4646
Password: password,
4747
Roles: []roles.Role{{Scope: roles.Account, RoleName: roles.Billing}},
4848
})
49-
// Handle the error.
5049
if err != nil {
5150
fmt.Println(err)
5251
return
@@ -58,21 +57,23 @@ func main() {
5857
_, err = serviceUsersAPI.Update(ctx, serviceUser.ID, serviceusers.UpdateRequest{
5958
Enabled: false,
6059
})
61-
// Handle the error.
6260
if err != nil {
6361
fmt.Println(err)
6462
return
6563
}
6664

6765
fmt.Printf("Step 2: Disabled Service User ID %s\n", serviceUser.ID)
6866

69-
// // Delete an existing Service User.
70-
// err = serviceUsersAPI.Delete(ctx, serviceUser.ID)
67+
// Disabled by default
68+
if deleteAfterRun {
69+
// Delete an existing Service User.
70+
err = serviceUsersAPI.Delete(ctx, serviceUser.ID)
7171

72-
// // Handle the error.
73-
// if err != nil {
74-
// fmt.Println(err)
75-
// }
72+
// Handle the error.
73+
if err != nil {
74+
fmt.Println(err)
75+
}
7676

77-
// fmt.Printf("Step 3: Deleted Service User ID %s\n", serviceUser.ID)
77+
fmt.Printf("Step 3: Deleted Service User ID %s\n", serviceUser.ID)
78+
}
7879
}

examples/transfer-role/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Running this file will execute the following operations:
1010

1111
1. **List:** List is used to retrieve all Users. The first one, who has a billing role, will be selected as 'transferer'.
1212
2. **UnassignRole:** UnassinRole will remove Billing role from chosen user.
13-
3. **AssignRole** AssignRole will add Billing role to the predefined User ID.
13+
3. **AssignRole:** AssignRole will add Billing role to the predefined User ID.
1414

1515
You should see an output like the following:
1616

0 commit comments

Comments
 (0)