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

Error when trying to create a Type of Object or map or list in Terraform #32

Closed
mazobeid opened this issue May 19, 2023 · 2 comments
Closed

Comments

@mazobeid
Copy link

Hello,

I want to create a terraform resource that have an attribute as object type.
In C#, I create an IDictionary<string, object> but when execute the Terraform plan I got this error below:
System.NotSupportedException: Unable to convert System.Collections.Generic.KeyValuePair`2[[System.String, System.Private.CoreLib, Version=7.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.Object, System.Private.CoreLib, Version=7.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]] to Terraform type.

Any idea how to create an object in terraform using the C# plugin ?
ex.:
resource "demo_player" "player" {
name = ""
player = {
name = ""
streams = ""
}
}

THank you

@mazobeid mazobeid changed the title System.NotSupportedException: Unable to convert System.Collections.Generic.KeyValuePair`2[[System.String, System.Private.CoreLib, Version=7.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.Object, System.Private.CoreLib, Version=7.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]] to Terraform type. Error when trying to create a Type of Object or map or list in Terraform May 19, 2023
@mcintyre321
Copy link

mcintyre321 commented Jun 5, 2023

If you register a custom version of ISchemaBuilder it should work

class MySchemaBuilder : ISchemaBuilder
{
    ... existing code from SchemaBuilder.cs ...
  private static string GetTerraformType(Type t)
  {
   ... existing code ...
   
   // handle object instead of throwing exception
   var properties = t.GetProperties().Select(p => //maybe filter to only properties with [Key] attribute?
   {
      var key = p.GetCustomAttribute<KeyAttribute>() ?? throw new InvalidOperationException($"Missing {nameof(KeyAttribute)} on {p.Name} in {p.PropertyType.Name}.");

      return "\"" + key.StringKey + "\":" + GetTerraformType(p.PropertyType);
   });
   return "[\"object\",{"+ string.Join(",", properties) + "}]";   
}

I should probably get a PR raised...

@SamuelFisher
Copy link
Owner

Object types are now supported (535e246).

Please see this example:

[Key("object_2")]
[Description("An object.")]
[Required]
public TestObjectNoOptionalAttributes Object2 { get; set; }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants