github.com/terramate-io/tf@v0.0.0-20230830114523-fce866b4dfcd/addrs/check.go (about)

     1  // Copyright (c) HashiCorp, Inc.
     2  // SPDX-License-Identifier: MPL-2.0
     3  
     4  package addrs
     5  
     6  import "fmt"
     7  
     8  // Check is the address of a check block within a module.
     9  //
    10  // For now, checks do not support meta arguments such as "count" or "for_each"
    11  // so this address uniquely describes a single check within a module.
    12  type Check struct {
    13  	referenceable
    14  	Name string
    15  }
    16  
    17  func (c Check) String() string {
    18  	return fmt.Sprintf("check.%s", c.Name)
    19  }
    20  
    21  // InModule returns a ConfigCheck from the receiver and the given module
    22  // address.
    23  func (c Check) InModule(modAddr Module) ConfigCheck {
    24  	return ConfigCheck{
    25  		Module: modAddr,
    26  		Check:  c,
    27  	}
    28  }
    29  
    30  // Absolute returns an AbsCheck from the receiver and the given module instance
    31  // address.
    32  func (c Check) Absolute(modAddr ModuleInstance) AbsCheck {
    33  	return AbsCheck{
    34  		Module: modAddr,
    35  		Check:  c,
    36  	}
    37  }
    38  
    39  func (c Check) Equal(o Check) bool {
    40  	return c.Name == o.Name
    41  }
    42  
    43  func (c Check) UniqueKey() UniqueKey {
    44  	return c // A Check is its own UniqueKey
    45  }
    46  
    47  func (c Check) uniqueKeySigil() {}
    48  
    49  // ConfigCheck is an address for a check block within a configuration.
    50  //
    51  // This contains a Check address and a Module address, meaning this describes
    52  // a check block within the entire configuration.
    53  type ConfigCheck struct {
    54  	Module Module
    55  	Check  Check
    56  }
    57  
    58  var _ ConfigCheckable = ConfigCheck{}
    59  
    60  func (c ConfigCheck) UniqueKey() UniqueKey {
    61  	return configCheckUniqueKey(c.String())
    62  }
    63  
    64  func (c ConfigCheck) configCheckableSigil() {}
    65  
    66  func (c ConfigCheck) CheckableKind() CheckableKind {
    67  	return CheckableCheck
    68  }
    69  
    70  func (c ConfigCheck) String() string {
    71  	if len(c.Module) == 0 {
    72  		return c.Check.String()
    73  	}
    74  	return fmt.Sprintf("%s.%s", c.Module, c.Check)
    75  }
    76  
    77  // AbsCheck is an absolute address for a check block under a given module path.
    78  //
    79  // This contains an actual ModuleInstance address (compared to the Module within
    80  // a ConfigCheck), meaning this uniquely describes a check block within the
    81  // entire configuration after any "count" or "foreach" meta arguments have been
    82  // evaluated on the containing module.
    83  type AbsCheck struct {
    84  	Module ModuleInstance
    85  	Check  Check
    86  }
    87  
    88  var _ Checkable = AbsCheck{}
    89  
    90  func (c AbsCheck) UniqueKey() UniqueKey {
    91  	return absCheckUniqueKey(c.String())
    92  }
    93  
    94  func (c AbsCheck) checkableSigil() {}
    95  
    96  // CheckRule returns an address for a given rule type within the check block.
    97  //
    98  // There will be at most one CheckDataResource rule within a check block (with
    99  // an index of 0). There will be at least one, but potentially many,
   100  // CheckAssertion rules within a check block.
   101  func (c AbsCheck) CheckRule(typ CheckRuleType, i int) CheckRule {
   102  	return CheckRule{
   103  		Container: c,
   104  		Type:      typ,
   105  		Index:     i,
   106  	}
   107  }
   108  
   109  // ConfigCheckable returns the ConfigCheck address for this absolute reference.
   110  func (c AbsCheck) ConfigCheckable() ConfigCheckable {
   111  	return ConfigCheck{
   112  		Module: c.Module.Module(),
   113  		Check:  c.Check,
   114  	}
   115  }
   116  
   117  func (c AbsCheck) CheckableKind() CheckableKind {
   118  	return CheckableCheck
   119  }
   120  
   121  func (c AbsCheck) String() string {
   122  	if len(c.Module) == 0 {
   123  		return c.Check.String()
   124  	}
   125  	return fmt.Sprintf("%s.%s", c.Module, c.Check)
   126  }
   127  
   128  type configCheckUniqueKey string
   129  
   130  func (k configCheckUniqueKey) uniqueKeySigil() {}
   131  
   132  type absCheckUniqueKey string
   133  
   134  func (k absCheckUniqueKey) uniqueKeySigil() {}