Skip to content

Commit

Permalink
get
Browse files Browse the repository at this point in the history
  • Loading branch information
9marco committed May 27, 2024
1 parent a2afcdc commit 6c415d0
Show file tree
Hide file tree
Showing 3 changed files with 196 additions and 1 deletion.
2 changes: 1 addition & 1 deletion get/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ type Cmd struct {
Releases releasesCmd `cmd:"" group:"deplo.io" name:"releases" aliases:"release" help:"Get deplo.io Releases. (Beta - requires access)"`
Configs configsCmd `cmd:"" group:"deplo.io" name:"configs" aliases:"config" help:"Get deplo.io Project Configuration. (Beta - requires access)"`
MySQL mySQLCmd `cmd:"" group:"storage.nine.ch" name:"mysql" help:"Get MySQL instances."`
Postgres postgresCmd `cmd:"" group:"storage.nine.ch" name:"postgres" help:"Get PostgreSQL instances."`
KeyValueStore keyValueStoreCmd `cmd:"" group:"storage.nine.ch" name:"keyvaluestore" aliases:"kvs" help:"Get KeyValueStore instances."`
All allCmd `cmd:"" name:"all" help:"Get project content"`
CloudVirtualMachine cloudVMCmd `cmd:"" group:"infrastructure.nine.ch" name:"cloudvirtualmachine" aliases:"cloudvm" help:"Get a CloudVM."`
opts []runtimeclient.ListOption
//Postgres postgresCmd `cmd:"" group:"storage.nine.ch" name:"postgres" help:"Get PostgreSQL instances."`
}

type output string
Expand Down
79 changes: 79 additions & 0 deletions get/postgres.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package get

import (
"context"
"fmt"
"io"
"text/tabwriter"

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

type postgresCmd struct {
Name string `arg:"" help:"Name of the PostgreSQL instance to get. If omitted all in the project will be listed." default:""`
PrintPassword bool `help:"Print the password of the PostgreSQL User. Requires name to be set." xor:"print"`
PrintUser bool `help:"Print the name of the PostgreSQL User. Requires name to be set." xor:"print"`

out io.Writer
}

func (cmd *postgresCmd) Run(ctx context.Context, client *api.Client, get *Cmd) error {
cmd.out = defaultOut(cmd.out)

if cmd.Name != "" && cmd.PrintUser {
fmt.Fprintln(cmd.out, storage.PostgresUser)
return nil
}

postgresList := &storage.PostgresList{}

if err := get.list(ctx, client, postgresList, matchName(cmd.Name)); err != nil {
return err
}

if len(postgresList.Items) == 0 {
printEmptyMessage(cmd.out, storage.PostgresKind, client.Project)
return nil
}

if cmd.Name != "" && cmd.PrintPassword {
return cmd.printPassword(ctx, client, &postgresList.Items[0])
}

switch get.Output {
case full:
return cmd.printPostgresInstances(postgresList.Items, get, true)
case noHeader:
return cmd.printPostgresInstances(postgresList.Items, get, false)
case yamlOut:
return format.PrettyPrintObjects(postgresList.GetItems(), format.PrintOpts{})
}

return nil
}

func (cmd *postgresCmd) printPostgresInstances(list []storage.Postgres, get *Cmd, header bool) error {
w := tabwriter.NewWriter(cmd.out, 0, 0, 4, ' ', 0)

if header {
get.writeHeader(w, "NAME", "FQDN", "LOCATION", "MACHINE TYPE")
}

for _, postgres := range list {
get.writeTabRow(w, postgres.Namespace, postgres.Name, postgres.Status.AtProvider.FQDN, string(postgres.Spec.ForProvider.Location), string(postgres.Spec.ForProvider.MachineType))
}

return w.Flush()
}

func (cmd *postgresCmd) printPassword(ctx context.Context, client *api.Client, postgres *storage.Postgres) error {
pw, err := getConnectionSecret(ctx, client, storage.PostgresUser, postgres)
if err != nil {
return err
}

fmt.Fprintln(cmd.out, pw)
return nil
}
116 changes: 116 additions & 0 deletions get/postgres_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package get

import (
"bytes"
"context"
"strings"
"testing"

infra "github.com/ninech/apis/infrastructure/v1alpha1"
storage "github.com/ninech/apis/storage/v1alpha1"
"github.com/ninech/nctl/api"
"github.com/ninech/nctl/internal/test"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
)

func TestPostgres(t *testing.T) {
tests := []struct {
name string
instances map[string]storage.PostgresParameters
get postgresCmd
// out defines the output format and will bet set to "full" if
// not given
out output
wantContain []string
wantErr bool
}{
{
name: "simple",
wantContain: []string{"no Postgres found"},
},
{
name: "single",
instances: map[string]storage.PostgresParameters{"test": {MachineType: infra.MachineType("nine-standard-1")}},
wantContain: []string{"nine-standard-1"},
},
{
name: "multiple",
instances: map[string]storage.PostgresParameters{
"test1": {MachineType: infra.MachineType("nine-standard-1")},
"test2": {MachineType: infra.MachineType("nine-standard-2")},
"test3": {MachineType: infra.MachineType("nine-standard-4")},
},
wantContain: []string{"nine-standard-1", "nine-standard-2", "test3"},
},
{
name: "get-by-name",
instances: map[string]storage.PostgresParameters{
"test1": {MachineType: infra.MachineType("nine-standard-1")},
"test2": {MachineType: infra.MachineType("nine-standard-2")},
},
get: postgresCmd{Name: "test1"},
wantContain: []string{"test1", "nine-standard-1"},
},
{
name: "show-password",
instances: map[string]storage.PostgresParameters{
"test1": {MachineType: infra.MachineType("nine-standard-1")},
"test2": {MachineType: infra.MachineType("nine-standard-2")},
},
get: postgresCmd{Name: "test2", PrintPassword: true},
wantContain: []string{"test2-topsecret"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
buf := &bytes.Buffer{}
tt.get.out = buf

scheme, err := api.NewScheme()
if err != nil {
t.Fatal(err)
}

objects := []client.Object{}
for name, instance := range tt.instances {
created := test.Postgres(name, "default", "nine-es34")
created.Spec.ForProvider = instance
objects = append(objects, created, &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: created.GetWriteConnectionSecretToReference().Name,
Namespace: created.GetWriteConnectionSecretToReference().Namespace,
},
Data: map[string][]byte{storage.PostgresUser: []byte(created.GetWriteConnectionSecretToReference().Name + "-topsecret")},
})
}

client := fake.NewClientBuilder().
WithScheme(scheme).
WithIndex(&storage.Postgres{}, "metadata.name", func(o client.Object) []string {
return []string{o.GetName()}
}).
WithObjects(objects...).Build()
apiClient := &api.Client{WithWatch: client, Project: "default"}
ctx := context.Background()

if tt.out == "" {
tt.out = full
}
if err := tt.get.Run(ctx, apiClient, &Cmd{Output: tt.out}); (err != nil) != tt.wantErr {
t.Errorf("postgresCmd.Run() error = %v, wantErr %v", err, tt.wantErr)
}
if tt.wantErr {
return
}

for _, substr := range tt.wantContain {
if !strings.Contains(buf.String(), substr) {
t.Errorf("postgresCmd.Run() did not contain %q, out = %q", tt.wantContain, buf.String())
}
}
})
}
}

0 comments on commit 6c415d0

Please sign in to comment.