github.com/lukahartwig/terraform@v0.11.4-0.20180302171601-664391c254ea/configs/module_merge.go (about)

     1  package configs
     2  
     3  import (
     4  	"github.com/hashicorp/hcl2/hcl"
     5  	"github.com/zclconf/go-cty/cty"
     6  )
     7  
     8  // The methods in this file are used by Module.mergeFile to apply overrides
     9  // to our different configuration elements. These methods all follow the
    10  // pattern of mutating the receiver to incorporate settings from the parameter,
    11  // returning error diagnostics if any aspect of the parameter cannot be merged
    12  // into the receiver for some reason.
    13  //
    14  // User expectation is that anything _explicitly_ set in the given object
    15  // should take precedence over the corresponding settings in the receiver,
    16  // but that anything omitted in the given object should be left unchanged.
    17  // In some cases it may be reasonable to do a "deep merge" of certain nested
    18  // features, if it is possible to unambiguously correlate the nested elements
    19  // and their behaviors are orthogonal to each other.
    20  
    21  func (p *Provider) merge(op *Provider) hcl.Diagnostics {
    22  	var diags hcl.Diagnostics
    23  
    24  	if op.Version.Required != nil {
    25  		p.Version = op.Version
    26  	}
    27  
    28  	p.Config = mergeBodies(p.Config, op.Config)
    29  
    30  	return diags
    31  }
    32  
    33  func mergeProviderVersionConstraints(recv map[string][]VersionConstraint, ovrd []*ProviderRequirement) {
    34  	// Any provider name that's mentioned in the override gets nilled out in
    35  	// our map so that we'll rebuild it below. Any provider not mentioned is
    36  	// left unchanged.
    37  	for _, reqd := range ovrd {
    38  		delete(recv, reqd.Name)
    39  	}
    40  	for _, reqd := range ovrd {
    41  		recv[reqd.Name] = append(recv[reqd.Name], reqd.Requirement)
    42  	}
    43  }
    44  
    45  func (v *Variable) merge(ov *Variable) hcl.Diagnostics {
    46  	var diags hcl.Diagnostics
    47  
    48  	if ov.DescriptionSet {
    49  		v.Description = ov.Description
    50  		v.DescriptionSet = ov.DescriptionSet
    51  	}
    52  	if ov.Default != cty.NilVal {
    53  		v.Default = ov.Default
    54  	}
    55  	if ov.TypeHint != TypeHintNone {
    56  		v.TypeHint = ov.TypeHint
    57  	}
    58  
    59  	return diags
    60  }
    61  
    62  func (l *Local) merge(ol *Local) hcl.Diagnostics {
    63  	var diags hcl.Diagnostics
    64  
    65  	// Since a local is just a single expression in configuration, the
    66  	// override definition entirely replaces the base definition, including
    67  	// the source range so that we'll send the user to the right place if
    68  	// there is an error.
    69  	l.Expr = ol.Expr
    70  	l.DeclRange = ol.DeclRange
    71  
    72  	return diags
    73  }
    74  
    75  func (o *Output) merge(oo *Output) hcl.Diagnostics {
    76  	var diags hcl.Diagnostics
    77  
    78  	if oo.Description != "" {
    79  		o.Description = oo.Description
    80  	}
    81  	if oo.Expr != nil {
    82  		o.Expr = oo.Expr
    83  	}
    84  	if oo.SensitiveSet {
    85  		o.Sensitive = oo.Sensitive
    86  		o.SensitiveSet = oo.SensitiveSet
    87  	}
    88  
    89  	// We don't allow depends_on to be overridden because that is likely to
    90  	// cause confusing misbehavior.
    91  	if len(oo.DependsOn) != 0 {
    92  		diags = append(diags, &hcl.Diagnostic{
    93  			Severity: hcl.DiagError,
    94  			Summary:  "Unsupported override",
    95  			Detail:   "The depends_on argument may not be overridden.",
    96  			Subject:  oo.DependsOn[0].SourceRange().Ptr(), // the first item is the closest range we have
    97  		})
    98  	}
    99  
   100  	return diags
   101  }
   102  
   103  func (mc *ModuleCall) merge(omc *ModuleCall) hcl.Diagnostics {
   104  	var diags hcl.Diagnostics
   105  
   106  	if omc.SourceSet {
   107  		mc.SourceAddr = omc.SourceAddr
   108  		mc.SourceAddrRange = omc.SourceAddrRange
   109  		mc.SourceSet = omc.SourceSet
   110  	}
   111  
   112  	if omc.Count != nil {
   113  		mc.Count = omc.Count
   114  	}
   115  
   116  	if omc.ForEach != nil {
   117  		mc.ForEach = omc.ForEach
   118  	}
   119  
   120  	if len(omc.Version.Required) != 0 {
   121  		mc.Version = omc.Version
   122  	}
   123  
   124  	mc.Config = mergeBodies(mc.Config, omc.Config)
   125  
   126  	// We don't allow depends_on to be overridden because that is likely to
   127  	// cause confusing misbehavior.
   128  	if len(mc.DependsOn) != 0 {
   129  		diags = append(diags, &hcl.Diagnostic{
   130  			Severity: hcl.DiagError,
   131  			Summary:  "Unsupported override",
   132  			Detail:   "The depends_on argument may not be overridden.",
   133  			Subject:  mc.DependsOn[0].SourceRange().Ptr(), // the first item is the closest range we have
   134  		})
   135  	}
   136  
   137  	return diags
   138  }
   139  
   140  func (r *ManagedResource) merge(or *ManagedResource) hcl.Diagnostics {
   141  	var diags hcl.Diagnostics
   142  
   143  	if or.Connection != nil {
   144  		r.Connection = or.Connection
   145  	}
   146  	if or.Count != nil {
   147  		r.Count = or.Count
   148  	}
   149  	if or.CreateBeforeDestroySet {
   150  		r.CreateBeforeDestroy = or.CreateBeforeDestroy
   151  		r.CreateBeforeDestroySet = or.CreateBeforeDestroySet
   152  	}
   153  	if or.ForEach != nil {
   154  		r.ForEach = or.ForEach
   155  	}
   156  	if len(or.IgnoreChanges) != 0 {
   157  		r.IgnoreChanges = or.IgnoreChanges
   158  	}
   159  	if or.PreventDestroySet {
   160  		r.PreventDestroy = or.PreventDestroy
   161  		r.PreventDestroySet = or.PreventDestroySet
   162  	}
   163  	if or.ProviderConfigRef != nil {
   164  		r.ProviderConfigRef = or.ProviderConfigRef
   165  	}
   166  	if len(or.Provisioners) != 0 {
   167  		r.Provisioners = or.Provisioners
   168  	}
   169  
   170  	r.Config = mergeBodies(r.Config, or.Config)
   171  
   172  	// We don't allow depends_on to be overridden because that is likely to
   173  	// cause confusing misbehavior.
   174  	if len(r.DependsOn) != 0 {
   175  		diags = append(diags, &hcl.Diagnostic{
   176  			Severity: hcl.DiagError,
   177  			Summary:  "Unsupported override",
   178  			Detail:   "The depends_on argument may not be overridden.",
   179  			Subject:  r.DependsOn[0].SourceRange().Ptr(), // the first item is the closest range we have
   180  		})
   181  	}
   182  
   183  	return diags
   184  }
   185  
   186  func (r *DataResource) merge(or *DataResource) hcl.Diagnostics {
   187  	var diags hcl.Diagnostics
   188  
   189  	if or.Count != nil {
   190  		r.Count = or.Count
   191  	}
   192  	if or.ForEach != nil {
   193  		r.ForEach = or.ForEach
   194  	}
   195  	if or.ProviderConfigRef != nil {
   196  		r.ProviderConfigRef = or.ProviderConfigRef
   197  	}
   198  
   199  	r.Config = mergeBodies(r.Config, or.Config)
   200  
   201  	// We don't allow depends_on to be overridden because that is likely to
   202  	// cause confusing misbehavior.
   203  	if len(r.DependsOn) != 0 {
   204  		diags = append(diags, &hcl.Diagnostic{
   205  			Severity: hcl.DiagError,
   206  			Summary:  "Unsupported override",
   207  			Detail:   "The depends_on argument may not be overridden.",
   208  			Subject:  r.DependsOn[0].SourceRange().Ptr(), // the first item is the closest range we have
   209  		})
   210  	}
   211  
   212  	return diags
   213  }