From 2bb19e9d7b90c109e1ca235f6dc690294aaa4c1b Mon Sep 17 00:00:00 2001 From: Anshul Gupta Date: Mon, 16 Nov 2026 07:17:43 +0000 Subject: [PATCH 1/6] serving-endpoints: accept the endpoint name in permissions commands The permissions API only accepts a serving endpoint's ID, but every other serving-endpoints command takes the name, and the ID only shows up in `get` output. Passing the name failed with "is not a valid Inference Endpoint ID". Resolve a value that isn't shaped like an ID to the endpoint's ID with a get; IDs and names that don't resolve are passed through unchanged, so the API's own error is still what the user sees. The fake server now rejects non-ID values for serving endpoint permissions the way the real API does. Co-authored-by: Isaac Signed-off-by: Anshul Gupta --- .../serving-endpoints-permissions-by-name.md | 1 + .../permissions-by-name/out.test.toml | 1 + .../permissions-by-name/output.txt | 71 +++++++++++++++++++ .../permissions-by-name/script | 18 +++++ .../permissions-by-name/test.toml | 2 + .../serving-endpoints/permissions.go | 68 ++++++++++++++++++ .../serving-endpoints/permissions_test.go | 26 +++++++ libs/testserver/permissions.go | 28 ++++++++ 8 files changed, 215 insertions(+) create mode 100644 .nextchanges/cli/serving-endpoints-permissions-by-name.md create mode 100644 acceptance/cmd/workspace/serving-endpoints/permissions-by-name/out.test.toml create mode 100644 acceptance/cmd/workspace/serving-endpoints/permissions-by-name/output.txt create mode 100644 acceptance/cmd/workspace/serving-endpoints/permissions-by-name/script create mode 100644 acceptance/cmd/workspace/serving-endpoints/permissions-by-name/test.toml create mode 100644 cmd/workspace/serving-endpoints/permissions.go create mode 100644 cmd/workspace/serving-endpoints/permissions_test.go diff --git a/.nextchanges/cli/serving-endpoints-permissions-by-name.md b/.nextchanges/cli/serving-endpoints-permissions-by-name.md new file mode 100644 index 00000000000..4b1986143f6 --- /dev/null +++ b/.nextchanges/cli/serving-endpoints-permissions-by-name.md @@ -0,0 +1 @@ +* `serving-endpoints get-permissions`, `set-permissions` and `update-permissions` now also accept the endpoint name, and resolve it to the endpoint ID. Passing a name previously failed with `is not a valid Inference Endpoint ID`. diff --git a/acceptance/cmd/workspace/serving-endpoints/permissions-by-name/out.test.toml b/acceptance/cmd/workspace/serving-endpoints/permissions-by-name/out.test.toml new file mode 100644 index 00000000000..18b1a88417e --- /dev/null +++ b/acceptance/cmd/workspace/serving-endpoints/permissions-by-name/out.test.toml @@ -0,0 +1 @@ +Cloud = false diff --git a/acceptance/cmd/workspace/serving-endpoints/permissions-by-name/output.txt b/acceptance/cmd/workspace/serving-endpoints/permissions-by-name/output.txt new file mode 100644 index 00000000000..540249c448a --- /dev/null +++ b/acceptance/cmd/workspace/serving-endpoints/permissions-by-name/output.txt @@ -0,0 +1,71 @@ + +=== Permissions commands accept the endpoint name + +>>> [CLI] serving-endpoints set-permissions my-endpoint --json {"access_control_list": [{"group_name": "users", "permission_level": "CAN_QUERY"}]} +{"group_name":"users","permission_level":"CAN_QUERY"} + +>>> [CLI] serving-endpoints get-permissions my-endpoint +{"object_id":"/serving-endpoints/[ENDPOINT_ID]"} + +=== The ID still works + +>>> [CLI] serving-endpoints get-permissions [ENDPOINT_ID] +{"object_id":"/serving-endpoints/[ENDPOINT_ID]"} + +=== A name that doesn't resolve gets the API's error + +>>> musterr [CLI] serving-endpoints get-permissions no-such-endpoint +Error: 'no-such-endpoint' is not a valid Inference Endpoint ID. + +>>> print_requests.py //serving-endpoints //permissions --get +{ + "method": "POST", + "path": "/api/2.0/serving-endpoints", + "body": { + "name": "my-endpoint" + } +} +{ + "method": "GET", + "path": "/api/2.0/serving-endpoints/my-endpoint" +} +{ + "method": "GET", + "path": "/api/2.0/serving-endpoints/my-endpoint" +} +{ + "method": "PUT", + "path": "/api/2.0/permissions/serving-endpoints/[ENDPOINT_ID]", + "body": { + "access_control_list": [ + { + "group_name": "users", + "permission_level": "CAN_QUERY" + } + ] + } +} +{ + "method": "GET", + "path": "/api/2.0/serving-endpoints/my-endpoint" +} +{ + "method": "GET", + "path": "/api/2.0/permissions/serving-endpoints/[ENDPOINT_ID]" +} +{ + "method": "GET", + "path": "/api/2.0/serving-endpoints/[ENDPOINT_ID]" +} +{ + "method": "GET", + "path": "/api/2.0/permissions/serving-endpoints/[ENDPOINT_ID]" +} +{ + "method": "GET", + "path": "/api/2.0/serving-endpoints/no-such-endpoint" +} +{ + "method": "GET", + "path": "/api/2.0/permissions/serving-endpoints/no-such-endpoint" +} diff --git a/acceptance/cmd/workspace/serving-endpoints/permissions-by-name/script b/acceptance/cmd/workspace/serving-endpoints/permissions-by-name/script new file mode 100644 index 00000000000..12b8cd84918 --- /dev/null +++ b/acceptance/cmd/workspace/serving-endpoints/permissions-by-name/script @@ -0,0 +1,18 @@ +$CLI serving-endpoints create my-endpoint --no-wait > /dev/null +endpoint_id=$($CLI serving-endpoints get my-endpoint | jq -r .id) +add_repl "$endpoint_id" ENDPOINT_ID + + +title "Permissions commands accept the endpoint name" +trace $CLI serving-endpoints set-permissions my-endpoint --json '{"access_control_list": [{"group_name": "users", "permission_level": "CAN_QUERY"}]}' | jq -c '.access_control_list[] | {group_name, permission_level: .all_permissions[0].permission_level}' +trace $CLI serving-endpoints get-permissions my-endpoint | jq -c '{object_id}' + +# The fake server uses dashed UUIDs as endpoint IDs, so this also does a lookup by +# name first. A real 32-character ID is passed through without one. +title "The ID still works" +trace $CLI serving-endpoints get-permissions "$endpoint_id" | jq -c '{object_id}' + +title "A name that doesn't resolve gets the API's error" +trace musterr $CLI serving-endpoints get-permissions no-such-endpoint + +trace print_requests.py //serving-endpoints //permissions --get diff --git a/acceptance/cmd/workspace/serving-endpoints/permissions-by-name/test.toml b/acceptance/cmd/workspace/serving-endpoints/permissions-by-name/test.toml new file mode 100644 index 00000000000..7061886de47 --- /dev/null +++ b/acceptance/cmd/workspace/serving-endpoints/permissions-by-name/test.toml @@ -0,0 +1,2 @@ +Cloud = false +RecordRequests = true diff --git a/cmd/workspace/serving-endpoints/permissions.go b/cmd/workspace/serving-endpoints/permissions.go new file mode 100644 index 00000000000..29267609ac8 --- /dev/null +++ b/cmd/workspace/serving-endpoints/permissions.go @@ -0,0 +1,68 @@ +package serving_endpoints + +import ( + "context" + "strings" + + "github.com/databricks/cli/libs/cmdctx" + "github.com/databricks/cli/libs/log" + "github.com/databricks/databricks-sdk-go/service/serving" + "github.com/spf13/cobra" +) + +const permissionsNameHelp = ` + You can also pass the endpoint NAME; it is resolved to the ID with + "serving-endpoints get".` + +// endpointIDLength is the length of a serving endpoint ID (32 hex characters). +const endpointIDLength = 32 + +// isEndpointID reports whether s has the shape of a serving endpoint ID. +func isEndpointID(s string) bool { + if len(s) != endpointIDLength { + return false + } + for _, r := range s { + if !strings.ContainsRune("0123456789abcdef", r) { + return false + } + } + return true +} + +// resolveEndpointID returns the ID of the endpoint named arg. The permissions +// API only accepts endpoint IDs, and anything else fails with "is not a valid +// Inference Endpoint ID", but users and agents know endpoints by name. Values +// shaped like an ID are passed through unchanged, as is any value that doesn't +// resolve, so the API's own error is still what the user sees. +func resolveEndpointID(ctx context.Context, arg string) string { + if isEndpointID(arg) { + return arg + } + w := cmdctx.WorkspaceClient(ctx) + endpoint, err := w.ServingEndpoints.GetByName(ctx, arg) + if err != nil || endpoint.Id == "" { + log.Debugf(ctx, "could not resolve serving endpoint name %q to an ID: %v", arg, err) + return arg + } + return endpoint.Id +} + +// permissionsOverride lets a permissions command take an endpoint name as well +// as an ID. It's generic over the request type of each permissions command. +func permissionsOverride[T any](cmd *cobra.Command, _ T) { + cmd.Long += permissionsNameHelp + generatedRunE := cmd.RunE + cmd.RunE = func(cmd *cobra.Command, args []string) error { + if len(args) > 0 { + args = append([]string{resolveEndpointID(cmd.Context(), args[0])}, args[1:]...) + } + return generatedRunE(cmd, args) + } +} + +func init() { + getPermissionsOverrides = append(getPermissionsOverrides, permissionsOverride[*serving.GetServingEndpointPermissionsRequest]) + setPermissionsOverrides = append(setPermissionsOverrides, permissionsOverride[*serving.ServingEndpointPermissionsRequest]) + updatePermissionsOverrides = append(updatePermissionsOverrides, permissionsOverride[*serving.ServingEndpointPermissionsRequest]) +} diff --git a/cmd/workspace/serving-endpoints/permissions_test.go b/cmd/workspace/serving-endpoints/permissions_test.go new file mode 100644 index 00000000000..738353f8fe0 --- /dev/null +++ b/cmd/workspace/serving-endpoints/permissions_test.go @@ -0,0 +1,26 @@ +package serving_endpoints + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestIsEndpointID(t *testing.T) { + tests := []struct { + in string + want bool + }{ + {"31ee674b7e6643f6930d5e12148fbe6f", true}, + {"31EE674B7E6643F6930D5E12148FBE6F", false}, + {"31ee674b-7e66-43f6-930d-5e12148fbe6f", false}, + {"31ee674b7e6643f6930d5e12148fbe6", false}, + {"my-endpoint", false}, + {"", false}, + } + for _, tt := range tests { + t.Run(tt.in, func(t *testing.T) { + assert.Equal(t, tt.want, isEndpointID(tt.in)) + }) + } +} diff --git a/libs/testserver/permissions.go b/libs/testserver/permissions.go index a3584c1d49a..de4efa67c59 100644 --- a/libs/testserver/permissions.go +++ b/libs/testserver/permissions.go @@ -7,6 +7,8 @@ import ( "strconv" "strings" + "github.com/google/uuid" + "github.com/databricks/databricks-sdk-go/service/iam" ) @@ -151,6 +153,10 @@ func (s *FakeWorkspace) GetPermissions(req Request) any { } } + if resp := servingEndpointIDError(requestObjectType, objectId); resp != nil { + return *resp + } + responseObjectID := fmt.Sprintf("/%s/%s", requestObjectType, objectId) // V2 permissions APIs cascade-delete ACLs with the parent, so the cloud @@ -198,6 +204,24 @@ func (s *FakeWorkspace) permissionsParentExists(requestObjectType, objectId stri return true } +// servingEndpointIDError mirrors the permissions API, which rejects a value +// that isn't shaped like a serving endpoint ID, such as the endpoint's name. +func servingEndpointIDError(requestObjectType, objectId string) *Response { + if requestObjectType != "serving-endpoints" { + return nil + } + if _, err := uuid.Parse(objectId); err == nil { + return nil + } + return &Response{ + StatusCode: 400, + Body: map[string]string{ + "error_code": "INVALID_PARAMETER_VALUE", + "message": fmt.Sprintf("'%s' is not a valid Inference Endpoint ID.", objectId), + }, + } +} + func (s *FakeWorkspace) SetPermissions(req Request) any { defer s.LockUnlock()() @@ -230,6 +254,10 @@ func (s *FakeWorkspace) SetPermissions(req Request) any { } } + if resp := servingEndpointIDError(requestObjectType, objectId); resp != nil { + return *resp + } + var updateRequest iam.UpdateObjectPermissions if err := json.Unmarshal(req.Body, &updateRequest); err != nil { return Response{ From 5badb04ca1c7af360430a3670c64ecb76de4b41a Mon Sep 17 00:00:00 2001 From: Anshul Gupta Date: Mon, 16 Nov 2026 07:18:11 +0000 Subject: [PATCH 2/6] Point changelog fragment at #7662 Co-authored-by: Isaac Signed-off-by: Anshul Gupta --- .nextchanges/cli/serving-endpoints-permissions-by-name.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.nextchanges/cli/serving-endpoints-permissions-by-name.md b/.nextchanges/cli/serving-endpoints-permissions-by-name.md index 4b1986143f6..f242f16caac 100644 --- a/.nextchanges/cli/serving-endpoints-permissions-by-name.md +++ b/.nextchanges/cli/serving-endpoints-permissions-by-name.md @@ -1 +1 @@ -* `serving-endpoints get-permissions`, `set-permissions` and `update-permissions` now also accept the endpoint name, and resolve it to the endpoint ID. Passing a name previously failed with `is not a valid Inference Endpoint ID`. +* `serving-endpoints get-permissions`, `set-permissions` and `update-permissions` now also accept the endpoint name, and resolve it to the endpoint ID. Passing a name previously failed with `is not a valid Inference Endpoint ID`. ([#7662](https://github.com/databricks/cli/pull/7662)) From 665b5e811baa64f8c511d6da54e0814b0cd47f6b Mon Sep 17 00:00:00 2001 From: Anshul Gupta Date: Mon, 16 Nov 2026 07:26:00 +0000 Subject: [PATCH 3/6] testserver: use the standard library uuid package Co-authored-by: Isaac Signed-off-by: Anshul Gupta --- libs/testserver/permissions.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/libs/testserver/permissions.go b/libs/testserver/permissions.go index de4efa67c59..f59d720c627 100644 --- a/libs/testserver/permissions.go +++ b/libs/testserver/permissions.go @@ -6,8 +6,7 @@ import ( "slices" "strconv" "strings" - - "github.com/google/uuid" + "uuid" "github.com/databricks/databricks-sdk-go/service/iam" ) From d2b269b9e5a44a65e7107d23620a0fd9ddef5661 Mon Sep 17 00:00:00 2001 From: Anshul Gupta Date: Mon, 16 Nov 2026 07:44:13 +0000 Subject: [PATCH 4/6] Use the endpoint ID in the serving endpoint permissions fixture The fake server now rejects non-ID values for serving endpoint permissions, like the real API. The direct engine already uses the endpoint ID (endpoint_id); only this test fixture used the name. Co-authored-by: Isaac Signed-off-by: Anshul Gupta --- bundle/direct/dresources/all_test.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/bundle/direct/dresources/all_test.go b/bundle/direct/dresources/all_test.go index f80fda21d14..75d50cdf127 100644 --- a/bundle/direct/dresources/all_test.go +++ b/bundle/direct/dresources/all_test.go @@ -657,8 +657,9 @@ var testDeps = map[string]prepareWorkspace{ return nil, err } + // The permissions API takes the endpoint's ID, not its name. return &PermissionsState{ - ObjectID: "/serving-endpoints/" + waiter.Response.Name, + ObjectID: "/serving-endpoints/" + waiter.Response.Id, EmbeddedSlice: []StatePermission{{ Level: "CAN_MANAGE", UserName: "user@example.com", From 8f907435ab9376f1d89943e4b33611e1906d715e Mon Sep 17 00:00:00 2001 From: Anshul Gupta Date: Mon, 16 Nov 2026 07:47:43 +0000 Subject: [PATCH 5/6] Match the server's endpoint ID validation (root, any-case hex) Co-authored-by: Isaac Signed-off-by: Anshul Gupta --- cmd/workspace/serving-endpoints/permissions.go | 16 ++++++++++++---- .../serving-endpoints/permissions_test.go | 3 ++- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/cmd/workspace/serving-endpoints/permissions.go b/cmd/workspace/serving-endpoints/permissions.go index 29267609ac8..6a474c213eb 100644 --- a/cmd/workspace/serving-endpoints/permissions.go +++ b/cmd/workspace/serving-endpoints/permissions.go @@ -14,16 +14,24 @@ const permissionsNameHelp = ` You can also pass the endpoint NAME; it is resolved to the ID with "serving-endpoints get".` -// endpointIDLength is the length of a serving endpoint ID (32 hex characters). -const endpointIDLength = 32 +const ( + // endpointIDLength is the length of a serving endpoint ID (32 hex characters). + endpointIDLength = 32 + // rootEndpointID is the ID of the root object that endpoints inherit permissions from. + rootEndpointID = "root" +) -// isEndpointID reports whether s has the shape of a serving endpoint ID. +// isEndpointID reports whether s is a value the permissions API accepts as an +// endpoint ID: "root" or 32 hex characters, matching the server's validation. func isEndpointID(s string) bool { + if s == rootEndpointID { + return true + } if len(s) != endpointIDLength { return false } for _, r := range s { - if !strings.ContainsRune("0123456789abcdef", r) { + if !strings.ContainsRune("0123456789abcdefABCDEF", r) { return false } } diff --git a/cmd/workspace/serving-endpoints/permissions_test.go b/cmd/workspace/serving-endpoints/permissions_test.go index 738353f8fe0..56f7217dce6 100644 --- a/cmd/workspace/serving-endpoints/permissions_test.go +++ b/cmd/workspace/serving-endpoints/permissions_test.go @@ -12,7 +12,8 @@ func TestIsEndpointID(t *testing.T) { want bool }{ {"31ee674b7e6643f6930d5e12148fbe6f", true}, - {"31EE674B7E6643F6930D5E12148FBE6F", false}, + {"31EE674B7E6643F6930D5E12148FBE6F", true}, + {"root", true}, {"31ee674b-7e66-43f6-930d-5e12148fbe6f", false}, {"31ee674b7e6643f6930d5e12148fbe6", false}, {"my-endpoint", false}, From 53b30a0765dd255010e16911f0255f920809a2ef Mon Sep 17 00:00:00 2001 From: Anshul Gupta Date: Tue, 17 Nov 2026 04:05:28 +0000 Subject: [PATCH 6/6] Return the name lookup error and show NAME_OR_ID in usage Address review feedback on permissions-by-name: - A failed name lookup now returns that error instead of passing the value through to the API's "not a valid Inference Endpoint ID" error. The API only accepts root or 32 hex characters, so such values always failed; only the message changes. Endpoints without an ID get a clear error too. - The fake server accepts "root", and its comment explains why dashed UUIDs pass. - The acceptance test passes a literal 32-character ID and root and shows no lookup request for them. - Usage and help say NAME_OR_ID. Co-authored-by: Isaac Signed-off-by: Anshul Gupta --- .../permissions-by-name/output.txt | 27 +++++++----- .../permissions-by-name/script | 12 ++--- .../serving-endpoints/permissions.go | 39 +++++++++------- .../serving-endpoints/permissions_test.go | 44 +++++++++++++++++++ libs/testserver/permissions.go | 6 +++ 5 files changed, 97 insertions(+), 31 deletions(-) diff --git a/acceptance/cmd/workspace/serving-endpoints/permissions-by-name/output.txt b/acceptance/cmd/workspace/serving-endpoints/permissions-by-name/output.txt index 540249c448a..536c4dca4f1 100644 --- a/acceptance/cmd/workspace/serving-endpoints/permissions-by-name/output.txt +++ b/acceptance/cmd/workspace/serving-endpoints/permissions-by-name/output.txt @@ -7,15 +7,24 @@ >>> [CLI] serving-endpoints get-permissions my-endpoint {"object_id":"/serving-endpoints/[ENDPOINT_ID]"} -=== The ID still works +=== A 32-character ID and root are passed through with no lookup ->>> [CLI] serving-endpoints get-permissions [ENDPOINT_ID] -{"object_id":"/serving-endpoints/[ENDPOINT_ID]"} +>>> [CLI] serving-endpoints get-permissions 0123456789abcdef0123456789ABCDEF +{"object_id":"/serving-endpoints/0123456789abcdef0123456789ABCDEF"} + +>>> [CLI] serving-endpoints get-permissions root +{"object_id":"/serving-endpoints/root"} -=== A name that doesn't resolve gets the API's error +=== A name that doesn't resolve returns the lookup error >>> musterr [CLI] serving-endpoints get-permissions no-such-endpoint -Error: 'no-such-endpoint' is not a valid Inference Endpoint ID. +Error: "no-such-endpoint" is not a serving endpoint ID, and looking it up as an endpoint name failed: Resource serving.ServingEndpointDetailed not found: no-such-endpoint + +=== Help shows the name is accepted + +>>> [CLI] serving-endpoints get-permissions --help + NAME_OR_ID: The name or ID of the serving endpoint for which to get or manage + databricks serving-endpoints get-permissions NAME_OR_ID [flags] >>> print_requests.py //serving-endpoints //permissions --get { @@ -55,17 +64,13 @@ Error: 'no-such-endpoint' is not a valid Inference Endpoint ID. } { "method": "GET", - "path": "/api/2.0/serving-endpoints/[ENDPOINT_ID]" + "path": "/api/2.0/permissions/serving-endpoints/0123456789abcdef0123456789ABCDEF" } { "method": "GET", - "path": "/api/2.0/permissions/serving-endpoints/[ENDPOINT_ID]" + "path": "/api/2.0/permissions/serving-endpoints/root" } { "method": "GET", "path": "/api/2.0/serving-endpoints/no-such-endpoint" } -{ - "method": "GET", - "path": "/api/2.0/permissions/serving-endpoints/no-such-endpoint" -} diff --git a/acceptance/cmd/workspace/serving-endpoints/permissions-by-name/script b/acceptance/cmd/workspace/serving-endpoints/permissions-by-name/script index 12b8cd84918..5ee3f675a30 100644 --- a/acceptance/cmd/workspace/serving-endpoints/permissions-by-name/script +++ b/acceptance/cmd/workspace/serving-endpoints/permissions-by-name/script @@ -7,12 +7,14 @@ title "Permissions commands accept the endpoint name" trace $CLI serving-endpoints set-permissions my-endpoint --json '{"access_control_list": [{"group_name": "users", "permission_level": "CAN_QUERY"}]}' | jq -c '.access_control_list[] | {group_name, permission_level: .all_permissions[0].permission_level}' trace $CLI serving-endpoints get-permissions my-endpoint | jq -c '{object_id}' -# The fake server uses dashed UUIDs as endpoint IDs, so this also does a lookup by -# name first. A real 32-character ID is passed through without one. -title "The ID still works" -trace $CLI serving-endpoints get-permissions "$endpoint_id" | jq -c '{object_id}' +title "A 32-character ID and root are passed through with no lookup" +trace $CLI serving-endpoints get-permissions 0123456789abcdef0123456789ABCDEF | jq -c '{object_id}' +trace $CLI serving-endpoints get-permissions root | jq -c '{object_id}' -title "A name that doesn't resolve gets the API's error" +title "A name that doesn't resolve returns the lookup error" trace musterr $CLI serving-endpoints get-permissions no-such-endpoint +title "Help shows the name is accepted" +trace $CLI serving-endpoints get-permissions --help | grep -E 'get-permissions NAME_OR_ID|NAME_OR_ID: ' + trace print_requests.py //serving-endpoints //permissions --get diff --git a/cmd/workspace/serving-endpoints/permissions.go b/cmd/workspace/serving-endpoints/permissions.go index 6a474c213eb..402ace1a5f5 100644 --- a/cmd/workspace/serving-endpoints/permissions.go +++ b/cmd/workspace/serving-endpoints/permissions.go @@ -2,18 +2,14 @@ package serving_endpoints import ( "context" + "fmt" "strings" "github.com/databricks/cli/libs/cmdctx" - "github.com/databricks/cli/libs/log" "github.com/databricks/databricks-sdk-go/service/serving" "github.com/spf13/cobra" ) -const permissionsNameHelp = ` - You can also pass the endpoint NAME; it is resolved to the ID with - "serving-endpoints get".` - const ( // endpointIDLength is the length of a serving endpoint ID (32 hex characters). endpointIDLength = 32 @@ -41,29 +37,42 @@ func isEndpointID(s string) bool { // resolveEndpointID returns the ID of the endpoint named arg. The permissions // API only accepts endpoint IDs, and anything else fails with "is not a valid // Inference Endpoint ID", but users and agents know endpoints by name. Values -// shaped like an ID are passed through unchanged, as is any value that doesn't -// resolve, so the API's own error is still what the user sees. -func resolveEndpointID(ctx context.Context, arg string) string { +// shaped like an ID are passed through unchanged. Any other value can't be an +// ID the API accepts, so if the name lookup fails we return the lookup error, +// which says what is actually wrong (a typo, a missing permission), instead of +// sending the value on and getting the API's ID error. +func resolveEndpointID(ctx context.Context, arg string) (string, error) { if isEndpointID(arg) { - return arg + return arg, nil } w := cmdctx.WorkspaceClient(ctx) endpoint, err := w.ServingEndpoints.GetByName(ctx, arg) - if err != nil || endpoint.Id == "" { - log.Debugf(ctx, "could not resolve serving endpoint name %q to an ID: %v", arg, err) - return arg + if err != nil { + return "", fmt.Errorf("%q is not a serving endpoint ID, and looking it up as an endpoint name failed: %w", arg, err) + } + if endpoint.Id == "" { + // Endpoints without an ID, such as Foundation Model API pay-per-token + // endpoints, have no permissions object of their own. + return "", fmt.Errorf("serving endpoint %q has no endpoint ID, so its permissions can't be read or changed with this command", arg) } - return endpoint.Id + return endpoint.Id, nil } // permissionsOverride lets a permissions command take an endpoint name as well // as an ID. It's generic over the request type of each permissions command. func permissionsOverride[T any](cmd *cobra.Command, _ T) { - cmd.Long += permissionsNameHelp + cmd.Use = strings.Replace(cmd.Use, "SERVING_ENDPOINT_ID", "NAME_OR_ID", 1) + cmd.Long = strings.Replace(cmd.Long, + "SERVING_ENDPOINT_ID: The serving endpoint for which to get or manage permissions.", + "NAME_OR_ID: The name or ID of the serving endpoint for which to get or manage\n permissions. A name is resolved to the ID with \"serving-endpoints get\".", 1) generatedRunE := cmd.RunE cmd.RunE = func(cmd *cobra.Command, args []string) error { if len(args) > 0 { - args = append([]string{resolveEndpointID(cmd.Context(), args[0])}, args[1:]...) + id, err := resolveEndpointID(cmd.Context(), args[0]) + if err != nil { + return err + } + args = append([]string{id}, args[1:]...) } return generatedRunE(cmd, args) } diff --git a/cmd/workspace/serving-endpoints/permissions_test.go b/cmd/workspace/serving-endpoints/permissions_test.go index 56f7217dce6..f9b2c917ca1 100644 --- a/cmd/workspace/serving-endpoints/permissions_test.go +++ b/cmd/workspace/serving-endpoints/permissions_test.go @@ -1,9 +1,15 @@ package serving_endpoints import ( + "errors" "testing" + "github.com/databricks/cli/libs/cmdctx" + "github.com/databricks/databricks-sdk-go/experimental/mocks" + "github.com/databricks/databricks-sdk-go/service/serving" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/mock" + "github.com/stretchr/testify/require" ) func TestIsEndpointID(t *testing.T) { @@ -25,3 +31,41 @@ func TestIsEndpointID(t *testing.T) { }) } } + +func TestResolveEndpointID(t *testing.T) { + lookupErr := errors.New("Endpoint with name 'no-such-endpoint' does not exist.") + tests := []struct { + name string + arg string + endpoint *serving.ServingEndpointDetailed + err error + want string + wantErr string + }{ + {name: "an ID is passed through", arg: "31ee674b7e6643f6930d5e12148fbe6f", want: "31ee674b7e6643f6930d5e12148fbe6f"}, + {name: "root is passed through", arg: "root", want: "root"}, + {name: "a name resolves to its ID", arg: "my-endpoint", endpoint: &serving.ServingEndpointDetailed{Id: "31ee674b7e6643f6930d5e12148fbe6f"}, want: "31ee674b7e6643f6930d5e12148fbe6f"}, + {name: "a failed lookup returns the lookup error", arg: "no-such-endpoint", err: lookupErr, wantErr: `"no-such-endpoint" is not a serving endpoint ID, and looking it up as an endpoint name failed: Endpoint with name 'no-such-endpoint' does not exist.`}, + {name: "an endpoint without an ID is an error", arg: "databricks-gpt-5-4", endpoint: &serving.ServingEndpointDetailed{Name: "databricks-gpt-5-4"}, wantErr: `serving endpoint "databricks-gpt-5-4" has no endpoint ID`}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + m := mocks.NewMockWorkspaceClient(t) + if tt.endpoint != nil || tt.err != nil { + m.GetMockServingEndpointsAPI().EXPECT().GetByName(mock.Anything, tt.arg).Return(tt.endpoint, tt.err) + } + ctx := cmdctx.SetWorkspaceClient(t.Context(), m.WorkspaceClient) + + got, err := resolveEndpointID(ctx, tt.arg) + if tt.wantErr != "" { + require.ErrorContains(t, err, tt.wantErr) + if tt.err != nil { + assert.ErrorIs(t, err, tt.err) + } + return + } + require.NoError(t, err) + assert.Equal(t, tt.want, got) + }) + } +} diff --git a/libs/testserver/permissions.go b/libs/testserver/permissions.go index f59d720c627..e2c8f160bbc 100644 --- a/libs/testserver/permissions.go +++ b/libs/testserver/permissions.go @@ -205,10 +205,16 @@ func (s *FakeWorkspace) permissionsParentExists(requestObjectType, objectId stri // servingEndpointIDError mirrors the permissions API, which rejects a value // that isn't shaped like a serving endpoint ID, such as the endpoint's name. +// The real API accepts "root" or 32 hex characters (InferenceEndpointId). This +// fake also accepts dashed UUIDs, because that is the form nextUUID mints for +// its endpoint IDs; uuid.Parse accepts both that and the 32-character form. func servingEndpointIDError(requestObjectType, objectId string) *Response { if requestObjectType != "serving-endpoints" { return nil } + if objectId == "root" { + return nil + } if _, err := uuid.Parse(objectId); err == nil { return nil }