Skip to content

Commit

Permalink
Get data source string from protobuf struct (#5125)
Browse files Browse the repository at this point in the history
This adds a helper function to the data sources protobuf struct to get a
string representation of the driver. This is handy for clients to be
able to render relevant info about data sources.

Signed-off-by: Juan Antonio Osorio <[email protected]>
  • Loading branch information
JAORMX authored Dec 3, 2024
1 parent 8e6e17d commit 3837e7b
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 11 deletions.
10 changes: 1 addition & 9 deletions cmd/cli/app/datasource/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func appendDataSourcePropertiesToName(ds *minderv1.DataSource) string {
name := ds.Name
properties := []string{}
// add the type property if it is present
dType := getDataSourceType(ds)
dType := ds.GetDriverType()
if dType != "" {
properties = append(properties, fmt.Sprintf("type: %s", dType))
}
Expand All @@ -138,14 +138,6 @@ func appendDataSourcePropertiesToName(ds *minderv1.DataSource) string {
return name
}

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

// initializeTableForList initializes the table for listing data sources
func initializeTableForList() table.Table {
return table.New(table.Simple, layouts.DataSourceList, nil)
Expand Down
2 changes: 1 addition & 1 deletion cmd/cli/app/datasource/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func outputDataSource(cmd *cobra.Command, format string, ds *minderv1.DataSource
cmd.Println(out)
case app.Table:
t := table.New(table.Simple, layouts.Default, []string{"ID", "Name", "Type"})
t.AddRow(ds.Id, ds.Name, getDataSourceType(ds))
t.AddRow(ds.Id, ds.Name, ds.GetDriverType())
t.Render()
default:
return fmt.Errorf("unsupported output format: %s", format)
Expand Down
2 changes: 1 addition & 1 deletion cmd/cli/app/datasource/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func listCommand(ctx context.Context, cmd *cobra.Command, _ []string, conn *grpc
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.AddRow(ds.Id, ds.Name, ds.GetDriverType())
}
t.Render()
default:
Expand Down
16 changes: 16 additions & 0 deletions pkg/api/protobuf/go/minder/v1/datasources.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

package v1

import v1datasources "github.com/mindersec/minder/pkg/datasources/v1"

// GetContext returns the v2 context from the CreateDataSourceRequest data source.
func (r *CreateDataSourceRequest) GetContext() *ContextV2 {
return r.DataSource.GetContext()
Expand All @@ -13,3 +15,17 @@ func (r *CreateDataSourceRequest) GetContext() *ContextV2 {
func (r *UpdateDataSourceRequest) GetContext() *ContextV2 {
return r.DataSource.GetContext()
}

// GetDriverType returns the string representation of the driver type of the data source.
func (ds *DataSource) GetDriverType() string {
if ds == nil {
return ""
}

switch ds.GetDriver().(type) {
case *DataSource_Rest:
return v1datasources.DataSourceDriverRest
default:
return "unknown"
}
}

0 comments on commit 3837e7b

Please sign in to comment.