Skip to content

Commit 6772f58

Browse files
author
Farhan Nawaz
committed
2 parents 9dc600d + d621e52 commit 6772f58

2 files changed

Lines changed: 26 additions & 0 deletions

File tree

cli/azd/extensions/azure.ai.rle/internal/cmd/client.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"bytes"
88
"context"
99
"encoding/json"
10+
"errors"
1011
"fmt"
1112
"io"
1213
"net/http"
@@ -130,6 +131,14 @@ func (e *rleHTTPError) Error() string {
130131
return fmt.Sprintf("RLE control plane returned HTTP %d: %s", e.statusCode, strings.TrimSpace(e.body))
131132
}
132133

134+
// isNotFoundError reports whether err is an RLE control plane error with HTTP 404 status.
135+
func isNotFoundError(err error) bool {
136+
if httpErr, ok := errors.AsType[*rleHTTPError](err); ok {
137+
return httpErr.statusCode == http.StatusNotFound
138+
}
139+
return false
140+
}
141+
133142
func newRleClient(endpoint string) *rleClient {
134143
return &rleClient{
135144
baseUrl: strings.TrimRight(endpoint, "/"),

cli/azd/extensions/azure.ai.rle/internal/cmd/deploy.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ func newDeployCommand() *cobra.Command {
4949
if err != nil {
5050
return err
5151
}
52+
5253
environmentId := firstNonEmpty(state.EnvironmentId, slug(state.Name))
5354
client := newRleClient(resolveControlPlaneEndpoint(""))
5455
request := v1EnvironmentRequest{
@@ -62,16 +63,32 @@ func newDeployCommand() *cobra.Command {
6263
if !created {
6364
action = "Updating"
6465
}
66+
6567
if _, err := fmt.Fprintf(cmd.OutOrStdout(), "Skipping build; using existing image '%s'.\n", image); err != nil {
6668
return err
6769
}
70+
6871
if _, err := fmt.Fprintf(cmd.OutOrStdout(), "%s environment '%s' (image=%s) ...\n", action, state.Name, image); err != nil {
6972
return err
7073
}
7174
if state.EnvironmentId == "" {
7275
environment, err = client.createV1Environment(cmd.Context(), state.Project, request)
7376
} else {
7477
environment, err = client.updateV1Environment(cmd.Context(), state.Project, environmentId, request)
78+
if isNotFoundError(err) {
79+
// The recorded environment no longer exists in the target project
80+
// (e.g. the project changed or the control plane was reset). Recreate it.
81+
if _, msgErr := fmt.Fprintf(
82+
cmd.OutOrStdout(),
83+
"Environment '%s' not found in project '%s'; creating a new one.\n",
84+
environmentId,
85+
state.Project,
86+
); msgErr != nil {
87+
return msgErr
88+
}
89+
created = true
90+
environment, err = client.createV1Environment(cmd.Context(), state.Project, request)
91+
}
7592
}
7693
if err != nil {
7794
return serviceError(err)

0 commit comments

Comments
 (0)