-
Notifications
You must be signed in to change notification settings - Fork 306
refactor: migrate project endpoint commands to new scaffold #8243
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
Draft
huimiu
wants to merge
5
commits into
main
Choose a base branch
from
huimiu/hui-migrate-project-command-to-projects
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.
Draft
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
3c255fc
feat(azure.ai.projects): migrate project endpoint commands from azure…
huimiu 45736ba
feat(azure.ai.projects): address PR 8243 review feedback
huimiu 587fa6c
fix(azure.ai.agents): bridge resolver to new extensions.ai-projects.c…
huimiu 3c0858f
chore: revert CHANGELOG edits and tighten new comments
huimiu 32390f6
fix(ai.projects): clear legacy endpoint
huimiu 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
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
18 changes: 18 additions & 0 deletions
18
cli/azd/extensions/azure.ai.projects/internal/cmd/extension_context.go
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,18 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| package cmd | ||
|
|
||
| import "github.com/azure/azure-dev/cli/azd/pkg/azdext" | ||
|
|
||
| // ensureExtensionContext returns a non-nil [azdext.ExtensionContext] so command | ||
| // constructors can be safely invoked from tests with a nil receiver. The SDK's | ||
| // [azdext.NewExtensionRootCommand] populates the real context (and its env-var | ||
| // fallback) before any leaf RunE runs, so tests that don't exercise RunE can | ||
| // safely pass nil here. | ||
| func ensureExtensionContext(extCtx *azdext.ExtensionContext) *azdext.ExtensionContext { | ||
| if extCtx == nil { | ||
| return &azdext.ExtensionContext{} | ||
| } | ||
| return extCtx | ||
| } |
32 changes: 32 additions & 0 deletions
32
cli/azd/extensions/azure.ai.projects/internal/cmd/flag_options_test.go
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,32 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| package cmd | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "testing" | ||
|
|
||
| "github.com/spf13/cobra" | ||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| // assertOutputFlagOptions verifies that cmd has the per-command --output flag | ||
| // configuration registered via [azdext.RegisterFlagOptions]. The SDK records | ||
| // these as cobra annotations rather than as a redeclared flag value, so we | ||
| // inspect cmd.Annotations directly rather than reading from cmd.Flags(). | ||
| func assertOutputFlagOptions(t *testing.T, cmd *cobra.Command, wantDefault string, wantAllowed []string) { | ||
| t.Helper() | ||
| require.NotNil(t, cmd) | ||
| require.NotNil(t, cmd.Annotations, "cmd.Annotations should be set by RegisterFlagOptions") | ||
|
|
||
| got := cmd.Annotations["azdext.default/output"] | ||
| assert.Equal(t, wantDefault, got, "default for --output") | ||
|
|
||
| allowedJSON := cmd.Annotations["azdext.allowed-values/output"] | ||
| require.NotEmpty(t, allowedJSON, "allowed values for --output should be set") | ||
| var allowed []string | ||
| require.NoError(t, json.Unmarshal([]byte(allowedJSON), &allowed)) | ||
| assert.Equal(t, wantAllowed, allowed, "allowed values for --output") | ||
| } |
101 changes: 101 additions & 0 deletions
101
cli/azd/extensions/azure.ai.projects/internal/cmd/project_context_store.go
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,101 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| package cmd | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "time" | ||
|
|
||
| "github.com/azure/azure-dev/cli/azd/pkg/azdext" | ||
| ) | ||
|
|
||
| // projectContextConfigPath is the UserConfig path for the persisted project context. | ||
| const projectContextConfigPath = configPathPrefix + ".context" | ||
|
|
||
| // projectContextState is the JSON shape stored at extensions.ai-projects.context | ||
| // in ~/.azd/config.json. | ||
| type projectContextState struct { | ||
| Endpoint string `json:"endpoint"` | ||
| SetAt string `json:"setAt"` | ||
| } | ||
|
|
||
| // getProjectContext reads the persisted project context from global config. | ||
| // Returns (state, true, nil) when present, (zero, false, nil) when absent. | ||
| func getProjectContext( | ||
| ctx context.Context, | ||
| azdClient *azdext.AzdClient, | ||
| ) (projectContextState, bool, error) { | ||
| ch, err := azdext.NewConfigHelper(azdClient) | ||
| if err != nil { | ||
| return projectContextState{}, false, fmt.Errorf("getProjectContext: %w", err) | ||
| } | ||
|
|
||
| var state projectContextState | ||
| found, err := ch.GetUserJSON(ctx, projectContextConfigPath, &state) | ||
| if err != nil { | ||
| return projectContextState{}, false, | ||
| fmt.Errorf("getProjectContext: failed to read config: %w", err) | ||
| } | ||
|
|
||
| if !found || state.Endpoint == "" { | ||
| return projectContextState{}, false, nil | ||
| } | ||
|
|
||
| return state, true, nil | ||
| } | ||
|
|
||
| // setProjectContext persists a validated project endpoint to global config. | ||
| // The caller is responsible for validating the endpoint before calling this function. | ||
| // Returns the setAt timestamp that was written to config. | ||
| func setProjectContext( | ||
| ctx context.Context, | ||
| azdClient *azdext.AzdClient, | ||
| endpoint string, | ||
| ) (setAt string, err error) { | ||
| ch, chErr := azdext.NewConfigHelper(azdClient) | ||
| if chErr != nil { | ||
| return "", fmt.Errorf("setProjectContext: %w", chErr) | ||
| } | ||
|
|
||
| state := projectContextState{ | ||
| Endpoint: endpoint, | ||
| SetAt: time.Now().UTC().Format(time.RFC3339), | ||
| } | ||
|
|
||
| if err := ch.SetUserJSON(ctx, projectContextConfigPath, state); err != nil { | ||
| return "", fmt.Errorf("setProjectContext: failed to write config: %w", err) | ||
| } | ||
|
|
||
| return state.SetAt, nil | ||
| } | ||
|
|
||
| // clearProjectContext removes the context subtree from global config. | ||
| // Returns the previously stored endpoint (empty if none was set). | ||
| // The operation is idempotent — calling it when no context is set is not an error. | ||
| func clearProjectContext( | ||
| ctx context.Context, | ||
| azdClient *azdext.AzdClient, | ||
| ) (previousEndpoint string, err error) { | ||
| // Read existing state first so we can return the previous endpoint. | ||
| state, found, err := getProjectContext(ctx, azdClient) | ||
| if err != nil { | ||
| return "", err | ||
| } | ||
|
|
||
| if found { | ||
| previousEndpoint = state.Endpoint | ||
| } | ||
|
|
||
| ch, chErr := azdext.NewConfigHelper(azdClient) | ||
| if chErr != nil { | ||
| return "", fmt.Errorf("clearProjectContext: %w", chErr) | ||
| } | ||
|
|
||
| if err := ch.UnsetUser(ctx, projectContextConfigPath); err != nil { | ||
| return "", fmt.Errorf("clearProjectContext: failed to clear config: %w", err) | ||
| } | ||
|
|
||
| return previousEndpoint, nil | ||
| } | ||
134 changes: 134 additions & 0 deletions
134
cli/azd/extensions/azure.ai.projects/internal/cmd/project_endpoint.go
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,134 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| package cmd | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "net/url" | ||
| "strings" | ||
|
|
||
| "azure.ai.projects/internal/exterrors" | ||
| ) | ||
|
|
||
| // configPathPrefix is the UserConfig namespace for the Foundry project context | ||
| // persisted by this extension (stored in ~/.azd/config.json). | ||
| const configPathPrefix = "extensions.ai-projects" | ||
|
|
||
| // EndpointSource identifies where the resolved project endpoint came from. | ||
| type EndpointSource string | ||
|
|
||
| const ( | ||
| // SourceFlag means the endpoint came from a --project-endpoint flag (level 1). | ||
| SourceFlag EndpointSource = "flag" | ||
| // SourceAzdEnv means the endpoint came from the active azd environment's | ||
| // AZURE_AI_PROJECT_ENDPOINT value (level 2). | ||
| SourceAzdEnv EndpointSource = "azdEnv" | ||
| // SourceGlobalConfig means the endpoint came from ~/.azd/config.json | ||
| // (extensions.ai-projects.context.endpoint) (level 3). | ||
| SourceGlobalConfig EndpointSource = "globalConfig" | ||
| // SourceFoundryEnv means the endpoint came from the FOUNDRY_PROJECT_ENDPOINT | ||
| // host environment variable (level 4). | ||
| SourceFoundryEnv EndpointSource = "foundryEnv" | ||
| ) | ||
|
|
||
| // foundryHostSuffixes is the authoritative list of accepted Foundry host suffixes. | ||
| var foundryHostSuffixes = []string{ | ||
| ".services.ai.azure.com", | ||
| } | ||
|
|
||
| // projectEndpointPathPrefix is the expected path prefix for Foundry project endpoints. | ||
| const projectEndpointPathPrefix = "/api/projects/" | ||
|
|
||
| // isFoundryHost reports whether the hostname ends with one of the recognized | ||
| // Foundry host suffixes. | ||
| func isFoundryHost(hostname string) bool { | ||
| h := strings.ToLower(hostname) | ||
| for _, suffix := range foundryHostSuffixes { | ||
| if strings.HasSuffix(h, suffix) { | ||
| return true | ||
| } | ||
| } | ||
| return false | ||
| } | ||
|
|
||
| // validateProjectEndpoint validates and normalizes a Foundry project endpoint URL. | ||
| // | ||
| // The URL must be an absolute https:// URL whose host ends with a recognized | ||
| // Foundry suffix (see [foundryHostSuffixes]). Whitespace is trimmed, trailing | ||
| // slashes are stripped, and the result is returned in normalized form. | ||
| // | ||
| // The second return value is true when the path does not look like | ||
| // /api/projects/<proj> — callers may use this as a non-fatal warning. | ||
| func validateProjectEndpoint(raw string) (normalized string, pathWarning bool, err error) { | ||
| raw = strings.TrimSpace(raw) | ||
| if raw == "" { | ||
| return "", false, exterrors.Validation( | ||
| exterrors.CodeInvalidParameter, | ||
| "project endpoint must not be empty", | ||
| "provide a Foundry project endpoint URL "+ | ||
| "(e.g. https://<account>.services.ai.azure.com/api/projects/<project>)", | ||
| ) | ||
| } | ||
|
|
||
| u, parseErr := url.Parse(raw) | ||
| if parseErr != nil { | ||
| return "", false, exterrors.Validation( | ||
| exterrors.CodeInvalidParameter, | ||
| fmt.Sprintf("invalid project endpoint URL: %v", parseErr), | ||
| "provide a valid https:// Foundry project endpoint URL", | ||
| ) | ||
| } | ||
|
|
||
| if !strings.EqualFold(u.Scheme, "https") { | ||
| return "", false, exterrors.Validation( | ||
| exterrors.CodeInvalidParameter, | ||
| "project endpoint must use https", | ||
| "provide an https:// URL", | ||
| ) | ||
| } | ||
|
|
||
| host := u.Hostname() | ||
| if host == "" || !isFoundryHost(host) { | ||
| return "", false, exterrors.Validation( | ||
| exterrors.CodeInvalidParameter, | ||
| fmt.Sprintf( | ||
| "project endpoint host %q is not a recognized Foundry host (*%s)", | ||
| host, foundryHostSuffixes[0], | ||
| ), | ||
| "the host must end with "+foundryHostSuffixes[0], | ||
| ) | ||
| } | ||
|
|
||
| if u.Port() != "" { | ||
| return "", false, exterrors.Validation( | ||
| exterrors.CodeInvalidParameter, | ||
| fmt.Sprintf("project endpoint host %q must not include a port", u.Host), | ||
| "remove the explicit port from the URL", | ||
| ) | ||
| } | ||
|
|
||
| // Normalize: lowercase host, strip trailing slash. | ||
| path := strings.TrimRight(u.EscapedPath(), "/") | ||
| normalized = fmt.Sprintf("https://%s%s", strings.ToLower(host), path) | ||
|
|
||
| // Warn when the path does not look like /api/projects/<proj>. | ||
| if !strings.HasPrefix(path, projectEndpointPathPrefix) || | ||
| strings.TrimPrefix(path, projectEndpointPathPrefix) == "" { | ||
| pathWarning = true | ||
| } | ||
|
|
||
| return normalized, pathWarning, nil | ||
| } | ||
|
|
||
| // noProjectEndpointError returns the structured dependency error used when no | ||
| // project endpoint could be resolved from any source. | ||
| func noProjectEndpointError() error { | ||
| return exterrors.Dependency( | ||
| exterrors.CodeMissingProjectEndpoint, | ||
| "no Foundry project endpoint resolved", | ||
| "persist a workspace default with `azd ai project set <endpoint>`, "+ | ||
| "or set AZURE_AI_PROJECT_ENDPOINT in the active azd environment, "+ | ||
| "or export FOUNDRY_PROJECT_ENDPOINT in your shell", | ||
| ) | ||
| } |
Oops, something went wrong.
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.