/
space_manifest_handler.go
91 lines (77 loc) · 3.08 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
package apis
import (
"context"
"fmt"
"net/http"
"net/url"
"github.com/go-http-utils/headers"
"github.com/go-logr/logr"
"github.com/gorilla/mux"
"code.cloudfoundry.org/cf-k8s-controllers/api/apierrors"
"code.cloudfoundry.org/cf-k8s-controllers/api/authorization"
"code.cloudfoundry.org/cf-k8s-controllers/api/payloads"
"code.cloudfoundry.org/cf-k8s-controllers/api/repositories"
)
const (
SpaceManifestApplyPath = "/v3/spaces/{spaceGUID}/actions/apply_manifest"
SpaceManifestDiffPath = "/v3/spaces/{spaceGUID}/manifest_diff"
)
type SpaceManifestHandler struct {
logger logr.Logger
serverURL url.URL
defaultDomainName string
applyManifestAction ApplyManifestAction
spaceRepo repositories.CFSpaceRepository
decoderValidator *DecoderValidator
}
//counterfeiter:generate -o fake -fake-name ApplyManifestAction . ApplyManifestAction
type ApplyManifestAction func(ctx context.Context, authInfo authorization.Info, spaceGUID string, defaultDomainName string, manifest payloads.Manifest) error
func NewSpaceManifestHandler(
logger logr.Logger,
serverURL url.URL,
defaultDomainName string,
applyManifestAction ApplyManifestAction,
spaceRepo repositories.CFSpaceRepository,
decoderValidator *DecoderValidator,
) *SpaceManifestHandler {
return &SpaceManifestHandler{
logger: logger,
serverURL: serverURL,
defaultDomainName: defaultDomainName,
applyManifestAction: applyManifestAction,
spaceRepo: spaceRepo,
decoderValidator: decoderValidator,
}
}
func (h *SpaceManifestHandler) RegisterRoutes(router *mux.Router) {
w := NewAuthAwareHandlerFuncWrapper(h.logger)
router.Path(SpaceManifestApplyPath).Methods("POST").HandlerFunc(w.Wrap(h.applyManifestHandler))
router.Path(SpaceManifestDiffPath).Methods("POST").HandlerFunc(w.Wrap(h.diffManifestHandler))
}
func (h *SpaceManifestHandler) applyManifestHandler(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, err
}
if err := h.applyManifestAction(r.Context(), authInfo, spaceGUID, h.defaultDomainName, manifest); err != nil {
h.logger.Error(err, "Error applying manifest")
return nil, apierrors.AsUnprocessibleEntity(
err,
fmt.Sprintf("The configured default domain %q was not found", h.defaultDomainName),
apierrors.NotFoundError{},
)
}
return NewHandlerResponse(http.StatusAccepted).
WithHeader(headers.Location, fmt.Sprintf("%s/v3/jobs/space.apply_manifest-%s", h.serverURL.String(), spaceGUID)), nil
}
func (h *SpaceManifestHandler) diffManifestHandler(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 {
h.logger.Error(err, "failed to get space", "guid", spaceGUID)
return nil, err
}
return NewHandlerResponse(http.StatusAccepted).WithBody(map[string]interface{}{"diff": []string{}}), nil
}