Skip to content

Commit

Permalink
Correctly handle missing WebApps and document the custom resource int…
Browse files Browse the repository at this point in the history
…erface better (#3533)

Fixes #3526

The WebApp custom resource currently returns (notFound, error) when the
requested app doesn't exist. Although undocumented, the downstream
assumes that custom resources return (notFound, nil) in that case. I.e.,
it's the responsibility of the custom resource to distinguish between
"not found" and other kinds of errors.
  • Loading branch information
thomas11 authored Aug 21, 2024
1 parent 8c7b1a5 commit d60a23b
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
8 changes: 8 additions & 0 deletions provider/pkg/azure/azure.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"os"
"strings"

"github.com/Azure/azure-sdk-for-go/sdk/azcore"
"github.com/Azure/go-autorest/autorest"
"github.com/Azure/go-autorest/autorest/azure"
"github.com/pulumi/pulumi-azure-native/v2/provider/pkg/version"
Expand Down Expand Up @@ -38,6 +39,13 @@ func IsNotFound(err error) bool {
if requestError, ok := err.(azure.RequestError); ok {
return requestError.StatusCode == http.StatusNotFound
}
if requestError, ok := err.(*azure.RequestError); ok {
return requestError.StatusCode == http.StatusNotFound
}

if responseError, ok := err.(*azcore.ResponseError); ok {
return responseError.StatusCode == http.StatusNotFound
}
return false
}

Expand Down
5 changes: 4 additions & 1 deletion provider/pkg/resources/customresources/custom_webapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func makeWebAppResource(resourceType, path string, crudClientFactory crud.Resour
// the WebApp does not return the SiteConfig. We need to make a separate GET request to
// /config/web and merge the results. #1468
Read: func(ctx context.Context, id string, inputs resource.PropertyMap) (map[string]any, bool, error) {
// We can reasanably assume that WebApp and WebAppSlot share their API version since they are almost identical.
// We assume that WebApp and WebAppSlot share their API version since they are almost identical.
apiVersion, ok := versionLookup.GetDefaultApiVersionForResource("Web", "WebApp")
if !ok {
apiVersion = "2022-09-01" // default as of 2024-07
Expand All @@ -61,6 +61,9 @@ func makeWebAppResource(resourceType, path string, crudClientFactory crud.Resour

webAppResponse, err := crudClient.Read(ctx, id)
if err != nil {
if azure.IsNotFound(err) {
return nil, false, nil
}
return nil, false, err
}

Expand Down
3 changes: 2 additions & 1 deletion provider/pkg/resources/customresources/customresources.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ type CustomResource struct {
// Create a new resource from a map of input values. Returns a map of resource outputs that match the schema shape.
Create func(ctx context.Context, id string, inputs resource.PropertyMap) (map[string]interface{}, error)
// Read the state of an existing resource. Constructs the resource ID based on input values. Returns a map of
// resource outputs. If the requested resource does not exist, the second result is false.
// resource outputs. If the requested resource does not exist, the second result is false. In that case, the
// error must be nil.
Read CustomReadFunc
// Update an existing resource with a map of input values. Returns a map of resource outputs that match the schema shape.
Update func(ctx context.Context, id string, news, olds resource.PropertyMap) (map[string]interface{}, error)
Expand Down

0 comments on commit d60a23b

Please sign in to comment.