Skip to content

Commit

Permalink
Merge pull request #21 from Luzilla/fix-response-usage
Browse files Browse the repository at this point in the history
Fix(response): return response also in case of error
  • Loading branch information
till authored Oct 2, 2024
2 parents 8b2da20 + 5365b3a commit 809ce7a
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 34 deletions.
2 changes: 1 addition & 1 deletion internal/cmd/buckets.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
func ListBuckets(cCtx *cli.Context) error {
client := cCtx.Context.Value(OstorClient).(*ostor.Ostor)

buckets, err := client.GetBuckets("")
buckets, _, err := client.GetBuckets("")
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions internal/cmd/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func ShowStats(cCtx *cli.Context) error {
}

page++
items, err := client.List(after)
items, _, err := client.List(after)
if err != nil {
return err
}
Expand All @@ -58,7 +58,7 @@ func ShowStats(cCtx *cli.Context) error {

for _, obj := range items.Items {
// fmt.Println(obj)
usage, err := client.ObjectUsage(obj)
usage, _, err := client.ObjectUsage(obj)
if err != nil {
fmt.Println("usage: " + err.Error())
os.Exit(2)
Expand Down
13 changes: 8 additions & 5 deletions internal/cmd/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ import (
func Users(cCtx *cli.Context) error {
client := cCtx.Context.Value(OstorClient).(*ostor.Ostor)

resp, err := client.ListUsers()
users, _, err := client.ListUsers()
if err != nil {
return err
}

tbl := table.New("Email", "ID", "State")
tbl.WithHeaderFormatter(headerFmt()).WithFirstColumnFormatter(columnFmt())

for _, u := range resp.Users {
for _, u := range users.Users {
tbl.AddRow(u.Email, u.ID, u.State)
}
tbl.Print()
Expand Down Expand Up @@ -55,8 +55,11 @@ func ShowUser(cCtx *cli.Context) error {

email := cCtx.String("email")

user, _, err := client.GetUser(email)
user, resp, err := client.GetUser(email)
if err != nil {
if resp.StatusCode() == 404 {
return fmt.Errorf("no user with email %q found", email)
}
return err
}

Expand All @@ -76,7 +79,7 @@ func ShowUser(cCtx *cli.Context) error {

fmt.Println("")

buckets, err := client.GetBuckets(email)
buckets, _, err := client.GetBuckets(email)
if err != nil {
return err
}
Expand Down Expand Up @@ -112,7 +115,7 @@ func CreateKey(cCtx *cli.Context) error {

email := cCtx.String("email")

_, err := client.GenerateCredentials(email)
_, _, err := client.GenerateCredentials(email)
if err != nil {
return err
}
Expand Down
8 changes: 5 additions & 3 deletions pkg/ostor/buckets.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package ostor

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

const qBuckets string = "ostor-buckets"

func (o *Ostor) GetBuckets(email string) (*OstorBucketListResponse, error) {
func (o *Ostor) GetBuckets(email string) (*OstorBucketListResponse, *resty.Response, error) {
var buckets *OstorBucketListResponse
_, err := o.get(qBuckets, map[string]string{"emailAddress": email}, &buckets)
return buckets, err
resp, err := o.get(qBuckets, map[string]string{"emailAddress": email}, &buckets)
return buckets, resp, err
}
6 changes: 3 additions & 3 deletions pkg/ostor/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package ostor

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

func (o *Ostor) GenerateCredentials(email string) (*OstorCreateUserResponse, error) {
func (o *Ostor) GenerateCredentials(email string) (*OstorCreateUserResponse, *resty.Response, error) {
var user *OstorCreateUserResponse
_, err := o.post(qUsers, qUsers+"&emailAddress="+email+"&genKey", user)
return user, err
resp, err := o.post(qUsers, qUsers+"&emailAddress="+email+"&genKey", user)
return user, resp, err
}

func (o *Ostor) RevokeKey(email, accessKeyID string) (*resty.Response, error) {
Expand Down
6 changes: 3 additions & 3 deletions pkg/ostor/ostor.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,16 +103,16 @@ func (o *Ostor) request(req *resty.Request, cmd, method, url string) (*resty.Res
// fmt.Printf("%v", res.Request)
// b, _ := io.ReadAll(res.RawBody())
// fmt.Println(b)
return nil, fmt.Errorf("request failed: %s", err)
return res, fmt.Errorf("request failed: %s", err)
}

if res.IsError() {
headers := res.Header()
if headers.Get("X-Amz-Err-Message") != "" {
return nil, fmt.Errorf("request failed: %s", headers.Get("X-Amz-Err-Message"))
return res, fmt.Errorf("request failed: %s", headers.Get("X-Amz-Err-Message"))
}

return nil, fmt.Errorf("unable to make request: %d", res.StatusCode())
return res, fmt.Errorf("unable to make request: %d", res.StatusCode())
}

return res, nil
Expand Down
22 changes: 8 additions & 14 deletions pkg/ostor/stats.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package ostor

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

const qStats string = "ostor-usage"

func (o *Ostor) List(after *string) (*OStorResponse, error) {
func (o *Ostor) List(after *string) (*OStorResponse, *resty.Response, error) {
var stats *OStorResponse

queryString := map[string]string{}
Expand All @@ -16,21 +18,13 @@ func (o *Ostor) List(after *string) (*OStorResponse, error) {
// - limit cannot be used with pagination (unless we implement other logic)
// queryString["limit"] = strconv.Itoa(710)

_, err := o.get(qStats, queryString, &stats)
if err != nil {
return stats, err
}

return stats, err
resp, err := o.get(qStats, queryString, &stats)
return stats, resp, err
}

func (o *Ostor) ObjectUsage(object string) (*OStorObjectUsageResponse, error) {
func (o *Ostor) ObjectUsage(object string) (*OStorObjectUsageResponse, *resty.Response, error) {
var usage *OStorObjectUsageResponse

_, err := o.get(qStats, map[string]string{"obj": object}, &usage)
if err != nil {
return usage, err
}

return usage, err
resp, err := o.get(qStats, map[string]string{"obj": object}, &usage)
return usage, resp, err
}
6 changes: 3 additions & 3 deletions pkg/ostor/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ func (o *Ostor) CreateUser(email string) (*OstorCreateUserResponse, *resty.Respo
return user, resp, err
}

func (o *Ostor) ListUsers() (*OstorUsersListResponse, error) {
func (o *Ostor) ListUsers() (*OstorUsersListResponse, *resty.Response, error) {
var users *OstorUsersListResponse
_, err := o.get(qUsers, map[string]string{}, &users)
return users, err
resp, err := o.get(qUsers, map[string]string{}, &users)
return users, resp, err
}

func (o *Ostor) GetUser(email string) (*OstorUser, *resty.Response, error) {
Expand Down

0 comments on commit 809ce7a

Please sign in to comment.