forked from kyma-project/control-plane
-
Notifications
You must be signed in to change notification settings - Fork 0
/
instance_deprovision.go
116 lines (98 loc) · 4.25 KB
/
instance_deprovision.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package broker
import (
"context"
"fmt"
"net/http"
"github.com/kyma-project/control-plane/components/kyma-environment-broker/internal"
"github.com/google/uuid"
"github.com/pkg/errors"
"github.com/kyma-project/control-plane/components/kyma-environment-broker/internal/storage"
"github.com/kyma-project/control-plane/components/kyma-environment-broker/internal/storage/dberr"
"github.com/pivotal-cf/brokerapi/v7/domain"
"github.com/pivotal-cf/brokerapi/v7/domain/apiresponses"
"github.com/sirupsen/logrus"
)
type DeprovisionEndpoint struct {
log logrus.FieldLogger
instancesStorage storage.Instances
operationsStorage storage.Deprovisioning
queue Queue
}
func NewDeprovision(instancesStorage storage.Instances, operationsStorage storage.Operations, q Queue, log logrus.FieldLogger) *DeprovisionEndpoint {
return &DeprovisionEndpoint{
log: log.WithField("service", "DeprovisionEndpoint"),
instancesStorage: instancesStorage,
operationsStorage: operationsStorage,
queue: q,
}
}
// Deprovision deletes an existing service instance
// DELETE /v2/service_instances/{instance_id}
func (b *DeprovisionEndpoint) Deprovision(ctx context.Context, instanceID string, details domain.DeprovisionDetails, asyncAllowed bool) (domain.DeprovisionServiceSpec, error) {
logger := b.log.WithFields(logrus.Fields{"instanceID": instanceID})
logger.Infof("Deprovisioning triggered, details: %+v", details)
instance, err := b.instancesStorage.GetByID(instanceID)
switch {
case err == nil:
case dberr.IsNotFound(err):
logger.Warn("instance does not exist")
return domain.DeprovisionServiceSpec{
IsAsync: false,
}, nil
default:
logger.Errorf("unable to get instance from a storage: %s", err)
return domain.DeprovisionServiceSpec{}, apiresponses.NewFailureResponse(fmt.Errorf("unable to get instance from the storage"), http.StatusInternalServerError, fmt.Sprintf("could not deprovision runtime, instanceID %s", instanceID))
}
logger = logger.WithFields(logrus.Fields{"runtimeID": instance.RuntimeID, "globalAccountID": instance.GlobalAccountID, "planID": instance.ServicePlanID})
// check if operation with the same instance ID is already created
existingOperation, errStorage := b.operationsStorage.GetDeprovisioningOperationByInstanceID(instanceID)
switch {
case errStorage != nil && !dberr.IsNotFound(errStorage):
logger.Errorf("cannot get existing operation from storage %s", errStorage)
return domain.DeprovisionServiceSpec{}, errors.New("cannot get existing operation from storage")
// there is an operation and it is not a temporary deprovision
case existingOperation != nil && !existingOperation.Temporary && !dberr.IsNotFound(errStorage):
logger = logger.WithField("operationID", existingOperation.ID)
if existingOperation.State == domain.Failed {
err := b.reprocessOperation(existingOperation)
if err != nil {
return domain.DeprovisionServiceSpec{}, errors.Wrap(err, "while reprocessing operation")
}
logger.Info("Reprocessing failed deprovisioning of runtime")
b.queue.Add(existingOperation.ID)
}
// return existing operation
return domain.DeprovisionServiceSpec{
IsAsync: true,
OperationData: existingOperation.ID,
}, nil
}
// create and save new operation
operationID := uuid.New().String()
logger = logger.WithField("operationID", operationID)
operation, err := internal.NewDeprovisioningOperationWithID(operationID, instance)
if err != nil {
logger.Errorf("cannot create new operation: %s", err)
return domain.DeprovisionServiceSpec{}, errors.New("cannot create new operation")
}
err = b.operationsStorage.InsertDeprovisioningOperation(operation)
if err != nil {
logger.Errorf("cannot save operation: %s", err)
return domain.DeprovisionServiceSpec{}, errors.New("cannot save operation")
}
logger.Info("Adding operation to deprovisioning queue")
b.queue.Add(operationID)
return domain.DeprovisionServiceSpec{
IsAsync: true,
OperationData: operationID,
}, nil
}
func (b *DeprovisionEndpoint) reprocessOperation(operation *internal.DeprovisioningOperation) error {
operation.State = domain.InProgress
operation.ProvisionerOperationID = ""
_, err := b.operationsStorage.UpdateDeprovisioningOperation(*operation)
if err != nil {
return errors.New("cannot update existing operation")
}
return nil
}