Skip to content

Commit

Permalink
azurerm_static_web_app - add public_network_access_enabled attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexPykavy committed Jun 15, 2024
1 parent bd0d5e0 commit 83af6c8
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 0 deletions.
7 changes: 7 additions & 0 deletions internal/services/appservice/static_web_app_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ type StaticWebAppDataSourceModel struct {
DefaultHostName string `tfschema:"default_host_name"`
Identity []identity.ModelSystemAssignedUserAssigned `tfschema:"identity"`
PreviewEnvironments bool `tfschema:"preview_environments_enabled"`
PublicNetworkAccess bool `tfschema:"public_network_access_enabled"`
SkuTier string `tfschema:"sku_tier"`
SkuSize string `tfschema:"sku_size"`
Tags map[string]string `tfschema:"tags"`
Expand Down Expand Up @@ -70,6 +71,11 @@ func (s StaticWebAppDataSource) Attributes() map[string]*pluginsdk.Schema {
Computed: true,
},

"public_network_access_enabled": {
Type: pluginsdk.TypeBool,
Computed: true,
},

"sku_tier": {
Type: pluginsdk.TypeString,
Computed: true,
Expand Down Expand Up @@ -151,6 +157,7 @@ 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.PublicNetworkAccess = !strings.EqualFold(pointer.From(props.PublicNetworkAccess), helpers.PublicNetworkAccessDisabled)
}

if sku := model.Sku; sku != nil {
Expand Down
21 changes: 21 additions & 0 deletions internal/services/appservice/static_web_app_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ type StaticWebAppResourceModel struct {
ConfigFileChanges bool `tfschema:"configuration_file_changes_enabled"`
Identity []identity.ModelSystemAssignedUserAssigned `tfschema:"identity"`
PreviewEnvironments bool `tfschema:"preview_environments_enabled"`
PublicNetworkAccess bool `tfschema:"public_network_access_enabled"`
SkuTier string `tfschema:"sku_tier"`
SkuSize string `tfschema:"sku_size"`
Tags map[string]string `tfschema:"tags"`
Expand Down Expand Up @@ -73,6 +74,12 @@ func (r StaticWebAppResource) Arguments() map[string]*pluginsdk.Schema {
Default: true,
},

"public_network_access_enabled": {
Type: pluginsdk.TypeBool,
Optional: true,
Default: true,
},

"sku_tier": {
Type: pluginsdk.TypeString,
Optional: true,
Expand Down Expand Up @@ -182,12 +189,17 @@ func (r StaticWebAppResource) Create() sdk.ResourceFunc {
props := &staticsites.StaticSite{
AllowConfigFileUpdates: pointer.To(model.ConfigFileChanges),
StagingEnvironmentPolicy: pointer.To(staticsites.StagingEnvironmentPolicyEnabled),
PublicNetworkAccess: pointer.To(helpers.PublicNetworkAccessEnabled),
}

if !model.PreviewEnvironments {
props.StagingEnvironmentPolicy = pointer.To(staticsites.StagingEnvironmentPolicyDisabled)
}

if !model.PublicNetworkAccess {
props.PublicNetworkAccess = pointer.To(helpers.PublicNetworkAccessDisabled)
}

envelope.Properties = props

if err := client.CreateOrUpdateStaticSiteThenPoll(ctx, id, envelope); err != nil {
Expand Down Expand Up @@ -267,6 +279,7 @@ 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.PublicNetworkAccess = !strings.EqualFold(pointer.From(props.PublicNetworkAccess), helpers.PublicNetworkAccessDisabled)
}

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

if metadata.ResourceData.HasChange("public_network_access_enabled") {
if !config.PublicNetworkAccess {
model.Properties.PublicNetworkAccess = pointer.To(helpers.PublicNetworkAccessDisabled)
} else {
model.Properties.PublicNetworkAccess = pointer.To(helpers.PublicNetworkAccessEnabled)
}
}

if metadata.ResourceData.HasChange("tags") {
model.Tags = pointer.To(config.Tags)
}
Expand Down
71 changes: 71 additions & 0 deletions internal/services/appservice/static_web_app_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,54 @@ func TestAccAzureStaticWebApp_basicWithConfigShouldFail(t *testing.T) {
})
}

func TestAccAzureStaticWebApp_publicNetworkAccessDisabled(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_static_web_app", "test")
r := StaticWebAppResource{}

data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.publicNetworkAccessDisabled(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
check.That(data.ResourceName).Key("public_network_access_enabled").HasValue("false"),
),
},
data.ImportStep(),
})
}

func TestAccAzureStaticWebApp_publicNetworkAccessUpdate(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_static_web_app", "test")
r := StaticWebAppResource{}

data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.basic(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
check.That(data.ResourceName).Key("public_network_access_enabled").HasValue("true"),
),
},
data.ImportStep(),
{
Config: r.publicNetworkAccessDisabled(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
check.That(data.ResourceName).Key("public_network_access_enabled").HasValue("false"),
),
},
data.ImportStep(),
{
Config: r.basic(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
check.That(data.ResourceName).Key("public_network_access_enabled").HasValue("true"),
),
},
data.ImportStep(),
})
}

func (r StaticWebAppResource) Exists(ctx context.Context, clients *clients.Client, state *pluginsdk.InstanceState) (*bool, error) {
id, err := staticsites.ParseStaticSiteID(state.ID)
if err != nil {
Expand Down Expand Up @@ -643,3 +691,26 @@ resource "azurerm_static_web_app" "test" {
}
`, data.RandomInteger, data.Locations.Primary)
}

func (r StaticWebAppResource) publicNetworkAccessDisabled(data acceptance.TestData) string {
return fmt.Sprintf(`
provider "azurerm" {
features {}
}
resource "azurerm_resource_group" "test" {
name = "acctestRG-%d"
location = "%s"
}
resource "azurerm_static_web_app" "test" {
name = "acctestSS-%d"
location = azurerm_resource_group.test.location
resource_group_name = azurerm_resource_group.test.name
sku_size = "Free"
sku_tier = "Free"
public_network_access_enabled = false
}
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger)
}
2 changes: 2 additions & 0 deletions website/docs/d/static_web_app.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ The following arguments are supported:

* `preview_environments_enabled` - Are Preview (Staging) environments enabled.

* `public_network_access_enabled` - (Optional) Should public network access be enabled for the Static Web App. Defaults to `true`.

* `sku_tier` - The SKU tier of the Static Web App.

* `sku_size` - The SKU size of the Static Web App.
Expand Down
2 changes: 2 additions & 0 deletions website/docs/r/static_web_app.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ The following arguments are supported:

* `preview_environments_enabled` - (Optional) Are Preview (Staging) environments enabled. Defaults to `true`.

* `public_network_access_enabled` - (Optional) Should public network access be enabled for the Static Web App. Defaults to `true`.

* `sku_tier` - (Optional) Specifies the SKU tier of the Static Web App. Possible values are `Free` or `Standard`. Defaults to `Free`.

* `sku_size` - (Optional) Specifies the SKU size of the Static Web App. Possible values are `Free` or `Standard`. Defaults to `Free`.
Expand Down

0 comments on commit 83af6c8

Please sign in to comment.