-
Notifications
You must be signed in to change notification settings - Fork 19
/
claim_detach.go
63 lines (53 loc) · 1.93 KB
/
claim_detach.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
package component
import (
"fmt"
"runtime/debug"
"time"
"github.com/Aptomi/aptomi/pkg/engine/apply/action"
"github.com/Aptomi/aptomi/pkg/engine/resolve"
"github.com/Aptomi/aptomi/pkg/runtime"
"github.com/Aptomi/aptomi/pkg/util"
)
// DetachClaimActionObject is an informational data structure with Kind and Constructor for the action
var DetachClaimActionObject = &runtime.Info{
Kind: "action-component-claim-detach",
Constructor: func() runtime.Object { return &DetachClaimAction{} },
}
// DetachClaimAction is a action which gets called when a consumer is removed from an existing component
type DetachClaimAction struct {
runtime.TypeKind `yaml:",inline"`
*action.Metadata
ComponentKey string
ClaimKey string
}
// NewDetachClaimAction creates new DetachClaimAction
func NewDetachClaimAction(componentKey string, claimKey string) *DetachClaimAction {
return &DetachClaimAction{
Metadata: action.NewMetadata(DetachClaimActionObject.Kind, componentKey, claimKey),
ComponentKey: componentKey,
ClaimKey: claimKey,
}
}
// Apply applies the action
func (a *DetachClaimAction) Apply(context *action.Context) (errResult error) {
start := time.Now()
defer func() {
if err := recover(); err != nil {
errResult = fmt.Errorf("panic: %s\n%s", err, string(debug.Stack()))
}
action.CollectMetricsFor(a, start, errResult)
}()
context.EventLog.NewEntry().Debugf("Detaching claim '%s' from component instance: '%s'", a.ClaimKey, a.ComponentKey)
return context.ActualStateUpdater.UpdateComponentInstance(a.ComponentKey, func(obj *resolve.ComponentInstance) {
delete(obj.ClaimKeys, a.ClaimKey)
})
}
// DescribeChanges returns text-based description of changes that will be applied
func (a *DetachClaimAction) DescribeChanges() util.NestedParameterMap {
return util.NestedParameterMap{
"kind": a.Kind,
"key": a.ComponentKey,
"claim": a.ClaimKey,
"pretty": fmt.Sprintf("[<] %s = %s", a.ComponentKey, a.ClaimKey),
}
}