-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14524 from datarootsio/template-data-sources
New data sources `azurerm_management_group_template_deployment`, `azurerm_resource_group_template_deployment`, `azurerm_subscription_template_deployment`, `azurerm_tenant_template_deployment`
- Loading branch information
Showing
14 changed files
with
773 additions
and
4 deletions.
There are no files selected for viewing
76 changes: 76 additions & 0 deletions
76
internal/services/resource/management_group_template_deployment_data_source.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
73 changes: 73 additions & 0 deletions
73
internal/services/resource/management_group_template_deployment_data_source_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 = <<TEMPLATE | ||
{ | ||
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#", | ||
"contentVersion": "1.0.0.0", | ||
"parameters": {}, | ||
"variables": {}, | ||
"resources": [], | ||
"outputs": { | ||
"testOutput": { | ||
"type": "String", | ||
"value": "some-value" | ||
} | ||
} | ||
} | ||
TEMPLATE | ||
} | ||
`, data.RandomInteger, data.Locations.Primary) | ||
} | ||
|
||
func (r ManagementGroupTemplateDeploymentDataSource) withDataSource(data acceptance.TestData) string { | ||
return fmt.Sprintf(` | ||
%s | ||
data "azurerm_management_group_template_deployment" "test" { | ||
name = azurerm_management_group_template_deployment.test.name | ||
management_group_id = azurerm_management_group.test.id | ||
} | ||
`, r.withOutputsConfig(data)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
internal/services/resource/resource_group_template_deployment_data_source.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package resource | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
"github.com/hashicorp/terraform-provider-azurerm/helpers/azure" | ||
"github.com/hashicorp/terraform-provider-azurerm/internal/clients" | ||
"github.com/hashicorp/terraform-provider-azurerm/internal/services/resource/parse" | ||
"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 dataSourceResourceGroupTemplateDeployment() *pluginsdk.Resource { | ||
return &pluginsdk.Resource{ | ||
Read: dataSourceResourceGroupTemplateDeploymentRead, | ||
|
||
Timeouts: &pluginsdk.ResourceTimeout{ | ||
Read: pluginsdk.DefaultTimeout(5 * time.Minute), | ||
}, | ||
|
||
Schema: map[string]*pluginsdk.Schema{ | ||
"name": { | ||
Type: pluginsdk.TypeString, | ||
Required: true, | ||
ValidateFunc: validate.TemplateDeploymentName, | ||
}, | ||
|
||
"resource_group_name": azure.SchemaResourceGroupNameForDataSource(), | ||
|
||
// 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 dataSourceResourceGroupTemplateDeploymentRead(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*clients.Client).Resource.DeploymentsClient | ||
subscriptionId := meta.(*clients.Client).Account.SubscriptionId | ||
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d) | ||
defer cancel() | ||
|
||
resourceGroupName := d.Get("resource_group_name").(string) | ||
deploymentName := d.Get("name").(string) | ||
id := parse.NewResourceGroupTemplateDeploymentID(subscriptionId, resourceGroupName, deploymentName) | ||
|
||
resp, err := client.Get(ctx, id.ResourceGroup, id.DeploymentName) | ||
if err != nil { | ||
if utils.ResponseWasNotFound(resp.Response) { | ||
return fmt.Errorf("template %s in resource Group %s was not found", deploymentName, resourceGroupName) | ||
} | ||
|
||
return fmt.Errorf("retrieving Template Deployment %q (Resource Group %q): %+v", id.DeploymentName, id.ResourceGroup, err) | ||
} | ||
|
||
d.SetId(id.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 | ||
} |
74 changes: 74 additions & 0 deletions
74
internal/services/resource/resource_group_template_deployment_data_source_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package resource_test | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance" | ||
"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance/check" | ||
) | ||
|
||
type ResourceGroupTemplateDeploymentDataSource struct{} | ||
|
||
func TestAccDataSourceResourceGroupTemplateDeployment(t *testing.T) { | ||
data := acceptance.BuildTestData(t, "data.azurerm_resource_group_template_deployment", "test") | ||
r := ResourceGroupTemplateDeploymentDataSource{} | ||
|
||
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 (ResourceGroupTemplateDeploymentDataSource) withOutputsConfig(data acceptance.TestData) string { | ||
return fmt.Sprintf(` | ||
provider "azurerm" { | ||
features {} | ||
} | ||
resource "azurerm_resource_group" "test" { | ||
name = "acctestRG-%d" | ||
location = %q | ||
} | ||
resource "azurerm_resource_group_template_deployment" "test" { | ||
name = "acctest" | ||
resource_group_name = azurerm_resource_group.test.name | ||
deployment_mode = "Complete" | ||
template_content = <<TEMPLATE | ||
{ | ||
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#", | ||
"contentVersion": "1.0.0.0", | ||
"parameters": {}, | ||
"variables": {}, | ||
"resources": [], | ||
"outputs": { | ||
"testOutput": { | ||
"type": "String", | ||
"value": "some-value" | ||
} | ||
} | ||
} | ||
TEMPLATE | ||
} | ||
`, data.RandomInteger, data.Locations.Primary) | ||
} | ||
|
||
func (r ResourceGroupTemplateDeploymentDataSource) withDataSource(data acceptance.TestData) string { | ||
return fmt.Sprintf(` | ||
%s | ||
data "azurerm_resource_group_template_deployment" "test" { | ||
name = azurerm_resource_group_template_deployment.test.name | ||
resource_group_name = azurerm_resource_group.test.name | ||
} | ||
`, r.withOutputsConfig(data)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
internal/services/resource/subscription_template_deployment_data_source.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package resource | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
"github.com/hashicorp/terraform-provider-azurerm/internal/clients" | ||
"github.com/hashicorp/terraform-provider-azurerm/internal/services/resource/parse" | ||
"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 dataSourceSubscriptionTemplateDeployment() *pluginsdk.Resource { | ||
return &pluginsdk.Resource{ | ||
Read: dataSourceSubscriptionTemplateDeploymentRead, | ||
Timeouts: &pluginsdk.ResourceTimeout{ | ||
Read: pluginsdk.DefaultTimeout(5 * time.Minute), | ||
}, | ||
Schema: map[string]*pluginsdk.Schema{ | ||
"name": { | ||
Type: pluginsdk.TypeString, | ||
Required: true, | ||
ValidateFunc: validate.TemplateDeploymentName, | ||
}, | ||
|
||
// 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 dataSourceSubscriptionTemplateDeploymentRead(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*clients.Client).Resource.DeploymentsClient | ||
subscriptionId := meta.(*clients.Client).Account.SubscriptionId | ||
ctx, cancel := timeouts.ForCreate(meta.(*clients.Client).StopContext, d) | ||
defer cancel() | ||
|
||
id := parse.NewSubscriptionTemplateDeploymentID(subscriptionId, d.Get("name").(string)) | ||
|
||
resp, err := client.GetAtSubscriptionScope(ctx, id.DeploymentName) | ||
if err != nil { | ||
if utils.ResponseWasNotFound(resp.Response) { | ||
return fmt.Errorf("template %s in subscription %s was not found", id.DeploymentName, subscriptionId) | ||
} | ||
|
||
return fmt.Errorf("retrieving Subscription Template Deployment %q: %+v", id.DeploymentName, err) | ||
} | ||
|
||
d.SetId(id.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 | ||
} |
Oops, something went wrong.