Skip to content

Commit

Permalink
first commit adding instance deletion protection flag.
Browse files Browse the repository at this point in the history
  • Loading branch information
elkezza committed May 22, 2024
1 parent 0a13aaa commit 47869f6
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
17 changes: 17 additions & 0 deletions cmd/instance_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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"`
Expand Down Expand Up @@ -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
Expand Down
18 changes: 18 additions & 0 deletions cmd/instance_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"errors"
"fmt"
egoscale3 "github.com/exoscale/egoscale/v3"
"strings"

"github.com/spf13/cobra"
Expand All @@ -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"`
}
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 47869f6

Please sign in to comment.