/// <summary> /// Resolves the input dependencies of a module imported by the current module. /// </summary> /// <param name="referencedModule">The referenced module.</param> public void ResolveModuleDependencies(ModuleInfo referencedModule) { // Visit the AWS::CloudFormation::Stack resource gathering all intrinsics that might imply reference to another resource or input var intrinsicVisitorContext = new IntrinsicVisitorContext( this.settings, this.terraformResources, this.module.Inputs, referencedModule.StackResource, this.warnings, this.module); // Visit the AWS::CloudFormation::Stack associated with the given module and pull out // intrinsic references in Parameters property. var intrinsicVisitor = new IntrinsicVisitor(this.template); referencedModule.StackResource.Accept(intrinsicVisitor, intrinsicVisitorContext); foreach (var intrinsicInfo in intrinsicVisitorContext.ReferenceLocations) { var reference = this.GetModuleInputReference(referencedModule, intrinsicInfo); if (reference != null) { ApplyModuleInputReference(referencedModule, intrinsicInfo, reference); } } }
/// <summary> /// Resolves the dependencies for the given terraform resource. /// </summary> /// <param name="terraformStateFileResource">The current terraform resource from state file.</param> public void ResolveResourceDependencies(StateFileResourceDeclaration terraformStateFileResource) { try { var referenceLocations = new List <IntrinsicInfo>(); // Get CF resource for the current state file resource entry this.currentCloudFormationResource = this.settings.Resources.First(r => r.LogicalResourceId == terraformStateFileResource.Name); // Find related resources that may be merged into this one var relatedResources = this.template.DependencyGraph.Edges.Where( e => e.Source.Name == this.currentCloudFormationResource.LogicalResourceId && e.Target.TemplateObject is IResource res && TerraformExporterConstants.MergedResources.Contains(res.Type)) .Select(e => (IResource)e.Target.TemplateObject); foreach (var cloudFormationResource in new[] { this.currentCloudFormationResource.TemplateResource } .Concat(relatedResources)) { // Visit the CF resource gathering all intrinsics that might imply reference to another resource or input var intrinsicVisitorContext = new IntrinsicVisitorContext( this.settings, this.terraformResources, this.module.Inputs, cloudFormationResource, this.warnings, this.module); var intrinsicVisitor = new IntrinsicVisitor(this.template); cloudFormationResource.Accept(intrinsicVisitor, intrinsicVisitorContext); referenceLocations.AddRange(intrinsicVisitorContext.ReferenceLocations); } // Visit the terraform resource finding value matches between resource attributes and intrinsic evaluations, recording what needs to be modified var dependencyContext = new TerraformAttributeSetterContext( referenceLocations, this.template, terraformStateFileResource, this.module.Inputs); terraformStateFileResource.ResourceInstance.Attributes.Accept( new TerraformAttributeSetterVisitor(), dependencyContext); // For each found modification, update attribute value with JSON encoded reference expression or string interpolation. foreach (var modification in dependencyContext.Modifications.Where(m => m.Reference != null)) { if (modification.ContainingProperty == null) { // Normal resource attribute ApplyResourceAttributeReference( terraformStateFileResource.ResourceInstance.Attributes.SelectToken( modification.ValueToReplace.Path), modification); } else { // Modification lies within nested JSON, e.g. a policy document. // First, deserialize the nested JSON - which will work because we already did it once. StateFileSerializer.TryGetJson( modification.ContainingProperty.Value.Value <string>(), false, terraformStateFileResource.Name, terraformStateFileResource.Type, out var document); ApplyResourceAttributeReference( document.SelectToken(modification.ValueToReplace.Path), modification); // Now put back the nested JSON complete with added reference var enc = document.ToString(Formatting.None); modification.ContainingProperty.Value = enc; } } } catch (HclSerializerException) { throw; } catch (Exception e) { throw new HclSerializerException( $"Internal error: {e.Message}", terraformStateFileResource.Name, terraformStateFileResource.Type, e); } }