Skip to content

Commit

Permalink
return error if ams org id is not found (RedHatInsights#117)
Browse files Browse the repository at this point in the history
* return error if ams org id is not found

* changed log message

* handle ams client errors in controller

* switch logs back to debug
  • Loading branch information
dagbay-rh authored Apr 21, 2023
1 parent 46b1c6a commit 715946c
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
37 changes: 35 additions & 2 deletions ams/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"fmt"
l "github.com/RedHatInsights/entitlements-api-go/logger"
"github.com/sirupsen/logrus"
"net/http"
"strings"
"time"

"github.com/RedHatInsights/entitlements-api-go/config"
Expand Down Expand Up @@ -55,6 +57,27 @@ type AMSInterface interface {
QuotaAuthorization(accountUsername, quotaVersion string) (*v1.QuotaAuthorizationResponse, error)
}

type ClientError struct {
Message string
StatusCode int
OrgId string
AmsOrgId string
}

func (e *ClientError) Error() string {
b := strings.Builder{}

b.WriteString(e.Message)
if e.OrgId != "" {
b.WriteString(fmt.Sprintf(" [OrgId: %s]", e.OrgId))
}
if e.AmsOrgId != "" {
b.WriteString(fmt.Sprintf(" [AMS OrgId: %s]", e.AmsOrgId))
}

return b.String()
}

type TestClient struct{}

var _ AMSInterface = &TestClient{}
Expand Down Expand Up @@ -150,7 +173,7 @@ func (c *Client) convertOrg(organizationId string) (string, error) {
item := c.cache.Get(organizationId)
if item != nil && !item.Expired() {
converted := item.Value().(string)
l.Log.WithFields(logrus.Fields{"ams_org_id": converted, "org_id": organizationId}).Info("pull converted ams org id from cache")
l.Log.WithFields(logrus.Fields{"ams_org_id": converted, "org_id": organizationId}).Debug("found converted ams org id in cache")
return converted, nil
}

Expand All @@ -162,9 +185,19 @@ func (c *Client) convertOrg(organizationId string) (string, error) {
}

converted, err := listResp.Items().Get(0).ID(), nil

if converted == "" {
return "", &ClientError{
Message: "no corresponding ams org id found for user org",
StatusCode: http.StatusBadRequest,
OrgId: organizationId,
AmsOrgId: "",
}
}

c.cache.Set(organizationId, converted, time.Minute*30)

l.Log.WithFields(logrus.Fields{"ams_org_id": converted, "org_id": organizationId}).Info("converted org id to ams org ig")
l.Log.WithFields(logrus.Fields{"ams_org_id": converted, "org_id": organizationId}).Debug("converted org id to ams org ig")

return converted, err
}
Expand Down
5 changes: 5 additions & 0 deletions controllers/ams.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,11 @@ func (s *SeatManagerApi) GetSeats(w http.ResponseWriter, r *http.Request, params

quotaCost, err := s.client.GetQuotaCost(idObj.Internal.OrgID)
if err != nil {
var clientError *ams.ClientError
if errors.As(err, &clientError) {
doError(w, clientError.StatusCode, clientError)
}

do500(w, fmt.Errorf("AMS GetQuotaCost [%w]", err))
return
}
Expand Down

0 comments on commit 715946c

Please sign in to comment.