Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New Resource: azurerm_static_web_app_function_app_registration #25331

Merged
merged 5 commits into from
Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions internal/services/appservice/registration.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ func (r Registration) Resources() []sdk.Resource {
SourceControlSlotResource{},
StaticWebAppResource{},
StaticWebAppCustomDomainResource{},
StaticWebAppFunctionAppRegistrationResource{},
WebAppActiveSlotResource{},
WebAppHybridConnectionResource{},
WindowsFunctionAppResource{},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
package appservice

import (
"context"
"fmt"
"time"

"github.com/hashicorp/go-azure-helpers/lang/pointer"
"github.com/hashicorp/go-azure-helpers/lang/response"
"github.com/hashicorp/go-azure-helpers/resourcemanager/commonids"
"github.com/hashicorp/go-azure-helpers/resourcemanager/location"
"github.com/hashicorp/go-azure-sdk/resource-manager/web/2023-01-01/staticsites"
"github.com/hashicorp/terraform-provider-azurerm/internal/sdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
)

type StaticWebAppFunctionAppRegistrationResource struct{}

var _ sdk.Resource = StaticWebAppFunctionAppRegistrationResource{}

type StaticWebAppFunctionAppRegistrationModel struct {
StaticWebAppID string `tfschema:"static_web_app_id"`
FunctionAppID string `tfschema:"function_app_id"`
}

func (r StaticWebAppFunctionAppRegistrationResource) ResourceType() string {
return "azurerm_static_web_app_function_app_registration"
Comment on lines +26 to +27
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

worth calling this association rather than registration for consistency within the provider?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given the service nomenclature, and the fact it updates the target Function App authentication, I think registration is probably appropriate here?

}

func (r StaticWebAppFunctionAppRegistrationResource) ModelObject() interface{} {
return &StaticWebAppFunctionAppRegistrationModel{}
}

func (r StaticWebAppFunctionAppRegistrationResource) IDValidationFunc() pluginsdk.SchemaValidateFunc {
return staticsites.ValidateUserProvidedFunctionAppID
}

func (r StaticWebAppFunctionAppRegistrationResource) Arguments() map[string]*pluginsdk.Schema {
return map[string]*pluginsdk.Schema{
"static_web_app_id": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: staticsites.ValidateStaticSiteID,
},

"function_app_id": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: commonids.ValidateFunctionAppID,
},
}
}

func (r StaticWebAppFunctionAppRegistrationResource) Attributes() map[string]*pluginsdk.Schema {
return map[string]*pluginsdk.Schema{}
}

func (r StaticWebAppFunctionAppRegistrationResource) Create() sdk.ResourceFunc {
return sdk.ResourceFunc{
Timeout: 30 * time.Minute,
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error {
client := metadata.Client.AppService.StaticSitesClient
appClient := metadata.Client.AppService.WebAppsClient

model := StaticWebAppFunctionAppRegistrationModel{}

if err := metadata.Decode(&model); err != nil {
return err
}

staticAppId, err := staticsites.ParseStaticSiteID(model.StaticWebAppID)
if err != nil {
return err
}

functionAppId, err := commonids.ParseAppServiceID(model.FunctionAppID)
if err != nil {
return err
}

app, err := appClient.Get(ctx, *functionAppId)
if err != nil {
return fmt.Errorf("reading specified %s: %+v", *functionAppId, err)
}

loc := ""
if appModel := app.Model; appModel != nil {
loc = location.Normalize(appModel.Location)
}

id := staticsites.NewUserProvidedFunctionAppID(staticAppId.SubscriptionId, staticAppId.ResourceGroupName, staticAppId.StaticSiteName, functionAppId.SiteName)

existing, err := client.GetUserProvidedFunctionAppForStaticSite(ctx, id)
if err != nil {
if !response.WasNotFound(existing.HttpResponse) {
return fmt.Errorf("checking for presence of existing %s: %+v", id, err)
}
}
if !response.WasNotFound(existing.HttpResponse) {
return metadata.ResourceRequiresImport(r.ResourceType(), id)
}

backends, err := client.GetLinkedBackends(ctx, *staticAppId)
if err != nil {
return fmt.Errorf("checking for existing Static Site backends for %s: %+v", id, err)
}

if backendList := backends.Model; backendList != nil {
if len(*backendList) != 0 {
return fmt.Errorf("%s already has a backend and cannot have another", id)
tombuildsstuff marked this conversation as resolved.
Show resolved Hide resolved
}
}

payload := staticsites.StaticSiteUserProvidedFunctionAppARMResource{
Properties: &staticsites.StaticSiteUserProvidedFunctionAppARMResourceProperties{
FunctionAppRegion: pointer.To(loc),
FunctionAppResourceId: pointer.To(functionAppId.ID()),
},
}

if err = client.RegisterUserProvidedFunctionAppWithStaticSiteThenPoll(ctx, id, payload, staticsites.DefaultRegisterUserProvidedFunctionAppWithStaticSiteOperationOptions()); err != nil {
return fmt.Errorf("creating %s: %+v", id, err)
}

metadata.SetID(id)

return nil
},
}
}

func (r StaticWebAppFunctionAppRegistrationResource) Read() sdk.ResourceFunc {
return sdk.ResourceFunc{
Timeout: 5 * time.Minute,
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error {
client := metadata.Client.AppService.StaticSitesClient

state := StaticWebAppFunctionAppRegistrationModel{}

id, err := staticsites.ParseUserProvidedFunctionAppID(metadata.ResourceData.Id())
if err != nil {
return err
}

state.StaticWebAppID = staticsites.NewStaticSiteID(id.SubscriptionId, id.ResourceGroupName, id.StaticSiteName).ID()

result, err := client.GetUserProvidedFunctionAppForStaticSite(ctx, *id)
if err != nil {
if response.WasNotFound(result.HttpResponse) {
return metadata.MarkAsGone(*id)
}
return fmt.Errorf("retrieving %s: %+v", *id, err)
}

if model := result.Model; model != nil {
if props := model.Properties; props != nil {
functionAppId, err := commonids.ParseAppServiceIDInsensitively(pointer.From(props.FunctionAppResourceId))
if err != nil {
return err
}

state.FunctionAppID = functionAppId.ID()
}
}

return metadata.Encode(&state)
},
}
}

func (r StaticWebAppFunctionAppRegistrationResource) Delete() sdk.ResourceFunc {
return sdk.ResourceFunc{
Timeout: 30 * time.Minute,
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error {
client := metadata.Client.AppService.StaticSitesClient

id, err := staticsites.ParseUserProvidedFunctionAppID(metadata.ResourceData.Id())
if err != nil {
return err
}

if _, err := client.DetachUserProvidedFunctionAppFromStaticSite(ctx, *id); err != nil {
return fmt.Errorf("deleting %s: %+v", *id, err)
}

return nil
},
}
}
Loading
Loading