github.com/crossplane/upjet@v1.3.0/pkg/terraform/finalizer.go (about) 1 // SPDX-FileCopyrightText: 2023 The Crossplane Authors <https://crossplane.io> 2 // 3 // SPDX-License-Identifier: Apache-2.0 4 5 package terraform 6 7 import ( 8 "context" 9 10 xpresource "github.com/crossplane/crossplane-runtime/pkg/resource" 11 "github.com/pkg/errors" 12 ) 13 14 const ( 15 errRemoveWorkspace = "cannot remove workspace from the store" 16 ) 17 18 // StoreCleaner is the interface that the workspace finalizer needs to work with. 19 type StoreCleaner interface { 20 Remove(obj xpresource.Object) error 21 } 22 23 // TODO(muvaf): A FinalizerChain in crossplane-runtime? 24 25 // NewWorkspaceFinalizer returns a new WorkspaceFinalizer. 26 func NewWorkspaceFinalizer(ws StoreCleaner, af xpresource.Finalizer) *WorkspaceFinalizer { 27 return &WorkspaceFinalizer{ 28 Finalizer: af, 29 Store: ws, 30 } 31 } 32 33 // WorkspaceFinalizer removes the workspace from the workspace store and only 34 // then calls RemoveFinalizer of the underlying Finalizer. 35 type WorkspaceFinalizer struct { 36 xpresource.Finalizer 37 Store StoreCleaner 38 } 39 40 // AddFinalizer to the supplied Managed resource. 41 func (wf *WorkspaceFinalizer) AddFinalizer(ctx context.Context, obj xpresource.Object) error { 42 return wf.Finalizer.AddFinalizer(ctx, obj) 43 } 44 45 // RemoveFinalizer removes the workspace from workspace store before removing 46 // the finalizer. 47 func (wf *WorkspaceFinalizer) RemoveFinalizer(ctx context.Context, obj xpresource.Object) error { 48 if err := wf.Store.Remove(obj); err != nil { 49 return errors.Wrap(err, errRemoveWorkspace) 50 } 51 return wf.Finalizer.RemoveFinalizer(ctx, obj) 52 }