diff --git a/internal/services/resource/management_group_template_deployment_data_source.go b/internal/services/resource/management_group_template_deployment_data_source.go new file mode 100644 index 000000000000..a89243141f09 --- /dev/null +++ b/internal/services/resource/management_group_template_deployment_data_source.go @@ -0,0 +1,76 @@ +package resource + +import ( + "fmt" + "time" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + "github.com/hashicorp/terraform-provider-azurerm/internal/clients" + mgValidate "github.com/hashicorp/terraform-provider-azurerm/internal/services/managementgroup/validate" + "github.com/hashicorp/terraform-provider-azurerm/internal/services/resource/validate" + "github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk" + "github.com/hashicorp/terraform-provider-azurerm/internal/timeouts" + "github.com/hashicorp/terraform-provider-azurerm/utils" +) + +func dataSourceManagementGroupTemplateDeployment() *pluginsdk.Resource { + return &pluginsdk.Resource{ + Read: dataSourceManagementGroupTemplateDeploymentRead, + + Timeouts: &pluginsdk.ResourceTimeout{ + Read: pluginsdk.DefaultTimeout(5 * time.Minute), + }, + + Schema: map[string]*pluginsdk.Schema{ + "name": { + Type: pluginsdk.TypeString, + Required: true, + ValidateFunc: validate.TemplateDeploymentName, + }, + + "management_group_id": { + Type: pluginsdk.TypeString, + Required: true, + ValidateFunc: mgValidate.ManagementGroupID, + }, + + // Computed + "output_content": { + Type: pluginsdk.TypeString, + Computed: true, + // NOTE: outputs can be strings, ints, objects etc - whilst using a nested object was considered + // parsing the JSON using `jsondecode` allows the users to interact with/map objects as required + }, + }, + } +} + +func dataSourceManagementGroupTemplateDeploymentRead(d *schema.ResourceData, meta interface{}) error { + client := meta.(*clients.Client).Resource.DeploymentsClient + ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d) + defer cancel() + + managementGroupId := d.Get("management_group_id").(string) + deploymentName := d.Get("name").(string) + + resp, err := client.GetAtManagementGroupScope(ctx, managementGroupId, deploymentName) + if err != nil { + if utils.ResponseWasNotFound(resp.Response) { + return fmt.Errorf("deployment %s in Management Group %s was not found", deploymentName, managementGroupId) + } + + return fmt.Errorf("retrieving Management Group Template Deployment %s in management group %s: %+v", deploymentName, managementGroupId, err) + } + + d.SetId(*resp.ID) + + if props := resp.Properties; props != nil { + flattenedOutputs, err := flattenTemplateDeploymentBody(props.Outputs) + if err != nil { + return fmt.Errorf("flattening `output_content`: %+v", err) + } + return d.Set("output_content", flattenedOutputs) + } + + return nil +} diff --git a/internal/services/resource/management_group_template_deployment_data_source_test.go b/internal/services/resource/management_group_template_deployment_data_source_test.go new file mode 100644 index 000000000000..f48874661a0c --- /dev/null +++ b/internal/services/resource/management_group_template_deployment_data_source_test.go @@ -0,0 +1,73 @@ +package resource_test + +import ( + "fmt" + "testing" + + "github.com/hashicorp/terraform-provider-azurerm/internal/acceptance" + "github.com/hashicorp/terraform-provider-azurerm/internal/acceptance/check" +) + +type ManagementGroupTemplateDeploymentDataSource struct{} + +func TestAccDataSourceManagementGroupTemplateDeployment(t *testing.T) { + data := acceptance.BuildTestData(t, "data.azurerm_management_group_template_deployment", "test") + r := ManagementGroupTemplateDeploymentDataSource{} + + data.DataSourceTest(t, []acceptance.TestStep{ + { + Config: r.withOutputsConfig(data), + }, + { + Config: r.withDataSource(data), + Check: acceptance.ComposeTestCheckFunc( + check.That(data.ResourceName).Key("output_content").HasValue("{\"testOutput\":{\"type\":\"String\",\"value\":\"some-value\"}}"), + ), + }, + }) +} + +func (ManagementGroupTemplateDeploymentDataSource) withOutputsConfig(data acceptance.TestData) string { + return fmt.Sprintf(` +provider "azurerm" { + features {} +} + +resource "azurerm_management_group" "test" { + name = "TestAcc-Deployment-%[1]d" +} + +resource "azurerm_management_group_template_deployment" "test" { + name = "acctestMGdeploy-%[1]d" + management_group_id = azurerm_management_group.test.id + location = %[2]q + + template_content = <