Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cloud): Add cloud purge subcommand #1597

Open
wants to merge 4 commits into
base: staging
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 23 additions & 15 deletions internal/cli/kraft/cloud/certificate/remove/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@ import (
)

type RemoveOptions struct {
Output string `long:"output" short:"o" usage:"Set output format. Options: table,yaml,json,list,raw" default:"table"`
All bool `long:"all" usage:"Remove all certificates"`

metro string
token string
Output string `long:"output" short:"o" usage:"Set output format. Options: table,yaml,json,list,raw" default:"table"`
All bool `long:"all" usage:"Remove all certificates"`
Metro string `noattribute:"true"`
Token string `noattribute:"true"`
Auth *config.AuthConfig `noattribute:"true"`
Client kraftcloud.KraftCloud `noattribute:"true"`
}

// Remove a KraftCloud certificate.
Expand Down Expand Up @@ -72,7 +73,7 @@ func (opts *RemoveOptions) Pre(cmd *cobra.Command, args []string) error {
return fmt.Errorf("either specify a certificate name or UUID, or use the --all flag")
}

err := utils.PopulateMetroToken(cmd, &opts.metro, &opts.token)
err := utils.PopulateMetroToken(cmd, &opts.Metro, &opts.Token)
if err != nil {
return fmt.Errorf("could not populate metro and token: %w", err)
}
Expand All @@ -85,17 +86,23 @@ func (opts *RemoveOptions) Pre(cmd *cobra.Command, args []string) error {
}

func (opts *RemoveOptions) Run(ctx context.Context, args []string) error {
auth, err := config.GetKraftCloudAuthConfig(ctx, opts.token)
if err != nil {
return fmt.Errorf("could not retrieve credentials: %w", err)
var err error

if opts.Auth == nil {
opts.Auth, err = config.GetKraftCloudAuthConfig(ctx, opts.Token)
if err != nil {
return fmt.Errorf("could not retrieve credentials: %w", err)
}
}

client := kraftcloud.NewCertificatesClient(
kraftcloud.WithToken(config.GetKraftCloudTokenAuthConfig(*auth)),
)
if opts.Client == nil {
opts.Client = kraftcloud.NewClient(
kraftcloud.WithToken(config.GetKraftCloudTokenAuthConfig(*opts.Auth)),
)
}

if opts.All {
certListResp, err := client.WithMetro(opts.metro).List(ctx)
certListResp, err := opts.Client.Certificates().WithMetro(opts.Metro).List(ctx)
if err != nil {
return fmt.Errorf("could not list certificates: %w", err)
}
Expand All @@ -104,6 +111,7 @@ func (opts *RemoveOptions) Run(ctx context.Context, args []string) error {
return fmt.Errorf("could not list certificates: %w", err)
}
if len(certList) == 0 {
log.G(ctx).Info("no certificates found")
return nil
}

Expand All @@ -114,7 +122,7 @@ func (opts *RemoveOptions) Run(ctx context.Context, args []string) error {
uuids = append(uuids, certItem.UUID)
}

delResp, err := client.WithMetro(opts.metro).Delete(ctx, uuids...)
delResp, err := opts.Client.Certificates().WithMetro(opts.Metro).Delete(ctx, uuids...)
if err != nil {
return fmt.Errorf("removing %d certificate(s): %w", len(uuids), err)
}
Expand All @@ -126,7 +134,7 @@ func (opts *RemoveOptions) Run(ctx context.Context, args []string) error {

log.G(ctx).Infof("removing %d certificate(s)", len(args))

delResp, err := client.WithMetro(opts.metro).Delete(ctx, args...)
delResp, err := opts.Client.Certificates().WithMetro(opts.Metro).Delete(ctx, args...)
if err != nil {
return fmt.Errorf("removing %d certificate(s): %w", len(args), err)
}
Expand Down
2 changes: 2 additions & 0 deletions internal/cli/kraft/cloud/cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"kraftkit.sh/internal/cli/kraft/cloud/image"
"kraftkit.sh/internal/cli/kraft/cloud/instance"
"kraftkit.sh/internal/cli/kraft/cloud/metro"
"kraftkit.sh/internal/cli/kraft/cloud/purge"
"kraftkit.sh/internal/cli/kraft/cloud/quota"
"kraftkit.sh/internal/cli/kraft/cloud/scale"
"kraftkit.sh/internal/cli/kraft/cloud/service"
Expand Down Expand Up @@ -88,6 +89,7 @@ func NewCmd() *cobra.Command {
cmd.AddCommand(deploy.NewCmd())
cmd.AddCommand(quota.NewCmd())
cmd.AddCommand(tunnel.NewCmd())
cmd.AddCommand(purge.NewCmd())

cmd.AddGroup(&cobra.Group{ID: "kraftcloud-image", Title: "IMAGE COMMANDS"})
cmd.AddCommand(image.NewCmd())
Expand Down
26 changes: 15 additions & 11 deletions internal/cli/kraft/cloud/image/remove/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"github.com/spf13/cobra"

kraftcloud "sdk.kraft.cloud"
kcimages "sdk.kraft.cloud/images"

"kraftkit.sh/cmdfactory"
"kraftkit.sh/config"
Expand All @@ -23,11 +22,11 @@ import (
)

type RemoveOptions struct {
All bool `long:"all" usage:"Remove all images"`
Auth *config.AuthConfig `noattribute:"true"`
Client kcimages.ImagesService `noattribute:"true"`
Metro string `noattribute:"true"`
Token string `noattribute:"true"`
All bool `long:"all" usage:"Remove all images"`
Auth *config.AuthConfig `noattribute:"true"`
Client kraftcloud.KraftCloud `noattribute:"true"`
Metro string `noattribute:"true"`
Token string `noattribute:"true"`
}

func NewCmd() *cobra.Command {
Expand Down Expand Up @@ -82,7 +81,8 @@ func (opts *RemoveOptions) Pre(cmd *cobra.Command, args []string) error {
return nil
}

func (opts *RemoveOptions) Run(ctx context.Context, args []string) error {
// Remove an image from your account.
func Remove(ctx context.Context, opts *RemoveOptions, args ...string) error {
var err error

if opts.Auth == nil {
Expand All @@ -93,13 +93,13 @@ func (opts *RemoveOptions) Run(ctx context.Context, args []string) error {
}

if opts.Client == nil {
opts.Client = kraftcloud.NewImagesClient(
opts.Client = kraftcloud.NewClient(
kraftcloud.WithToken(config.GetKraftCloudTokenAuthConfig(*opts.Auth)),
)
}

if opts.All {
imgListResp, err := opts.Client.WithMetro(opts.Metro).List(ctx)
imgListResp, err := opts.Client.Images().WithMetro(opts.Metro).List(ctx)
if err != nil {
return fmt.Errorf("listing images: %w", err)
}
Expand All @@ -119,7 +119,7 @@ func (opts *RemoveOptions) Run(ctx context.Context, args []string) error {

log.G(ctx).Infof("removing %s", image.Digest)

if err := opts.Client.WithMetro(opts.Metro).DeleteByName(ctx, image.Digest); err != nil {
if err := opts.Client.Images().WithMetro(opts.Metro).DeleteByName(ctx, image.Digest); err != nil {
log.G(ctx).Warnf("could not delete image: %s", err.Error())

if strings.Contains(err.Error(), "NOT_FOUND") {
Expand All @@ -136,7 +136,7 @@ func (opts *RemoveOptions) Run(ctx context.Context, args []string) error {
for _, arg := range args {
log.G(ctx).Infof("removing %s", arg)

if err := opts.Client.WithMetro(opts.Metro).DeleteByName(ctx, arg); err != nil {
if err := opts.Client.Images().WithMetro(opts.Metro).DeleteByName(ctx, arg); err != nil {
if strings.Contains(err.Error(), "NOT_FOUND") {
log.G(ctx).Warnf("%s not found. This is expected if you have already removed it.", arg)
} else {
Expand All @@ -147,3 +147,7 @@ func (opts *RemoveOptions) Run(ctx context.Context, args []string) error {

return err
}

func (opts *RemoveOptions) Run(ctx context.Context, args []string) error {
return Remove(ctx, opts, args...)
}
Loading
Loading