New SDK: Unmarshall RawState with OptionalAttributes fails

Using the new terraform-plugin-go SDK and I basically have the following tftypes.Type

Object{
  AttributeTypes: map[string]Type{
    "id":          Number,
    "name":        String,
    "description": String,
    "role": Map{AttributeType: Object{
      AttributeTypes: map[string]Type{
        "id":          Number,
        "description": String,
      },
      OptionalTypes: map[string]struct{}{
        "description": struct{}{},
      },
    },
  },
  OptionalTypes: map[string]struct{}[
    "description": struct{}{},
  ]
}

I used this type when importing, resulting in a state like this:

"id": 1,
"name": "foo",
"role": {
  "owner": {
    "description": "owner role",
    "id": 2
  },
}

Running a plan requires you to call RawState.Unmarshall(type), so it seems obivous I should supply the same type as I did when importing (defined above). However, it panics with this this error (expanded for clarity):

ElementKeyString("owner"): cant use 
  tftypes.Object["id":tftypes.Number, "description":tftypes.String] 
as 
  tftypes.Object["id":tftypes.Number, "description":tftypes.String?]

Where did it get the idea that description was not optional? I definitely passed in a type marking it as optional so I am not sure how it is possible inferring otherwise.

If I make description non-optional (post-import), it generates a healthy plan and doesn’t panic.

PS: I would open an issue in the repo tracker but seems like its pointing me here but I am unsure if its a bug or if I am using it wrong.

Oh this is very interesting! OK, I need to think about this.

Essentially what is happening here is that Terraform doesn’t (for various reasons) send markers like “optional” over the wire to the providers, so the type of the data Terraform sends us isn’t optional. I believe.

Your assumption that you should be able to just use the same type in RawState.Unmarshal is, however, a good and fair assumption.

I think my hot take on this is that it should be filed as a bug on the repo, and I’ll look into how we can make this work most intuitively.

Sorry for the problems there :frowning:

@paddy Thanks for the reply. Opened in issue on the plugin repo: Unable to Unmarshal RawState when to a type with OptionalAttributes · Issue #82 · hashicorp/terraform-plugin-go · GitHub

1 Like

Thank you! Sorry, I know we’re slow in getting around to fixing this, there’s a lot going on at the moment. :smiley: I’ll circle up with the core team and work through what expectations of providers are in this case, and see if we can’t figure out a fix.

1 Like