Skip to content

Commit

Permalink
azurerm_batch_pool - fixes TestAccBatchPool_extensions (#24075)
Browse files Browse the repository at this point in the history
* update `virtual_network_configuration` Read function

* rewrite json processing logic

* Update internal/services/batch/batch_pool.go

Co-authored-by: stephybun <[email protected]>

* Update internal/services/batch/batch_pool.go

Co-authored-by: stephybun <[email protected]>

---------

Co-authored-by: stephybun <[email protected]>
  • Loading branch information
liuwuliuyun and stephybun authored Dec 11, 2023
1 parent 428f6a8 commit 8a04d99
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 18 deletions.
11 changes: 9 additions & 2 deletions internal/services/batch/batch_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package batch

import (
"encoding/json"
"fmt"
"log"
"strconv"
Expand Down Expand Up @@ -868,11 +869,17 @@ func expandBatchPoolExtension(ref map[string]interface{}) (*pool.VmExtension, er
}

if settings, ok := ref["settings_json"]; ok {
result.Settings = pointer.To(settings)
err := json.Unmarshal([]byte(settings.(string)), &result.Settings)
if err != nil {
return nil, fmt.Errorf("unmarshaling `settings_json`: %+v", err)
}
}

if protectedSettings, ok := ref["protected_settings"]; ok {
result.ProtectedSettings = pointer.To(protectedSettings)
err := json.Unmarshal([]byte(protectedSettings.(string)), &result.ProtectedSettings)
if err != nil {
return nil, fmt.Errorf("unmarshaling `protected_settings`: %+v", err)
}
}

if tmpItem, ok := ref["provision_after_extensions"]; ok {
Expand Down
5 changes: 4 additions & 1 deletion internal/services/batch/batch_pool_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -1208,7 +1208,10 @@ func resourceBatchPoolRead(d *pluginsdk.ResourceData, meta interface{}) error {
extension["automatic_upgrade_enabled"] = *item.EnableAutomaticUpgrade
}
if item.Settings != nil {
extension["settings_json"] = item.Settings
extension["settings_json"], err = pluginsdk.FlattenJsonToString((*item.Settings).(map[string]interface{}))
if err != nil {
return fmt.Errorf("flattening `settings_json`: %+v", err)
}
}

for i := 0; i < n; i++ {
Expand Down
35 changes: 20 additions & 15 deletions internal/services/batch/batch_pool_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -692,15 +692,12 @@ func TestAccBatchPool_extensions(t *testing.T) {
Config: r.extensions(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
check.That(data.ResourceName).Key("extensions.0.name").HasValue("KeyVaultForLinux"),
check.That(data.ResourceName).Key("extensions.0.publisher").HasValue("Microsoft.Azure.KeyVault"),
check.That(data.ResourceName).Key("extensions.0.type").HasValue("KeyVaultForLinux"),
check.That(data.ResourceName).Key("extensions.0.type_handler_version").HasValue("2.0"),
check.That(data.ResourceName).Key("extensions.0.name").HasValue("OmsAgentForLinux"),
check.That(data.ResourceName).Key("extensions.0.publisher").HasValue("Microsoft.EnterpriseCloud.Monitoring"),
check.That(data.ResourceName).Key("extensions.0.type").HasValue("OmsAgentForLinux"),
check.That(data.ResourceName).Key("extensions.0.type_handler_version").HasValue("1.17"),
check.That(data.ResourceName).Key("extensions.0.auto_upgrade_minor_version").HasValue("true"),
check.That(data.ResourceName).Key("extensions.0.automatic_upgrade_enabled").HasValue("true"),
check.That(data.ResourceName).Key("extensions.0.settings_json").HasValue("{}"),
check.That(data.ResourceName).Key("extensions.0.protected_settings").HasValue("sensitive"),
check.That(data.ResourceName).Key("extensions.0.provision_after_extensions.0").HasValue("newProv1"),
),
},
})
Expand Down Expand Up @@ -2360,22 +2357,30 @@ resource "azurerm_batch_account" "test" {
resource_group_name = azurerm_resource_group.test.name
location = azurerm_resource_group.test.location
}
resource "azurerm_log_analytics_workspace" "test" {
name = "testaccloganalytics%s"
resource_group_name = azurerm_resource_group.test.name
location = azurerm_resource_group.test.location
sku = "PerGB2018"
retention_in_days = 30
}
resource "azurerm_batch_pool" "test" {
name = "testaccpool%s"
resource_group_name = azurerm_resource_group.test.name
account_name = azurerm_batch_account.test.name
node_agent_sku_id = "batch.node.ubuntu 22.04"
vm_size = "Standard_A1"
extensions {
name = "KeyVaultForLinux"
publisher = "Microsoft.Azure.KeyVault"
type = "KeyVaultForLinux"
type_handler_version = "2.0"
name = "OmsAgentForLinux"
publisher = "Microsoft.EnterpriseCloud.Monitoring"
settings_json = jsonencode({ "workspaceId" = "${azurerm_log_analytics_workspace.test.id}", "skipDockerProviderInstall" = true })
protected_settings = jsonencode({ "workspaceKey" = "${azurerm_log_analytics_workspace.test.primary_shared_key}" })
type = "OmsAgentForLinux"
type_handler_version = "1.17"
auto_upgrade_minor_version = true
automatic_upgrade_enabled = true
settings_json = "{}"
protected_settings = "sensitive"
provision_after_extensions = ["newProv1"]
}
fixed_scale {
target_dedicated_nodes = 1
Expand All @@ -2387,7 +2392,7 @@ resource "azurerm_batch_pool" "test" {
version = "latest"
}
}
`, template, data.RandomString, data.RandomString)
`, template, data.RandomString, data.RandomString, data.RandomString)
}

func (BatchPoolResource) diskSettings(data acceptance.TestData) string {
Expand Down

0 comments on commit 8a04d99

Please sign in to comment.