Skip to content

Commit

Permalink
Update(ostor): delete account feature
Browse files Browse the repository at this point in the history
  • Loading branch information
till committed Oct 28, 2024
1 parent fcaa8f0 commit 0eca6db
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 2 deletions.
7 changes: 7 additions & 0 deletions cmd/ostor/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,13 @@ func main() {
},
Action: cmd.CreateUser,
},
{
Name: "delete",
Flags: []cli.Flag{
emailFlag(),
},
Action: cmd.DeleteUser,
},
{
Name: "lock",
Flags: []cli.Flag{
Expand Down
16 changes: 16 additions & 0 deletions internal/cmd/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,22 @@ func CreateUser(cCtx *cli.Context) error {
return nil
}

func DeleteUser(cCtx *cli.Context) error {
client := cCtx.Context.Value(OstorClient).(*ostor.Ostor)

email := cCtx.String("email")

resp, err := client.DeleteUser(email)
if err != nil {
fmt.Println(resp.Request.URL)

return err
}

fmt.Println("Account deleted")
return nil
}

func LockUser(cCtx *cli.Context) error {
client := cCtx.Context.Value(OstorClient).(*ostor.Ostor)

Expand Down
12 changes: 11 additions & 1 deletion pkg/ostor/ostor.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ func New(endpoint, accessKeyID, secretKeyID string) (*Ostor, error) {
}, nil
}

func (o *Ostor) delete(cmd string, query map[string]string) (*resty.Response, error) {
return o.request(o.client.R().
SetQueryParams(query), cmd, resty.MethodDelete, "/?"+cmd)
}

func (o *Ostor) get(cmd string, query map[string]string, into any) (*resty.Response, error) {
return o.request(o.client.R().
SetQueryParams(query).
Expand Down Expand Up @@ -89,6 +94,8 @@ func (o *Ostor) request(req *resty.Request, cmd, method, url string) (*resty.Res
var res *resty.Response

switch method {
case resty.MethodDelete:
res, err = req.Delete(url)
case resty.MethodGet:
res, err = req.Get(url)
case resty.MethodPost:
Expand All @@ -109,7 +116,10 @@ func (o *Ostor) request(req *resty.Request, cmd, method, url string) (*resty.Res
if res.IsError() {
headers := res.Header()
if headers.Get("X-Amz-Err-Message") != "" {
return res, fmt.Errorf("request failed: %s", headers.Get("X-Amz-Err-Message"))
return res, fmt.Errorf("request failed: %s (http status code: %d)",
headers.Get("X-Amz-Err-Message"),
res.StatusCode(),
)
}

return res, fmt.Errorf("unable to make request: %d", res.StatusCode())
Expand Down
21 changes: 20 additions & 1 deletion pkg/ostor/user.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package ostor

import "github.com/go-resty/resty/v2"
import (
"fmt"
"net/http"

"github.com/go-resty/resty/v2"
)

// query parameter for user management
const qUsers string = "ostor-users"
Expand All @@ -11,6 +16,20 @@ func (o *Ostor) CreateUser(email string) (*OstorCreateUserResponse, *resty.Respo
return user, resp, err
}

func (o *Ostor) DeleteUser(email string) (*resty.Response, error) {
resp, err := o.delete(qUsers, emailMap(email))
if err != nil {
return resp, err
}

if resp.StatusCode() != http.StatusNoContent {
err = fmt.Errorf("wrong status code: %d", resp.StatusCode())
return resp, err
}

return resp, nil
}

func (o *Ostor) ListUsers(usage bool) (*OstorUsersListResponse, *resty.Response, error) {
var users *OstorUsersListResponse

Expand Down
4 changes: 4 additions & 0 deletions pkg/ostor/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,7 @@ func createSignature(httpMethod, awsSecretKey, query string) (signature string,
func authHeader(keyID, signature string) string {
return fmt.Sprintf("AWS %s:%s", keyID, signature)
}

func emailMap(email string) map[string]string {
return map[string]string{"emailAddress": email}
}

0 comments on commit 0eca6db

Please sign in to comment.