Skip to content

Commit

Permalink
Merge pull request #31 from Luzilla/delete-account
Browse files Browse the repository at this point in the history
delete account
  • Loading branch information
till authored Oct 28, 2024
2 parents fcaa8f0 + 07f10ac commit 132a73c
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 5 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
2 changes: 1 addition & 1 deletion pkg/ostor/buckets.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ const qBuckets string = "ostor-buckets"

func (o *Ostor) GetBuckets(email string) (*OstorBucketListResponse, *resty.Response, error) {
var buckets *OstorBucketListResponse
resp, err := o.get(qBuckets, map[string]string{"emailAddress": email}, &buckets)
resp, err := o.get(qBuckets, emailMap(email), &buckets)
return buckets, resp, err
}
2 changes: 1 addition & 1 deletion pkg/ostor/limits.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ const qLimits string = "ostor-limits"

func (o *Ostor) GetUserLimits(email string) (*OstorUserLimits, *resty.Response, error) {
var limits *OstorUserLimits
resp, err := o.get(qLimits, map[string]string{"emailAddress": email}, &limits)
resp, err := o.get(qLimits, emailMap(email), &limits)
return limits, resp, err
}
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
23 changes: 21 additions & 2 deletions 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 All @@ -25,7 +44,7 @@ func (o *Ostor) ListUsers(usage bool) (*OstorUsersListResponse, *resty.Response,

func (o *Ostor) GetUser(email string) (*OstorUser, *resty.Response, error) {
var user *OstorUser
resp, err := o.get(qUsers, map[string]string{"emailAddress": email}, &user)
resp, err := o.get(qUsers, emailMap(email), &user)
return user, resp, err
}

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 132a73c

Please sign in to comment.