Skip to content

Commit

Permalink
azapi_* - support payload
Browse files Browse the repository at this point in the history
  • Loading branch information
ms-henglu committed Apr 16, 2024
1 parent 18a8aba commit 9ac559b
Show file tree
Hide file tree
Showing 4 changed files with 403 additions and 0 deletions.
345 changes: 345 additions & 0 deletions cmd/migrate_command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,30 @@ func TestMigrate_metaArguments(t *testing.T) {
migrateTestCase(t, metaArguments())
}

func TestMigrate_basic_payload(t *testing.T) {
migrateTestCase(t, basic_payload())
}

func TestMigrate_foreach_payload(t *testing.T) {
migrateTestCase(t, foreach_payload())
}

func TestMigrate_nestedBlock_payload(t *testing.T) {
migrateTestCase(t, nestedBlock_payload())
}

func TestMigrate_count_payload(t *testing.T) {
migrateTestCase(t, count_payload())
}

func TestMigrate_nestedBlockUpdate_payload(t *testing.T) {
migrateTestCase(t, nestedBlockUpdate_payload())
}

func TestMigrate_metaArguments_payload(t *testing.T) {
migrateTestCase(t, metaArguments_payload())
}

func migrateTestCase(t *testing.T, content string, ignore ...string) {
if len(os.Getenv("TF_ACC")) == 0 {
t.Skipf("Set `TF_ACC=true` to enable this test")
Expand Down Expand Up @@ -544,3 +568,324 @@ resource "azapi_resource" "test1" {
}
`, template(), randomResourceName(), randomResourceName())
}

func basic_payload() string {
return fmt.Sprintf(`
%s
data "azurerm_client_config" "current" {
}
variable "AutomationName" {
type = string
default = "%s"
}
variable "Label" {
type = string
default = "value"
}
locals {
AutomationSku = "Basic"
}
resource "azapi_resource" "test" {
name = var.AutomationName
parent_id = azurerm_resource_group.test.id
type = "Microsoft.Automation/automationAccounts@2020-01-13-preview"
response_export_values = ["name", "identity", "properties.sku"]
location = azurerm_resource_group.test.location
identity {
type = "SystemAssigned"
}
payload = {
properties = {
sku = {
name = local.AutomationSku
}
}
}
}
resource "azapi_resource" "test2" {
name = "${var.AutomationName}another"
parent_id = azurerm_resource_group.test.id
type = "Microsoft.Automation/automationAccounts@2020-01-13-preview"
location = azurerm_resource_group.test.location
body = jsonencode({
properties = {
sku = {
name = jsondecode(azapi_resource.test.output).properties.sku.name
}
}
})
}
resource "azurerm_automation_account" "test1" {
location = "westeurope"
name = "%s"
resource_group_name = azurerm_resource_group.test.name
sku_name = "Basic"
}
resource "azapi_update_resource" "test" {
resource_id = azurerm_automation_account.test1.id
type = "Microsoft.Automation/automationAccounts@2020-01-13-preview"
response_export_values = ["properties.sku"]
body = jsonencode({
tags = {
key = var.Label
}
})
}
output "accountName" {
value = jsondecode(azapi_resource.test.output).name
}
output "patchAccountSKU" {
value = jsondecode(azapi_update_resource.test.output).properties.sku.name
}
`, template(), randomResourceName(), randomResourceName())
}

func foreach_payload() string {
return fmt.Sprintf(`
%s
data "azurerm_client_config" "current" {
}
variable "accounts" {
type = map(any)
default = {
"item1" = {
name = "%s"
sku = "Basic"
}
"item2" = {
name = "%s"
sku = "Basic"
}
}
}
resource "azapi_resource" "test" {
name = "henglu${each.value.name}"
parent_id = azurerm_resource_group.test.id
type = "Microsoft.Automation/automationAccounts@2020-01-13-preview"
location = azurerm_resource_group.test.location
identity {
type = "SystemAssigned"
}
payload = {
properties = {
sku = {
name = each.value.sku
}
}
}
for_each = var.accounts
}
`, template(), randomResourceName(), randomResourceName())
}

func nestedBlock_payload() string {
return fmt.Sprintf(`
%s
resource "azurerm_storage_account" "test" {
name = "%s"
resource_group_name = azurerm_resource_group.test.name
location = azurerm_resource_group.test.location
account_tier = "Standard"
account_replication_type = "GRS"
}
variable "description" {
type = string
default = "this is my desc"
}
variable "defName" {
type = string
default = "def1"
}
resource "azapi_resource" "test" {
name = "%s"
parent_id = azurerm_resource_group.test.id
type = "Microsoft.Network/serviceEndpointPolicies@2020-11-01"
payload = {
location = "westeurope"
tags = {}
properties = {
serviceEndpointPolicyDefinitions = [
{
name= var.defName
properties = {
service = "Microsoft.Storage"
description = var.description
serviceResources= [
azurerm_storage_account.test.id,
azurerm_resource_group.test.id
]
}
}
]
}
}
}
`, template(), randomResourceName(), randomResourceName())
}

func count_payload() string {
return fmt.Sprintf(`
%s
resource "azapi_resource" "test" {
name = "%s${count.index}"
parent_id = azurerm_resource_group.test.id
type = "Microsoft.Automation/automationAccounts@2020-01-13-preview"
location = azurerm_resource_group.test.location
payload = {
properties = {
sku = {
name = "Basic"
}
}
}
count = 2
}
`, template(), randomResourceName())
}

func nestedBlockUpdate_payload() string {
return fmt.Sprintf(`
%s
resource "azurerm_container_registry" "test" {
name = "%s"
resource_group_name = azurerm_resource_group.test.name
location = azurerm_resource_group.test.location
sku = "Premium"
admin_enabled = false
network_rule_set = [{
default_action = "Deny"
ip_rule = [{
action = "Allow"
ip_range = "1.1.1.1/32"
}, {
action = "Allow"
ip_range = "8.8.8.8/32"
}]
virtual_network = []
}]
}
variable "action" {
type = string
default = "Allow"
}
resource "azapi_update_resource" "test" {
resource_id = azurerm_container_registry.test.id
type = "Microsoft.ContainerRegistry/registries@2019-05-01"
payload = {
properties = {
networkRuleSet = {
defaultAction = "Deny"
ipRules = [
{
action = var.action
value = "7.7.7.7"
},
{
action = var.action
value = "2.2.2.2"
}
]
}
}
}
}
`, template(), randomResourceName())
}

func metaArguments_payload() string {
return fmt.Sprintf(`
%s
resource "azapi_resource" "test" {
name = "%s"
parent_id = azurerm_resource_group.test.id
type = "Microsoft.Automation/automationAccounts@2020-01-13-preview"
response_export_values = ["name", "identity", "properties.sku"]
location = azurerm_resource_group.test.location
identity {
type = "SystemAssigned"
}
payload = {
properties = {
sku = {
name = "Basic"
}
}
}
depends_on = [azurerm_resource_group.test]
lifecycle {
create_before_destroy = false
prevent_destroy = false
}
provisioner "local-exec" {
command = "echo the resource id is ${self.id}"
}
}
resource "azapi_resource" "test1" {
name = "%s"
parent_id = azurerm_resource_group.test.id
type = "Microsoft.Automation/automationAccounts@2020-01-13-preview"
location = azurerm_resource_group.test.location
identity {
type = "SystemAssigned"
}
payload = {
properties = {
sku = {
name = "Basic"
}
}
}
depends_on = [azurerm_resource_group.test, azapi_resource.test]
lifecycle {
create_before_destroy = false
prevent_destroy = false
}
provisioner "local-exec" {
command = "echo the resource id is ${self.id}"
}
}
`, template(), randomResourceName(), randomResourceName())
}
28 changes: 28 additions & 0 deletions cmd/plan_command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,34 @@ func TestPlan_strictMode(t *testing.T) {
planTestCase(t, basic(), []string{}, true)
}

func TestPlan_basic_payload(t *testing.T) {
planTestCase(t, basic_payload(), []string{"azapi_resource.test2", "azapi_update_resource.test"}, false)
}

func TestPlan_foreach_payload(t *testing.T) {
planTestCase(t, foreach_payload(), []string{"azapi_resource.test"}, false)
}

func TestPlan_nestedBlock_payload(t *testing.T) {
planTestCase(t, nestedBlock_payload(), []string{"azapi_resource.test"}, false)
}

func TestPlan_count_payload(t *testing.T) {
planTestCase(t, count_payload(), []string{"azapi_resource.test"}, false)
}

func TestPlan_nestedBlockUpdate_payload(t *testing.T) {
planTestCase(t, nestedBlockUpdate_payload(), []string{"azapi_update_resource.test"}, false)
}

func TestPlan_metaArguments_payload(t *testing.T) {
planTestCase(t, metaArguments_payload(), []string{"azapi_resource.test1"}, false)
}

func TestPlan_strictMode_payload(t *testing.T) {
planTestCase(t, basic_payload(), []string{}, true)
}

func planTestCase(t *testing.T, content string, expectMigratedAddresses []string, strictMode bool) {
if len(os.Getenv("TF_ACC")) == 0 {
t.Skipf("Set `TF_ACC=true` to enable this test")
Expand Down
1 change: 1 addition & 0 deletions tf/terraform.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ func (t *Terraform) ListGenericResources(p *tfjson.Plan) []types.GenericResource
resources[i].Instances[j].Outputs = getOutputsForAddress(resource.OldAddress(instance.Index), refValueMap)
for _, output := range resources[i].Instances[j].Outputs {
prop := strings.TrimPrefix(output.OldName, fmt.Sprintf("jsondecode(%s.output).", resource.OldAddress(instance.Index)))
prop = strings.TrimPrefix(prop, fmt.Sprintf("%s.output_payload.", resource.OldAddress(instance.Index)))
if strings.HasPrefix(prop, "identity.userAssignedIdentities") {
prop = "identity.userAssignedIdentities"
}
Expand Down
Loading

0 comments on commit 9ac559b

Please sign in to comment.