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

return error if ams org id is not found #117

Merged
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
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