Skip to content

Commit

Permalink
instance: add a protection flag to exo compute (#608)
Browse files Browse the repository at this point in the history
# Description
added add/remove a protection flag to exo compute, however it did not
work as expected

## Checklist
(For exoscale contributors)

* [ ] Changelog updated (under *Unreleased* block)
* [x] Testing

## Testing

I tested the cli after the changes locally but the protection flag does
not work.


```bash
❯ ./exo compute instance update --help
This command updates an Instance .

Supported output template annotations: .ID, .Name, .CreationDate, .InstanceType, .Template, .Zone, .AntiAffinityGroups, .DeployTarget, .SecurityGroups, .PrivateInstance, .PrivateNetworks, .ElasticIPs, .IPAddress, .IPv6Address, .SSHKey, .DiskSize, .State, .Labels, .ReverseDNS

Usage:
  exo compute instance update NAME|ID [flags]

Flags:
  -c, --cloud-init string      instance cloud-init user data configuration file path
      --cloud-init-compress    compress instance cloud-init user data
  -h, --help                   help for update
      --label stringToString   instance label (format: key=value) (default [])
  -n, --name string            instance name
      --protection             enable delete protection
      --reverse-dns string     Reverse DNS Domain
  -z, --zone string            instance zone

Global Flags:
  -C, --config string            Specify an alternate config file [env EXOSCALE_CONFIG]
  -O, --output-format string     Output format (table|json|text), see "exo output --help" for more information
      --output-template string   Template to use if output format is "text"
  -Q, --quiet                    Quiet mode (disable non-essential command output)
  -A, --use-account string       Account to use in config file [env EXOSCALE_ACCOUNT]
❯ ./exo compute instance update 4ed629e1-9a63-4d74-b8c9-165d83292f9d --protection
┼──────────────────────┼──────────────────────────────────────┼
│   COMPUTE INSTANCE   │                                      │
┼──────────────────────┼──────────────────────────────────────┼
│ ID                   │ 4ed629e1-9a63-4d74-b8c9-165d83292f9d │
│ Name                 │ test                                 │
│ Creation Date        │ 2024-05-28 08:31:56 +0000 UTC        │
│ Instance Type        │ standard.medium                      │
│ Template             │ Linux Ubuntu 22.04 LTS 64-bit        │
│ Zone                 │ at-vie-1                             │
│ Anti-Affinity Groups │ n/a                                  │
│ Deploy Target        │ -                                    │
│ Security Groups      │ default                              │
│ Private Instance     │ No                                   │
│ Private Networks     │ n/a                                  │
│ Elastic IPs          │ n/a                                  │
│ IP Address           │ 194.182.184.154                      │
│ IPv6 Address         │ -                                    │
│ SSH Key              │ -                                    │
│ Disk Size            │ 50 GiB                               │
│ State                │ running                              │
│ Labels               │ n/a                                  │
│ Reverse DNS          │                                      │
┼──────────────────────┼──────────────────────────────────────┼
❯ ./exo compute instance delete 4ed629e1-9a63-4d74-b8c9-165d83292f9d
[+] Are you sure you want to delete instance "4ed629e1-9a63-4d74-b8c9-165d83292f9d"? [yN]: y
 ✔ Deleting instance "4ed629e1-9a63-4d74-b8c9-165d83292f9d"... 6s
```
  • Loading branch information
elkezza authored Aug 22, 2024
1 parent b88d773 commit 3dbd9b5
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ contrib
*.xdelta3
go.work
.aptlydata
.idea/
8 changes: 8 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

## 1.79.0

### Features

- instance: add a protection flag to exo compute #608

### Improvements

- DBaaS: external endpoints and integrations commands #631
Expand Down
16 changes: 16 additions & 0 deletions cmd/instance_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ type instanceCreateCmd struct {
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"`
SSHKeys []string `cli-flag:"ssh-key" cli-usage:"SSH key to deploy on the instance (can be specified multiple times)"`
Protection bool `cli-flag:"protection" cli-usage:"enable delete protection"`
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"`
TemplateVisibility string `cli-usage:"instance template visibility (public|private)"`
Expand Down Expand Up @@ -262,6 +263,21 @@ func (c *instanceCreateCmd) cmdRun(_ *cobra.Command, _ []string) error { //nolin
return
}
}

if c.Protection {
var value v3.UUID
var op *v3.Operation
value, err = v3.ParseUUID(instanceID.String())
if err != nil {
return
}
op, err = globalstate.EgoscaleV3Client.AddInstanceProtection(ctx, value)
if err != nil {
return
}
_, err = globalstate.EgoscaleV3Client.Wait(ctx, op, v3.OperationStateSuccess)

}
})
if err != nil {
return err
Expand Down
24 changes: 23 additions & 1 deletion cmd/instance_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"fmt"
"strings"

egoscale3 "github.com/exoscale/egoscale/v3"

"github.com/spf13/cobra"

"github.com/exoscale/cli/pkg/account"
Expand All @@ -25,6 +27,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 @@ -82,7 +85,7 @@ func (c *instanceUpdateCmd) cmdRun(cmd *cobra.Command, _ []string) error {
updatedRDNS = true
}

if updatedInstance || updatedRDNS {
if updatedInstance || updatedRDNS || cmd.Flags().Changed(mustCLICommandFlagName(c, &c.Protection)) {
decorateAsyncOperation(fmt.Sprintf("Updating instance %q...", c.Instance), func() {
if updatedInstance {
if err = globalstate.EgoscaleClient.UpdateInstance(ctx, c.Zone, instance); err != nil {
Expand All @@ -97,6 +100,25 @@ func (c *instanceUpdateCmd) cmdRun(cmd *cobra.Command, _ []string) error {
err = globalstate.EgoscaleClient.UpdateInstanceReverseDNS(ctx, c.Zone, *instance.ID, c.ReverseDNS)
}
}

if cmd.Flags().Changed(mustCLICommandFlagName(c, &c.Protection)) {
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
}
_, err = globalstate.EgoscaleV3Client.Wait(ctx, op, egoscale3.OperationStateSuccess)
}

})

if err != nil {
Expand Down

0 comments on commit 3dbd9b5

Please sign in to comment.