Skip to content

Commit

Permalink
Change roles to one format (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
Icerzack authored Mar 1, 2024
1 parent de8f2ed commit 2392784
Show file tree
Hide file tree
Showing 10 changed files with 139 additions and 155 deletions.
3 changes: 2 additions & 1 deletion examples/serviceuser-create-update-delete/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"

"github.com/selectel/iam-go"
"github.com/selectel/iam-go/service/roles"
"github.com/selectel/iam-go/service/serviceusers"
)

Expand Down Expand Up @@ -43,7 +44,7 @@ func main() {
Enabled: true,
Name: name,
Password: password,
Roles: []serviceusers.Role{{Scope: serviceusers.Account, RoleName: serviceusers.Billing}},
Roles: []roles.Role{{Scope: roles.Account, RoleName: roles.Billing}},
})
// Handle the error.
if err != nil {
Expand Down
7 changes: 4 additions & 3 deletions examples/transfer-role/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"

"github.com/selectel/iam-go"
"github.com/selectel/iam-go/service/roles"
"github.com/selectel/iam-go/service/users"
)

Expand Down Expand Up @@ -45,7 +46,7 @@ func main() {
var chosenUser *users.User
for _, user := range allUsers {
for _, role := range user.Roles {
if role.RoleName == users.Billing && user.ID != "account_root" {
if role.RoleName == roles.Billing && user.ID != "account_root" {
chosenUser = &user
break
}
Expand All @@ -67,7 +68,7 @@ func main() {
err = usersAPI.UnassignRoles(
ctx,
chosenUser.ID,
[]users.Role{{Scope: users.Account, RoleName: users.Billing}},
[]roles.Role{{Scope: roles.Account, RoleName: roles.Billing}},
)

// Handle the error.
Expand All @@ -83,7 +84,7 @@ func main() {
err = usersAPI.AssignRoles(
ctx,
userID,
[]users.Role{{Scope: users.Account, RoleName: users.Billing}},
[]roles.Role{{Scope: roles.Account, RoleName: roles.Billing}},
)

// Handle the error.
Expand Down
4 changes: 2 additions & 2 deletions examples/user-create-delete/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"

"github.com/selectel/iam-go"
"github.com/selectel/iam-go/service/roles"
"github.com/selectel/iam-go/service/users"
)

Expand All @@ -23,7 +24,6 @@ func main() {
iam.WithAuthOpts(&iam.AuthOpts{KeystoneToken: token}),
iam.WithUserAgentPrefix(prefix),
)

// Handle the error.
if err != nil {
fmt.Println(err)
Expand All @@ -41,7 +41,7 @@ func main() {
AuthType: users.Local,
Email: email,
Federation: nil,
Roles: []users.Role{{Scope: users.Account, RoleName: users.Billing}},
Roles: []roles.Role{{Scope: roles.Account, RoleName: roles.Billing}},
})
// Handle the error.
if err != nil {
Expand Down
46 changes: 46 additions & 0 deletions service/roles/schemas.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package roles

// Name represents a role, which can be assigned to a user or a service user.
// For additional information, see
// https://docs.selectel.ru/control-panel-actions/users-and-roles/user-types-and-roles/#user-roles.
type Name string

const (
// Account owner.
AccountOwner Name = "account_owner"

// User administrator.
IAMAdmin Name = "iam_admin"

// Account/Project administrator.
Member Name = "member"

// Account/Project reader.
Reader Name = "reader"

// Billing administrator.
Billing Name = "billing"

// Object storage administrator. Can be assigned only to a service user.
ObjectStorageAdmin Name = "object_storage:admin"

// Object storage user. Can be assigned only to a service user.
ObjectStorageUser Name = "object_storage_user"
)

// Scope represents a scope of a role.
type Scope string

const (
// Project scope.
Project Scope = "project"

// Account scope.
Account Scope = "account"
)

type Role struct {
ProjectID string `json:"project_id,omitempty"`
RoleName Name `json:"role_name"`
Scope Scope `json:"scope"`
}
7 changes: 4 additions & 3 deletions service/serviceusers/requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/selectel/iam-go/iamerrors"
"github.com/selectel/iam-go/internal/client"
"github.com/selectel/iam-go/service/roles"
)

const apiVersion = "iam/v1"
Expand Down Expand Up @@ -187,7 +188,7 @@ func (su *ServiceUsers) Update(ctx context.Context, userID string, input UpdateR
}

// AssignRoles adds new roles for a Service User with the given userID.
func (su *ServiceUsers) AssignRoles(ctx context.Context, userID string, roles []Role) error {
func (su *ServiceUsers) AssignRoles(ctx context.Context, userID string, roles []roles.Role) error {
if userID == "" {
return iamerrors.Error{Err: iamerrors.ErrUserIDRequired, Desc: "No userID was provided."}
}
Expand All @@ -202,7 +203,7 @@ func (su *ServiceUsers) AssignRoles(ctx context.Context, userID string, roles []
}

// UnassignRoles removes roles from a Service User with the given userID.
func (su *ServiceUsers) UnassignRoles(ctx context.Context, userID string, roles []Role) error {
func (su *ServiceUsers) UnassignRoles(ctx context.Context, userID string, roles []roles.Role) error {
if userID == "" {
return iamerrors.Error{Err: iamerrors.ErrUserIDRequired, Desc: "No userID was provided."}
}
Expand All @@ -216,7 +217,7 @@ func (su *ServiceUsers) UnassignRoles(ctx context.Context, userID string, roles
return su.manageRoles(ctx, http.MethodDelete, userID, roles)
}

func (su *ServiceUsers) manageRoles(ctx context.Context, method string, userID string, roles []Role) error {
func (su *ServiceUsers) manageRoles(ctx context.Context, method string, userID string, roles []roles.Role) error {
path, err := url.JoinPath(apiVersion, "service_users", userID, "roles")
if err != nil {
return iamerrors.Error{Err: iamerrors.ErrInternalAppError, Desc: err.Error()}
Expand Down
47 changes: 24 additions & 23 deletions service/serviceusers/requests_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"github.com/selectel/iam-go/iamerrors"
"github.com/selectel/iam-go/internal/client"
"github.com/selectel/iam-go/service/roles"
"github.com/selectel/iam-go/service/serviceusers/testdata"
)

Expand Down Expand Up @@ -41,8 +42,8 @@ func TestList(t *testing.T) {
Name: "test",
Enabled: true,
ID: "123",
Roles: []Role{
{Scope: Account, RoleName: Member},
Roles: []roles.Role{
{Scope: roles.Account, RoleName: roles.Member},
},
},
},
Expand Down Expand Up @@ -115,8 +116,8 @@ func TestGet(t *testing.T) {
Name: "test",
Enabled: true,
ID: "123",
Roles: []Role{
{Scope: Account, RoleName: Member},
Roles: []roles.Role{
{Scope: roles.Account, RoleName: roles.Member},
},
},
expectedError: nil,
Expand Down Expand Up @@ -235,7 +236,7 @@ func TestCreate(t *testing.T) {
enabled bool
name string
password string
roles []Role
roles []roles.Role
}
tests := []struct {
name string
Expand All @@ -250,8 +251,8 @@ func TestCreate(t *testing.T) {
enabled: true,
name: "test",
password: "Qazwsxedc123",
roles: []Role{
{Scope: Account, RoleName: Member},
roles: []roles.Role{
{Scope: roles.Account, RoleName: roles.Member},
},
},
prepare: func() {
Expand All @@ -265,8 +266,8 @@ func TestCreate(t *testing.T) {
Name: "test",
Enabled: true,
ID: "123",
Roles: []Role{
{Scope: Account, RoleName: Member},
Roles: []roles.Role{
{Scope: roles.Account, RoleName: roles.Member},
},
},
expectedError: nil,
Expand All @@ -277,8 +278,8 @@ func TestCreate(t *testing.T) {
enabled: true,
name: "test",
password: "123",
roles: []Role{
{Scope: Account, RoleName: Member},
roles: []roles.Role{
{Scope: roles.Account, RoleName: roles.Member},
},
},
prepare: func() {
Expand All @@ -300,8 +301,8 @@ func TestCreate(t *testing.T) {
enabled: true,
name: "test",
password: "123",
roles: []Role{
{Scope: Account, RoleName: Member},
roles: []roles.Role{
{Scope: roles.Account, RoleName: roles.Member},
},
},
prepare: func() {
Expand Down Expand Up @@ -434,7 +435,7 @@ func TestUpdate(t *testing.T) {
func TestAssignRoles(t *testing.T) {
type args struct {
userID string
roles []Role
roles []roles.Role
}
tests := []struct {
name string
Expand All @@ -446,8 +447,8 @@ func TestAssignRoles(t *testing.T) {
name: "Test AssignRoles return output",
args: args{
userID: "123",
roles: []Role{
{Scope: Account, RoleName: Member},
roles: []roles.Role{
{Scope: roles.Account, RoleName: roles.Member},
},
},
prepare: func() {
Expand All @@ -464,8 +465,8 @@ func TestAssignRoles(t *testing.T) {
name: "Test AssignRoles return error",
args: args{
userID: "123",
roles: []Role{
{Scope: Account, RoleName: Member},
roles: []roles.Role{
{Scope: roles.Account, RoleName: roles.Member},
},
},
prepare: func() {
Expand Down Expand Up @@ -506,7 +507,7 @@ func TestAssignRoles(t *testing.T) {
func TestUnassignRoles(t *testing.T) {
type args struct {
userID string
roles []Role
roles []roles.Role
}
tests := []struct {
name string
Expand All @@ -518,8 +519,8 @@ func TestUnassignRoles(t *testing.T) {
name: "Test UnassignRoles return output",
args: args{
userID: "123",
roles: []Role{
{Scope: Account, RoleName: Member},
roles: []roles.Role{
{Scope: roles.Account, RoleName: roles.Member},
},
},
prepare: func() {
Expand All @@ -536,8 +537,8 @@ func TestUnassignRoles(t *testing.T) {
name: "Test UnassignRoles return error",
args: args{
userID: "123",
roles: []Role{
{Scope: Account, RoleName: Member},
roles: []roles.Role{
{Scope: roles.Account, RoleName: roles.Member},
},
},
prepare: func() {
Expand Down
61 changes: 11 additions & 50 deletions service/serviceusers/schemas.go
Original file line number Diff line number Diff line change
@@ -1,60 +1,21 @@
package serviceusers

type RoleName string

const (
// Account owner.
AccountOwner RoleName = "account_owner"

// User administrator.
IAMAdmin RoleName = "iam_admin"

// Account/Project administrator.
Member RoleName = "member"

// Account/Project reader.
Reader RoleName = "reader"

// Billing administrator.
Billing RoleName = "billing"

// Object storage administrator.
ObjectStorageAdmin RoleName = "object_storage:admin"

// Object storage user.
ObjectStorageUser RoleName = "object_storage_user"
)

type Scope string

const (
// Project scope.
Project Scope = "project"

// Account scope.
Account Scope = "account"
)
import "github.com/selectel/iam-go/service/roles"

// ServiceUser represents a Selectel Service User.
type ServiceUser struct {
ID string `json:"id"`
Enabled bool `json:"enabled"`
Name string `json:"name"`
Roles []Role `json:"roles"`
}

type Role struct {
ProjectID string `json:"project_id,omitempty"`
RoleName RoleName `json:"role_name"`
Scope Scope `json:"scope"`
ID string `json:"id"`
Enabled bool `json:"enabled"`
Name string `json:"name"`
Roles []roles.Role `json:"roles"`
}

// CreateRequest is used to set options for Create method.
type CreateRequest struct {
Enabled bool
Name string
Password string
Roles []Role
Roles []roles.Role
}

// UpdateRequest is used to set options for Update method.
Expand All @@ -65,10 +26,10 @@ type UpdateRequest struct {
}

type createRequest struct {
Enabled bool `json:"enabled,omitempty"`
Name string `json:"name,omitempty"`
Password string `json:"password,omitempty"`
Roles []Role `json:"roles,omitempty"`
Enabled bool `json:"enabled,omitempty"`
Name string `json:"name,omitempty"`
Password string `json:"password,omitempty"`
Roles []roles.Role `json:"roles,omitempty"`
}

type updateRequest struct {
Expand All @@ -78,7 +39,7 @@ type updateRequest struct {
}

type manageRolesRequest struct {
Roles []Role `json:"roles"`
Roles []roles.Role `json:"roles"`
}

type listResponse struct {
Expand Down
Loading

0 comments on commit 2392784

Please sign in to comment.