Skip to content

Commit

Permalink
delete
Browse files Browse the repository at this point in the history
  • Loading branch information
9marco committed May 27, 2024
1 parent 091561c commit a2afcdc
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 1 deletion.
2 changes: 1 addition & 1 deletion delete/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ type Cmd struct {
Config configCmd `cmd:"" group:"deplo.io" name:"config" help:"Delete a deplo.io Project Configuration. (Beta - requires access)"`
Application applicationCmd `cmd:"" group:"deplo.io" name:"application" aliases:"app" help:"Delete a deplo.io Application. (Beta - requires access)"`
MySQL mySQLCmd `cmd:"" group:"storage.nine.ch" name:"mysql" help:"Delete a MySQL instance."`
Postgres postgresCmd `cmd:"" group:"storage.nine.ch" name:"postgres" help:"Delete a PostgreSQL instance."`
KeyValueStore keyValueStoreCmd `cmd:"" group:"storage.nine.ch" name:"keyvaluestore" aliases:"kvs" help:"Delete a KeyValueStore instance."`
CloudVirtualMachine cloudVMCmd `cmd:"" group:"infrastructure.nine.ch" name:"cloudvirtualmachine" aliases:"cloudvm" help:"Delete a CloudVM."`
//Postgres postgresCmd `cmd:"" group:"storage.nine.ch" name:"postgres" help:"Delete a PostgreSQL instance."`
}

// cleanupFunc is called after the resource has been deleted in order to do
Expand Down
32 changes: 32 additions & 0 deletions delete/postgres.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package delete

import (
"context"
"fmt"
"time"

"k8s.io/apimachinery/pkg/types"

storage "github.com/ninech/apis/storage/v1alpha1"
"github.com/ninech/nctl/api"
)

type postgresCmd struct {
Name string `arg:"" help:"Name of the PostgreSQL resource."`
Force bool `default:"false" help:"Do not ask for confirmation of deletion."`
Wait bool `default:"true" help:"Wait until PostgreSQL is fully deleted."`
WaitTimeout time.Duration `default:"300s" help:"Duration to wait for the deletion. Only relevant if wait is set."`
}

func (cmd *postgresCmd) Run(ctx context.Context, client *api.Client) error {
ctx, cancel := context.WithTimeout(ctx, cmd.WaitTimeout)
defer cancel()

postgres := &storage.Postgres{}
postgresName := types.NamespacedName{Name: cmd.Name, Namespace: client.Project}
if err := client.Get(ctx, postgresName, postgres); err != nil {
return fmt.Errorf("unable to get postgres %q: %w", postgres.Name, err)
}

return newDeleter(postgres, storage.PostgresKind).deleteResource(ctx, client, cmd.WaitTimeout, cmd.Wait, cmd.Force)
}
48 changes: 48 additions & 0 deletions delete/postgres_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package delete

import (
"context"
"testing"
"time"

"github.com/ninech/nctl/api"
"github.com/ninech/nctl/internal/test"
"k8s.io/apimachinery/pkg/api/errors"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
)

func TestPostgres(t *testing.T) {
cmd := postgresCmd{
Name: "test",
Force: true,
Wait: false,
WaitTimeout: time.Second,
}

postgres := test.Postgres("test", "default", "nine-es34")

scheme, err := api.NewScheme()
if err != nil {
t.Fatal(err)
}
client := fake.NewClientBuilder().WithScheme(scheme).Build()
apiClient := &api.Client{WithWatch: client, Project: "default"}
ctx := context.Background()

if err := apiClient.Create(ctx, postgres); err != nil {
t.Fatalf("postgres create error, got: %s", err)
}
if err := apiClient.Get(ctx, api.ObjectName(postgres), postgres); err != nil {
t.Fatalf("expected postgres to exist, got: %s", err)
}
if err := cmd.Run(ctx, apiClient); err != nil {
t.Fatal(err)
}
err = apiClient.Get(ctx, api.ObjectName(postgres), postgres)
if err == nil {
t.Fatalf("expected postgres to be deleted, but exists")
}
if !errors.IsNotFound(err) {
t.Fatalf("expected postgres to be deleted, got: %s", err.Error())
}
}

0 comments on commit a2afcdc

Please sign in to comment.