Skip to content

Commit

Permalink
Add Civo v2 API (#20)
Browse files Browse the repository at this point in the history
* Add Civo v2 API written by us.

* Reorganize function parameters to have the context be the first value.

* Cleanup of pointer names.

* Remove post body objects.

* Replace functions with ours.

* Remove no longer needed parameters.

* Ensure conflicts are pretty printed.

* Reorganize code.

* Document functions.

* Make linter happy.

* Ensure context is passed.

* Add unit tests.

* Remove indirection for easier testing.

* Add finishing touches.

* Disable dupl rule.

* Add timeouts in the client.

* Read additional data from the API to make orphaned choices.

* Remove checking against the client since we create a custom one now.

* Redo found boolean inside for-within-for.
  • Loading branch information
patrickdappollonio authored Oct 7, 2024
1 parent 991df25 commit 0119c57
Show file tree
Hide file tree
Showing 39 changed files with 2,805 additions and 3,435 deletions.
1 change: 0 additions & 1 deletion .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ linters:
- grouper
- importas
- inamedparam
- interfacebloat
- ireturn
- loggercheck
- makezero
Expand Down
43 changes: 10 additions & 33 deletions cmd/civo.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
"context"
"errors"
"fmt"
"io"
Expand Down Expand Up @@ -29,18 +30,14 @@ func getCivoCommand() *cobra.Command {
Long: `clean civo resources`,
RunE: func(cmd *cobra.Command, _ []string) error {
opts.quiet = cmd.Flags().Lookup("quiet").Value.String() == "true"
return runCivo(cmd.OutOrStderr(), opts, os.Getenv("CIVO_TOKEN"))
return runCivo(cmd.Context(), cmd.OutOrStderr(), opts, os.Getenv("CIVO_TOKEN"))
},
}

civoCmd.Flags().BoolVar(&opts.nuke, "nuke", false, "required to confirm deletion of resources")
civoCmd.Flags().StringVar(&opts.region, "region", "", "the civo region to clean")
civoCmd.Flags().StringVar(&opts.nameFilter, "name-contains", "", "if set, only resources with a name containing this string will be selected")
civoCmd.Flags().BoolVar(&opts.onlyOrphans, "orphans-only", false, "only delete orphaned resources (only volumes, object store credentials, SSH keys, networks and firewalls)")

// On orphaned resources, we don't want to filter by name since the
// filter is already that's just for the resources we want to delete
civoCmd.MarkFlagsMutuallyExclusive("name-contains", "orphans-only")
civoCmd.Flags().BoolVar(&opts.onlyOrphans, "orphans-only", false, "only delete orphaned resources (only load balancers, volumes, object store credentials, SSH keys, networks and firewalls)")

if err := civoCmd.MarkFlagRequired("region"); err != nil {
log.Fatal(err)
Expand All @@ -49,7 +46,7 @@ func getCivoCommand() *cobra.Command {
return civoCmd
}

func runCivo(output io.Writer, opts civoOptions, token string) error {
func runCivo(ctx context.Context, output io.Writer, opts civoOptions, token string) error {
if token == "" {
return errors.New("required environment variable $CIVO_TOKEN not found: get one at https://dashboard.civo.com/security")
}
Expand All @@ -74,38 +71,18 @@ func runCivo(output io.Writer, opts civoOptions, token string) error {
}

if opts.onlyOrphans {
if err := client.NukeOrphanedResources(); err != nil {
if err := client.NukeOrphanedResources(ctx); err != nil {
return fmt.Errorf("unable to nuke orphaned resources: %w", err)
}
return nil
}

if err := client.NukeKubernetesClusters(); err != nil {
return fmt.Errorf("unable to cleanup Kubernetes clusters: %w", err)
}

if err := client.NukeObjectStores(); err != nil {
return fmt.Errorf("unable to cleanup object stores: %w", err)
}

if err := client.NukeObjectStoreCredentials(); err != nil {
return fmt.Errorf("unable to cleanup object store credentials: %w", err)
}

if err := client.NukeVolumes(); err != nil {
return fmt.Errorf("unable to cleanup volumes: %w", err)
}

if err := client.NukeNetworks(); err != nil {
return fmt.Errorf("unable to cleanup networks: %w", err)
}

if err := client.NukeSSHKeys(); err != nil {
return fmt.Errorf("unable to cleanup SSH keys: %w", err)
}
if err := client.NukeEverything(ctx); err != nil {
if opts.nuke {
return fmt.Errorf("unable to nuke resources: %w", err)
}

if err := client.NukeFirewalls(); err != nil {
return fmt.Errorf("unable to cleanup firewalls: %w", err)
return fmt.Errorf("unable to process resources: %w", err)
}

return nil
Expand Down
17 changes: 1 addition & 16 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,44 +4,29 @@ go 1.23

require (
github.com/aws/aws-sdk-go v1.55.5
github.com/civo/civogo v0.3.65
github.com/digitalocean/godo v1.119.0
github.com/fatih/color v1.17.0
github.com/sirupsen/logrus v1.9.3
github.com/spf13/cobra v1.8.0
)

require (
github.com/go-logr/logr v1.2.3 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/google/go-querystring v1.1.0 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
github.com/hashicorp/go-retryablehttp v0.7.7 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/stretchr/testify v1.9.0 // indirect
golang.org/x/mod v0.17.0 // indirect
golang.org/x/net v0.26.0 // indirect
golang.org/x/oauth2 v0.0.0-20220411215720-9780585627b5 // indirect
golang.org/x/sys v0.21.0 // indirect
golang.org/x/text v0.16.0 // indirect
golang.org/x/time v0.0.0-20220922220347-f3bd1da661af // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.33.0 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
k8s.io/api v0.27.1 // indirect
k8s.io/apimachinery v0.27.1 // indirect
k8s.io/klog/v2 v2.90.1 // indirect
k8s.io/utils v0.0.0-20230209194617-a36077c30491 // indirect
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect
)
Loading

0 comments on commit 0119c57

Please sign in to comment.