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

feat(mongodb): add CreateUser grpc layer #2285

Merged
merged 2 commits into from
Oct 29, 2024
Merged
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
55 changes: 55 additions & 0 deletions api/mongodb/v1alpha1/mongodb_sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -718,6 +718,21 @@ type CreateSnapshotRequest struct {
ExpiresAt *time.Time `json:"expires_at,omitempty"`
}

// CreateUserRequest: create user request.
type CreateUserRequest struct {
// Region: region to target. If none is passed will use default region from the config.
Region scw.Region `json:"-"`

// InstanceID: UUID of the Database Instance the user belongs to.
InstanceID string `json:"-"`

// Name: name of the database user.
Name string `json:"-"`

// Password: password of the database user.
Password *string `json:"password,omitempty"`
}

// DeleteInstanceRequest: delete instance request.
type DeleteInstanceRequest struct {
// Region: region to target. If none is passed will use default region from the config.
Expand Down Expand Up @@ -1673,6 +1688,46 @@ func (s *API) ListUsers(req *ListUsersRequest, opts ...scw.RequestOption) (*List
return &resp, nil
}

// CreateUser: Create an user on a Database Instance. You must define the `name`, `password` of the user and `instance_id` parameters in the request.
func (s *API) CreateUser(req *CreateUserRequest, opts ...scw.RequestOption) (*User, error) {
var err error

if req.Region == "" {
defaultRegion, _ := s.client.GetDefaultRegion()
req.Region = defaultRegion
}

if fmt.Sprint(req.Region) == "" {
return nil, errors.New("field Region cannot be empty in request")
}

if fmt.Sprint(req.InstanceID) == "" {
return nil, errors.New("field InstanceID cannot be empty in request")
}

if fmt.Sprint(req.Name) == "" {
return nil, errors.New("field Name cannot be empty in request")
}

scwReq := &scw.ScalewayRequest{
Method: "POST",
Path: "/mongodb/v1alpha1/regions/" + fmt.Sprint(req.Region) + "/instances/" + fmt.Sprint(req.InstanceID) + "/users/" + fmt.Sprint(req.Name) + "",
}

err = scwReq.SetBody(req)
if err != nil {
return nil, err
}

var resp User

err = s.client.Do(scwReq, &resp, opts...)
if err != nil {
return nil, err
}
return &resp, nil
}

// UpdateUser: Update the parameters of a user on a Database Instance. You can update the `password` parameter, but you cannot change the name of the user.
func (s *API) UpdateUser(req *UpdateUserRequest, opts ...scw.RequestOption) (*User, error) {
var err error
Expand Down