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_resource_group_template_deployment - output_content can now be referenced in other resources #25403

Merged
merged 5 commits into from
Apr 3, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"context"
"fmt"
"log"
"strings"
"time"

"github.com/Azure/azure-sdk-for-go/services/resources/mgmt/2020-06-01/resources" // nolint: staticcheck
Expand Down Expand Up @@ -107,6 +108,31 @@ func resourceGroupTemplateDeploymentResource() *pluginsdk.Resource {
// parsing the JSON using `jsondecode` allows the users to interact with/map objects as required
},
},

// this is needed to fix https://github.com/hashicorp/terraform-provider-azurerm/issues/12828
// On a change to `template_content` or `parameters_content`, we'll set `output_content` to empty
// The adverse effect of this is that any change to `template_content` will also cause any resource referencing `output_content` to update
CustomizeDiff: func(ctx context.Context, d *pluginsdk.ResourceDiff, i interface{}) error {
if d.HasChange("template_content") {
o, n := d.GetChange("template_content")

// the json has to be normalized and then compared against to see if a change has occurred
if !strings.EqualFold(o.(string), utils.NormalizeJson(n)) {
return d.SetNewComputed("output_content")
}
}

if d.HasChange("parameters_content") {
o, n := d.GetChange("parameters_content")

// the json has to be normalized and then compared against to see if a change has occurred
if !strings.EqualFold(o.(string), utils.NormalizeJson(n)) {
return d.SetNewComputed("output_content")
}
}

return nil
},
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,29 @@ func TestAccResourceGroupTemplateDeployment_nestedResources(t *testing.T) {
})
}

func TestAccResourceGroupTemplateDeployment_outputReference(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_resource_group_template_deployment", "test")
r := ResourceGroupTemplateDeploymentResource{}

data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.noOutputReference(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep(),
{
// set some tags
Config: r.outputReference(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep(),
})
}

func (t ResourceGroupTemplateDeploymentResource) Exists(ctx context.Context, clients *clients.Client, state *pluginsdk.InstanceState) (*bool, error) {
id, err := parse.ResourceGroupTemplateDeploymentID(state.ID)
if err != nil {
Expand Down Expand Up @@ -968,3 +991,135 @@ TEMPLATE
}
`, data.RandomInteger, data.Locations.Primary)
}

func (ResourceGroupTemplateDeploymentResource) noOutputReference(data acceptance.TestData) string {
return fmt.Sprintf(`
provider "azurerm" {
features {}
}

resource "azurerm_resource_group" "test" {
name = "acctestRG-%[1]d"
location = %[2]q
}

resource "azurerm_storage_account" "test" {
name = "acctestsa%[3]s"
resource_group_name = azurerm_resource_group.test.name
location = azurerm_resource_group.test.location
account_tier = "Standard"
account_replication_type = "LRS"
}


resource "azurerm_resource_group_template_deployment" "test" {
name = "acctest%[3]s"
resource_group_name = azurerm_resource_group.test.name
deployment_mode = "Incremental"

parameters_content = jsonencode({
"logic-app-name" = {
value = "logic-app-name"
}
})

template_content = <<TEMPLATE
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"logic-app-name": {
"type": "String"
}
},
"variables": {},
"resources": [
{
"type": "Microsoft.Logic/workflows",
"apiVersion": "2017-07-01",
"name": "[parameters('logic-app-name')]",
"location": "westeurope",
"properties": {
"state": "Enabled",
"definition": {
"$schema": "https://pluginsdk.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
"contentVersion": "1.0.0.0",
"triggers": {},
"actions": {}
},
"parameters": {}
}
}
]
}
TEMPLATE
}
`, data.RandomInteger, data.Locations.Primary, data.RandomString)
}

func (ResourceGroupTemplateDeploymentResource) outputReference(data acceptance.TestData) string {
return fmt.Sprintf(`
provider "azurerm" {
features {
resource_group {
prevent_deletion_if_contains_resources = false
}
}
}

resource "azurerm_resource_group" "test" {
name = "acctestRG-%[1]d"
location = %[2]q
}

resource "azurerm_storage_account" "test" {
name = "acctestsa%[3]s"
resource_group_name = azurerm_resource_group.test.name
location = azurerm_resource_group.test.location
account_tier = "Standard"
account_replication_type = "LRS"
}


resource "azurerm_resource_group_template_deployment" "test" {
name = "acctest%[3]s"
resource_group_name = azurerm_resource_group.test.name
deployment_mode = "Incremental"

parameters_content = jsonencode({
"logic-app-name" = {
value = "logic-app-name"
}
})

template_content = <<TEMPLATE
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"logic-app-name": {
"type": "String"
}
},
"variables": {},
"resources": [],
"outputs": {
"resourceName": {
"type": "String",
"value": "acctest-2-%[1]d"
}
}
}
TEMPLATE
}

resource "azurerm_resource_group" "test2" {
name = jsondecode(azurerm_resource_group_template_deployment.test.output_content).resourceName.value
location = %[2]q
}




`, data.RandomInteger, data.Locations.Primary, data.RandomString)
}
Loading