/// <summary> /// Initializes a new instance of the <see cref="TerraformAttributeSetterContext"/> class. /// </summary> /// <param name="intrinsicInfo">The intrinsic information.</param> /// <param name="template">Reference to parsed CloudFormation template.</param> /// <param name="resource">Reference to resource being updated.</param> /// <param name="inputs">The list of input variables and data sources.</param> public TerraformAttributeSetterContext( IReadOnlyCollection <IntrinsicInfo> intrinsicInfo, ITemplate template, StateFileResourceDeclaration resource, IList <InputVariable> inputs) { this.Template = template; this.IntrinsicInfos = intrinsicInfo; this.Resource = resource; this.Inputs = inputs; this.Schema = AwsSchema.GetResourceSchema(resource.Type); }
/// <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); } }