-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
jackofallops
merged 5 commits into
main
from
f/static-web-app-function-app-registration
Mar 26, 2024
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
7537942
add support for static web app function app registration
jackofallops ee8fc8f
add doc
jackofallops 6d6fe51
add missed timeouts and import to doc
jackofallops 5911cc1
update appservice regions for static web app supported regions
jackofallops a559917
remove duplicate provider block from import test
jackofallops File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
191 changes: 191 additions & 0 deletions
191
internal/services/appservice/static_web_app_function_app_registration_resource.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
|
||
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 | ||
}, | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 thanregistration
for consistency within the provider?There was a problem hiding this comment.
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?