From 1caf2d0b39d019a03eea0673716058123b3e2c92 Mon Sep 17 00:00:00 2001 From: Alba Hita Catala Date: Wed, 28 Feb 2024 10:10:49 +0100 Subject: [PATCH] OCM-5941 | add enable delete protection parameter Added --enable-delete-protection argument to the edit option. Signed-off-by: Alba Hita Catala --- cmd/ocm/edit/cluster/cmd.go | 21 ++++++++++++++++++--- pkg/cluster/cluster.go | 10 +++++++++- 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/cmd/ocm/edit/cluster/cmd.go b/cmd/ocm/edit/cluster/cmd.go index fff4b8ed..7c59c7b6 100644 --- a/cmd/ocm/edit/cluster/cmd.go +++ b/cmd/ocm/edit/cluster/cmd.go @@ -29,9 +29,9 @@ import ( var args struct { // Basic options - expirationTime string - expirationDuration time.Duration - + expirationTime string + expirationDuration time.Duration + enableDeleteProtection bool // Networking options private bool @@ -119,6 +119,12 @@ func init() { "A file contains a PEM-encoded X.509 certificate bundle that will be "+ "added to the nodes' trusted certificate store.") + flags.BoolVar( + &args.enableDeleteProtection, + "enable-delete-protection", + false, + "Enable cluster delete protection against accidental cluster deletion.", + ) } func isGCPNetworkEmpty(network *cmv1.GCPNetwork) bool { @@ -222,6 +228,15 @@ func run(cmd *cobra.Command, argv []string) error { noProxy = args.clusterWideProxy.NoProxy } + var enableDeleteProtection bool + if cmd.Flags().Changed("enable-delete-protection") { + enableDeleteProtection = args.enableDeleteProtection + err := c.UpdateDeleteProtection(clusterCollection, cluster.ID(), enableDeleteProtection) + if err != nil { + return err + } + } + var additionalTrustBundleFile *string var additionalTrustBundleFileValue string if cmd.Flags().Changed("additional-trust-bundle-file") { diff --git a/pkg/cluster/cluster.go b/pkg/cluster/cluster.go index 1b03f3d8..a5220860 100644 --- a/pkg/cluster/cluster.go +++ b/pkg/cluster/cluster.go @@ -580,7 +580,6 @@ func UpdateCluster(client *cmv1.ClustersClient, clusterID string, config Spec) e if err != nil { return err } - _, err = client.Cluster(clusterID).Update().Body(clusterSpec).Send() if err != nil { return err @@ -589,6 +588,15 @@ func UpdateCluster(client *cmv1.ClustersClient, clusterID string, config Spec) e return nil } +func UpdateDeleteProtection(client *cmv1.ClustersClient, clusterID string, enable bool) error { + deleteProtection, _ := cmv1.NewDeleteProtection().Enabled(enable).Build() + _, err := client.Cluster(clusterID).DeleteProtection().Update().Body(deleteProtection).Send() + if err != nil { + return err + } + return nil +} + func buildCompute(config Spec, clusterNodesBuilder *cmv1.ClusterNodesBuilder) *cmv1.ClusterNodesBuilder { if config.Autoscaling.Enabled { autoscalingBuilder := cmv1.NewMachinePoolAutoscaling()