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

azurerm_static_web_app - support repository_url, repository_branch, and repository_token properties #27401

Merged
merged 13 commits into from
Jan 8, 2025
Merged
14 changes: 14 additions & 0 deletions internal/services/appservice/static_web_app_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ type StaticWebAppDataSourceModel struct {
SkuTier string `tfschema:"sku_tier"`
SkuSize string `tfschema:"sku_size"`
Tags map[string]string `tfschema:"tags"`
RepositoryUrl string `tfschema:"repository_url"`
RepositoryBranch string `tfschema:"repository_branch"`
}

func (s StaticWebAppDataSource) Arguments() map[string]*pluginsdk.Schema {
Expand Down Expand Up @@ -103,6 +105,16 @@ func (s StaticWebAppDataSource) Attributes() map[string]*pluginsdk.Schema {
Computed: true,
},

"repository_url": {
Type: pluginsdk.TypeString,
Computed: true,
},

"repository_branch": {
Type: pluginsdk.TypeString,
Computed: true,
},

"tags": tags.SchemaDataSource(),
}
}
Expand Down Expand Up @@ -151,6 +163,8 @@ func (s StaticWebAppDataSource) Read() sdk.ResourceFunc {
state.ConfigFileChanges = pointer.From(props.AllowConfigFileUpdates)
state.DefaultHostName = pointer.From(props.DefaultHostname)
state.PreviewEnvironments = pointer.From(props.StagingEnvironmentPolicy) == staticsites.StagingEnvironmentPolicyEnabled
state.RepositoryUrl = pointer.From(props.RepositoryUrl)
state.RepositoryBranch = pointer.From(props.Branch)
}

if sku := model.Sku; sku != nil {
Expand Down
61 changes: 61 additions & 0 deletions internal/services/appservice/static_web_app_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ type StaticWebAppResourceModel struct {

ApiKey string `tfschema:"api_key"`
DefaultHostName string `tfschema:"default_host_name"`

RepositoryUrl string `tfschema:"repository_url"`
RepositoryToken string `tfschema:"repository_token"`
RepositoryBranch string `tfschema:"repository_branch"`
}

func (r StaticWebAppResource) Arguments() map[string]*pluginsdk.Schema {
Expand Down Expand Up @@ -105,6 +109,23 @@ func (r StaticWebAppResource) Arguments() map[string]*pluginsdk.Schema {

"identity": commonschema.SystemAssignedUserAssignedIdentityOptional(),

"repository_url": {
Type: pluginsdk.TypeString,
Optional: true,
ValidateFunc: validation.IsURLWithHTTPorHTTPS,
},

"repository_token": {
Type: pluginsdk.TypeString,
Optional: true,
Sensitive: true,
},

"repository_branch": {
Type: pluginsdk.TypeString,
Optional: true,
},
ned1313 marked this conversation as resolved.
Show resolved Hide resolved

"tags": tags.Schema(),
}
}
Expand Down Expand Up @@ -188,6 +209,17 @@ func (r StaticWebAppResource) Create() sdk.ResourceFunc {
props.StagingEnvironmentPolicy = pointer.To(staticsites.StagingEnvironmentPolicyDisabled)
}

// Check if repository URL, branch, or token are set
if model.RepositoryUrl != "" || model.RepositoryBranch != "" || model.RepositoryToken != "" {
// Make sure all three are set or throw an error
if model.RepositoryUrl == "" || model.RepositoryBranch == "" || model.RepositoryToken == "" {
return fmt.Errorf("repository_url, repository_branch, and repository_token must all be set")
}
props.Branch = pointer.To(model.RepositoryBranch)
props.RepositoryUrl = pointer.To(model.RepositoryUrl)
props.RepositoryToken = pointer.To(model.RepositoryToken)
}
ned1313 marked this conversation as resolved.
Show resolved Hide resolved

envelope.Properties = props

if err := client.CreateOrUpdateStaticSiteThenPoll(ctx, id, envelope); err != nil {
Expand Down Expand Up @@ -267,6 +299,13 @@ func (r StaticWebAppResource) Read() sdk.ResourceFunc {
state.ConfigFileChanges = pointer.From(props.AllowConfigFileUpdates)
state.DefaultHostName = pointer.From(props.DefaultHostname)
state.PreviewEnvironments = pointer.From(props.StagingEnvironmentPolicy) == staticsites.StagingEnvironmentPolicyEnabled
state.RepositoryUrl = pointer.From(props.RepositoryUrl)
state.RepositoryBranch = pointer.From(props.Branch)

// Token isn't returned in the response, so we need to grab it from the config
if repositoryToken, ok := metadata.ResourceData.GetOk("repository_token"); ok {
state.RepositoryToken = repositoryToken.(string)
}
}

if sku := model.Sku; sku != nil {
Expand Down Expand Up @@ -436,6 +475,28 @@ func (r StaticWebAppResource) Update() sdk.ResourceFunc {
}
}

if metadata.ResourceData.HasChange("repository_url") {
// If the repository URL is being changes to nothing, make sure the branch and token are also empty
if config.RepositoryUrl == "" && (config.RepositoryBranch != "" || config.RepositoryToken != "") {
return fmt.Errorf("repository_url cannot be empty if repository_branch or repository_token are set")
}
model.Properties.RepositoryUrl = pointer.To(config.RepositoryUrl)
}

if metadata.ResourceData.HasChange("repository_branch") {
if config.RepositoryBranch == "" && (config.RepositoryUrl != "" || config.RepositoryToken != "") {
return fmt.Errorf("repository_branch cannot be empty if repository_url or repository_token are set")
}
model.Properties.Branch = pointer.To(config.RepositoryBranch)
}

if metadata.ResourceData.HasChange("repository_token") {
if config.RepositoryToken == "" && (config.RepositoryBranch != "" || config.RepositoryUrl != "") {
return fmt.Errorf("repository_token cannot be empty if repository_branch or repository_url are set")
}
model.Properties.RepositoryToken = pointer.To(config.RepositoryToken)
}
ned1313 marked this conversation as resolved.
Show resolved Hide resolved

return nil
},
}
Expand Down
4 changes: 4 additions & 0 deletions website/docs/d/static_web_app.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ The following arguments are supported:

* `identity` - An `identity` block as defined below.

* `repository_branch` - Repository branch of the Static Web App.

* `repository_url` - Repository URL of the Static Web App.

* `tags` - The mapping of tags assigned to the resource.

---
Expand Down
6 changes: 6 additions & 0 deletions website/docs/r/static_web_app.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ The following arguments are supported:

* `app_settings` - (Optional) A key-value pair of App Settings.

* `repository_branch` - (Optional) Repository branch to use for the Static Web App. `repository_url` and `repository_token` must also be set.

* `repository_url` - (Optional) Repository URL to use for the Static Web App. `repository_branch` and `repository_token` must also be set.

* `reposistory_token` - (Optional) Repository Token with `admin` privileges to use for the Static Web App. `repository_branch` and `repository_url` must also be set.
ned1313 marked this conversation as resolved.
Show resolved Hide resolved

* `tags` - (Optional) A mapping of tags to assign to the resource.

---
Expand Down
Loading