SamuelFisher / TerraformPluginDotNet

Write Terraform providers in C#.
MIT License
46 stars 14 forks source link

Can't parse `public_dns` as list<string> #5

Closed omerlewitz closed 3 years ago

omerlewitz commented 3 years ago

Hi Samuel,

I was wondering if you have any clue how to handle dynamic attributes, and in general all of the non-primitive types.

I have attached three files as shown below.

The main issue is within the GetTerraformType which parse the types.

With that being said I was able to catch list<string> which was inserted by a simple text string without any interpolation whatsoever. For instance in the .tf file machines = ["omer", "omer ,"etc"].

resource "aws_instance" "ubuntu_machine" {
  ami                         = "ami-09e67e426f25ce0d7"
  instance_type               = "t2.micro"
  key_name                    = "raven"
  associate_public_ip_address = true

  vpc_security_group_ids = [
    aws_security_group.ravendb_access.id
  ]

  tags = {
    Name = "RavenDB Instance"
  }
}

resource "ravendb_cluster" "my_cluster" {
    depends_on = [aws_instance.ubuntu_machine]

    machines = [flatten(aws_instance.ubuntu_machine.public_dns)]

}

output "ravendb_cluster_ubuntu_machine_id" {
  value = aws_instance.ubuntu_machine.public_dns
}
namespace SampleProvider
{
    [MessagePackObject]
    public class ResourceRavenCluster
    {
        [Key("id")]
        [Computed]
        [Description("Unique ID for this resource.")]
        [MessagePackFormatter(typeof(ComputedValueFormatter))]
        public string Id { get; set; }

        [Key("machines")]
        [Description("Machine Public Ips")]
        public dynamic Machines { get; set; }    
    }
        private static string GetTerraformType(Type t)
        {
            if (t == typeof(string))
            {
                return "\"string\"";
            }

            if (t == typeof(int))
            {
                return "\"number\"";
            }

            if (t == typeof(List<string>))
            {
                 return "[\"list\",\"string\"]";
            }

            if (t == typeof(List<object>))
            {
                return "dynamic";
            }
            if (t == typeof(List<object>))
            {
                return "[\"list\",\"dynamic\"]";
            }

            throw new NotSupportedException();
        }
    }