Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ func newJobCommand() *cobra.Command {
cmd.AddCommand(newJobListCommand())
cmd.AddCommand(newJobSubmitCommand())
cmd.AddCommand(newJobShowCommand())
cmd.AddCommand(newJobDeleteCommand())
cmd.AddCommand(newJobCancelCommand())

return cmd
Expand Down
Original file line number Diff line number Diff line change
@@ -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
}