/// <summary> /// Renders a <c>!FindInMap</c> to a lookup on <c>locals.mappings</c> /// </summary> /// <param name="findInMapIntrinsic">The <see href="https://fireflycons.github.io/Firefly.CloudFormationParser/api/Firefly.CloudFormationParser.Intrinsics.Functions.FindInMapIntrinsic.html"><c>FindInMap</c></see> intrinsic to render.</param> /// <param name="template">The <see href="https://fireflycons.github.io/Firefly.CloudFormationParser/api/Firefly.CloudFormationParser.ITemplate.html">imported template</see>.</param> /// <param name="resource">The related new terraform resource where this map lookup TODO might need to be all imported resources</param> /// <param name="inputs">The list of input variables and data sources.</param> /// <param name="index">Index of second level key item to return, when this is an array</param> /// <returns>A <see cref="MapReference"/>.</returns> private static Reference Render( FindInMapIntrinsic findInMapIntrinsic, ITemplate template, ResourceMapping resource, IList <InputVariable> inputs, int index) { var sb = new StringBuilder(); var mapParts = new Stack <string>(); mapParts.Push("local"); mapParts.Push("mappings"); foreach (var property in new[] { findInMapIntrinsic.MapName, findInMapIntrinsic.TopLevelKey, findInMapIntrinsic.SecondLevelKey }) { switch (property) { case string s: sb.Append($".{s}"); break; case RefIntrinsic refIntrinsic: sb.Append($"[{refIntrinsic.Render(template, resource, inputs)}]"); break; case IIntrinsic intrinsic: throw new InvalidOperationException($"Intrinsic \"{intrinsic.TagName}\" cannot be resolved."); } } if (index > -1) { sb.Append($"[{index}]"); } return(new MapReference(sb.ToString())); }
public void FindInMapWithRefAtTopKeyRendersASExpectedLocalLookup() { var pseudo = "AWS::AccountId"; var expected = "local.mappings.MapName[data.aws_caller_identity.current.account_id].SecondKey"; var parameter = new Mock <IParameter>(); parameter.Setup(p => p.Name).Returns(pseudo); var template = new Mock <ITemplate>(); template.Setup(t => t.Parameters).Returns(new List <IParameter>()); template.Setup(t => t.PseudoParameters).Returns(new List <IParameter> { parameter.Object }); var @ref = new RefIntrinsic(pseudo); var findInMap = new FindInMapIntrinsic(new object[] { "MapName", @ref, "SecondKey" }); findInMap.Render(template.Object, (ResourceMapping)null, null).ReferenceExpression.Should().Be(expected); }