From 0eca6db5d599ab1a47033a0331c58677583e90fc Mon Sep 17 00:00:00 2001 From: Till Klampaeckel Date: Mon, 28 Oct 2024 14:48:38 +0100 Subject: [PATCH] Update(ostor): delete account feature --- cmd/ostor/main.go | 7 +++++++ internal/cmd/users.go | 16 ++++++++++++++++ pkg/ostor/ostor.go | 12 +++++++++++- pkg/ostor/user.go | 21 ++++++++++++++++++++- pkg/ostor/utils.go | 4 ++++ 5 files changed, 58 insertions(+), 2 deletions(-) diff --git a/cmd/ostor/main.go b/cmd/ostor/main.go index dcc44d3..2b2ba0e 100644 --- a/cmd/ostor/main.go +++ b/cmd/ostor/main.go @@ -97,6 +97,13 @@ func main() { }, Action: cmd.CreateUser, }, + { + Name: "delete", + Flags: []cli.Flag{ + emailFlag(), + }, + Action: cmd.DeleteUser, + }, { Name: "lock", Flags: []cli.Flag{ diff --git a/internal/cmd/users.go b/internal/cmd/users.go index 1329e7f..ae991f5 100644 --- a/internal/cmd/users.go +++ b/internal/cmd/users.go @@ -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) diff --git a/pkg/ostor/ostor.go b/pkg/ostor/ostor.go index 8ea9f33..c3bdde0 100644 --- a/pkg/ostor/ostor.go +++ b/pkg/ostor/ostor.go @@ -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). @@ -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: @@ -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()) diff --git a/pkg/ostor/user.go b/pkg/ostor/user.go index 7c28554..4a272f4 100644 --- a/pkg/ostor/user.go +++ b/pkg/ostor/user.go @@ -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" @@ -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 diff --git a/pkg/ostor/utils.go b/pkg/ostor/utils.go index ad8c4bf..9bb99d3 100644 --- a/pkg/ostor/utils.go +++ b/pkg/ostor/utils.go @@ -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} +}