Skip to content

Commit

Permalink
azurerm_static_site - Add support for app_settings (#23421)
Browse files Browse the repository at this point in the history
  • Loading branch information
mbfrahry authored Sep 29, 2023
1 parent d32061a commit 5ead1b7
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 0 deletions.
38 changes: 38 additions & 0 deletions internal/services/web/static_site_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,14 @@ func resourceStaticSite() *pluginsdk.Resource {
}, false),
},

"app_settings": {
Type: pluginsdk.TypeMap,
Optional: true,
Elem: &pluginsdk.Schema{
Type: pluginsdk.TypeString,
},
},

"default_host_name": {
Type: pluginsdk.TypeString,
Computed: true,
Expand Down Expand Up @@ -150,6 +158,16 @@ func resourceStaticSiteCreateOrUpdate(d *pluginsdk.ResourceData, meta interface{

d.SetId(id.ID())

if d.HasChange("app_settings") {
settings := web.StringDictionary{
Properties: expandStaticSiteAppSettings(d),
}

if _, err := client.CreateOrUpdateStaticSiteAppSettings(ctx, id.ResourceGroup, id.Name, settings); err != nil {
return fmt.Errorf("updating Application Settings for %s: %+v", id, err)
}
}

return resourceStaticSiteRead(d, meta)
}

Expand Down Expand Up @@ -216,6 +234,15 @@ func resourceStaticSiteRead(d *pluginsdk.ResourceData, meta interface{}) error {
}
d.Set("api_key", apiKey)

appSettingsResp, err := client.ListStaticSiteAppSettings(ctx, id.ResourceGroup, id.Name)
if err != nil {
return fmt.Errorf("making Read request for app settings on %s: %+v", id, err)
}

if err := d.Set("app_settings", appSettingsResp.Properties); err != nil {
return fmt.Errorf("setting `app_settings`: %s", err)
}

return tags.FlattenAndSet(d, resp.Tags)
}

Expand Down Expand Up @@ -290,3 +317,14 @@ func flattenStaticSiteIdentity(input *web.ManagedServiceIdentity) (*[]interface{

return identity.FlattenSystemAndUserAssignedMap(transform)
}

func expandStaticSiteAppSettings(d *pluginsdk.ResourceData) map[string]*string {
input := d.Get("app_settings").(map[string]interface{})
output := make(map[string]*string, len(input))

for k, v := range input {
output[k] = utils.String(v.(string))
}

return output
}
78 changes: 78 additions & 0 deletions internal/services/web/static_site_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,37 @@ func TestAccAzureStaticSite_requiresImport(t *testing.T) {
})
}

func TestAccStaticSite_appSettings(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_static_site", "test")
r := StaticSiteResource{}

data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.appSettings(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
check.That(data.ResourceName).Key("app_settings.foo").HasValue("bar"),
),
},
data.ImportStep(),
{
Config: r.appSettingsUpdate(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
check.That(data.ResourceName).Key("app_settings.foo").HasValue("bar"),
),
},
data.ImportStep(),
{
Config: r.basic(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep(),
})
}

func (r StaticSiteResource) Exists(ctx context.Context, clients *clients.Client, state *pluginsdk.InstanceState) (*bool, error) {
id, err := parse.StaticSiteID(state.ID)
if err != nil {
Expand Down Expand Up @@ -338,3 +369,50 @@ resource "azurerm_static_site" "import" {
}
`, template)
}

func (r StaticSiteResource) appSettings(data acceptance.TestData) string {
return fmt.Sprintf(`
provider "azurerm" {
features {}
}
resource "azurerm_resource_group" "test" {
name = "acctestRG-%d"
location = "%s"
}
resource "azurerm_static_site" "test" {
name = "acctestSS-%d"
location = azurerm_resource_group.test.location
resource_group_name = azurerm_resource_group.test.name
app_settings = {
"foo" = "bar"
}
}
`, data.RandomInteger, data.Locations.Secondary, data.RandomInteger)
}

func (r StaticSiteResource) appSettingsUpdate(data acceptance.TestData) string {
return fmt.Sprintf(`
provider "azurerm" {
features {}
}
resource "azurerm_resource_group" "test" {
name = "acctestRG-%d"
location = "%s"
}
resource "azurerm_static_site" "test" {
name = "acctestSS-%d"
location = azurerm_resource_group.test.location
resource_group_name = azurerm_resource_group.test.name
app_settings = {
"foo" = "bar"
"baz" = "foo"
}
}
`, data.RandomInteger, data.Locations.Secondary, data.RandomInteger)
}
2 changes: 2 additions & 0 deletions website/docs/r/static_site.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ The following arguments are supported:

* `identity` - (Optional) An `identity` block as defined below.

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

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

---
Expand Down

0 comments on commit 5ead1b7

Please sign in to comment.