-
Notifications
You must be signed in to change notification settings - Fork 54
/
droplet_handler.go
65 lines (54 loc) · 1.88 KB
/
droplet_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
package handlers
import (
"context"
"fmt"
"net/http"
"net/url"
"github.com/go-logr/logr"
"github.com/gorilla/mux"
ctrl "sigs.k8s.io/controller-runtime"
"code.cloudfoundry.org/korifi/api/apierrors"
"code.cloudfoundry.org/korifi/api/authorization"
"code.cloudfoundry.org/korifi/api/presenter"
"code.cloudfoundry.org/korifi/api/repositories"
)
const (
DropletPath = "/v3/droplets/{guid}"
)
//counterfeiter:generate -o fake -fake-name CFDropletRepository . CFDropletRepository
type CFDropletRepository interface {
GetDroplet(context.Context, authorization.Info, string) (repositories.DropletRecord, error)
ListDroplets(context.Context, authorization.Info, repositories.ListDropletsMessage) ([]repositories.DropletRecord, error)
}
type DropletHandler struct {
serverURL url.URL
dropletRepo CFDropletRepository
handlerWrapper *AuthAwareHandlerFuncWrapper
}
func NewDropletHandler(
serverURL url.URL,
dropletRepo CFDropletRepository,
) *DropletHandler {
return &DropletHandler{
handlerWrapper: NewAuthAwareHandlerFuncWrapper(ctrl.Log.WithName("DropletHandler")),
serverURL: serverURL,
dropletRepo: dropletRepo,
}
}
func (h *DropletHandler) dropletGetHandler(ctx context.Context, logger logr.Logger, authInfo authorization.Info, r *http.Request) (*HandlerResponse, error) {
vars := mux.Vars(r)
dropletGUID := vars["guid"]
droplet, err := h.dropletRepo.GetDroplet(ctx, authInfo, dropletGUID)
if err != nil {
return nil, apierrors.LogAndReturn(
logger,
apierrors.ForbiddenAsNotFound(err),
fmt.Sprintf("Failed to fetch %s from Kubernetes", repositories.DropletResourceType),
"guid", dropletGUID,
)
}
return NewHandlerResponse(http.StatusOK).WithBody(presenter.ForDroplet(droplet, h.serverURL)), nil
}
func (h *DropletHandler) RegisterRoutes(router *mux.Router) {
router.Path(DropletPath).Methods("GET").HandlerFunc(h.handlerWrapper.Wrap(h.dropletGetHandler))
}