-
Notifications
You must be signed in to change notification settings - Fork 59
/
space_manifest_handler.go
92 lines (76 loc) · 3.51 KB
/
space_manifest_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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package handlers
import (
"context"
"net/http"
"net/url"
"github.com/go-http-utils/headers"
"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/payloads"
"code.cloudfoundry.org/korifi/api/presenter"
"code.cloudfoundry.org/korifi/api/repositories"
)
const (
SpaceManifestApplyPath = "/v3/spaces/{spaceGUID}/actions/apply_manifest"
SpaceManifestDiffPath = "/v3/spaces/{spaceGUID}/manifest_diff"
)
//counterfeiter:generate -o fake -fake-name CFSpaceRepository . CFSpaceRepository
type CFSpaceRepository interface {
CreateSpace(context.Context, authorization.Info, repositories.CreateSpaceMessage) (repositories.SpaceRecord, error)
ListSpaces(context.Context, authorization.Info, repositories.ListSpacesMessage) ([]repositories.SpaceRecord, error)
GetSpace(context.Context, authorization.Info, string) (repositories.SpaceRecord, error)
DeleteSpace(context.Context, authorization.Info, repositories.DeleteSpaceMessage) error
}
type SpaceManifestHandler struct {
handlerWrapper *AuthAwareHandlerFuncWrapper
serverURL url.URL
manifestApplier ManifestApplier
spaceRepo CFSpaceRepository
decoderValidator *DecoderValidator
}
//counterfeiter:generate -o fake -fake-name ManifestApplier . ManifestApplier
type ManifestApplier interface {
Apply(ctx context.Context, authInfo authorization.Info, spaceGUID string, manifest payloads.Manifest) error
}
func NewSpaceManifestHandler(
serverURL url.URL,
manifestApplier ManifestApplier,
spaceRepo CFSpaceRepository,
decoderValidator *DecoderValidator,
) *SpaceManifestHandler {
return &SpaceManifestHandler{
handlerWrapper: NewAuthAwareHandlerFuncWrapper(ctrl.Log.WithName("SpaceManifestHandler")),
serverURL: serverURL,
manifestApplier: manifestApplier,
spaceRepo: spaceRepo,
decoderValidator: decoderValidator,
}
}
func (h *SpaceManifestHandler) RegisterRoutes(router *mux.Router) {
router.Path(SpaceManifestApplyPath).Methods("POST").HandlerFunc(h.handlerWrapper.Wrap(h.applyManifestHandler))
router.Path(SpaceManifestDiffPath).Methods("POST").HandlerFunc(h.handlerWrapper.Wrap(h.diffManifestHandler))
}
func (h *SpaceManifestHandler) applyManifestHandler(ctx context.Context, logger logr.Logger, authInfo authorization.Info, r *http.Request) (*HandlerResponse, error) {
vars := mux.Vars(r)
spaceGUID := vars["spaceGUID"]
var manifest payloads.Manifest
if err := h.decoderValidator.DecodeAndValidateYAMLPayload(r, &manifest); err != nil {
return nil, apierrors.LogAndReturn(logger, err, "failed to decode payload")
}
if err := h.manifestApplier.Apply(ctx, authInfo, spaceGUID, manifest); err != nil {
return nil, apierrors.LogAndReturn(logger, err, "Error applying manifest")
}
return NewHandlerResponse(http.StatusAccepted).
WithHeader(headers.Location, presenter.JobURLForRedirects(spaceGUID, presenter.SpaceApplyManifestOperation, h.serverURL)), nil
}
func (h *SpaceManifestHandler) diffManifestHandler(ctx context.Context, logger logr.Logger, authInfo authorization.Info, r *http.Request) (*HandlerResponse, error) {
vars := mux.Vars(r)
spaceGUID := vars["spaceGUID"]
if _, err := h.spaceRepo.GetSpace(r.Context(), authInfo, spaceGUID); err != nil {
return nil, apierrors.LogAndReturn(logger, apierrors.ForbiddenAsNotFound(err), "failed to get space", "guid", spaceGUID)
}
return NewHandlerResponse(http.StatusAccepted).WithBody(map[string]interface{}{"diff": []string{}}), nil
}