Skip to content

Commit

Permalink
[PLATFORM-565] feat: Add new resources commands (#138)
Browse files Browse the repository at this point in the history
* feat(resources): Add create command

* feat(resources): Add describe cmd

* feat(resources): Add list command

* fix(resources): Make subcommands available

* feat(resources): Add list types command

* feat(resources): Add remove command

* refactor(resources): Include test keys

* feat(resources): Add update command

* feat(resources): Add update command
  • Loading branch information
raulb authored May 6, 2021
1 parent 20af748 commit b3e51f1
Show file tree
Hide file tree
Showing 19 changed files with 1,678 additions and 8 deletions.
1 change: 1 addition & 0 deletions cmd/meroxa/root/api/api.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package api
2 changes: 1 addition & 1 deletion cmd/meroxa/root/deprecated/add/add_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func TestAddResourceFlags(t *testing.T) {
func TestAddResourceExecution(t *testing.T) {
ctx := context.Background()
ctrl := gomock.NewController(t)
client := mock.NewMockAddResourceClient(ctrl)
client := mock.NewMockCreateResourceClient(ctrl)
logger := log.NewTestLogger()

r := meroxa.CreateResourceInput{
Expand Down
152 changes: 152 additions & 0 deletions cmd/meroxa/root/resources/create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
/*
Copyright © 2021 Meroxa Inc
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package resources

import (
"context"
"encoding/json"

"github.com/meroxa/cli/log"

"github.com/meroxa/cli/cmd/meroxa/builder"
"github.com/meroxa/meroxa-go"
)

type createResourceClient interface {
CreateResource(ctx context.Context, resource *meroxa.CreateResourceInput) (*meroxa.Resource, error)
}

type Create struct {
client createResourceClient
logger log.Logger

args struct {
Name string
}

flags struct {
Type string `long:"type" short:"" usage:"resource type" required:"true"`
URL string `long:"url" short:"u" usage:"resource url" required:"true"`
Metadata string `long:"metadata" short:"m" usage:"resource metadata"`

// credentials
Username string `long:"username" short:"" usage:"username"`
Password string `long:"password" short:"" usage:"password"`
CaCert string `long:"ca-cert" short:"" usage:"trusted certificates for verifying resource"`
ClientCert string `long:"client-cert" short:"" usage:"client certificate for authenticating to the resource"`
ClientKey string `long:"client-key" short:"" usage:"client private key for authenticating to the resource"`
SSL bool `long:"ssl" short:"" usage:"use SSL"`
}
}

var (
_ builder.CommandWithDocs = (*Create)(nil)
_ builder.CommandWithArgs = (*Create)(nil)
_ builder.CommandWithFlags = (*Create)(nil)
_ builder.CommandWithClient = (*Create)(nil)
_ builder.CommandWithLogger = (*Create)(nil)
_ builder.CommandWithExecute = (*Create)(nil)
_ builder.CommandWithAliases = (*Create)(nil)
)

func (c *Create) Usage() string {
return "create [NAME] --type TYPE --url URL"
}

func (c *Create) Docs() builder.Docs {
return builder.Docs{
Short: "Add a resource to your Meroxa resource catalog",
Long: `Use the create command to add resources to your Meroxa resource catalog.`,
Example: `
meroxa resources create store --type postgres -u $DATABASE_URL --metadata '{"logical_replication":true}'
meroxa resources create datalake --type s3 -u "s3://$AWS_ACCESS_KEY_ID:$AWS_ACCESS_KEY_SECRET@us-east-1/meroxa-demos"
meroxa resources create warehouse --type redshift -u $REDSHIFT_URL
meroxa resources create slack --type url -u $WEBHOOK_URL
`,
}
}

func (c *Create) Client(client *meroxa.Client) {
c.client = client
}

func (c *Create) Logger(logger log.Logger) {
c.logger = logger
}

func (c *Create) Aliases() []string {
return []string{"add"}
}

func (c *Create) Flags() []builder.Flag {
return builder.BuildFlags(&c.flags)
}

func (c *Create) ParseArgs(args []string) error {
if len(args) > 0 {
c.args.Name = args[0]
}
return nil
}

func (c *Create) Execute(ctx context.Context) error {
input := meroxa.CreateResourceInput{
Type: c.flags.Type,
Name: c.args.Name,
URL: c.flags.URL,
Metadata: nil,
}

if c.hasCredentials() {
input.Credentials = &meroxa.Credentials{
Username: c.flags.Username,
Password: c.flags.Password,
CACert: c.flags.CaCert,
ClientCert: c.flags.ClientCert,
ClientCertKey: c.flags.ClientKey,
UseSSL: c.flags.SSL,
}
}

if c.flags.Metadata != "" {
err := json.Unmarshal([]byte(c.flags.Metadata), &input.Metadata)
if err != nil {
return err
}
}

c.logger.Infof(ctx, "Creating %q resource...", input.Type)

res, err := c.client.CreateResource(ctx, &input)
if err != nil {
return err
}

c.logger.Infof(ctx, "%q resource successfully created!", res.Name)
c.logger.JSON(ctx, res)

return nil
}

func (c *Create) hasCredentials() bool {
return c.flags.Username != "" ||
c.flags.Password != "" ||
c.flags.CaCert != "" ||
c.flags.ClientCert != "" ||
c.flags.ClientKey != "" ||
c.flags.SSL
}
132 changes: 132 additions & 0 deletions cmd/meroxa/root/resources/create_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
package resources

import (
"context"
"encoding/json"
"fmt"
"reflect"
"testing"

"github.com/golang/mock/gomock"
"github.com/meroxa/cli/cmd/meroxa/builder"
"github.com/meroxa/cli/log"
mock "github.com/meroxa/cli/mock-cmd"
"github.com/meroxa/cli/utils"
"github.com/meroxa/meroxa-go"
)

func TestCreateResourceArgs(t *testing.T) {
tests := []struct {
args []string
err error
name string
}{
{nil, nil, ""},
{[]string{"my-resource"}, nil, "my-resource"},
}

for _, tt := range tests {
c := &Create{}
err := c.ParseArgs(tt.args)

if tt.err != err {
t.Fatalf("expected \"%s\" got \"%s\"", tt.err, err)
}

if tt.name != c.args.Name {
t.Fatalf("expected \"%s\" got \"%s\"", tt.name, c.args.Name)
}
}
}

func TestCreateResourceFlags(t *testing.T) {
expectedFlags := []struct {
name string
required bool
shorthand string
}{
{name: "type", required: true, shorthand: ""},
{name: "url", required: true, shorthand: "u"},
{name: "username", required: false, shorthand: ""},
{name: "password", required: false, shorthand: ""},
{name: "ca-cert", required: false, shorthand: ""},
{name: "client-cert", required: false, shorthand: ""},
{name: "client-key", required: false, shorthand: ""},
{name: "ssl", required: false, shorthand: ""},
{name: "metadata", required: false, shorthand: "m"},
}

c := builder.BuildCobraCommand(&Create{})

for _, f := range expectedFlags {
cf := c.Flags().Lookup(f.name)
if cf == nil {
t.Fatalf("expected flag \"%s\" to be present", f.name)
}

if f.shorthand != cf.Shorthand {
t.Fatalf("expected shorthand \"%s\" got \"%s\" for flag \"%s\"", f.shorthand, cf.Shorthand, f.name)
}

if f.required && !utils.IsFlagRequired(cf) {
t.Fatalf("expected flag \"%s\" to be required", f.name)
}
}
}

func TestCreateResourceExecution(t *testing.T) {
ctx := context.Background()
ctrl := gomock.NewController(t)
client := mock.NewMockCreateResourceClient(ctrl)
logger := log.NewTestLogger()

r := meroxa.CreateResourceInput{
Type: "postgres",
Name: "",
URL: "https://foo.url",
Credentials: nil,
Metadata: nil,
}

cr := utils.GenerateResource()
client.
EXPECT().
CreateResource(
ctx,
&r,
).
Return(&cr, nil)

c := &Create{
client: client,
logger: logger,
}
c.args.Name = r.Name
c.flags.Type = r.Type
c.flags.URL = r.URL

err := c.Execute(ctx)
if err != nil {
t.Fatalf("not expected error, got %q", err.Error())
}

gotLeveledOutput := logger.LeveledOutput()
wantLeveledOutput := fmt.Sprintf(`Creating %q resource...
%q resource successfully created!
`, cr.Type, cr.Name)

if gotLeveledOutput != wantLeveledOutput {
t.Fatalf("expected output:\n%s\ngot:\n%s", wantLeveledOutput, gotLeveledOutput)
}

gotJSONOutput := logger.JSONOutput()
var gotResource meroxa.Resource
err = json.Unmarshal([]byte(gotJSONOutput), &gotResource)
if err != nil {
t.Fatalf("not expected error, got %q", err.Error())
}

if !reflect.DeepEqual(gotResource, cr) {
t.Fatalf("expected \"%v\", got \"%v\"", cr, gotResource)
}
}
87 changes: 87 additions & 0 deletions cmd/meroxa/root/resources/describe.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
Copyright © 2021 Meroxa Inc
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package resources

import (
"context"
"errors"

"github.com/meroxa/cli/cmd/meroxa/builder"
"github.com/meroxa/cli/log"
"github.com/meroxa/cli/utils"
"github.com/meroxa/meroxa-go"
)

var (
_ builder.CommandWithDocs = (*Describe)(nil)
_ builder.CommandWithArgs = (*Describe)(nil)
_ builder.CommandWithClient = (*Describe)(nil)
_ builder.CommandWithLogger = (*Describe)(nil)
_ builder.CommandWithExecute = (*Describe)(nil)
)

type describeResourceClient interface {
GetResourceByName(ctx context.Context, name string) (*meroxa.Resource, error)
}

type Describe struct {
client describeResourceClient
logger log.Logger

args struct {
Name string
}
}

func (d *Describe) Usage() string {
return "describe [NAME]"
}

func (d *Describe) Docs() builder.Docs {
return builder.Docs{
Short: "Describe resource",
}
}

func (d *Describe) Execute(ctx context.Context) error {
resource, err := d.client.GetResourceByName(ctx, d.args.Name)
if err != nil {
return err
}

d.logger.Info(ctx, utils.ResourcesTable([]*meroxa.Resource{resource}))
d.logger.JSON(ctx, resource)

return nil
}

func (d *Describe) Client(client *meroxa.Client) {
d.client = client
}

func (d *Describe) Logger(logger log.Logger) {
d.logger = logger
}

func (d *Describe) ParseArgs(args []string) error {
if len(args) < 1 {
return errors.New("requires resource name")
}

d.args.Name = args[0]
return nil
}
Loading

0 comments on commit b3e51f1

Please sign in to comment.