Repository navigation
serving-endpoints: accept the endpoint name in permissions commands #7662
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
anshul0713
wants to merge
6
commits into
main
Choose a base branch
from
anshul0713/serving-endpoints-permissions-by-name
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
2bb19e9
serving-endpoints: accept the endpoint name in permissions commands
anshul0713 5badb04
Point changelog fragment at #7662
anshul0713 665b5e8
testserver: use the standard library uuid package
anshul0713 d2b269b
Use the endpoint ID in the serving endpoint permissions fixture
anshul0713 8f90743
Match the server's endpoint ID validation (root, any-case hex)
anshul0713 53b30a0
Return the name lookup error and show NAME_OR_ID in usage
anshul0713 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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`. ([#7662](https://github.com/databricks/cli/pull/7662)) |
1 change: 1 addition & 0 deletions
1
acceptance/cmd/workspace/serving-endpoints/permissions-by-name/out.test.toml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Cloud = false |
76 changes: 76 additions & 0 deletions
76
acceptance/cmd/workspace/serving-endpoints/permissions-by-name/output.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
|
|
||
| === 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]"} | ||
|
|
||
| === A 32-character ID and root are passed through with no lookup | ||
|
|
||
| >>> [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 returns the lookup error | ||
|
|
||
| >>> musterr [CLI] serving-endpoints get-permissions no-such-endpoint | ||
| 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 | ||
| { | ||
| "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/permissions/serving-endpoints/0123456789abcdef0123456789ABCDEF" | ||
| } | ||
| { | ||
| "method": "GET", | ||
| "path": "/api/2.0/permissions/serving-endpoints/root" | ||
| } | ||
| { | ||
| "method": "GET", | ||
| "path": "/api/2.0/serving-endpoints/no-such-endpoint" | ||
| } |
20 changes: 20 additions & 0 deletions
20
acceptance/cmd/workspace/serving-endpoints/permissions-by-name/script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| $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}' | ||
|
|
||
| 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 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 |
2 changes: 2 additions & 0 deletions
2
acceptance/cmd/workspace/serving-endpoints/permissions-by-name/test.toml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| Cloud = false | ||
| RecordRequests = true |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| package serving_endpoints | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "strings" | ||
|
|
||
| "github.com/databricks/cli/libs/cmdctx" | ||
| "github.com/databricks/databricks-sdk-go/service/serving" | ||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| 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 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("0123456789abcdefABCDEF", 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. 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, nil | ||
| } | ||
| w := cmdctx.WorkspaceClient(ctx) | ||
| endpoint, err := w.ServingEndpoints.GetByName(ctx, 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, 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.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 { | ||
| id, err := resolveEndpointID(cmd.Context(), args[0]) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| args = append([]string{id}, 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]) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| 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) { | ||
| tests := []struct { | ||
| in string | ||
| want bool | ||
| }{ | ||
| {"31ee674b7e6643f6930d5e12148fbe6f", true}, | ||
| {"31EE674B7E6643F6930D5E12148FBE6F", true}, | ||
| {"root", true}, | ||
| {"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)) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| 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) | ||
| }) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.