-
Notifications
You must be signed in to change notification settings - Fork 28
/
platform_termination_plugin.go
80 lines (71 loc) · 2.87 KB
/
platform_termination_plugin.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
package osb
import (
"fmt"
"github.com/Peripli/service-manager/pkg/log"
"github.com/Peripli/service-manager/pkg/query"
"github.com/Peripli/service-manager/pkg/types"
"github.com/Peripli/service-manager/pkg/util"
"github.com/Peripli/service-manager/pkg/web"
"github.com/Peripli/service-manager/storage"
"net/http"
)
const PlatformTerminationPluginName = "PlatformTerminationPlugin"
type platformTerminationPlugin struct {
repository storage.Repository
}
// NewPlatformTerminationPlugin creates new plugin that checks the platform deletion status
func NewPlatformTerminationPlugin(repository storage.Repository) *platformTerminationPlugin {
return &platformTerminationPlugin{
repository: repository,
}
}
// Name returns the name of the plugin
func (p *platformTerminationPlugin) Name() string {
return PlatformTerminationPluginName
}
// UpdateService intercepts update service instance requests and check if the instance is in the platform from where the request comes
func (p *platformTerminationPlugin) UpdateService(req *web.Request, next web.Handler) (*web.Response, error) {
return p.validateNotPendingTermination(req, next)
}
// Bind intercepts bind requests and check if the instance is in the platform from where the request comes
func (p *platformTerminationPlugin) Bind(req *web.Request, next web.Handler) (*web.Response, error) {
return p.validateNotPendingTermination(req, next)
}
func (p *platformTerminationPlugin) Provision(req *web.Request, next web.Handler) (*web.Response, error) {
return p.validateNotPendingTermination(req, next)
}
func (p *platformTerminationPlugin) validateNotPendingTermination(req *web.Request, next web.Handler) (*web.Response, error) {
ctx := req.Context()
user, _ := web.UserFromContext(ctx)
if user == nil {
return next.Handle(req)
}
platform := &types.Platform{}
if err := user.Data(platform); err != nil {
return nil, err
}
if err := platform.Validate(); err != nil {
log.C(ctx).WithError(err).Errorf("Invalid platform found in context")
return nil, err
}
platformID := platform.GetID()
criteria := []query.Criterion{
query.ByField(query.NotEqualsOperator, "cascade_root_id", ""),
query.ByField(query.EqualsOperator, "type", string(types.DELETE)),
query.ByField(query.EqualsOperator, "resource_type", string(types.PlatformType)),
query.ByField(query.InOperator, "state", string(types.PENDING), string(types.IN_PROGRESS)),
query.ByField(query.EqualsOperator, "resource_id", platformID),
}
operationCount, err := p.repository.Count(ctx, types.OperationType, criteria...)
if err != nil {
return nil, err
}
if operationCount > 0 {
return nil, &util.HTTPError{
ErrorType: http.StatusText(http.StatusUnprocessableEntity),
Description: fmt.Sprintf("Operations are not possible for resources of platform %s which is during termination", platformID),
StatusCode: http.StatusUnprocessableEntity,
}
}
return next.Handle(req)
}