Skip to content

Commit

Permalink
feat(resources): Add update resources cmd (#63)
Browse files Browse the repository at this point in the history
* feat(resources): Add update resources cmd

https://meroxa.atlassian.net/browse/PLATFORM-13

* Update Kind to Type

* Remove fetching resource beforehand

* Update to latest meroxa-go

* Update credentials too

* Use latest of meroxa-go
  • Loading branch information
raulb authored Mar 11, 2021
1 parent 92932c5 commit ba48451
Show file tree
Hide file tree
Showing 53 changed files with 242 additions and 220 deletions.
4 changes: 2 additions & 2 deletions cmd/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (

const clientTimeOut = 5 * time.Second

var resName, resType, resURL, resCredentials, resMetadata string
var resName, resType string

var addCmd = &cobra.Command{
Use: "add",
Expand Down Expand Up @@ -58,7 +58,7 @@ var addResourceCmd = &cobra.Command{
return err
}

r := meroxa.Resource{
r := meroxa.CreateResourceInput{
Type: resType,
Name: resName,
URL: resURL,
Expand Down
6 changes: 5 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ package cmd

import (
"fmt"
"github.com/spf13/pflag"
"os"
"strings"

"github.com/spf13/pflag"

"github.com/spf13/cobra"

homedir "github.com/mitchellh/go-homedir"
Expand All @@ -41,6 +42,9 @@ var (
cfgFile string
source string
destination string
resURL string
resMetadata string
resCredentials string
flagRootOutputJSON bool
cfg *viper.Viper
)
Expand Down
86 changes: 86 additions & 0 deletions cmd/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ package cmd

import (
"context"
"encoding/json"
"errors"
"fmt"
"time"

"github.com/meroxa/meroxa-go"

"github.com/meroxa/cli/display"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -65,6 +68,82 @@ var updateConnectorCmd = &cobra.Command{
},
}

var updateResourceCmd = &cobra.Command{
Use: "resource <resource-name>",
Short: "Update a resource",
Long: `Use the update command to update various Meroxa resources.`,
Aliases: []string{"resources"},
// TODO: Change the design so a new name for the resource could be set
// meroxa update resource <old-resource-name> --name <new-resource-name>
PreRunE: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 || (resURL == "" && resMetadata == "" && resCredentials == "") {
return errors.New("requires a resource name and either `--metadata`, `--url` or `--credentials` to update the resource \n\nUsage:\n meroxa update resource <resource-name> [--url <url> | --metadata <metadata> | --credentials <credentials>]")
}

return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
// Resource Name
resName = args[0]
c, err := client()

if err != nil {
return err
}
ctx := context.Background()
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()

var res meroxa.UpdateResourceInput

// If url was provided, update it
if resURL != "" {
res.URL = resURL
}

// TODO: Figure out best way to handle creds and metadata
// Get credentials (expect a JSON string)
if resCredentials != "" {
var creds meroxa.Credentials
err = json.Unmarshal([]byte(resCredentials), &creds)
if err != nil {
return err
}

res.Credentials = &creds
}

// If metadata was provided, update it
if resMetadata != "" {
var metadata map[string]string
err = json.Unmarshal([]byte(resMetadata), &metadata)
if err != nil {
return err
}

res.Metadata = metadata
}

// call meroxa-go to update resource
if !flagRootOutputJSON {
fmt.Printf("Updating %s resource...\n", resName)
}

resource, err := c.UpdateResource(ctx, resName, res)
if err != nil {
return err
}

if flagRootOutputJSON {
display.JSONPrint(resource)
} else {
fmt.Printf("Resource %s successfully updated!\n", resName)
}

return nil
},
}

var updatePipelineCmd = &cobra.Command{
Use: "pipeline <name> --state <pause|resume|restart>",
Aliases: []string{"pipelines"},
Expand Down Expand Up @@ -130,4 +209,11 @@ func init() {
updateCmd.AddCommand(updatePipelineCmd)
updatePipelineCmd.Flags().StringVarP(&state, "state", "", "", "pipeline state")
updatePipelineCmd.MarkFlagRequired("state")

updateCmd.AddCommand(updateResourceCmd)
RootCmd.AddCommand(updateCmd)

updateResourceCmd.Flags().StringVarP(&resURL, "url", "u", "", "resource url")
updateResourceCmd.Flags().StringVarP(&resMetadata, "metadata", "m", "", "resource metadata")
updateResourceCmd.Flags().StringVarP(&resCredentials, "credentials", "", "", "resource credentials")
}
4 changes: 2 additions & 2 deletions display/display.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,17 +70,17 @@ func PrintResourcesTable(resources []*meroxa.Resource) {
table.Header = &simpletable.Header{
Cells: []*simpletable.Cell{
{Align: simpletable.AlignCenter, Text: "ID"},
{Align: simpletable.AlignCenter, Text: "TYPE"},
{Align: simpletable.AlignCenter, Text: "NAME"},
{Align: simpletable.AlignCenter, Text: "TYPE"},
{Align: simpletable.AlignCenter, Text: "URL"},
},
}

for _, res := range resources {
r := []*simpletable.Cell{
{Align: simpletable.AlignRight, Text: fmt.Sprintf("%d", res.ID)},
{Text: res.Type},
{Text: res.Name},
{Text: res.Type},
{Text: res.URL},
}

Expand Down
3 changes: 2 additions & 1 deletion docs/commands/meroxa.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ meroxa list resource-types
* [meroxa open](meroxa_open.md) - Open in a web browser
* [meroxa remove](meroxa_remove.md) - Remove a component
* [meroxa update](meroxa_update.md) - Update a component
* [meroxa update](meroxa_update.md) - Update a component
* [meroxa version](meroxa_version.md) - Display the Meroxa CLI version

###### Auto generated by spf13/cobra on 8-Mar-2021
###### Auto generated by spf13/cobra on 10-Mar-2021
2 changes: 1 addition & 1 deletion docs/commands/meroxa_add.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ Add a resource to your Meroxa resource catalog
* [meroxa](meroxa.md) - The Meroxa CLI
* [meroxa add resource](meroxa_add_resource.md) - Add a resource to your Meroxa resource catalog

###### Auto generated by spf13/cobra on 8-Mar-2021
###### Auto generated by spf13/cobra on 10-Mar-2021
2 changes: 1 addition & 1 deletion docs/commands/meroxa_add_resource.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,4 @@ meroxa add resource slack --type url -u $WEBHOOK_URL

* [meroxa add](meroxa_add.md) - Add a resource to your Meroxa resource catalog

###### Auto generated by spf13/cobra on 8-Mar-2021
###### Auto generated by spf13/cobra on 10-Mar-2021
2 changes: 1 addition & 1 deletion docs/commands/meroxa_api.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@ meroxa api POST /v1/endpoints '{"protocol": "HTTP", "stream": "resource-2-499379

* [meroxa](meroxa.md) - The Meroxa CLI

###### Auto generated by spf13/cobra on 8-Mar-2021
###### Auto generated by spf13/cobra on 10-Mar-2021
2 changes: 1 addition & 1 deletion docs/commands/meroxa_billing.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ meroxa billing [flags]

* [meroxa](meroxa.md) - The Meroxa CLI

###### Auto generated by spf13/cobra on 8-Mar-2021
###### Auto generated by spf13/cobra on 10-Mar-2021
2 changes: 1 addition & 1 deletion docs/commands/meroxa_completion.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,4 @@ meroxa completion [bash|zsh|fish|powershell]

* [meroxa](meroxa.md) - The Meroxa CLI

###### Auto generated by spf13/cobra on 8-Mar-2021
###### Auto generated by spf13/cobra on 10-Mar-2021
2 changes: 1 addition & 1 deletion docs/commands/meroxa_connect.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,4 @@ meroxa connect --from <resource-name> --to <resource-name> [flags]

* [meroxa](meroxa.md) - The Meroxa CLI

###### Auto generated by spf13/cobra on 8-Mar-2021
###### Auto generated by spf13/cobra on 10-Mar-2021
2 changes: 1 addition & 1 deletion docs/commands/meroxa_create.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@ including connectors.
* [meroxa create endpoint](meroxa_create_endpoint.md) - Create an endpoint
* [meroxa create pipeline](meroxa_create_pipeline.md) - Create a pipeline

###### Auto generated by spf13/cobra on 8-Mar-2021
###### Auto generated by spf13/cobra on 10-Mar-2021
2 changes: 1 addition & 1 deletion docs/commands/meroxa_create_connector.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,4 @@ meroxa create connector [<custom-connector-name>] --to pg2redshift --input order

* [meroxa create](meroxa_create.md) - Create Meroxa pipeline components

###### Auto generated by spf13/cobra on 8-Mar-2021
###### Auto generated by spf13/cobra on 10-Mar-2021
2 changes: 1 addition & 1 deletion docs/commands/meroxa_create_endpoint.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,4 @@ meroxa create endpoint my-endpoint --protocol http --stream my-stream

* [meroxa create](meroxa_create.md) - Create Meroxa pipeline components

###### Auto generated by spf13/cobra on 8-Mar-2021
###### Auto generated by spf13/cobra on 10-Mar-2021
2 changes: 1 addition & 1 deletion docs/commands/meroxa_create_pipeline.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ meroxa create pipeline <name> [flags]

* [meroxa create](meroxa_create.md) - Create Meroxa pipeline components

###### Auto generated by spf13/cobra on 8-Mar-2021
###### Auto generated by spf13/cobra on 10-Mar-2021
29 changes: 0 additions & 29 deletions docs/commands/meroxa_delete.md

This file was deleted.

25 changes: 0 additions & 25 deletions docs/commands/meroxa_delete_connector.md

This file was deleted.

25 changes: 0 additions & 25 deletions docs/commands/meroxa_delete_pipeline.md

This file was deleted.

25 changes: 0 additions & 25 deletions docs/commands/meroxa_delete_resource.md

This file was deleted.

2 changes: 1 addition & 1 deletion docs/commands/meroxa_describe.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ Describe a component of the Meroxa data platform, including resources and connec
* [meroxa describe endpoint](meroxa_describe_endpoint.md) - Describe Endpoint
* [meroxa describe resource](meroxa_describe_resource.md) - Describe resource

###### Auto generated by spf13/cobra on 8-Mar-2021
###### Auto generated by spf13/cobra on 10-Mar-2021
2 changes: 1 addition & 1 deletion docs/commands/meroxa_describe_connector.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ meroxa describe connector [name] [flags]

* [meroxa describe](meroxa_describe.md) - Describe a component

###### Auto generated by spf13/cobra on 8-Mar-2021
###### Auto generated by spf13/cobra on 10-Mar-2021
2 changes: 1 addition & 1 deletion docs/commands/meroxa_describe_endpoint.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ meroxa describe endpoint <name> [flags]

* [meroxa describe](meroxa_describe.md) - Describe a component

###### Auto generated by spf13/cobra on 8-Mar-2021
###### Auto generated by spf13/cobra on 10-Mar-2021
2 changes: 1 addition & 1 deletion docs/commands/meroxa_describe_resource.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ meroxa describe resource <name> [flags]

* [meroxa describe](meroxa_describe.md) - Describe a component

###### Auto generated by spf13/cobra on 8-Mar-2021
###### Auto generated by spf13/cobra on 10-Mar-2021
2 changes: 1 addition & 1 deletion docs/commands/meroxa_list.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@ List the components of the Meroxa platform, including pipelines,
* [meroxa list resources](meroxa_list_resources.md) - List resources
* [meroxa list transforms](meroxa_list_transforms.md) - List transforms

###### Auto generated by spf13/cobra on 8-Mar-2021
###### Auto generated by spf13/cobra on 10-Mar-2021
2 changes: 1 addition & 1 deletion docs/commands/meroxa_list_connectors.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ meroxa list connectors [flags]

* [meroxa list](meroxa_list.md) - List components

###### Auto generated by spf13/cobra on 8-Mar-2021
###### Auto generated by spf13/cobra on 10-Mar-2021
2 changes: 1 addition & 1 deletion docs/commands/meroxa_list_endpoint.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ meroxa list endpoint [flags]

* [meroxa list](meroxa_list.md) - List components

###### Auto generated by spf13/cobra on 8-Mar-2021
###### Auto generated by spf13/cobra on 10-Mar-2021
2 changes: 1 addition & 1 deletion docs/commands/meroxa_list_pipelines.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ meroxa list pipelines [flags]

* [meroxa list](meroxa_list.md) - List components

###### Auto generated by spf13/cobra on 8-Mar-2021
###### Auto generated by spf13/cobra on 10-Mar-2021
Loading

0 comments on commit ba48451

Please sign in to comment.