Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[v11.2.2-beta] JSON unmarshalling error in monitor.AlertRuleResource #951

Closed
dominik-lekse opened this issue Jan 7, 2018 · 5 comments
Closed
Assignees

Comments

@dominik-lekse
Copy link

dominik-lekse commented Jan 7, 2018

While working on the pull request hashicorp/terraform-provider-azurerm#478, I encountered a JSON unmarshalling error when using the methods AlertRulesClient.CreateOrUpdate or AlertRulesClient.Get. Both methods unmarshal JSON to a monitor.AlertRuleResource instance.

panic: runtime error: invalid memory address or nil pointer dereference [recovered]
	panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x14c792d]

goroutine 507 [running]:
encoding/json.(*decodeState).unmarshal.func1(0xc420112be8)
	/usr/local/Cellar/go/1.9.2/libexec/src/encoding/json/decode.go:173 +0xe0
panic(0x2214200, 0x2ea9d90)
	/usr/local/Cellar/go/1.9.2/libexec/src/runtime/panic.go:491 +0x283
github.com/terraform-providers/terraform-provider-azurerm/vendor/github.com/Azure/azure-sdk-for-go/arm/monitor.(*AlertRule).UnmarshalJSON(0x0, 0xc42072e000, 0x90b, 0xe00, 0x0, 0x0)
	/Users/user/Projects/terraform/src/github.com/terraform-providers/terraform-provider-azurerm/vendor/github.com/Azure/azure-sdk-for-go/arm/monitor/models.go:330 +0x7dd
encoding/json.(*decodeState).object(0xc4205377a0, 0x227c400, 0xc420289ec0, 0x16)
	/usr/local/Cellar/go/1.9.2/libexec/src/encoding/json/decode.go:601 +0x1b77
encoding/json.(*decodeState).value(0xc4205377a0, 0x227c400, 0xc420289ec0, 0x16)
	/usr/local/Cellar/go/1.9.2/libexec/src/encoding/json/decode.go:405 +0x2e4
encoding/json.(*decodeState).unmarshal(0xc4205377a0, 0x227c400, 0xc420289ec0, 0x0, 0x0)
	/usr/local/Cellar/go/1.9.2/libexec/src/encoding/json/decode.go:187 +0x20e
encoding/json.Unmarshal(0xc42072e000, 0x90b, 0xe00, 0x227c400, 0xc420289ec0, 0x90b, 0xc42072e000)
	/usr/local/Cellar/go/1.9.2/libexec/src/encoding/json/decode.go:107 +0x148
github.com/terraform-providers/terraform-provider-azurerm/vendor/github.com/Azure/go-autorest/autorest.ByUnmarshallingJSON.func1.1(0xc4204003f0, 0x2255f60, 0xc4202c5500)
	/Users/user/Projects/terraform/src/github.com/terraform-providers/terraform-provider-azurerm/vendor/github.com/Azure/go-autorest/autorest/responder.go:169 +0x2d2
github.com/terraform-providers/terraform-provider-azurerm/vendor/github.com/Azure/go-autorest/autorest.ResponderFunc.Respond(0xc420509260, 0xc4204003f0, 0xc420509260, 0x2e76000)
	/Users/user/Projects/terraform/src/github.com/terraform-providers/terraform-provider-azurerm/vendor/github.com/Azure/go-autorest/autorest/responder.go:41 +0x30
github.com/terraform-providers/terraform-provider-azurerm/vendor/github.com/Azure/go-autorest/autorest.ByClosing.func1.1(0xc4204003f0, 0x2e76000, 0xc4202c5500)
	/Users/user/Projects/terraform/src/github.com/terraform-providers/terraform-provider-azurerm/vendor/github.com/Azure/go-autorest/autorest/responder.go:129 +0x3d
github.com/terraform-providers/terraform-provider-azurerm/vendor/github.com/Azure/go-autorest/autorest.ResponderFunc.Respond(0xc4202c5500, 0xc4204003f0, 0x4, 0x2e76000)
	/Users/user/Projects/terraform/src/github.com/terraform-providers/terraform-provider-azurerm/vendor/github.com/Azure/go-autorest/autorest/responder.go:41 +0x30
github.com/terraform-providers/terraform-provider-azurerm/vendor/github.com/Azure/go-autorest/autorest.Respond(0xc4204003f0, 0xc420112ed8, 0x4, 0x4, 0x0, 0x0)
	/Users/user/Projects/terraform/src/github.com/terraform-providers/terraform-provider-azurerm/vendor/github.com/Azure/go-autorest/autorest/responder.go:78 +0x6b
github.com/terraform-providers/terraform-provider-azurerm/vendor/github.com/Azure/azure-sdk-for-go/arm/monitor.AlertRulesClient.CreateOrUpdateResponder(0x2e70540, 0xc42044c5f0, 0x2e76040, 0xc420483980, 0x0, 0x0, 0xdf8475800, 0xd18c2e2800, 0x3, 0x6fc23ac00, ...)
	/Users/user/Projects/terraform/src/github.com/terraform-providers/terraform-provider-azurerm/vendor/github.com/Azure/azure-sdk-for-go/arm/monitor/alertrules.go:112 +0x19e
github.com/terraform-providers/terraform-provider-azurerm/vendor/github.com/Azure/azure-sdk-for-go/arm/monitor.AlertRulesClient.CreateOrUpdate(0x2e70540, 0xc42044c5f0, 0x2e76040, 0xc420483980, 0x0, 0x0, 0xdf8475800, 0xd18c2e2800, 0x3, 0x6fc23ac00, ...)
	/Users/user/Projects/terraform/src/github.com/terraform-providers/terraform-provider-azurerm/vendor/github.com/Azure/azure-sdk-for-go/arm/monitor/alertrules.go:70 +0x51e
github.com/terraform-providers/terraform-provider-azurerm/azurerm.resourceArmMetricAlertRuleCreateOrUpdate(0xc4201955e0, 0x2325a60, 0xc4204f9500, 0x24, 0x2edbe20)
	/Users/user/Projects/terraform/src/github.com/terraform-providers/terraform-provider-azurerm/azurerm/resource_arm_metric_alertrule.go:164 +0x398

Using the method AlertRulesClient.CreateOrUpdate provided by monitor.AlertRulesClient raises a runtime error due invalid memory address or nil pointer dereference. The error occurs during marshalling the JSON response in Get or CreateOrUpdate. The reason appears to be that the field AlertRule of type AlertRuleResource is generated as an embedded field. This leads to nil being passed to monitor.(*AlertRule).UnmarshalJSON when unmarshalling and causes the runtime error.

type AlertRuleResource struct {
	autorest.Response `json:"-"`
	ID                *string             `json:"id,omitempty"`
	Name              *string             `json:"name,omitempty"`
	Type              *string             `json:"type,omitempty"`
	Location          *string             `json:"location,omitempty"`
	Tags              *map[string]*string `json:"tags,omitempty"`
	*AlertRule        `json:"properties,omitempty"`
}

Changing the field AlertRule to an explicitly defined field as shown in the following excerpt solves the problem.

type AlertRuleResource struct {
	autorest.Response `json:"-"`
	ID                *string             `json:"id,omitempty"`
	Name              *string             `json:"name,omitempty"`
	Type              *string             `json:"type,omitempty"`
	Location          *string             `json:"location,omitempty"`
	Tags              *map[string]*string `json:"tags,omitempty"`
	AlertRule	  *AlertRule          `json:"properties,omitempty"`
}

This error occurs in version v11.2.2-beta of the azure-sdk-for-go which is used by the most recent version of the Terraform AzureRM provider. Although not tested, I assume that the most recent version of the azure-sdk-for-go encounters this error since the definition of AlertRuleResource in arm/monitor/models.go has not changed since v11.2.2-beta.

@jhendrixMSFT
Copy link
Member

@dominik-lekse This appears to be fixed in v12 as I can't repro this issue with it. Is it possible for you to move to v12? Also note that you want to be using content from under the services directory, the stuff under the arm directory has been deprecated.
https://github.com/Azure/azure-sdk-for-go/tree/master/services/monitor/mgmt/2017-05-01-preview/insights

@dominik-lekse
Copy link
Author

Hi @jhendrixMSFT, thanks for testing this for v12. I think the terraform provider will upgrade to v12 during this month so that I can test it again. Shall we keep this issue open until then?

@jhendrixMSFT
Copy link
Member

@dominik-lekse We can keep this open until you've had a chance to validate with v12 if that helps.

@dominik-lekse
Copy link
Author

Hi @jhendrixMSFT, I can confirm that using the latest v12 SDK, this error is fixed. Many thanks.

@jhendrixMSFT
Copy link
Member

Fantastic, thanks for letting me know. :)

@github-actions github-actions bot locked and limited conversation to collaborators Apr 11, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants