Esempio n. 1
0
 /// <summary>
 /// Initializes a new instance of the <see cref="UnsupportedPseudoParameterWarning"/> class.
 /// </summary>
 /// <param name="intrinsic">The intrinsic being warned about.</param>
 /// <param name="containingResource">The resource that contains the errant intrinsic.</param>
 /// <param name="location">The the AWS property path to the intrinsic being warned about.</param>
 public UnsupportedPseudoParameterWarning(
     RefIntrinsic intrinsic,
     IResource containingResource,
     PropertyPath location)
     : base(intrinsic, containingResource, location)
 {
 }
Esempio n. 2
0
        /// <summary>
        /// Renders the specified reference intrinsic.
        /// </summary>
        /// <param name="refIntrinsic">The reference intrinsic.</param>
        /// <param name="template">The template.</param>
        /// <param name="resource">The resource.</param>
        /// <param name="index">The index.</param>
        /// <returns>A <see cref="Reference"/> derivative according to what is being referenced.</returns>
        /// <exception cref="System.InvalidOperationException">
        /// Pseudo parameter \"{pseudo}\" cannot be referenced by terraform.
        /// or
        /// Reference \"{refIntrinsic.Reference}\" cannot be resolved.
        /// </exception>
        private static Reference Render(
            RefIntrinsic refIntrinsic,
            ITemplate template,
            ResourceMapping resource,
            int index)
        {
            switch (refIntrinsic.Reference)
            {
            case string pseudo when pseudo.StartsWith("AWS::"):

                if (PseudoParameterToDataBlock.ContainsKey(pseudo))
                {
                    return(PseudoParameterToDataBlock[pseudo]);
                }

                throw new InvalidOperationException(
                          $"Pseudo parameter \"{pseudo}\" cannot be referenced by terraform.");

            default:

                var param = template.Parameters.FirstOrDefault(p => p.Name == refIntrinsic.Reference);

                if (param != null)
                {
                    if (param.IsSsmParameter)
                    {
                        return(new DataSourceReference("aws_ssm_parameter", refIntrinsic.Reference, "value", true));
                    }

                    return(index < 0
                                   ? new InputVariableReference(refIntrinsic.Reference)
                                   : new InputVariableReference(refIntrinsic.Reference, index));
                }

                if (refIntrinsic.ExtraData is IntrinsicInfo intrinsicInfo)
                {
                    resource = intrinsicInfo.TargetResource;
                }

                if (resource != null && template.Resources.Any(r => r.Name == resource.LogicalId))
                {
                    return(new DirectReference(resource.Address));
                }

                throw new InvalidOperationException($"Reference \"{refIntrinsic.Reference}\" cannot be resolved.");
            }
        }
Esempio n. 3
0
            /// <summary>
            /// Creates an <see cref="IntrinsicInfo"/> for a Ref intrinsic.
            /// </summary>
            /// <param name="refIntrinsic">The reference intrinsic.</param>
            /// <param name="currentPath">The current path.</param>
            /// <returns>An <see cref="IntrinsicInfo"/></returns>
            private IntrinsicInfo ProcessRef(RefIntrinsic refIntrinsic, PropertyPath currentPath)
            {
                object evaluation;
                var    target = refIntrinsic.Reference;

                var param = this.Inputs.FirstOrDefault(i => i.Name == target);

                if (param != null)
                {
                    evaluation = param.IsScalar ? (object)param.ScalarIdentity : param.ListIdentity;
                    return(new IntrinsicInfo(currentPath.Clone(), refIntrinsic, null, evaluation));
                }

                if (target.StartsWith("AWS::"))
                {
                    // An unsupported AWS pseudo parameter like AWS::StackName etc.
                    throw new UnsupportedPseudoParameterWarning(
                              refIntrinsic,
                              this.currentCloudFormationResource,
                              currentPath);
                }

                var cloudFormationResource = this.CloudFormationResources
                                             .Where(r => TerraformExporterConstants.IgnoredResources.All(ir => ir != r.ResourceType))
                                             .FirstOrDefault(r => r.LogicalResourceId == target);

                if (cloudFormationResource == null)
                {
                    // If not found, then reference is to a resource that couldn't be imported eg. a custom resource
                    // or a known unsupported type.
                    throw new UnsupportedResourceWarning(refIntrinsic, this.currentCloudFormationResource, currentPath);
                }

                var targetResourceSummary = new ResourceMapping
                {
                    AwsType       = cloudFormationResource.ResourceType,
                    LogicalId     = cloudFormationResource.LogicalResourceId,
                    PhysicalId    = cloudFormationResource.PhysicalResourceId,
                    TerraformType = this.TerraformResources.First(
                        tr => tr.Name == cloudFormationResource.LogicalResourceId).Type
                };

                evaluation = cloudFormationResource.PhysicalResourceId;

                return(new IntrinsicInfo(currentPath, refIntrinsic, targetResourceSummary, evaluation));
            }
        public void RefToAwsPseudoParameterRedersAsExpectedReference(string pseudo, string reference)
        {
            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);

            @ref.Render(template.Object, (ResourceMapping)null, null).ReferenceExpression.Should().Be(reference);
        }
        public void RefToParameterRendersAsVar()
        {
            const string ParamName = "Param1";
            var          expected  = $"var.{ParamName}";

            var parameter = new Mock <IParameter>();

            parameter.Setup(p => p.Name).Returns(ParamName);

            var template = new Mock <ITemplate>();

            template.Setup(t => t.Parameters).Returns(new List <IParameter> {
                parameter.Object
            });

            var @ref = new RefIntrinsic(ParamName);

            @ref.Render(template.Object, (ResourceMapping)null, null).ReferenceExpression.Should().Be(expected);
        }
        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);
        }