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

Support nullable attributes #10

Merged
merged 1 commit into from
Aug 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions TerraformPluginDotNet.Test/Functional/TerraformResourceTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,36 @@ public async Task TestPlanCreateAllFields()

Assert.That(after, Is.EqualTo(expected));
}

[Test]
public async Task TestPlanCreateOnlyRequiredFields()
{
using var terraform = await _host.CreateTerraformTestInstanceAsync(ProviderName);

var resourcePath = Path.Combine(terraform.WorkDir, "file.tf");

await File.WriteAllTextAsync(resourcePath, $@"
resource ""test_resource"" ""test"" {{
required_attribute = ""value""
}}
");

var plan = await terraform.PlanWithOutputAsync();

Assert.That(plan.ResourceChanges, Has.Count.EqualTo(1));
Assert.That(plan.ResourceChanges.Single().Change.Actions.Single(), Is.EqualTo("create"));
Assert.That(plan.ResourceChanges.Single().Change.Before, Is.Null);

var after = plan.ResourceChanges.Single().Change.After.ToJsonString(new JsonSerializerOptions() { WriteIndented = true });
var expected = @"
{
""boolean_attribute"": null,
""double_attribute"": null,
""float_attribute"": null,
""int_attribute"": null,
""required_attribute"": ""value""
}".Trim();

Assert.That(after, Is.EqualTo(expected));
}
}
8 changes: 4 additions & 4 deletions TerraformPluginDotNet.Test/TestResource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,17 @@ public class TestResource

[Key("int_attribute")]
[Description("Int attribute.")]
public int IntAttribute { get; set; }
public int? IntAttribute { get; set; }

[Key("boolean_attribute")]
[Description("A boolean attribute.")]
public bool BooleanAttribute { get; set; }
public bool? BooleanAttribute { get; set; }

[Key("float_attribute")]
[Description("A float attribute.")]
public float FloatAttribute { get; set; }
public float? FloatAttribute { get; set; }

[Key("double_attribute")]
[Description("A double attribute.")]
public float DoubleAttribute { get; set; }
public float? DoubleAttribute { get; set; }
}
5 changes: 5 additions & 0 deletions TerraformPluginDotNet/Schemas/SchemaBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ public Schema BuildSchema(Type type)

private static string GetTerraformType(Type t)
{
if (t.IsValueType && Nullable.GetUnderlyingType(t) != null)
{
t = Nullable.GetUnderlyingType(t);
}

if (t == typeof(string))
{
return "string";
Expand Down