Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions .nextchanges/cli/serving-endpoints-name-with-json.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* `serving-endpoints create NAME --json ...` now accepts the endpoint name as the argument together with `--json` (it previously failed with `no positional arguments are allowed`), and `serving-endpoints update-config --json ...` now accepts the name as `"name"` in the JSON when the argument is omitted. ([#7665](https://github.com/databricks/cli/pull/7665))
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Cloud = false
144 changes: 144 additions & 0 deletions acceptance/cmd/workspace/serving-endpoints/name-with-json/output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@

=== create: NAME as the argument together with --json

>>> [CLI] serving-endpoints create my-endpoint --no-wait --json {"description": "from arg"}
{"name":"my-endpoint","description":"from arg"}

=== create: NAME inside --json still works

>>> [CLI] serving-endpoints create --no-wait --json {"name": "other-endpoint"}
{"name":"other-endpoint"}

=== create: the same name in both places is fine

>>> [CLI] serving-endpoints create third-endpoint --no-wait --json {"name": "third-endpoint"}
{"name":"third-endpoint"}

=== create: different names are rejected

>>> musterr [CLI] serving-endpoints create my-endpoint --json {"name": "something-else"}
Error: the endpoint name "my-endpoint" doesn't match "name" "something-else" in --json; pass it in one place only

=== update-config: NAME from --json when the argument is omitted

>>> [CLI] serving-endpoints update-config --no-wait --json {"name": "my-endpoint", "served_entities": [{"entity_name": "main.default.m", "entity_version": "1", "workload_size": "Small"}]}
{"name":"my-endpoint"}

=== update-config: the argument still works

>>> [CLI] serving-endpoints update-config my-endpoint --no-wait --json {"served_entities": [{"entity_name": "main.default.m", "entity_version": "2", "workload_size": "Small"}]}
{"name":"my-endpoint"}

=== update-config: no name anywhere

>>> musterr [CLI] serving-endpoints update-config --json {"served_entities": []}
Error: accepts 1 arg(s), received 0

Usage:
databricks serving-endpoints update-config NAME [flags]

Flags:
-h, --help help for update-config
--json JSON either inline JSON string or @path/to/file.json with request body (default JSON (0 bytes))
--no-wait do not wait to reach NOT_UPDATING state
--timeout duration maximum amount of time to reach NOT_UPDATING state (default 20m0s)

Global Flags:
--debug enable debug logging
-o, --output type output type: text or json (default text)
-p, --profile string ~/.databrickscfg profile
-t, --target string bundle target to use (if applicable)


=== Without --json, NAME is still required

>>> musterr [CLI] serving-endpoints create
Error: accepts 1 arg(s), received 0

Usage:
databricks serving-endpoints create NAME [flags]

Flags:
--budget-policy-id string The budget policy to be applied to the serving endpoint.
--description string The description of the serving endpoint.
-h, --help help for create
--json JSON either inline JSON string or @path/to/file.json with request body (default JSON (0 bytes))
--no-wait do not wait to reach NOT_UPDATING state
--route-optimized Enable route optimization for the serving endpoint.
--timeout duration maximum amount of time to reach NOT_UPDATING state (default 20m0s)

Global Flags:
--debug enable debug logging
-o, --output type output type: text or json (default text)
-p, --profile string ~/.databrickscfg profile
-t, --target string bundle target to use (if applicable)


>>> musterr [CLI] serving-endpoints update-config
Error: accepts 1 arg(s), received 0

Usage:
databricks serving-endpoints update-config NAME [flags]

Flags:
-h, --help help for update-config
--json JSON either inline JSON string or @path/to/file.json with request body (default JSON (0 bytes))
--no-wait do not wait to reach NOT_UPDATING state
--timeout duration maximum amount of time to reach NOT_UPDATING state (default 20m0s)

Global Flags:
--debug enable debug logging
-o, --output type output type: text or json (default text)
-p, --profile string ~/.databrickscfg profile
-t, --target string bundle target to use (if applicable)


>>> print_requests.py //serving-endpoints
{
"method": "POST",
"path": "/api/2.0/serving-endpoints",
"body": {
"description": "from arg",
"name": "my-endpoint"
}
}
{
"method": "POST",
"path": "/api/2.0/serving-endpoints",
"body": {
"name": "other-endpoint"
}
}
{
"method": "POST",
"path": "/api/2.0/serving-endpoints",
"body": {
"name": "third-endpoint"
}
}
{
"method": "PUT",
"path": "/api/2.0/serving-endpoints/my-endpoint/config",
"body": {
"served_entities": [
{
"entity_name": "main.default.m",
"entity_version": "1",
"workload_size": "Small"
}
]
}
}
{
"method": "PUT",
"path": "/api/2.0/serving-endpoints/my-endpoint/config",
"body": {
"served_entities": [
{
"entity_name": "main.default.m",
"entity_version": "2",
"workload_size": "Small"
}
]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
title "create: NAME as the argument together with --json"
trace $CLI serving-endpoints create my-endpoint --no-wait --json '{"description": "from arg"}' | jq -c '{name, description}'

title "create: NAME inside --json still works"
trace $CLI serving-endpoints create --no-wait --json '{"name": "other-endpoint"}' | jq -c '{name}'

title "create: the same name in both places is fine"
trace $CLI serving-endpoints create third-endpoint --no-wait --json '{"name": "third-endpoint"}' | jq -c '{name}'

title "create: different names are rejected"
trace musterr $CLI serving-endpoints create my-endpoint --json '{"name": "something-else"}'

title "update-config: NAME from --json when the argument is omitted"
trace $CLI serving-endpoints update-config --no-wait --json '{"name": "my-endpoint", "served_entities": [{"entity_name": "main.default.m", "entity_version": "1", "workload_size": "Small"}]}' | jq -c '{name}'

title "update-config: the argument still works"
trace $CLI serving-endpoints update-config my-endpoint --no-wait --json '{"served_entities": [{"entity_name": "main.default.m", "entity_version": "2", "workload_size": "Small"}]}' | jq -c '{name}'

title "update-config: no name anywhere"
trace musterr $CLI serving-endpoints update-config --json '{"served_entities": []}'

title "Without --json, NAME is still required"
trace musterr $CLI serving-endpoints create
trace musterr $CLI serving-endpoints update-config

trace print_requests.py //serving-endpoints
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Cloud = false
RecordRequests = true
115 changes: 115 additions & 0 deletions cmd/workspace/serving-endpoints/positional_name.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package serving_endpoints

// This file's overrides wrap RunE, so it must sort after any file whose
// overrides replace RunE outright: overrides run in init() order, which follows
// file names.

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

"github.com/databricks/cli/cmd/root"
"github.com/databricks/cli/libs/flags"
"github.com/databricks/databricks-sdk-go/service/serving"
"github.com/spf13/cobra"
)

const nameWithJSONHelp = `

With --json, the endpoint NAME can be passed either as the argument or as
"name" in the JSON.`

// jsonName returns the top-level "name" of --json, or "" if --json isn't set,
// has no name, or isn't a JSON object (the generated code reports bad JSON).
func jsonName(cmd *cobra.Command) string {
jsonFlag, ok := cmd.Flags().Lookup("json").Value.(*flags.JsonFlag)
if !ok || !cmd.Flags().Changed("json") {
return ""
}
var body struct {
Name string `json:"name"`
}
_ = json.Unmarshal(jsonFlag.Raw(), &body)
return body.Name
}

// atMostOneArgWithJSON allows NAME to be omitted when --json is set, and
// requires it otherwise.
func atMostOneArgWithJSON(cmd *cobra.Command, args []string) error {
if cmd.Flags().Changed("json") && len(args) == 0 {
return nil
}
return root.ExactArgs(1)(cmd, args)
}

// createNameOverride lets create take NAME as an argument together with
// --json. The generated command rejected that ("no positional arguments are
// allowed"), even though the usage line is "create NAME" and update-config
// requires the argument, so users and agents got one of the two wrong.
func createNameOverride(createCmd *cobra.Command, createReq *serving.CreateServingEndpoint) {
createCmd.Long += nameWithJSONHelp
createCmd.Args = atMostOneArgWithJSON
generatedRunE := createCmd.RunE
createCmd.RunE = func(cmd *cobra.Command, args []string) error {
if cmd.Flags().Changed("json") && len(args) == 1 {
if name := jsonName(cmd); name != "" && name != args[0] {
return fmt.Errorf("the endpoint name %q doesn't match \"name\" %q in --json; pass it in one place only", args[0], name)
}
// --json is decoded into createReq after this, and keeps fields
// it doesn't set.
createReq.Name = args[0]
}
return generatedRunE(cmd, args)
}
}

// updateConfigNameOverride lets update-config take the endpoint name from
// "name" in --json when NAME isn't passed, matching what create accepts.
func updateConfigNameOverride(updateConfigCmd *cobra.Command, _ *serving.EndpointCoreConfigInput) {
updateConfigCmd.Long += nameWithJSONHelp
updateConfigCmd.Args = atMostOneArgWithJSON
generatedRunE := updateConfigCmd.RunE
updateConfigCmd.RunE = func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
name, err := takeJSONName(cmd)
if err != nil {
return err
}
args = []string{name}
}
return generatedRunE(cmd, args)
}
}

// takeJSONName returns "name" from --json and removes it from the body. The
// update-config body has no "name" field, so leaving it in would print an
// "unknown field" warning for a value that was used.
func takeJSONName(cmd *cobra.Command) (string, error) {
jsonFlag, ok := cmd.Flags().Lookup("json").Value.(*flags.JsonFlag)
if !ok {
return "", errors.New("unexpected type for --json flag")
}
// Without a usable name, fail exactly as the generated command did when
// NAME was missing.
missingName := root.ExactArgs(1)(cmd, nil)
var body map[string]json.RawMessage
if err := json.Unmarshal(jsonFlag.Raw(), &body); err != nil {
return "", missingName
}
var name string
if err := json.Unmarshal(body["name"], &name); err != nil || name == "" {
return "", missingName
}
delete(body, "name")
stripped, err := json.Marshal(body)
if err != nil {
return "", err
}
return name, jsonFlag.Set(string(stripped))
}

func init() {
createOverrides = append(createOverrides, createNameOverride)
updateConfigOverrides = append(updateConfigOverrides, updateConfigNameOverride)
}
45 changes: 45 additions & 0 deletions cmd/workspace/serving-endpoints/positional_name_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package serving_endpoints

import (
"testing"

"github.com/databricks/cli/libs/flags"
"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func cmdWithJSON(t *testing.T, raw string) (*cobra.Command, *flags.JsonFlag) {
t.Helper()
cmd := &cobra.Command{Use: "update-config NAME"}
var jf flags.JsonFlag
cmd.Flags().Var(&jf, "json", "")
require.NoError(t, cmd.Flags().Set("json", raw))
return cmd, &jf
}

func TestTakeJSONName(t *testing.T) {
cmd, jf := cmdWithJSON(t, `{"name": "my-endpoint", "served_entities": [{"entity_version": 12345678901234567890}]}`)
name, err := takeJSONName(cmd)
require.NoError(t, err)
assert.Equal(t, "my-endpoint", name)
assert.JSONEq(t, `{"served_entities": [{"entity_version": 12345678901234567890}]}`, string(jf.Raw()))
}

func TestTakeJSONNameMissing(t *testing.T) {
for _, raw := range []string{`{"served_entities": []}`, `{"name": ""}`, `{"name": 1}`, `[1]`} {
t.Run(raw, func(t *testing.T) {
cmd, jf := cmdWithJSON(t, raw)
_, err := takeJSONName(cmd)
assert.ErrorContains(t, err, "accepts 1 arg(s), received 0")
assert.Equal(t, raw, string(jf.Raw()))
})
}
}

func TestJSONName(t *testing.T) {
cmd, _ := cmdWithJSON(t, `{"name": "a"}`)
assert.Equal(t, "a", jsonName(cmd))
cmd, _ = cmdWithJSON(t, `{"config": {}}`)
assert.Empty(t, jsonName(cmd))
}
Loading