Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhancement: add database user update #656

Merged
merged 2 commits into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions databases.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ type DatabasesService interface {
GetUser(context.Context, string, string) (*DatabaseUser, *Response, error)
ListUsers(context.Context, string, *ListOptions) ([]DatabaseUser, *Response, error)
CreateUser(context.Context, string, *DatabaseCreateUserRequest) (*DatabaseUser, *Response, error)
UpdateUser(context.Context, string, string, *DatabaseUpdateUserRequest) (*DatabaseUser, *Response, error)
DeleteUser(context.Context, string, string) (*Response, error)
ResetUserAuth(context.Context, string, string, *DatabaseResetUserAuthRequest) (*DatabaseUser, *Response, error)
ListDBs(context.Context, string, *ListOptions) ([]DatabaseDB, *Response, error)
Expand Down Expand Up @@ -412,6 +413,11 @@ type DatabaseCreateUserRequest struct {
Settings *DatabaseUserSettings `json:"settings,omitempty"`
}

// DatabaseUpdateUserRequest is used to update an existing database user
type DatabaseUpdateUserRequest struct {
Settings *DatabaseUserSettings `json:"settings,omitempty"`
}

// DatabaseResetUserAuthRequest is used to reset a users DB auth
type DatabaseResetUserAuthRequest struct {
MySQLSettings *DatabaseMySQLUserSettings `json:"mysql_settings,omitempty"`
Expand Down Expand Up @@ -870,6 +876,21 @@ func (svc *DatabasesServiceOp) CreateUser(ctx context.Context, databaseID string
return root.User, resp, nil
}

// UpdateUser will update an existing database user
func (svc *DatabasesServiceOp) UpdateUser(ctx context.Context, databaseID, userID string, updateUser *DatabaseUpdateUserRequest) (*DatabaseUser, *Response, error) {
path := fmt.Sprintf(databaseUserPath, databaseID, userID)
req, err := svc.client.NewRequest(ctx, http.MethodPut, path, updateUser)
if err != nil {
return nil, nil, err
}
root := new(databaseUserRoot)
resp, err := svc.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
return root.User, resp, nil
}

// ResetUserAuth will reset user authentication
func (svc *DatabasesServiceOp) ResetUserAuth(ctx context.Context, databaseID, userID string, resetAuth *DatabaseResetUserAuthRequest) (*DatabaseUser, *Response, error) {
path := fmt.Sprintf(databaseResetUserAuthPath, databaseID, userID)
Expand Down
55 changes: 55 additions & 0 deletions databases_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -739,6 +739,61 @@ func TestDatabases_CreateUser(t *testing.T) {
require.Equal(t, want, got)
}

func TestDatabases_UpdateUser(t *testing.T) {
setup()
defer teardown()

dbID := "deadbeef-dead-4aa5-beef-deadbeef347d"
userID := "test-user"

want := &DatabaseUser{
Name: userID,
Settings: &DatabaseUserSettings{
ACL: []*KafkaACL{
{
Topic: "events",
Permission: "produce_consume",
},
},
},
}

body := `
{
"user": {
"name": "test-user",
"settings": {
"acl": [
{
"permission": "produce_consume",
"topic": "events"
}
]
}
}
}
`
path := fmt.Sprintf("/v2/databases/%s/users/%s", dbID, userID)

mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodPut)
fmt.Fprint(w, body)
})

got, _, err := client.Databases.UpdateUser(ctx, dbID, userID, &DatabaseUpdateUserRequest{
Settings: &DatabaseUserSettings{
ACL: []*KafkaACL{
{
Topic: "events",
Permission: "produce_consume",
},
},
},
})
require.NoError(t, err)
require.Equal(t, want, got)
}

func TestDatabases_DeleteUser(t *testing.T) {
setup()
defer teardown()
Expand Down
Loading