Skip to content

Commit

Permalink
add: operations by name
Browse files Browse the repository at this point in the history
  • Loading branch information
teodor-yanev committed Nov 21, 2024
1 parent c092b8a commit b386b9e
Show file tree
Hide file tree
Showing 10 changed files with 4,182 additions and 3,354 deletions.
2 changes: 1 addition & 1 deletion cmd/cli/app/datasource/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func executeOnOneDataSource(
defer closer()

ds := &minderv1.DataSource{}
if err := minderv1.ParseResource(reader, ds); err != nil {
if err := minderv1.ParseResourceProto(reader, ds); err != nil {
return fmt.Errorf("error parsing data source: %w", err)
}

Expand Down
59 changes: 37 additions & 22 deletions cmd/cli/app/datasource/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"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"
)
Expand All @@ -32,37 +31,53 @@ func deleteCommand(ctx context.Context, cmd *cobra.Command, _ []string, conn *gr
project := viper.GetString("project")
format := viper.GetString("output")
id := viper.GetString("id")
name := viper.GetString("name")

// 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 id == "" && name == "" {
return fmt.Errorf("either id or name must be specified")
}

var err error

if id != "" {
_, err = client.DeleteDataSourceById(ctx, &minderv1.DeleteDataSourceByIdRequest{
Context: &minderv1.ContextV2{
ProjectId: project,
},
Id: id,
})
} else {
_, err = client.DeleteDataSourceByName(ctx, &minderv1.DeleteDataSourceByNameRequest{
Context: &minderv1.ContextV2{
ProjectId: project,
},
Name: name,
})
}

if err != nil {
return cli.MessageAndError("Failed to delete data source", err)
}

return outputDeleteResult(cmd, format, id, name)
}

func outputDeleteResult(cmd *cobra.Command, format, id, name string) error {
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)
cmd.Println(`{"status": "success"}`)
case app.YAML:
out, err := util.GetYamlFromProto(resp)
if err != nil {
return cli.MessageAndError("Error getting yaml from proto", err)
}
cmd.Println(out)
cmd.Println("status: success")
default:
cmd.Printf("Successfully deleted data source with ID: %s\n", id)
if id != "" {
cmd.Printf("Successfully deleted data source with ID: %s\n", id)
} else {
cmd.Printf("Successfully deleted data source with Name: %s\n", name)
}
}

return nil
}

Expand All @@ -72,8 +87,8 @@ func init() {
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")
deleteCmd.Flags().StringP("name", "n", "", "Name of the data source to delete")

if err := deleteCmd.MarkFlagRequired("id"); err != nil {
panic(err)
}
// Ensure at least one of id or name is required
deleteCmd.MarkFlagsOneRequired("id", "name")
}
52 changes: 38 additions & 14 deletions cmd/cli/app/datasource/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,43 +34,67 @@ func getCommand(ctx context.Context, cmd *cobra.Command, _ []string, conn *grpc.
project := viper.GetString("project")
format := viper.GetString("output")
id := viper.GetString("id")
name := viper.GetString("name")

// 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,
})
var err error
var ds *minderv1.DataSource

if id == "" && name == "" {
return fmt.Errorf("either id or name must be specified")
}

var resp interface {
GetDataSource() *minderv1.DataSource
}

if id != "" {
resp, err = client.GetDataSourceById(ctx, &minderv1.GetDataSourceByIdRequest{
Context: &minderv1.ContextV2{
ProjectId: project,
},
Id: id,
})
} else {
resp, err = client.GetDataSourceByName(ctx, &minderv1.GetDataSourceByNameRequest{
Context: &minderv1.ContextV2{
ProjectId: project,
},
Name: name,
})
}

if err != nil {
return cli.MessageAndError("Failed to get data source", err)
}

ds = resp.GetDataSource()
return outputDataSource(cmd, format, ds)
}

func outputDataSource(cmd *cobra.Command, format string, ds *minderv1.DataSource) error {
switch format {
case app.JSON:
out, err := util.GetJsonFromProto(resp)
out, err := util.GetJsonFromProto(ds)
if err != nil {
return cli.MessageAndError("Error getting json from proto", err)
}
cmd.Println(out)
case app.YAML:
out, err := util.GetYamlFromProto(resp)
out, err := util.GetYamlFromProto(ds)
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
}

Expand All @@ -80,8 +104,8 @@ func init() {
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")
getCmd.Flags().StringP("name", "n", "", "Name of the data source to get info from")

if err := getCmd.MarkFlagRequired("id"); err != nil {
panic(err)
}
// Ensure at least one of id or name is required
getCmd.MarkFlagsOneRequired("id", "name")
}
54 changes: 51 additions & 3 deletions docs/docs/ref/proto.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 26 additions & 4 deletions internal/controlplane/handlers_datasource.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,17 @@ func (s *Server) GetDataSourceById(ctx context.Context,
return &minderv1.GetDataSourceByIdResponse{}, nil
}

// GetDataSourceByName retrieves a data source by name
func (s *Server) GetDataSourceByName(ctx context.Context,
_ *minderv1.GetDataSourceByNameRequest) (*minderv1.GetDataSourceByNameResponse, error) {

if !flags.Bool(ctx, s.featureFlags, flags.DataSources) {
return nil, status.Errorf(codes.Unavailable, "DataSources feature is disabled")
}

return &minderv1.GetDataSourceByNameResponse{}, nil
}

// ListDataSources lists all data sources
func (s *Server) ListDataSources(ctx context.Context,
_ *minderv1.ListDataSourcesRequest) (*minderv1.ListDataSourcesResponse, error) {
Expand All @@ -57,13 +68,24 @@ func (s *Server) UpdateDataSource(ctx context.Context,
return &minderv1.UpdateDataSourceResponse{}, nil
}

// DeleteDataSource deletes a data source
func (s *Server) DeleteDataSource(ctx context.Context,
_ *minderv1.DeleteDataSourceRequest) (*minderv1.DeleteDataSourceResponse, error) {
// DeleteDataSourceById deletes a data source by ID
func (s *Server) DeleteDataSourceById(ctx context.Context,
_ *minderv1.DeleteDataSourceByIdRequest) (*minderv1.DeleteDataSourceByIdResponse, error) {

if !flags.Bool(ctx, s.featureFlags, flags.DataSources) {
return nil, status.Errorf(codes.Unavailable, "DataSources feature is disabled")
}

return &minderv1.DeleteDataSourceByIdResponse{}, nil
}

// DeleteDataSourceByName deletes a data source by name
func (s *Server) DeleteDataSourceByName(ctx context.Context,
_ *minderv1.DeleteDataSourceByNameRequest) (*minderv1.DeleteDataSourceByNameResponse, error) {

if !flags.Bool(ctx, s.featureFlags, flags.DataSources) {
return nil, status.Errorf(codes.Unavailable, "DataSources feature is disabled")
}

return &minderv1.DeleteDataSourceResponse{}, nil
return &minderv1.DeleteDataSourceByNameResponse{}, nil
}
Loading

0 comments on commit b386b9e

Please sign in to comment.