-
Notifications
You must be signed in to change notification settings - Fork 54
/
domain_handler.go
69 lines (55 loc) · 2.13 KB
/
domain_handler.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
69
package handlers
import (
"context"
"net/http"
"net/url"
"code.cloudfoundry.org/korifi/api/apierrors"
"code.cloudfoundry.org/korifi/api/authorization"
"code.cloudfoundry.org/korifi/api/payloads"
"code.cloudfoundry.org/korifi/api/presenter"
ctrl "sigs.k8s.io/controller-runtime"
"github.com/go-logr/logr"
"github.com/gorilla/mux"
"code.cloudfoundry.org/korifi/api/repositories"
)
const (
DomainsPath = "/v3/domains"
)
//counterfeiter:generate -o fake -fake-name CFDomainRepository . CFDomainRepository
type CFDomainRepository interface {
GetDomain(context.Context, authorization.Info, string) (repositories.DomainRecord, error)
ListDomains(context.Context, authorization.Info, repositories.ListDomainsMessage) ([]repositories.DomainRecord, error)
}
type DomainHandler struct {
handlerWrapper *AuthAwareHandlerFuncWrapper
serverURL url.URL
domainRepo CFDomainRepository
}
func NewDomainHandler(
serverURL url.URL,
domainRepo CFDomainRepository,
) *DomainHandler {
return &DomainHandler{
handlerWrapper: NewAuthAwareHandlerFuncWrapper(ctrl.Log.WithName("DomainHandler")),
serverURL: serverURL,
domainRepo: domainRepo,
}
}
func (h *DomainHandler) DomainListHandler(ctx context.Context, logger logr.Logger, authInfo authorization.Info, r *http.Request) (*HandlerResponse, error) { //nolint:dupl
if err := r.ParseForm(); err != nil {
return nil, apierrors.LogAndReturn(logger, err, "Unable to parse request query parameters")
}
domainListFilter := new(payloads.DomainList)
err := payloads.Decode(domainListFilter, r.Form)
if err != nil {
return nil, apierrors.LogAndReturn(logger, err, "Unable to decode request query parameters")
}
domainList, err := h.domainRepo.ListDomains(ctx, authInfo, domainListFilter.ToMessage())
if err != nil {
return nil, apierrors.LogAndReturn(logger, err, "Failed to fetch domain(s) from Kubernetes")
}
return NewHandlerResponse(http.StatusOK).WithBody(presenter.ForDomainList(domainList, h.serverURL, *r.URL)), nil
}
func (h *DomainHandler) RegisterRoutes(router *mux.Router) {
router.Path(DomainsPath).Methods("GET").HandlerFunc(h.handlerWrapper.Wrap(h.DomainListHandler))
}