Fixed requiring positional arguments for API URL parameters (#878)

## Changes
Some commands such as update commands have an argument in their url, for
example in pipeline we have `PUT pipelines/<id>` to update the pipeline.

Such parameters must be required and respected even if `--json` flag
with the payload passed.

Note: this depends on these PRs in Go SDK:
 https://github.com/databricks/databricks-sdk-go/pull/660
https://github.com/databricks/databricks-sdk-go/pull/661

## Tests
Manually running `databricks pipelines update`
This commit is contained in:
Andrew Nester 2023-10-19 16:19:17 +02:00 committed by GitHub
parent 3700785dfa
commit 4ad68eb314
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
15 changed files with 188 additions and 231 deletions

View File

@ -78,8 +78,7 @@ var {{.CamelName}}Overrides []func(
func new{{.PascalName}}() *cobra.Command { func new{{.PascalName}}() *cobra.Command {
cmd := &cobra.Command{} cmd := &cobra.Command{}
{{- $useJsonForAllFields := or .IsJsonOnly (and .Request (or (not .Request.IsAllRequiredFieldsPrimitive) .Request.HasRequiredNonBodyField)) -}} {{- $needJsonFlag := or .CanSetRequiredFieldsFromJson (and .Request (not .Request.IsOnlyPrimitiveFields)) -}}
{{- $needJsonFlag := or $useJsonForAllFields (and .Request (not .Request.IsOnlyPrimitiveFields)) -}}
{{- if .Request}} {{- if .Request}}
@ -143,7 +142,7 @@ func new{{.PascalName}}() *cobra.Command {
{{if $hasRequiredArgs }} {{if $hasRequiredArgs }}
cmd.Args = func(cmd *cobra.Command, args []string) error { cmd.Args = func(cmd *cobra.Command, args []string) error {
check := cobra.ExactArgs({{len .Request.RequiredFields}}) check := cobra.ExactArgs({{len .Request.RequiredFields}})
{{- if $useJsonForAllFields }} {{- if .CanSetRequiredFieldsFromJson }}
if cmd.Flags().Changed("json") { if cmd.Flags().Changed("json") {
check = cobra.ExactArgs(0) check = cobra.ExactArgs(0)
} }
@ -162,7 +161,7 @@ func new{{.PascalName}}() *cobra.Command {
if err != nil { if err != nil {
return err return err
} }
}{{end}}{{if $useJsonForAllFields }} else { }{{end}}{{if .CanSetRequiredFieldsFromJson }} else {
{{- end }} {{- end }}
{{- if $hasIdPrompt}} {{- if $hasIdPrompt}}
if len(args) == 0 { if len(args) == 0 {
@ -196,7 +195,7 @@ func new{{.PascalName}}() *cobra.Command {
{{- else -}} {{- else -}}
return fmt.Errorf("please provide command input in JSON format by specifying the --json flag") return fmt.Errorf("please provide command input in JSON format by specifying the --json flag")
{{- end -}} {{- end -}}
{{if $useJsonForAllFields }} {{if .CanSetRequiredFieldsFromJson }}
} }
{{end }} {{end }}
{{end}} {{end}}

View File

@ -478,7 +478,7 @@ func newUpdate() *cobra.Command {
if err != nil { if err != nil {
return err return err
} }
} else { }
if len(args) == 0 { if len(args) == 0 {
promptSpinner := cmdio.Spinner(ctx) promptSpinner := cmdio.Spinner(ctx)
promptSpinner <- "No ID argument specified. Loading names for Account Groups drop-down." promptSpinner <- "No ID argument specified. Loading names for Account Groups drop-down."
@ -497,7 +497,6 @@ func newUpdate() *cobra.Command {
return fmt.Errorf("expected to have databricks group id") return fmt.Errorf("expected to have databricks group id")
} }
updateReq.Id = args[0] updateReq.Id = args[0]
}
err = a.Groups.Update(ctx, updateReq) err = a.Groups.Update(ctx, updateReq)
if err != nil { if err != nil {

View File

@ -480,7 +480,7 @@ func newUpdate() *cobra.Command {
if err != nil { if err != nil {
return err return err
} }
} else { }
if len(args) == 0 { if len(args) == 0 {
promptSpinner := cmdio.Spinner(ctx) promptSpinner := cmdio.Spinner(ctx)
promptSpinner <- "No ID argument specified. Loading names for Account Service Principals drop-down." promptSpinner <- "No ID argument specified. Loading names for Account Service Principals drop-down."
@ -499,7 +499,6 @@ func newUpdate() *cobra.Command {
return fmt.Errorf("expected to have databricks service principal id") return fmt.Errorf("expected to have databricks service principal id")
} }
updateReq.Id = args[0] updateReq.Id = args[0]
}
err = a.ServicePrincipals.Update(ctx, updateReq) err = a.ServicePrincipals.Update(ctx, updateReq)
if err != nil { if err != nil {

View File

@ -488,7 +488,7 @@ func newUpdate() *cobra.Command {
if err != nil { if err != nil {
return err return err
} }
} else { }
if len(args) == 0 { if len(args) == 0 {
promptSpinner := cmdio.Spinner(ctx) promptSpinner := cmdio.Spinner(ctx)
promptSpinner <- "No ID argument specified. Loading names for Account Users drop-down." promptSpinner <- "No ID argument specified. Loading names for Account Users drop-down."
@ -507,7 +507,6 @@ func newUpdate() *cobra.Command {
return fmt.Errorf("expected to have databricks user id") return fmt.Errorf("expected to have databricks user id")
} }
updateReq.Id = args[0] updateReq.Id = args[0]
}
err = a.Users.Update(ctx, updateReq) err = a.Users.Update(ctx, updateReq)
if err != nil { if err != nil {

View File

@ -337,9 +337,6 @@ func newUpdate() *cobra.Command {
cmd.Args = func(cmd *cobra.Command, args []string) error { cmd.Args = func(cmd *cobra.Command, args []string) error {
check := cobra.ExactArgs(1) check := cobra.ExactArgs(1)
if cmd.Flags().Changed("json") {
check = cobra.ExactArgs(0)
}
return check(cmd, args) return check(cmd, args)
} }
@ -353,9 +350,8 @@ func newUpdate() *cobra.Command {
if err != nil { if err != nil {
return err return err
} }
} else {
updateReq.Name = args[0]
} }
updateReq.Name = args[0]
response, err := w.Catalogs.Update(ctx, updateReq) response, err := w.Catalogs.Update(ctx, updateReq)
if err != nil { if err != nil {

View File

@ -346,9 +346,6 @@ func newUpdate() *cobra.Command {
cmd.Args = func(cmd *cobra.Command, args []string) error { cmd.Args = func(cmd *cobra.Command, args []string) error {
check := cobra.ExactArgs(1) check := cobra.ExactArgs(1)
if cmd.Flags().Changed("json") {
check = cobra.ExactArgs(0)
}
return check(cmd, args) return check(cmd, args)
} }
@ -362,9 +359,8 @@ func newUpdate() *cobra.Command {
if err != nil { if err != nil {
return err return err
} }
} else {
updateReq.Name = args[0]
} }
updateReq.Name = args[0]
response, err := w.ExternalLocations.Update(ctx, updateReq) response, err := w.ExternalLocations.Update(ctx, updateReq)
if err != nil { if err != nil {

View File

@ -478,7 +478,7 @@ func newUpdate() *cobra.Command {
if err != nil { if err != nil {
return err return err
} }
} else { }
if len(args) == 0 { if len(args) == 0 {
promptSpinner := cmdio.Spinner(ctx) promptSpinner := cmdio.Spinner(ctx)
promptSpinner <- "No ID argument specified. Loading names for Groups drop-down." promptSpinner <- "No ID argument specified. Loading names for Groups drop-down."
@ -497,7 +497,6 @@ func newUpdate() *cobra.Command {
return fmt.Errorf("expected to have databricks group id") return fmt.Errorf("expected to have databricks group id")
} }
updateReq.Id = args[0] updateReq.Id = args[0]
}
err = w.Groups.Update(ctx, updateReq) err = w.Groups.Update(ctx, updateReq)
if err != nil { if err != nil {

View File

@ -1130,7 +1130,7 @@ func newUpdate() *cobra.Command {
if err != nil { if err != nil {
return err return err
} }
} else { }
if len(args) == 0 { if len(args) == 0 {
promptSpinner := cmdio.Spinner(ctx) promptSpinner := cmdio.Spinner(ctx)
promptSpinner <- "No PIPELINE_ID argument specified. Loading names for Pipelines drop-down." promptSpinner <- "No PIPELINE_ID argument specified. Loading names for Pipelines drop-down."
@ -1149,7 +1149,6 @@ func newUpdate() *cobra.Command {
return fmt.Errorf("expected to have unique identifier for this pipeline") return fmt.Errorf("expected to have unique identifier for this pipeline")
} }
updateReq.PipelineId = args[0] updateReq.PipelineId = args[0]
}
err = w.Pipelines.Update(ctx, updateReq) err = w.Pipelines.Update(ctx, updateReq)
if err != nil { if err != nil {

View File

@ -432,10 +432,8 @@ func newUpdate() *cobra.Command {
cmd := &cobra.Command{} cmd := &cobra.Command{}
var updateReq sharing.UpdateProvider var updateReq sharing.UpdateProvider
var updateJson flags.JsonFlag
// TODO: short flags // TODO: short flags
cmd.Flags().Var(&updateJson, "json", `either inline JSON string or @path/to/file.json with request body`)
cmd.Flags().StringVar(&updateReq.Comment, "comment", updateReq.Comment, `Description about the provider.`) cmd.Flags().StringVar(&updateReq.Comment, "comment", updateReq.Comment, `Description about the provider.`)
cmd.Flags().StringVar(&updateReq.Name, "name", updateReq.Name, `The name of the Provider.`) cmd.Flags().StringVar(&updateReq.Name, "name", updateReq.Name, `The name of the Provider.`)
@ -458,12 +456,6 @@ func newUpdate() *cobra.Command {
ctx := cmd.Context() ctx := cmd.Context()
w := root.WorkspaceClient(ctx) w := root.WorkspaceClient(ctx)
if cmd.Flags().Changed("json") {
err = updateJson.Unmarshal(&updateReq)
if err != nil {
return err
}
} else {
if len(args) == 0 { if len(args) == 0 {
promptSpinner := cmdio.Spinner(ctx) promptSpinner := cmdio.Spinner(ctx)
promptSpinner <- "No NAME argument specified. Loading names for Providers drop-down." promptSpinner <- "No NAME argument specified. Loading names for Providers drop-down."
@ -482,7 +474,6 @@ func newUpdate() *cobra.Command {
return fmt.Errorf("expected to have the name of the provider") return fmt.Errorf("expected to have the name of the provider")
} }
updateReq.Name = args[0] updateReq.Name = args[0]
}
response, err := w.Providers.Update(ctx, updateReq) response, err := w.Providers.Update(ctx, updateReq)
if err != nil { if err != nil {

View File

@ -547,7 +547,7 @@ func newUpdate() *cobra.Command {
if err != nil { if err != nil {
return err return err
} }
} else { }
if len(args) == 0 { if len(args) == 0 {
promptSpinner := cmdio.Spinner(ctx) promptSpinner := cmdio.Spinner(ctx)
promptSpinner <- "No NAME argument specified. Loading names for Recipients drop-down." promptSpinner <- "No NAME argument specified. Loading names for Recipients drop-down."
@ -566,7 +566,6 @@ func newUpdate() *cobra.Command {
return fmt.Errorf("expected to have name of recipient") return fmt.Errorf("expected to have name of recipient")
} }
updateReq.Name = args[0] updateReq.Name = args[0]
}
err = w.Recipients.Update(ctx, updateReq) err = w.Recipients.Update(ctx, updateReq)
if err != nil { if err != nil {

View File

@ -477,10 +477,8 @@ func newSetAlias() *cobra.Command {
cmd := &cobra.Command{} cmd := &cobra.Command{}
var setAliasReq catalog.SetRegisteredModelAliasRequest var setAliasReq catalog.SetRegisteredModelAliasRequest
var setAliasJson flags.JsonFlag
// TODO: short flags // TODO: short flags
cmd.Flags().Var(&setAliasJson, "json", `either inline JSON string or @path/to/file.json with request body`)
cmd.Use = "set-alias FULL_NAME ALIAS VERSION_NUM" cmd.Use = "set-alias FULL_NAME ALIAS VERSION_NUM"
cmd.Short = `Set a Registered Model Alias.` cmd.Short = `Set a Registered Model Alias.`
@ -497,9 +495,6 @@ func newSetAlias() *cobra.Command {
cmd.Args = func(cmd *cobra.Command, args []string) error { cmd.Args = func(cmd *cobra.Command, args []string) error {
check := cobra.ExactArgs(3) check := cobra.ExactArgs(3)
if cmd.Flags().Changed("json") {
check = cobra.ExactArgs(0)
}
return check(cmd, args) return check(cmd, args)
} }
@ -508,19 +503,12 @@ func newSetAlias() *cobra.Command {
ctx := cmd.Context() ctx := cmd.Context()
w := root.WorkspaceClient(ctx) w := root.WorkspaceClient(ctx)
if cmd.Flags().Changed("json") {
err = setAliasJson.Unmarshal(&setAliasReq)
if err != nil {
return err
}
} else {
setAliasReq.FullName = args[0] setAliasReq.FullName = args[0]
setAliasReq.Alias = args[1] setAliasReq.Alias = args[1]
_, err = fmt.Sscan(args[2], &setAliasReq.VersionNum) _, err = fmt.Sscan(args[2], &setAliasReq.VersionNum)
if err != nil { if err != nil {
return fmt.Errorf("invalid VERSION_NUM: %s", args[2]) return fmt.Errorf("invalid VERSION_NUM: %s", args[2])
} }
}
response, err := w.RegisteredModels.SetAlias(ctx, setAliasReq) response, err := w.RegisteredModels.SetAlias(ctx, setAliasReq)
if err != nil { if err != nil {

View File

@ -480,7 +480,7 @@ func newUpdate() *cobra.Command {
if err != nil { if err != nil {
return err return err
} }
} else { }
if len(args) == 0 { if len(args) == 0 {
promptSpinner := cmdio.Spinner(ctx) promptSpinner := cmdio.Spinner(ctx)
promptSpinner <- "No ID argument specified. Loading names for Service Principals drop-down." promptSpinner <- "No ID argument specified. Loading names for Service Principals drop-down."
@ -499,7 +499,6 @@ func newUpdate() *cobra.Command {
return fmt.Errorf("expected to have databricks service principal id") return fmt.Errorf("expected to have databricks service principal id")
} }
updateReq.Id = args[0] updateReq.Id = args[0]
}
err = w.ServicePrincipals.Update(ctx, updateReq) err = w.ServicePrincipals.Update(ctx, updateReq)
if err != nil { if err != nil {

View File

@ -398,9 +398,6 @@ func newUpdate() *cobra.Command {
cmd.Args = func(cmd *cobra.Command, args []string) error { cmd.Args = func(cmd *cobra.Command, args []string) error {
check := cobra.ExactArgs(1) check := cobra.ExactArgs(1)
if cmd.Flags().Changed("json") {
check = cobra.ExactArgs(0)
}
return check(cmd, args) return check(cmd, args)
} }
@ -414,9 +411,8 @@ func newUpdate() *cobra.Command {
if err != nil { if err != nil {
return err return err
} }
} else {
updateReq.Name = args[0]
} }
updateReq.Name = args[0]
response, err := w.Shares.Update(ctx, updateReq) response, err := w.Shares.Update(ctx, updateReq)
if err != nil { if err != nil {

View File

@ -378,7 +378,7 @@ func newUpdate() *cobra.Command {
if err != nil { if err != nil {
return err return err
} }
} else { }
if len(args) == 0 { if len(args) == 0 {
promptSpinner := cmdio.Spinner(ctx) promptSpinner := cmdio.Spinner(ctx)
promptSpinner <- "No NAME argument specified. Loading names for Storage Credentials drop-down." promptSpinner <- "No NAME argument specified. Loading names for Storage Credentials drop-down."
@ -397,7 +397,6 @@ func newUpdate() *cobra.Command {
return fmt.Errorf("expected to have the credential name") return fmt.Errorf("expected to have the credential name")
} }
updateReq.Name = args[0] updateReq.Name = args[0]
}
response, err := w.StorageCredentials.Update(ctx, updateReq) response, err := w.StorageCredentials.Update(ctx, updateReq)
if err != nil { if err != nil {

View File

@ -660,7 +660,7 @@ func newUpdate() *cobra.Command {
if err != nil { if err != nil {
return err return err
} }
} else { }
if len(args) == 0 { if len(args) == 0 {
promptSpinner := cmdio.Spinner(ctx) promptSpinner := cmdio.Spinner(ctx)
promptSpinner <- "No ID argument specified. Loading names for Users drop-down." promptSpinner <- "No ID argument specified. Loading names for Users drop-down."
@ -679,7 +679,6 @@ func newUpdate() *cobra.Command {
return fmt.Errorf("expected to have databricks user id") return fmt.Errorf("expected to have databricks user id")
} }
updateReq.Id = args[0] updateReq.Id = args[0]
}
err = w.Users.Update(ctx, updateReq) err = w.Users.Update(ctx, updateReq)
if err != nil { if err != nil {