-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathcandlepin.go
68 lines (58 loc) · 1.94 KB
/
candlepin.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package handler
import (
"errors"
"net/http"
"github.com/content-services/content-sources-backend/pkg/api"
"github.com/content-services/content-sources-backend/pkg/cache"
"github.com/content-services/content-sources-backend/pkg/candlepin_client"
ce "github.com/content-services/content-sources-backend/pkg/errors"
"github.com/content-services/content-sources-backend/pkg/rbac"
"github.com/labstack/echo/v4"
"github.com/rs/zerolog/log"
)
var RHProductIDs = []string{"479"}
type CandlepinHandler struct {
cpClient candlepin_client.CandlepinClient
cache cache.Cache
}
func RegisterCandlepinRoutes(engine *echo.Group, cpClient *candlepin_client.CandlepinClient, cache *cache.Cache) {
if engine == nil {
panic("engine is nil")
}
if cpClient == nil {
panic("cpClient is nil")
}
if cache == nil {
panic("cache is nil")
}
candlepinHandler := CandlepinHandler{
cpClient: *cpClient,
cache: *cache,
}
addRepoRoute(engine, http.MethodGet, "/subscription_check/", candlepinHandler.subscriptionCheck, rbac.RbacVerbRead)
}
func (h *CandlepinHandler) subscriptionCheck(c echo.Context) error {
var resp api.SubscriptionCheckResponse
_, orgID := getAccountIdOrgId(c)
check, err := h.cache.GetSubscriptionCheck(c.Request().Context())
if err != nil && !errors.Is(err, cache.NotFound) {
log.Logger.Error().Err(err).Msg("subscriptionCheck: error reading from cache")
}
if check != nil {
return c.JSON(http.StatusOK, check)
}
product, err := h.cpClient.ListProducts(c.Request().Context(), orgID, RHProductIDs)
if err != nil {
return ce.NewErrorResponse(http.StatusInternalServerError, "subscription check error", err.Error())
}
if len(product) == 0 {
resp.RedHatEnterpriseLinux = false
} else {
resp.RedHatEnterpriseLinux = true
err = h.cache.SetSubscriptionCheck(c.Request().Context(), resp)
if err != nil {
log.Logger.Error().Err(err).Msg("subscriptionCheck: error writing to cache")
}
}
return c.JSON(http.StatusOK, resp)
}