From 47869f659054ab2043550a7b3972c725afd42e93 Mon Sep 17 00:00:00 2001 From: elkezza <47871727+elkezza@users.noreply.github.com> Date: Wed, 22 May 2024 09:42:01 +0200 Subject: [PATCH] first commit adding instance deletion protection flag. --- cmd/instance_create.go | 17 +++++++++++++++++ cmd/instance_update.go | 18 ++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/cmd/instance_create.go b/cmd/instance_create.go index ec34aa88..8564b884 100644 --- a/cmd/instance_create.go +++ b/cmd/instance_create.go @@ -22,6 +22,7 @@ import ( "github.com/exoscale/cli/utils" egoscale "github.com/exoscale/egoscale/v2" exoapi "github.com/exoscale/egoscale/v2/api" + egoscale3 "github.com/exoscale/egoscale/v3" ) type instanceCreateCmd struct { @@ -41,6 +42,7 @@ type instanceCreateCmd struct { Labels map[string]string `cli-flag:"label" cli-usage:"instance label (format: key=value)"` PrivateNetworks []string `cli-flag:"private-network" cli-usage:"instance Private Network NAME|ID (can be specified multiple times)"` PrivateInstance bool `cli-flag:"private-instance" cli-usage:"enable private instance to be created"` + Protection bool `cli-flag:"protection" cli-usage:"enable delete protection"` SSHKey string `cli-flag:"ssh-key" cli-usage:"SSH key to deploy on the instance"` SecurityGroups []string `cli-flag:"security-group" cli-usage:"instance Security Group NAME|ID (can be specified multiple times)"` Template string `cli-usage:"instance template NAME|ID"` @@ -209,6 +211,21 @@ func (c *instanceCreateCmd) cmdRun(_ *cobra.Command, _ []string) error { //nolin return } } + + if c.Protection { + var value egoscale3.UUID + var op *egoscale3.Operation + value, err = egoscale3.ParseUUID(*instance.ID) + if err != nil { + return + } + op, err = globalstate.EgoscaleV3Client.AddInstanceProtection(ctx, value) + if err != nil { + return + } + op, err = globalstate.EgoscaleV3Client.Wait(ctx, op, egoscale3.OperationStateSuccess) + + } }) if err != nil { return err diff --git a/cmd/instance_update.go b/cmd/instance_update.go index ee5b133f..b5d131fd 100644 --- a/cmd/instance_update.go +++ b/cmd/instance_update.go @@ -3,6 +3,7 @@ package cmd import ( "errors" "fmt" + egoscale3 "github.com/exoscale/egoscale/v3" "strings" "github.com/spf13/cobra" @@ -25,6 +26,7 @@ type instanceUpdateCmd struct { CloudInitCompress bool `cli-flag:"cloud-init-compress" cli-usage:"compress instance cloud-init user data"` Labels map[string]string `cli-flag:"label" cli-usage:"instance label (format: key=value)"` Name string `cli-short:"n" cli-usage:"instance name"` + Protection bool `cli-flag:"protection" cli-usage:"enable delete protection"` Zone string `cli-short:"z" cli-usage:"instance zone"` ReverseDNS string `cli-usage:"Reverse DNS Domain"` } @@ -97,6 +99,22 @@ func (c *instanceUpdateCmd) cmdRun(cmd *cobra.Command, _ []string) error { err = globalstate.EgoscaleClient.UpdateInstanceReverseDNS(ctx, c.Zone, *instance.ID, c.ReverseDNS) } } + var value egoscale3.UUID + var op *egoscale3.Operation + value, err = egoscale3.ParseUUID(*instance.ID) + if err != nil { + return + } + if c.Protection { + op, err = globalstate.EgoscaleV3Client.AddInstanceProtection(ctx, value) + } else { + op, err = globalstate.EgoscaleV3Client.RemoveInstanceProtection(ctx, value) + } + if err != nil { + return + } + op, err = globalstate.EgoscaleV3Client.Wait(ctx, op, egoscale3.OperationStateSuccess) + }) if err != nil {