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

(v1.0.0) JSON error when attempting to import an existing machine. #16

Closed
john-terrell opened this issue Jul 13, 2022 · 6 comments
Closed
Labels
bug Something isn't working triaged Triaged to be addressed in a given cycle

Comments

@john-terrell
Copy link

john-terrell commented Jul 13, 2022

I'm getting this error when attempting to import a machine using Maas v3.2 and Provider v1.0.0:

$ TF_LOG=ERROR terraform import maas_machine.compute01 rested-tiger.maas
2022-07-13T15:13:28.033-0600 [ERROR] vertex "maas_machine.compute01 (import id \"rested-tiger.maas\")" error: json: cannot unmarshal array into Go value of type string
╷
│ Error: json: cannot unmarshal array into Go value of type string
│ 
│ 
@tFable
Copy link

tFable commented Mar 13, 2023

Hello,

I came here to report the same issue and I'm now I see that there is a ticket for this since July 2022 🙂

I'm having this same issue on 1.1.0 with Terraform 1.4.0

maas_machine.mahine2: Importing from ID "machine"...
╷
│ Error: json: cannot unmarshal array into Go value of type string
│ 
│ 
╵

Interestingly, I was able to import a couple of machines with the exact same versions of the provider and TF but it looks like I am no longer able to. This can totally be because of specifics with other machines triggering this error, of course.

Importing of instances appears to work.

Any help to solve this would be greatly appreciated. Without the ability to import machines, any machines configured outside of TF must be rebuilt if we wanted them added to TF.

Thanks for your help!

@skatsaounis
Copy link
Collaborator

Hi both,

With this PR #92, the new version of gomaasclient is used by the provider, which includes a fix on the parsing of a couple of machine fields. I am closing this ticket but please re-open if you hit the issue again so as to eliminate any remaining erroneous field.

@UnknownTy
Copy link

Hey @skatsaounis unfortunately it appears this bug still exists.

Not OP but encountering it on my own instance of MaaS.
terraform -v:

Terraform v1.5.7
on darwin_arm64
+ provider registry.terraform.io/maas/maas v1.3.0

terraform apply:

2023-10-03T09:09:20.753-0700 [ERROR] vertex "maas_machine.testSystem (import id \"*****\")" error: json: cannot unmarshal array into Go value of type string
2023-10-03T09:09:20.753-0700 [ERROR] vertex "maas_machine.testSystem (expand)" error: json: cannot unmarshal array into Go value of type string
│ Error: json: cannot unmarshal array into Go value of type string

From my own digging into this (with limited Go knowledge) I tracked it down to the gomaasclient entity.Machine.GetPowerParameters

func (m *Machine) GetPowerParameters(systemID string) (params map[string]string, err error) {
	params = map[string]string{}
	err = m.client(systemID).Get("power_parameters", url.Values{}, func(data []byte) error {
		return json.Unmarshal(data, &params)
	})
	return
}

This properly get the JSON from the MaaS API, however when it attempts to unmarshal into a map[string]string it will fail.
Some power providers (Such as IPMI) return an array within this object. From what I can tell this is the source of the bug since Go JSON cannot unmarshal an array into a string. Even with an empty array within this option the error still occurs.

I did attempt to fix this on my own however with my limited Go experience I couldn't find a clean solution. From what I could tell, changing this return to map[string]interface{} would resolve this as it could unmarshal the JSON array to a GO array. However I'm not sure if this is the best solution (Plus, you'd have to rework how the maasapiclient works with the data returned)

Sorry for all this info dump. I just wanted to help out but don't have the GO knowledge to put any real changes in place ._.
Let me know if you need any additional info!

@skatsaounis skatsaounis reopened this Oct 3, 2023
@skatsaounis
Copy link
Collaborator

Hi @UnknownTy

You were right on your investigation findings and thank you very much for your time. Indeed, the problematic power parameter is the workaround_flags of type IPMI. This parameter in contrast with any other is not a string but a []string. I developed a solution and I made it work locally with the linked PR: #96

Since this one requires changes to both gomaasclient and also to the power_parameters field it may delay to get landed but it is definitely a solution. Among other things, we also hit a Terraform SDK limitation, thus the proposed change to the field.

@UnknownTy
Copy link

Thanks @skatsaounis for the quick turnaround on that!
Glad info I could provide could help out with this old issue.
Was perfect timing as I encountered this issue, dug into it a bit and saw you updated the original issue just a few days later.

Always a wonder to see someone take an idea and do it much better than I could in a language I'm still learning ;)
Cheers.

@UnknownTy
Copy link

I see #96 was merged into Main.
Tested using a locally built version of the provider:

maas_machine.test_machine: Importing from ID "*****"...
maas_machine.test_machine: Import prepared!
  Prepared maas_machine for  import
maas_machine.test_machine: Refreshing state... [id=*****]

Import successful!

The resources that were imported are shown above. These resources are now in
your Terraform state and will henceforth be managed by Terraform.

Checking terraform.tfstate confirms the power_parameters are properly brought in.
Managing the imported state also functioning as expected.

Thanks again @skatsaounis!

@troyanov troyanov closed this as completed Oct 9, 2023
@skatsaounis skatsaounis added bug Something isn't working triaged Triaged to be addressed in a given cycle labels Oct 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working triaged Triaged to be addressed in a given cycle
Projects
None yet
Development

No branches or pull requests

5 participants