-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
196 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} | ||
} | ||
}) | ||
} | ||
} |