Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add datasources CLI stubs #5020

Merged
merged 9 commits into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 112 additions & 0 deletions cmd/cli/app/datasource/create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package datasource

Check failure on line 1 in cmd/cli/app/datasource/create.go

View workflow job for this annotation

GitHub Actions / lint / Run golangci-lint

package-comments: should have a package comment (revive)

import (
"context"
"fmt"
"strings"

"github.com/spf13/cobra"
"github.com/spf13/viper"
"google.golang.org/grpc"

"github.com/mindersec/minder/cmd/cli/app"
"github.com/mindersec/minder/internal/util"
"github.com/mindersec/minder/internal/util/cli"
"github.com/mindersec/minder/internal/util/cli/table"
"github.com/mindersec/minder/internal/util/cli/table/layouts"
minderv1 "github.com/mindersec/minder/pkg/api/protobuf/go/minder/v1"
)

var createCmd = &cobra.Command{
Use: "create",
Short: "Create a new data source",
Long: `The datasource create subcommand lets you create a new data source within Minder.`,
RunE: cli.GRPCClientWrapRunE(createCommand),
}

// createCommand is the datasource create subcommand
func createCommand(ctx context.Context, cmd *cobra.Command, _ []string, conn *grpc.ClientConn) error {
client := minderv1.NewDataSourceServiceClient(conn)

project := viper.GetString("project")
format := viper.GetString("output")
//name := viper.GetString("name")
//endpoint := viper.GetString("endpoint")
//version := viper.GetString("version")

// No longer print usage on returned error, since we've parsed our inputs
cmd.SilenceUsage = true

// Create the REST data source definition
// restDef := &minderv1.RestDataSource{
// Def: map[string]*minderv1.RestDataSource_Def{
// "default": {
// Endpoint: endpoint,
// },
// },
// }

// Create the data source
// dataSource := &minderv1.DataSource{
// Version: version,
// Type: "data-source",
// Name: name,
// Context: &minderv1.ContextV2{
// ProjectId: project,
// },
// Driver: &minderv1.DataSource_Rest{
// Rest: restDef,
// },
// }

resp, err := client.CreateDataSource(ctx, &minderv1.CreateDataSourceRequest{
Context: &minderv1.ContextV2{
ProjectId: project,
},
//DataSource: dataSource,
})
if err != nil {
return cli.MessageAndError("Failed to create data source", err)
}

switch format {
case app.JSON:
out, err := util.GetJsonFromProto(resp)
if err != nil {
return cli.MessageAndError("Error getting json from proto", err)
}
cmd.Println(out)
case app.YAML:
out, err := util.GetYamlFromProto(resp)
if err != nil {
return cli.MessageAndError("Error getting yaml from proto", err)
}
cmd.Println(out)
case app.Table:
t := table.New(table.Simple, layouts.Default, []string{"ID", "Name", "Type"})
ds := resp.GetDataSource()
t.AddRow(ds.Id, ds.Name, getDataSourceType(ds))
t.Render()
default:
return fmt.Errorf("unsupported output format: %s", format)
}

return nil
}

func init() {
DataSourceCmd.AddCommand(createCmd)

createCmd.Flags().StringP("output", "o", app.Table,
fmt.Sprintf("Output format (one of %s)", strings.Join(app.SupportedOutputFormats(), ",")))
createCmd.Flags().StringP("name", "n", "", "Name of the data source")
createCmd.Flags().StringP("endpoint", "e", "", "Endpoint URL for the REST data source")
createCmd.Flags().StringP("version", "v", "v1", "Version of the data source API")

if err := createCmd.MarkFlagRequired("name"); err != nil {
panic(err)
}
if err := createCmd.MarkFlagRequired("endpoint"); err != nil {
panic(err)
}
}
21 changes: 21 additions & 0 deletions cmd/cli/app/datasource/datasource.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package datasource

import (
"github.com/spf13/cobra"

"github.com/mindersec/minder/cmd/cli/app"
)

// DataSourceCmd is the root command for the data source subcommands
var DataSourceCmd = &cobra.Command{
Use: "datasource",
Short: "Manage data sources within a minder control plane",
Long: "The data source subcommand allows the management of data sources within Minder.",
RunE: func(cmd *cobra.Command, _ []string) error {
return cmd.Usage()
},
}

func init() {
app.RootCmd.AddCommand(DataSourceCmd)
}
76 changes: 76 additions & 0 deletions cmd/cli/app/datasource/delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package datasource

import (
"context"
"fmt"
"strings"

"github.com/spf13/cobra"
"github.com/spf13/viper"
"google.golang.org/grpc"

"github.com/mindersec/minder/cmd/cli/app"
"github.com/mindersec/minder/internal/util"
"github.com/mindersec/minder/internal/util/cli"
minderv1 "github.com/mindersec/minder/pkg/api/protobuf/go/minder/v1"
)

var deleteCmd = &cobra.Command{
Use: "delete",
Short: "Delete a data source",
Long: `The datasource delete subcommand lets you delete a data source within Minder.`,
RunE: cli.GRPCClientWrapRunE(deleteCommand),
}

// deleteCommand is the datasource delete subcommand
func deleteCommand(ctx context.Context, cmd *cobra.Command, _ []string, conn *grpc.ClientConn) error {
client := minderv1.NewDataSourceServiceClient(conn)

project := viper.GetString("project")
format := viper.GetString("output")
id := viper.GetString("id")

// No longer print usage on returned error, since we've parsed our inputs
cmd.SilenceUsage = true

resp, err := client.DeleteDataSource(ctx, &minderv1.DeleteDataSourceRequest{
Context: &minderv1.ContextV2{
ProjectId: project,
},
Id: id,
})
if err != nil {
return cli.MessageAndError("Failed to delete data source", err)
}

switch format {
case app.JSON:
out, err := util.GetJsonFromProto(resp)
if err != nil {
return cli.MessageAndError("Error getting json from proto", err)
}
cmd.Println(out)
case app.YAML:
out, err := util.GetYamlFromProto(resp)
if err != nil {
return cli.MessageAndError("Error getting yaml from proto", err)
}
cmd.Println(out)
default:
cmd.Printf("Successfully deleted data source with ID: %s\n", id)
}

return nil
}

func init() {
DataSourceCmd.AddCommand(deleteCmd)

deleteCmd.Flags().StringP("output", "o", app.Table,
fmt.Sprintf("Output format (one of %s)", strings.Join(app.SupportedOutputFormats(), ",")))
deleteCmd.Flags().StringP("id", "i", "", "ID of the data source to delete")

if err := deleteCmd.MarkFlagRequired("id"); err != nil {
panic(err)
}
}
92 changes: 92 additions & 0 deletions cmd/cli/app/datasource/get.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package datasource

import (
"context"
"fmt"
"strings"

"github.com/spf13/cobra"
"github.com/spf13/viper"
"google.golang.org/grpc"

"github.com/mindersec/minder/cmd/cli/app"
"github.com/mindersec/minder/internal/util"
"github.com/mindersec/minder/internal/util/cli"
"github.com/mindersec/minder/internal/util/cli/table"
"github.com/mindersec/minder/internal/util/cli/table/layouts"
minderv1 "github.com/mindersec/minder/pkg/api/protobuf/go/minder/v1"
)

var getCmd = &cobra.Command{
Use: "get",
Short: "Get data source details",
Long: `The datasource get subcommand lets you retrieve details for a data source within Minder.`,
RunE: cli.GRPCClientWrapRunE(getCommand),
}

// getCommand is the datasource get subcommand
func getCommand(ctx context.Context, cmd *cobra.Command, _ []string, conn *grpc.ClientConn) error {
client := minderv1.NewDataSourceServiceClient(conn)

project := viper.GetString("project")
format := viper.GetString("output")
id := viper.GetString("id")

// No longer print usage on returned error, since we've parsed our inputs
// See https://github.com/spf13/cobra/issues/340#issuecomment-374617413
cmd.SilenceUsage = true

resp, err := client.GetDataSourceById(ctx, &minderv1.GetDataSourceByIdRequest{
Context: &minderv1.ContextV2{
ProjectId: project,
},
Id: id,
})
if err != nil {
return cli.MessageAndError("Failed to get data source", err)
}

switch format {
case app.JSON:
out, err := util.GetJsonFromProto(resp)
if err != nil {
return cli.MessageAndError("Error getting json from proto", err)
}
cmd.Println(out)
case app.YAML:
out, err := util.GetYamlFromProto(resp)
if err != nil {
return cli.MessageAndError("Error getting yaml from proto", err)
}
cmd.Println(out)
case app.Table:
t := table.New(table.Simple, layouts.Default, []string{"ID", "Name", "Type"})
ds := resp.GetDataSource()
t.AddRow(ds.Id, ds.Name, getDataSourceType(ds))
t.Render()
default:
return fmt.Errorf("unsupported output format: %s", format)
}

return nil
}

// getDataSourceType returns the type of data source
func getDataSourceType(ds *minderv1.DataSource) string {
if ds.GetRest() != nil {
return "REST"
}
return "Unknown"
}

func init() {
DataSourceCmd.AddCommand(getCmd)

getCmd.Flags().StringP("output", "o", app.Table,
fmt.Sprintf("Output format (one of %s)", strings.Join(app.SupportedOutputFormats(), ",")))
getCmd.Flags().StringP("id", "i", "", "ID of the data source to get info from")

if err := getCmd.MarkFlagRequired("id"); err != nil {
panic(err)
}
}
78 changes: 78 additions & 0 deletions cmd/cli/app/datasource/list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package datasource

import (
"context"
"fmt"
"strings"

"github.com/spf13/cobra"
"github.com/spf13/viper"
"google.golang.org/grpc"

"github.com/mindersec/minder/cmd/cli/app"
"github.com/mindersec/minder/internal/util"
"github.com/mindersec/minder/internal/util/cli"
"github.com/mindersec/minder/internal/util/cli/table"
"github.com/mindersec/minder/internal/util/cli/table/layouts"
minderv1 "github.com/mindersec/minder/pkg/api/protobuf/go/minder/v1"
)

var listCmd = &cobra.Command{
Use: "list",
Short: "List data sources",
Long: `The datasource list subcommand lets you list all data sources within Minder.`,
RunE: cli.GRPCClientWrapRunE(listCommand),
}

// listCommand is the datasource list subcommand
func listCommand(ctx context.Context, cmd *cobra.Command, _ []string, conn *grpc.ClientConn) error {
client := minderv1.NewDataSourceServiceClient(conn)

project := viper.GetString("project")
format := viper.GetString("output")

// No longer print usage on returned error, since we've parsed our inputs
// See https://github.com/spf13/cobra/issues/340#issuecomment-374617413
cmd.SilenceUsage = true

resp, err := client.ListDataSources(ctx, &minderv1.ListDataSourcesRequest{
Context: &minderv1.ContextV2{
ProjectId: project,
},
})
if err != nil {
return cli.MessageAndError("Failed to list data sources", err)
}

switch format {
case app.JSON:
out, err := util.GetJsonFromProto(resp)
if err != nil {
return cli.MessageAndError("Error getting json from proto", err)
}
cmd.Println(out)
case app.YAML:
out, err := util.GetYamlFromProto(resp)
if err != nil {
return cli.MessageAndError("Error getting yaml from proto", err)
}
cmd.Println(out)
case app.Table:
t := table.New(table.Simple, layouts.Default, []string{"ID", "Name", "Type"})
for _, ds := range resp.GetDataSources() {
t.AddRow(ds.Id, ds.Name, getDataSourceType(ds))
}
t.Render()
default:
return fmt.Errorf("unsupported output format: %s", format)
}

return nil
}

func init() {
DataSourceCmd.AddCommand(listCmd)

listCmd.Flags().StringP("output", "o", app.Table,
fmt.Sprintf("Output format (one of %s)", strings.Join(app.SupportedOutputFormats(), ",")))
}
Loading
Loading