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_site - Add support for app_settings #23421

Merged
merged 3 commits into from
Sep 29, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 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,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)
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we just return an error here rather than removing from the state?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good shout!


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 +322,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
Loading