Repository: gruntwork-io/terratest (language: go)

Issue:
azure module
func GetVirtualMachineTagsE(vmName string, resGroupName string, subscriptionID string) (map[string]string, error) {

map has not been properly intialized

func GetVirtualMachineTagsE(vmName string, resGroupName string, subscriptionID string) (map[string]string, error) {
	// Setup a blank map to populate and return
	**var tags map[string]string**

	// Get VM Object
	vm, err := GetVirtualMachineE(vmName, resGroupName, subscriptionID)
	if err != nil {
		return nil, err
	}

	// Range through existing tags and populate above map accordingly
	for k, v := range vm.Tags {
		tags[k] = *v
	}

	return tags, nil
}
azure module
func GetVirtualMachineTagsE(vmName string, resGroupName string, subscriptionID string) (map[string]string, error) {

map has not been properly intialized

func GetVirtualMachineTagsE(vmName string, resGroupName string, subscriptionID string) (map[string]string, error) {
	// Setup a blank map to populate and return
	**var tags map[string]string**

	// Get VM Object
	vm, err := GetVirtualMachineE(vmName, resGroupName, subscriptionID)
	if err != nil {
		return nil, err
	}

	// Range through existing tags and populate above map accordingly
	for k, v := range vm.Tags {
		tags[k] = *v
	}

	return tags, nil
}


Discussion / hints:
@davesee Please take a look.  We will get this fixed.
@davesee Please take a look.  We will get this fixed.

Fix patch:
diff --git a/examples/azure/terraform-azure-vm-example/main.tf b/examples/azure/terraform-azure-vm-example/main.tf
index 37009fd7f..cf7d51414 100644
--- a/examples/azure/terraform-azure-vm-example/main.tf
+++ b/examples/azure/terraform-azure-vm-example/main.tf
@@ -127,6 +127,11 @@ resource "azurerm_virtual_machine" "vm_example" {
     provision_vm_agent = true
   }
 
+  tags = {
+    "Version"     = "0.0.1"
+    "Environment" = "dev"
+  }
+
   depends_on = [random_password.rand]
 }
 
diff --git a/examples/azure/terraform-azure-vm-example/outputs.tf b/examples/azure/terraform-azure-vm-example/outputs.tf
index 4ac75c9b4..4f2044539 100644
--- a/examples/azure/terraform-azure-vm-example/outputs.tf
+++ b/examples/azure/terraform-azure-vm-example/outputs.tf
@@ -58,3 +58,7 @@ output "vm_name" {
 output "vm_size" {
   value = azurerm_virtual_machine.vm_example.vm_size
 }
+
+output "vm_tags" {
+  value = azurerm_virtual_machine.vm_example.tags
+}
diff --git a/modules/azure/compute.go b/modules/azure/compute.go
index a40238065..8fc666698 100644
--- a/modules/azure/compute.go
+++ b/modules/azure/compute.go
@@ -240,12 +240,12 @@ func GetVirtualMachineTags(t testing.TestingT, vmName string, resGroupName strin
 // GetVirtualMachineTagsE gets the Tags of the specified Virtual Machine as a map.
 func GetVirtualMachineTagsE(vmName string, resGroupName string, subscriptionID string) (map[string]string, error) {
 	// Setup a blank map to populate and return
-	var tags map[string]string
+	tags := make(map[string]string)
 
 	// Get VM Object
 	vm, err := GetVirtualMachineE(vmName, resGroupName, subscriptionID)
 	if err != nil {
-		return nil, err
+		return tags, err
 	}
 
 	// Range through existing tags and populate above map accordingly


Test patch:
diff --git a/test/azure/terraform_azure_vm_example_test.go b/test/azure/terraform_azure_vm_example_test.go
index f74e00265..35f3aac8d 100644
--- a/test/azure/terraform_azure_vm_example_test.go
+++ b/test/azure/terraform_azure_vm_example_test.go
@@ -113,6 +113,7 @@ func testInformationOfVM(t *testing.T, terraformOptions *terraform.Options, subs
 	expectedImageSKU := terraform.OutputList(t, terraformOptions, "vm_image_sku")
 	expectedImageVersion := terraform.OutputList(t, terraformOptions, "vm_image_version")
 	expectedAvsName := terraform.Output(t, terraformOptions, "availability_set_name")
+	expectedVMTags := terraform.OutputMap(t, terraformOptions, "vm_tags")
 
 	// Check if the Virtual Machine exists.
 	assert.True(t, azure.VirtualMachineExists(t, virtualMachineName, resourceGroupName, subscriptionID))
@@ -131,6 +132,10 @@ func testInformationOfVM(t *testing.T, terraformOptions *terraform.Options, subs
 	// The AVS ID returned from the VM is always CAPS so ignoring case in the assertion.
 	actualexpectedAvsName := azure.GetVirtualMachineAvailabilitySetID(t, virtualMachineName, resourceGroupName, subscriptionID)
 	assert.True(t, strings.EqualFold(expectedAvsName, actualexpectedAvsName))
+
+	// Check the assigned Tags of the VM, assert empty if no tags.
+	actualVMTags := azure.GetVirtualMachineTags(t, virtualMachineName, resourceGroupName, "")
+	assert.Equal(t, expectedVMTags, actualVMTags)
 }
 
 // These tests check the OS Disk and Attached Managed Disks for the Azure Virtual Machine.