|
| 1 | +package create |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "github.com/spf13/cobra" |
| 8 | + |
| 9 | + "github.com/stackitcloud/stackit-cli/internal/pkg/args" |
| 10 | + cliErr "github.com/stackitcloud/stackit-cli/internal/pkg/errors" |
| 11 | + "github.com/stackitcloud/stackit-cli/internal/pkg/examples" |
| 12 | + "github.com/stackitcloud/stackit-cli/internal/pkg/flags" |
| 13 | + "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" |
| 14 | + "github.com/stackitcloud/stackit-cli/internal/pkg/print" |
| 15 | + "github.com/stackitcloud/stackit-cli/internal/pkg/projectname" |
| 16 | + "github.com/stackitcloud/stackit-cli/internal/pkg/services/modelexperiments/client" |
| 17 | + "github.com/stackitcloud/stackit-cli/internal/pkg/types" |
| 18 | + |
| 19 | + modelexperiments "github.com/stackitcloud/stackit-sdk-go/services/modelexperiments/v1api" |
| 20 | +) |
| 21 | + |
| 22 | +const ( |
| 23 | + nameFlag = "name" |
| 24 | + descriptionFlag = "description" |
| 25 | + labelFlag = "label" |
| 26 | + retentionFlag = "deleted-experiment-retention" |
| 27 | +) |
| 28 | + |
| 29 | +type inputModel struct { |
| 30 | + *globalflags.GlobalFlagModel |
| 31 | + |
| 32 | + Name string |
| 33 | + Description *string |
| 34 | + Labels *map[string]string |
| 35 | + Retention *string |
| 36 | + Region string |
| 37 | +} |
| 38 | + |
| 39 | +func NewCmd(params *types.CmdParams) *cobra.Command { |
| 40 | + cmd := &cobra.Command{ |
| 41 | + Use: "create", |
| 42 | + Short: "Creates an AI Model Experiments instance", |
| 43 | + Long: "Creates an AI Model Experiments (MLflow) instance in your STACKIT project.", |
| 44 | + Args: args.NoArgs, |
| 45 | + Example: examples.Build( |
| 46 | + examples.NewExample( |
| 47 | + `Create an AI Model Experiments instance with name "my-tracking"`, |
| 48 | + `$ stackit ai-model-experiments instance create --name my-tracking`), |
| 49 | + examples.NewExample( |
| 50 | + `Create an instance with a description and labels`, |
| 51 | + `$ stackit ai-model-experiments instance create --name my-tracking --description "team tracking server" --label env=prod`), |
| 52 | + ), |
| 53 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 54 | + ctx := context.Background() |
| 55 | + |
| 56 | + model, err := parseInput(params.Printer, cmd, args) |
| 57 | + if err != nil { |
| 58 | + return err |
| 59 | + } |
| 60 | + |
| 61 | + apiClient, err := client.ConfigureClient(params.Printer, params.CliVersion) |
| 62 | + if err != nil { |
| 63 | + return err |
| 64 | + } |
| 65 | + |
| 66 | + projectLabel, err := projectname.GetProjectName(ctx, params.Printer, params.CliVersion, cmd) |
| 67 | + if err != nil { |
| 68 | + params.Printer.Debug(print.ErrorLevel, "get project name: %v", err) |
| 69 | + projectLabel = model.ProjectId |
| 70 | + } |
| 71 | + |
| 72 | + prompt := fmt.Sprintf("Are you sure you want to create an AI Model Experiments instance for project %q?", projectLabel) |
| 73 | + if err := params.Printer.PromptForConfirmation(prompt); err != nil { |
| 74 | + return err |
| 75 | + } |
| 76 | + |
| 77 | + req := buildCreateInstanceRequest(ctx, model, apiClient) |
| 78 | + resp, err := req.Execute() |
| 79 | + if err != nil { |
| 80 | + return fmt.Errorf("create AI Model Experiments instance: %w", err) |
| 81 | + } |
| 82 | + |
| 83 | + return outputResult(params.Printer, model.OutputFormat, projectLabel, resp) |
| 84 | + }, |
| 85 | + } |
| 86 | + configureFlags(cmd) |
| 87 | + return cmd |
| 88 | +} |
| 89 | + |
| 90 | +func configureFlags(cmd *cobra.Command) { |
| 91 | + cmd.Flags().StringP(nameFlag, "n", "", "Instance name") |
| 92 | + cmd.Flags().String(descriptionFlag, "", "Instance description") |
| 93 | + cmd.Flags().StringToString(labelFlag, nil, `Labels as key-value pairs, e.g. "--label env=prod"`) |
| 94 | + cmd.Flags().String(retentionFlag, "", `Retention period for deleted experiments before permanent purge, e.g. "30d" (min 1d, max 90d)`) |
| 95 | + err := flags.MarkFlagsRequired(cmd, nameFlag) |
| 96 | + cobra.CheckErr(err) |
| 97 | +} |
| 98 | + |
| 99 | +func parseInput(p *print.Printer, cmd *cobra.Command, _ []string) (*inputModel, error) { |
| 100 | + globalFlags := globalflags.Parse(p, cmd) |
| 101 | + if globalFlags.ProjectId == "" { |
| 102 | + return nil, &cliErr.ProjectIdError{} |
| 103 | + } |
| 104 | + |
| 105 | + labels, err := cmd.Flags().GetStringToString(labelFlag) |
| 106 | + if err != nil { |
| 107 | + return nil, fmt.Errorf("parse %q flag: %w", labelFlag, err) |
| 108 | + } |
| 109 | + var labelsPtr *map[string]string |
| 110 | + if len(labels) > 0 { |
| 111 | + labelsPtr = &labels |
| 112 | + } |
| 113 | + |
| 114 | + model := inputModel{ |
| 115 | + GlobalFlagModel: globalFlags, |
| 116 | + Name: flags.FlagToStringValue(p, cmd, nameFlag), |
| 117 | + Description: flags.FlagToStringPointer(p, cmd, descriptionFlag), |
| 118 | + Labels: labelsPtr, |
| 119 | + Retention: flags.FlagToStringPointer(p, cmd, retentionFlag), |
| 120 | + Region: flags.FlagToStringValue(p, cmd, globalflags.RegionFlag), |
| 121 | + } |
| 122 | + |
| 123 | + p.DebugInputModel(model) |
| 124 | + return &model, nil |
| 125 | +} |
| 126 | + |
| 127 | +func buildCreateInstanceRequest(ctx context.Context, model *inputModel, apiClient *modelexperiments.APIClient) modelexperiments.ApiCreateInstanceRequest { |
| 128 | + req := apiClient.DefaultAPI.CreateInstance(ctx, model.ProjectId, model.Region) |
| 129 | + |
| 130 | + payload := modelexperiments.CreateInstancePayload{ |
| 131 | + Name: model.Name, |
| 132 | + } |
| 133 | + if model.Description != nil { |
| 134 | + payload.Description = model.Description |
| 135 | + } |
| 136 | + if model.Labels != nil { |
| 137 | + payload.Labels = model.Labels |
| 138 | + } |
| 139 | + if model.Retention != nil { |
| 140 | + payload.DeletedExperimentRetention = model.Retention |
| 141 | + } |
| 142 | + |
| 143 | + return req.CreateInstancePayload(payload) |
| 144 | +} |
| 145 | + |
| 146 | +func outputResult(p *print.Printer, outputFormat, projectLabel string, resp *modelexperiments.CreateInstanceResponse) error { |
| 147 | + if resp == nil { |
| 148 | + return fmt.Errorf("response instance is nil") |
| 149 | + } |
| 150 | + |
| 151 | + return p.OutputResult(outputFormat, resp.Instance, func() error { |
| 152 | + p.Outputf("Creating AI Model Experiments instance for project %q. Instance ID: %s\n", projectLabel, resp.Instance.Id) |
| 153 | + return nil |
| 154 | + }) |
| 155 | +} |
0 commit comments