Skip to content

Commit 89138fe

Browse files
authored
Merge pull request hashicorp#9122 from hashicorp/b-arm-vm-diagnostics
provider/azurerm: arm_virtual_machine diagnostics_profile was causing a panic on the Read func
2 parents c953a41 + dcfdc6a commit 89138fe

3 files changed

Lines changed: 59 additions & 31 deletions

File tree

builtin/providers/azurerm/resource_arm_virtual_machine.go

Lines changed: 53 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/Azure/azure-sdk-for-go/arm/compute"
1212
"github.com/hashicorp/terraform/helper/hashcode"
1313
"github.com/hashicorp/terraform/helper/schema"
14+
riviera "github.com/jen20/riviera/azure"
1415
)
1516

1617
func resourceArmVirtualMachine() *schema.Resource {
@@ -214,9 +215,11 @@ func resourceArmVirtualMachine() *schema.Resource {
214215
},
215216

216217
"diagnostics_profile": {
217-
Type: schema.TypeSet,
218-
Optional: true,
219-
MaxItems: 1,
218+
Type: schema.TypeSet,
219+
Optional: true,
220+
MaxItems: 1,
221+
ConflictsWith: []string{"boot_diagnostics"},
222+
Deprecated: "Use field boot_diagnostics instead",
220223
Elem: &schema.Resource{
221224
Schema: map[string]*schema.Schema{
222225
"boot_diagnostics": {
@@ -241,6 +244,25 @@ func resourceArmVirtualMachine() *schema.Resource {
241244
},
242245
},
243246

247+
"boot_diagnostics": {
248+
Type: schema.TypeList,
249+
Optional: true,
250+
MaxItems: 1,
251+
Elem: &schema.Resource{
252+
Schema: map[string]*schema.Schema{
253+
"enabled": {
254+
Type: schema.TypeBool,
255+
Required: true,
256+
},
257+
258+
"storage_uri": {
259+
Type: schema.TypeString,
260+
Required: true,
261+
},
262+
},
263+
},
264+
},
265+
244266
"os_profile": {
245267
Type: schema.TypeSet,
246268
Required: true,
@@ -453,9 +475,11 @@ func resourceArmVirtualMachineCreate(d *schema.ResourceData, meta interface{}) e
453475
StorageProfile: &storageProfile,
454476
}
455477

456-
if _, ok := d.GetOk("diagnostics_profile"); ok {
478+
if _, ok := d.GetOk("boot_diagnostics"); ok {
457479
diagnosticsProfile := expandAzureRmVirtualMachineDiagnosticsProfile(d)
458-
properties.DiagnosticsProfile = &diagnosticsProfile
480+
if diagnosticsProfile != nil {
481+
properties.DiagnosticsProfile = diagnosticsProfile
482+
}
459483
}
460484

461485
osProfile, err := expandAzureRmVirtualMachineOsProfile(d)
@@ -577,8 +601,8 @@ func resourceArmVirtualMachineRead(d *schema.ResourceData, meta interface{}) err
577601
}
578602
}
579603

580-
if resp.Properties.DiagnosticsProfile != nil {
581-
if err := d.Set("diagnostics_profile", flattenAzureRmVirtualMachineDiagnosticsProfile(resp.Properties.DiagnosticsProfile)); err != nil {
604+
if resp.Properties.DiagnosticsProfile != nil && resp.Properties.DiagnosticsProfile.BootDiagnostics != nil {
605+
if err := d.Set("boot_diagnostics", flattenAzureRmVirtualMachineDiagnosticsProfile(resp.Properties.DiagnosticsProfile.BootDiagnostics)); err != nil {
582606
return fmt.Errorf("[DEBUG] Error setting Virtual Machine Diagnostics Profile: %#v", err)
583607
}
584608
}
@@ -751,14 +775,13 @@ func flattenAzureRmVirtualMachineImageReference(image *compute.ImageReference) [
751775
return []interface{}{result}
752776
}
753777

754-
func flattenAzureRmVirtualMachineDiagnosticsProfile(profile *compute.DiagnosticsProfile) map[string]interface{} {
778+
func flattenAzureRmVirtualMachineDiagnosticsProfile(profile *compute.BootDiagnostics) []interface{} {
755779
result := make(map[string]interface{})
756-
bootDiagnostics := make(map[string]interface{})
757-
bootDiagnostics["enabled"] = *profile.BootDiagnostics.Enabled
758-
bootDiagnostics["storage_uri"] = *profile.BootDiagnostics.StorageURI
759-
result["boot_diagnostics"] = bootDiagnostics
760780

761-
return result
781+
result["enabled"] = *profile.Enabled
782+
result["storage_uri"] = *profile.StorageURI
783+
784+
return []interface{}{result}
762785
}
763786

764787
func flattenAzureRmVirtualMachineNetworkInterfaces(profile *compute.NetworkProfile) []string {
@@ -1140,20 +1163,24 @@ func expandAzureRmVirtualMachineDataDisk(d *schema.ResourceData) ([]compute.Data
11401163
return data_disks, nil
11411164
}
11421165

1143-
func expandAzureRmVirtualMachineDiagnosticsProfile(d *schema.ResourceData) compute.DiagnosticsProfile {
1144-
diagnosticsProfiles := d.Get("diagnostics_profile").(*schema.Set).List()
1145-
diagnosticsProfile := diagnosticsProfiles[0].(map[string]interface{})
1146-
bootDiagnosticses := diagnosticsProfile["boot_diagnostics"].(*schema.Set).List()
1147-
bootDiagnostics := bootDiagnosticses[0].(map[string]interface{})
1148-
enabled := bootDiagnostics["enabled"].(bool)
1149-
storageURI := bootDiagnostics["storage_uri"].(string)
1150-
1151-
return compute.DiagnosticsProfile{
1152-
BootDiagnostics: &compute.BootDiagnostics{
1153-
Enabled: &enabled,
1154-
StorageURI: &storageURI,
1155-
},
1166+
func expandAzureRmVirtualMachineDiagnosticsProfile(d *schema.ResourceData) *compute.DiagnosticsProfile {
1167+
bootDiagnostics := d.Get("boot_diagnostics").([]interface{})
1168+
1169+
diagnosticsProfile := &compute.DiagnosticsProfile{}
1170+
if len(bootDiagnostics) > 0 {
1171+
bootDiagnostic := bootDiagnostics[0].(map[string]interface{})
1172+
1173+
diagnostic := &compute.BootDiagnostics{
1174+
Enabled: riviera.Bool(bootDiagnostic["enabled"].(bool)),
1175+
StorageURI: riviera.String(bootDiagnostic["storage_uri"].(string)),
1176+
}
1177+
1178+
diagnosticsProfile.BootDiagnostics = diagnostic
1179+
1180+
return diagnosticsProfile
11561181
}
1182+
1183+
return nil
11571184
}
11581185

11591186
func expandAzureRmVirtualMachineImageReference(d *schema.ResourceData) (*compute.ImageReference, error) {

builtin/providers/azurerm/resource_arm_virtual_machine_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1300,6 +1300,11 @@ resource "azurerm_virtual_machine" "test" {
13001300
admin_password = "Password1234!"
13011301
}
13021302
1303+
boot_diagnostics {
1304+
enabled = true
1305+
storage_uri = "${azurerm_storage_account.test.primary_blob_endpoint}"
1306+
}
1307+
13031308
os_profile_windows_config {
13041309
winrm {
13051310
protocol = "http"

website/source/docs/providers/azurerm/r/virtual_machine.html.markdown

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ The following arguments are supported:
207207
* `location` - (Required) Specifies the supported Azure location where the resource exists. Changing this forces a new resource to be created.
208208
* `plan` - (Optional) A plan block as documented below.
209209
* `availability_set_id` - (Optional) The Id of the Availability Set in which to create the virtual machine
210-
* `diagnostics_profile` - (Optional) A Diagnostics Profile block as referenced below.
210+
* `boot_diagnostics` - (Optional) A boot diagnostics profile block as referenced below.
211211
* `vm_size` - (Required) Specifies the [size of the virtual machine](https://azure.microsoft.com/en-us/documentation/articles/virtual-machines-size-specs/).
212212
* `storage_image_reference` - (Optional) A Storage Image Reference block as documented below.
213213
* `storage_os_disk` - (Required) A Storage OS Disk block as referenced below.
@@ -229,10 +229,6 @@ For more information on the different example configurations, please check out t
229229
* `publisher` - (Optional) Specifies the publisher of the image.
230230
* `product` - (Optional) Specifies the product of the image from the marketplace.
231231

232-
`diagnostics_profile` supports the following:
233-
234-
* `boot_diagnostics`: (Required) A Boot Diagnostics block as documented below.
235-
236232
`boot_diagnostics` supports the following:
237233

238234
* `enabled`: (Required) Whether to enable boot diagnostics for the virtual machine.

0 commit comments

Comments
 (0)