From 05253f734c2e8c21c17ec38d6ffe3235567d7efb Mon Sep 17 00:00:00 2001 From: Matthew Date: Thu, 28 Sep 2023 15:03:32 -0700 Subject: [PATCH 1/3] Add support for app_settings --- internal/services/web/static_site_resource.go | 52 +++++++++++++ .../services/web/static_site_resource_test.go | 78 +++++++++++++++++++ website/docs/r/static_site.html.markdown | 2 + 3 files changed, 132 insertions(+) diff --git a/internal/services/web/static_site_resource.go b/internal/services/web/static_site_resource.go index 0d048422cf50..9617085f2da8 100644 --- a/internal/services/web/static_site_resource.go +++ b/internal/services/web/static_site_resource.go @@ -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, @@ -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) } @@ -216,6 +234,20 @@ 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 { + if utils.ResponseWasNotFound(appSettingsResp.Response) { + log.Printf("[DEBUG] Application Settings of %s were not found", id) + d.SetId("") + return nil + } + return fmt.Errorf("making Read request 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) } @@ -290,3 +322,23 @@ 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 +} + +func flattenStaticSiteAppSettings(input map[string]*string) map[string]string { + output := make(map[string]string) + for k, v := range input { + output[k] = *v + } + + return output +} diff --git a/internal/services/web/static_site_resource_test.go b/internal/services/web/static_site_resource_test.go index 11cd41fbf1ae..19b92aa4571f 100644 --- a/internal/services/web/static_site_resource_test.go +++ b/internal/services/web/static_site_resource_test.go @@ -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 { @@ -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) +} diff --git a/website/docs/r/static_site.html.markdown b/website/docs/r/static_site.html.markdown index d792d1386b6d..1f3e6bf6f981 100644 --- a/website/docs/r/static_site.html.markdown +++ b/website/docs/r/static_site.html.markdown @@ -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. --- From 9e37e27346d18fcbb6b4cdee85cd294a826fe3ec Mon Sep 17 00:00:00 2001 From: Matthew Date: Thu, 28 Sep 2023 15:20:57 -0700 Subject: [PATCH 2/3] remove unused function --- internal/services/web/static_site_resource.go | 9 --------- 1 file changed, 9 deletions(-) diff --git a/internal/services/web/static_site_resource.go b/internal/services/web/static_site_resource.go index 9617085f2da8..4050683ec607 100644 --- a/internal/services/web/static_site_resource.go +++ b/internal/services/web/static_site_resource.go @@ -333,12 +333,3 @@ func expandStaticSiteAppSettings(d *pluginsdk.ResourceData) map[string]*string { return output } - -func flattenStaticSiteAppSettings(input map[string]*string) map[string]string { - output := make(map[string]string) - for k, v := range input { - output[k] = *v - } - - return output -} From f0e660584f96fe23a394b728b07c0bf2743497a2 Mon Sep 17 00:00:00 2001 From: Matthew Date: Fri, 29 Sep 2023 10:07:21 -0700 Subject: [PATCH 3/3] Address review --- internal/services/web/static_site_resource.go | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/internal/services/web/static_site_resource.go b/internal/services/web/static_site_resource.go index 4050683ec607..752fe3195337 100644 --- a/internal/services/web/static_site_resource.go +++ b/internal/services/web/static_site_resource.go @@ -236,12 +236,7 @@ func resourceStaticSiteRead(d *pluginsdk.ResourceData, meta interface{}) error { appSettingsResp, err := client.ListStaticSiteAppSettings(ctx, id.ResourceGroup, id.Name) if err != nil { - if utils.ResponseWasNotFound(appSettingsResp.Response) { - log.Printf("[DEBUG] Application Settings of %s were not found", id) - d.SetId("") - return nil - } - return fmt.Errorf("making Read request on %s: %+v", id, err) + return fmt.Errorf("making Read request for app settings on %s: %+v", id, err) } if err := d.Set("app_settings", appSettingsResp.Properties); err != nil {