@@ -2,8 +2,10 @@ package inference
22
33import (
44 "context"
5+ "errors"
6+ "fmt"
7+ "net/http"
58 "reflect"
6- "strings"
79 "time"
810
911 "github.com/fatih/color"
@@ -15,6 +17,8 @@ import (
1517
1618const (
1719 deploymentActionTimeout = 60 * time .Minute
20+ deploymentActionCreate = 1
21+ deploymentActionDelete = 2
1822)
1923
2024var deploymentStateMarshalSpecs = human.EnumMarshalSpecs {
@@ -43,40 +47,13 @@ func DeploymentMarshalerFunc(i any, opt *human.MarshalOpt) (string, error) {
4347 return str , nil
4448}
4549
46- var completeListNodeTypesCache * inference.ListNodeTypesResponse
50+ func deploymentDeleteBuilder (c * core.Command ) * core.Command {
51+ c .WaitFunc = waitForDeploymentFunc (deploymentActionDelete )
4752
48- func autocompleteDeploymentNodeType (
49- ctx context.Context ,
50- prefix string ,
51- request any ,
52- ) core.AutocompleteSuggestions {
53- req := request .(* inference.CreateDeploymentRequest )
54- suggestions := core .AutocompleteSuggestions (nil )
55-
56- client := core .ExtractClient (ctx )
57- api := inference .NewAPI (client )
58-
59- if completeListNodeTypesCache == nil {
60- res , err := api .ListNodeTypes (& inference.ListNodeTypesRequest {
61- Region : req .Region ,
62- })
63- if err != nil {
64- return nil
65- }
66- completeListNodeTypesCache = res
67- }
68-
69- for _ , nodeType := range completeListNodeTypesCache .NodeTypes {
70- if strings .HasPrefix (nodeType .Name , prefix ) {
71- suggestions = append (suggestions , nodeType .Name )
72- }
73- }
74-
75- return suggestions
53+ return c
7654}
7755
7856func deploymentCreateBuilder (c * core.Command ) * core.Command {
79- c .ArgSpecs .GetByName ("node-type-name" ).AutoCompleteFunc = autocompleteDeploymentNodeType
8057 type llmInferenceEndpointSpecCustom struct {
8158 * inference.EndpointSpec
8259 IsPublic bool `json:"is-public"`
@@ -96,17 +73,8 @@ func deploymentCreateBuilder(c *core.Command) *core.Command {
9673
9774 c .ArgsType = reflect .TypeOf (llmInferenceCreateDeploymentRequestCustom {})
9875
99- c .WaitFunc = func (ctx context.Context , _ , respI any ) (any , error ) {
100- api := inference .NewAPI (core .ExtractClient (ctx ))
76+ c .WaitFunc = waitForDeploymentFunc (deploymentActionCreate )
10177
102- return api .WaitForDeployment (& inference.WaitForDeploymentRequest {
103- DeploymentID : respI .(* inference.Deployment ).ID ,
104- Region : respI .(* inference.Deployment ).Region ,
105- Status : respI .(* inference.Deployment ).Status ,
106- Timeout : scw .TimeDurationPtr (deploymentActionTimeout ),
107- RetryInterval : core .DefaultRetryInterval ,
108- })
109- }
11078 c .Interceptor = func (ctx context.Context , argsI any , runner core.CommandRunner ) (any , error ) {
11179 deploymentCreateCustomRequest := argsI .(* llmInferenceCreateDeploymentRequestCustom )
11280 deploymentRequest := deploymentCreateCustomRequest .CreateDeploymentRequest
@@ -145,3 +113,35 @@ func deploymentCreateBuilder(c *core.Command) *core.Command {
145113
146114 return c
147115}
116+
117+ func waitForDeploymentFunc (action int ) core.WaitFunc {
118+ return func (ctx context.Context , _ , respI any ) (any , error ) {
119+ deployment , err := inference .NewAPI (core .ExtractClient (ctx )).WaitForDeployment (& inference.WaitForDeploymentRequest {
120+ DeploymentID : respI .(* inference.Deployment ).ID ,
121+ Region : respI .(* inference.Deployment ).Region ,
122+ Timeout : scw .TimeDurationPtr (deploymentActionTimeout ),
123+ RetryInterval : core .DefaultRetryInterval ,
124+ })
125+
126+ switch action {
127+ case deploymentActionCreate :
128+ return deployment , err
129+ case deploymentActionDelete :
130+ if err != nil {
131+ // if we get a 404 here, it means the resource was successfully deleted
132+ notFoundError := & scw.ResourceNotFoundError {}
133+ responseError := & scw.ResponseError {}
134+ if errors .As (err , & responseError ) &&
135+ responseError .StatusCode == http .StatusNotFound ||
136+ errors .As (err , & notFoundError ) {
137+ return fmt .Sprintf (
138+ "Server %s successfully deleted." ,
139+ respI .(* inference.Deployment ).ID ,
140+ ), nil
141+ }
142+ }
143+ }
144+
145+ return nil , err
146+ }
147+ }
0 commit comments