Skip to content

Commit 214d054

Browse files
authored
Add annotation during destroy to allow deletion (#130)
closes: #127
1 parent 2c1ff42 commit 214d054

File tree

1 file changed

+23
-2
lines changed

1 file changed

+23
-2
lines changed

cloud/resource_cloud_environment.go

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,16 @@ package cloud
1717
import (
1818
"context"
1919
"fmt"
20-
cloudv1alpha1 "github.com/streamnative/cloud-api-server/pkg/apis/cloud/v1alpha1"
21-
cloudclient "github.com/streamnative/cloud-api-server/pkg/client/clientset_generated/clientset"
2220
"strings"
2321
"time"
2422

23+
cloudv1alpha1 "github.com/streamnative/cloud-api-server/pkg/apis/cloud/v1alpha1"
24+
cloudclient "github.com/streamnative/cloud-api-server/pkg/client/clientset_generated/clientset"
25+
2526
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
2627
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
2728
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
29+
"k8s.io/apimachinery/pkg/api/errors"
2830
apierrors "k8s.io/apimachinery/pkg/api/errors"
2931
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3032
)
@@ -472,6 +474,25 @@ func resourceCloudEnvironmentDelete(ctx context.Context, d *schema.ResourceData,
472474
name := strings.Split(d.Id(), "/")[1]
473475
waitForCompletion := d.Get("wait_for_completion")
474476

477+
// Get CloudEnvironment to update the annotation
478+
cloudEnvironment, err := clientSet.CloudV1alpha1().CloudEnvironments(namespace).Get(ctx, name, metav1.GetOptions{})
479+
if err != nil {
480+
if errors.IsNotFound(err) {
481+
//If we can't find the CE, just return, as the CE is already deleted
482+
return nil
483+
} else {
484+
return diag.FromErr(fmt.Errorf("ERROR_READ_CLOUD_ENVIRONMENT: %w", err))
485+
}
486+
}
487+
488+
cloudEnvironment.Annotations["cloud.streamnative.io/destroy-protected"] = "false"
489+
490+
if _, err := clientSet.CloudV1alpha1().CloudEnvironments(namespace).Update(ctx, cloudEnvironment, metav1.UpdateOptions{
491+
FieldManager: "terraform-update",
492+
}); err != nil {
493+
return diag.FromErr(fmt.Errorf("ERROR_UPDATE_CLOUD_ENVIRONMENT: %w", err))
494+
}
495+
475496
if err != nil {
476497
return diag.FromErr(fmt.Errorf("ERROR_INIT_CLIENT_ON_DELETE_CLOUD_ENVIRONMENT: %w", err))
477498
}

0 commit comments

Comments
 (0)