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

delete account #31

Merged
merged 2 commits into from
Oct 28, 2024
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
7 changes: 7 additions & 0 deletions cmd/ostor/main.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main

Check warning on line 1 in cmd/ostor/main.go

View workflow job for this annotation

GitHub Actions / revive

should have a package comment

import (
"context"
Expand Down Expand Up @@ -97,6 +97,13 @@
},
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
@@ -1,4 +1,4 @@
package ostor

Check warning on line 1 in pkg/ostor/buckets.go

View workflow job for this annotation

GitHub Actions / revive

should have a package comment

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

Expand All @@ -6,6 +6,6 @@

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}
}
Loading