From 0f97a2c286c310b2a9210e113f03e3268590c3c2 Mon Sep 17 00:00:00 2001 From: Saanika Gupta Date: Tue, 24 Mar 2026 10:33:55 +0530 Subject: [PATCH] Impelement delete job command for custom training --- .../internal/cmd/job.go | 1 + .../internal/cmd/job_delete.go | 96 +++++++++++++++++++ 2 files changed, 97 insertions(+) create mode 100644 cli/azd/extensions/azure.ai.customtraining/internal/cmd/job_delete.go diff --git a/cli/azd/extensions/azure.ai.customtraining/internal/cmd/job.go b/cli/azd/extensions/azure.ai.customtraining/internal/cmd/job.go index e3c1e220fd1..98b33b6eeb9 100644 --- a/cli/azd/extensions/azure.ai.customtraining/internal/cmd/job.go +++ b/cli/azd/extensions/azure.ai.customtraining/internal/cmd/job.go @@ -32,6 +32,7 @@ func newJobCommand() *cobra.Command { cmd.AddCommand(newJobListCommand()) cmd.AddCommand(newJobSubmitCommand()) cmd.AddCommand(newJobShowCommand()) + cmd.AddCommand(newJobDeleteCommand()) return cmd } diff --git a/cli/azd/extensions/azure.ai.customtraining/internal/cmd/job_delete.go b/cli/azd/extensions/azure.ai.customtraining/internal/cmd/job_delete.go new file mode 100644 index 00000000000..e1d3c974b20 --- /dev/null +++ b/cli/azd/extensions/azure.ai.customtraining/internal/cmd/job_delete.go @@ -0,0 +1,96 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +package cmd + +import ( + "bufio" + "fmt" + "os" + "strings" + + "azure.ai.customtraining/internal/utils" + "azure.ai.customtraining/pkg/client" + + "github.com/Azure/azure-sdk-for-go/sdk/azidentity" + "github.com/azure/azure-dev/cli/azd/pkg/azdext" + "github.com/spf13/cobra" +) + +func newJobDeleteCommand() *cobra.Command { + var name string + var yes bool + + cmd := &cobra.Command{ + Use: "delete", + Short: "Delete a training job", + Args: cobra.NoArgs, + RunE: func(cmd *cobra.Command, args []string) error { + ctx := azdext.WithAccessToken(cmd.Context()) + + if name == "" { + return fmt.Errorf("--name is required: provide the job name/ID to delete") + } + + // Prompt for confirmation unless --yes is specified + if !yes { + fmt.Printf("? Are you sure you want to delete job '%s'? [y/N] ", name) + reader := bufio.NewReader(os.Stdin) + response, err := reader.ReadString('\n') + if err != nil { + return fmt.Errorf("failed to read input: %w", err) + } + response = strings.TrimSpace(strings.ToLower(response)) + if response != "y" && response != "yes" { + fmt.Println("Delete cancelled.") + return nil + } + } + + azdClient, err := azdext.NewAzdClient() + if err != nil { + return fmt.Errorf("failed to create azd client: %w", err) + } + defer azdClient.Close() + + envValues, err := utils.GetEnvironmentValues(ctx, azdClient) + if err != nil { + return fmt.Errorf("failed to get environment values: %w", err) + } + + accountName := envValues[utils.EnvAzureAccountName] + projectName := envValues[utils.EnvAzureProjectName] + tenantID := envValues[utils.EnvAzureTenantID] + + if accountName == "" || projectName == "" { + return fmt.Errorf("environment not configured. Run 'azd ai training init' first") + } + + credential, err := azidentity.NewAzureDeveloperCLICredential(&azidentity.AzureDeveloperCLICredentialOptions{ + TenantID: tenantID, + AdditionallyAllowedTenants: []string{"*"}, + }) + if err != nil { + return fmt.Errorf("failed to create azure credential: %w", err) + } + + endpoint := buildProjectEndpoint(accountName, projectName) + apiClient, err := client.NewClient(endpoint, credential) + if err != nil { + return fmt.Errorf("failed to create API client: %w", err) + } + + if err := apiClient.DeleteJob(ctx, name); err != nil { + return fmt.Errorf("failed to delete job: %w", err) + } + + fmt.Printf("✓ Job '%s' deleted.\n", name) + return nil + }, + } + + cmd.Flags().StringVar(&name, "name", "", "Job name/ID to delete (required)") + cmd.Flags().BoolVar(&yes, "yes", false, "Skip confirmation prompt") + + return cmd +}