Skip to content

Commit

Permalink
refactor: handle in idiomatic way errors in changePermission grpc ser…
Browse files Browse the repository at this point in the history
…vice. Fixes #368

Signed-off-by: Michele Meloni <[email protected]>
  • Loading branch information
mmeloni committed Aug 2, 2020
1 parent f4241bd commit 8a01e46
Show file tree
Hide file tree
Showing 14 changed files with 1,695 additions and 876 deletions.
16 changes: 3 additions & 13 deletions cmd/immuadmin/command/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,8 @@ func (cl *commandline) user(cmd *cobra.Command) {
PersistentPreRunE: cl.connect,
PersistentPostRun: cl.disconnect,
RunE: func(cmd *cobra.Command, args []string) (err error) {
var resp = ""
if resp, err = cl.setUserPermission(args); err == nil {
fmt.Fprintf(cmd.OutOrStdout(), resp)
if _, err = cl.setUserPermission(args); err == nil {
fmt.Fprintf(cmd.OutOrStdout(), "Permission changed successfully")
}
return err
},
Expand Down Expand Up @@ -257,15 +256,6 @@ func (cl *commandline) setUserPermission(args []string) (resp string, err error)
return "Permission value not recognized. Allowed permissions are read,readwrite,admin", nil
}
dbname := args[3]
req := &schema.ChangePermissionRequest{
Action: permissionAction,
Database: dbname,
Permission: userpermission,
Username: username,
}
if errResp, err := cl.immuClient.ChangePermission(context.Background(), req); err == nil {
return errResp.Errormessage, nil
}

return "", err
return "", cl.immuClient.ChangePermission(context.Background(), permissionAction, username, dbname, userpermission)
}
11 changes: 3 additions & 8 deletions cmd/immuclient/immuc/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,15 +218,10 @@ func (i *immuc) SetUserPermission(args []string) (string, error) {
}

dbname := args[3]
req := &schema.ChangePermissionRequest{
Action: permissionAction,
Database: dbname,
Permission: userpermission,
Username: username,
}
resp, err := i.ImmuClient.ChangePermission(context.Background(), req)

err := i.ImmuClient.ChangePermission(context.Background(), permissionAction, username, dbname, userpermission)
if err != nil {
return "", err
}
return resp.Errormessage, nil
return "Permission changed successfully", nil
}
4 changes: 2 additions & 2 deletions cmd/immuclient/immuc/login_errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,8 +292,8 @@ func TestLoginAndUserCommandsErrors(t *testing.T) {

args[2] = "readwrite"
errChangePermission := errors.New("change permission error")
immuClientMock.ChangePermissionF = func(context.Context, *schema.ChangePermissionRequest) (*schema.Error, error) {
return nil, errChangePermission
immuClientMock.ChangePermissionF = func(context.Context, schema.PermissionAction, string, string, uint32) error {
return errChangePermission
}
resp, err = ic.SetUserPermission(args)
require.Equal(t, errChangePermission, err)
Expand Down
25 changes: 12 additions & 13 deletions pkg/api/schema/docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -938,7 +938,9 @@
<a name="immudb.schema.ZAddOptions"></a>

### ZAddOptions

Why use double as score type?
Because it is not purely about the storage size, but also use cases.
64-bit floating point double gives a lot of flexibility and dynamic range, at the expense of having only 53-bits of integer.


| Field | Type | Label | Description |
Expand Down Expand Up @@ -1011,6 +1013,14 @@ IMPORTANT: All get and safeget functions return base64-encoded keys and values,

| Method Name | Request Type | Response Type | Description |
| ----------- | ------------ | ------------- | ------------|
| ListUsers | [.google.protobuf.Empty](#google.protobuf.Empty) | [UserList](#immudb.schema.UserList) | |
| CreateUser | [CreateUserRequest](#immudb.schema.CreateUserRequest) | [UserResponse](#immudb.schema.UserResponse) | |
| ChangePassword | [ChangePasswordRequest](#immudb.schema.ChangePasswordRequest) | [.google.protobuf.Empty](#google.protobuf.Empty) | |
| UpdateAuthConfig | [AuthConfig](#immudb.schema.AuthConfig) | [.google.protobuf.Empty](#google.protobuf.Empty) | |
| UpdateMTLSConfig | [MTLSConfig](#immudb.schema.MTLSConfig) | [.google.protobuf.Empty](#google.protobuf.Empty) | |
| PrintTree | [.google.protobuf.Empty](#google.protobuf.Empty) | [Tree](#immudb.schema.Tree) | |
| Login | [LoginRequest](#immudb.schema.LoginRequest) | [LoginResponse](#immudb.schema.LoginResponse) | |
| Logout | [.google.protobuf.Empty](#google.protobuf.Empty) | [.google.protobuf.Empty](#google.protobuf.Empty) | |
| Set | [KeyValue](#immudb.schema.KeyValue) | [Index](#immudb.schema.Index) | |
| SetSV | [StructuredKeyValue](#immudb.schema.StructuredKeyValue) | [Index](#immudb.schema.Index) | |
| SafeSet | [SafeSetOptions](#immudb.schema.SafeSetOptions) | [Proof](#immudb.schema.Proof) | |
Expand Down Expand Up @@ -1043,21 +1053,10 @@ IMPORTANT: All get and safeget functions return base64-encoded keys and values,
| SafeZAdd | [SafeZAddOptions](#immudb.schema.SafeZAddOptions) | [Proof](#immudb.schema.Proof) | |
| IScan | [IScanOptions](#immudb.schema.IScanOptions) | [Page](#immudb.schema.Page) | |
| IScanSV | [IScanOptions](#immudb.schema.IScanOptions) | [SPage](#immudb.schema.SPage) | |
| ListUsers | [.google.protobuf.Empty](#google.protobuf.Empty) | [UserList](#immudb.schema.UserList) | |
| GetUser | [UserRequest](#immudb.schema.UserRequest) | [UserResponse](#immudb.schema.UserResponse) | |
| CreateUser | [CreateUserRequest](#immudb.schema.CreateUserRequest) | [UserResponse](#immudb.schema.UserResponse) | |
| ChangePassword | [ChangePasswordRequest](#immudb.schema.ChangePasswordRequest) | [.google.protobuf.Empty](#google.protobuf.Empty) | |
| SetPermission | [Item](#immudb.schema.Item) | [.google.protobuf.Empty](#google.protobuf.Empty) | |
| DeactivateUser | [UserRequest](#immudb.schema.UserRequest) | [.google.protobuf.Empty](#google.protobuf.Empty) | |
| UpdateAuthConfig | [AuthConfig](#immudb.schema.AuthConfig) | [.google.protobuf.Empty](#google.protobuf.Empty) | |
| UpdateMTLSConfig | [MTLSConfig](#immudb.schema.MTLSConfig) | [.google.protobuf.Empty](#google.protobuf.Empty) | |
| PrintTree | [.google.protobuf.Empty](#google.protobuf.Empty) | [Tree](#immudb.schema.Tree) | |
| Login | [LoginRequest](#immudb.schema.LoginRequest) | [LoginResponse](#immudb.schema.LoginResponse) | |
| Logout | [.google.protobuf.Empty](#google.protobuf.Empty) | [.google.protobuf.Empty](#google.protobuf.Empty) | |
| Dump | [.google.protobuf.Empty](#google.protobuf.Empty) | [.pb.KVList](#pb.KVList) stream | |
| CreateDatabase | [Database](#immudb.schema.Database) | [CreateDatabaseReply](#immudb.schema.CreateDatabaseReply) | todo(joe-dz): Enable restore when the feature is required again rpc Restore(stream pb.KVList) returns (ItemsCount) { option (google.api.http) = { post: &#34;/v1/immurestproxy/restore&#34; body: &#34;*&#34; }; } |
| UseDatabase | [Database](#immudb.schema.Database) | [UseDatabaseReply](#immudb.schema.UseDatabaseReply) | |
| ChangePermission | [ChangePermissionRequest](#immudb.schema.ChangePermissionRequest) | [Error](#immudb.schema.Error) | |
| ChangePermission | [ChangePermissionRequest](#immudb.schema.ChangePermissionRequest) | [.google.protobuf.Empty](#google.protobuf.Empty) | |
| SetActiveUser | [SetActiveUserRequest](#immudb.schema.SetActiveUserRequest) | [.google.protobuf.Empty](#google.protobuf.Empty) | |
| DatabaseList | [.google.protobuf.Empty](#google.protobuf.Empty) | [DatabaseListResponse](#immudb.schema.DatabaseListResponse) | |

Expand Down
Loading

0 comments on commit 8a01e46

Please sign in to comment.