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

     1  // Copyright (c) HashiCorp, Inc.
     2  // SPDX-License-Identifier: MPL-2.0
     3  
     4  package addrs
     5  
     6  import (
     7  	"fmt"
     8  )
     9  
    10  // CheckRule is the address of a check rule within a checkable object.
    11  //
    12  // This represents the check rule globally within a configuration, and is used
    13  // during graph evaluation to identify a condition result object to update with
    14  // the result of check rule evaluation.
    15  //
    16  // The check address is not distinct from resource traversals, and check rule
    17  // values are not intended to be available to the language, so the address is
    18  // not Referenceable.
    19  //
    20  // Note also that the check address is only relevant within the scope of a run,
    21  // as reordering check blocks between runs will result in their addresses
    22  // changing. CheckRule is therefore for internal use only and should not be
    23  // exposed in durable artifacts such as state snapshots.
    24  type CheckRule struct {
    25  	Container Checkable
    26  	Type      CheckRuleType
    27  	Index     int
    28  }
    29  
    30  func NewCheckRule(container Checkable, typ CheckRuleType, index int) CheckRule {
    31  	return CheckRule{
    32  		Container: container,
    33  		Type:      typ,
    34  		Index:     index,
    35  	}
    36  }
    37  
    38  func (c CheckRule) String() string {
    39  	container := c.Container.String()
    40  	switch c.Type {
    41  	case ResourcePrecondition:
    42  		return fmt.Sprintf("%s.precondition[%d]", container, c.Index)
    43  	case ResourcePostcondition:
    44  		return fmt.Sprintf("%s.postcondition[%d]", container, c.Index)
    45  	case OutputPrecondition:
    46  		return fmt.Sprintf("%s.precondition[%d]", container, c.Index)
    47  	case CheckDataResource:
    48  		return fmt.Sprintf("%s.data[%d]", container, c.Index)
    49  	case CheckAssertion:
    50  		return fmt.Sprintf("%s.assert[%d]", container, c.Index)
    51  	case InputValidation:
    52  		return fmt.Sprintf("%s.validation[%d]", container, c.Index)
    53  	default:
    54  		// This should not happen
    55  		return fmt.Sprintf("%s.condition[%d]", container, c.Index)
    56  	}
    57  }
    58  
    59  func (c CheckRule) UniqueKey() UniqueKey {
    60  	return checkRuleKey{
    61  		ContainerKey: c.Container.UniqueKey(),
    62  		Type:         c.Type,
    63  		Index:        c.Index,
    64  	}
    65  }
    66  
    67  type checkRuleKey struct {
    68  	ContainerKey UniqueKey
    69  	Type         CheckRuleType
    70  	Index        int
    71  }
    72  
    73  func (k checkRuleKey) uniqueKeySigil() {}
    74  
    75  // CheckRuleType describes a category of check. We use this only to establish
    76  // uniqueness for Check values, and do not expose this concept of "check types"
    77  // (which is subject to change in future) in any durable artifacts such as
    78  // state snapshots.
    79  //
    80  // (See [CheckableKind] for an enumeration that we _do_ use externally, to
    81  // describe the type of object being checked rather than the type of the check
    82  // itself.)
    83  type CheckRuleType int
    84  
    85  //go:generate go run golang.org/x/tools/cmd/stringer -type=CheckRuleType check_rule.go
    86  
    87  const (
    88  	InvalidCondition      CheckRuleType = 0
    89  	ResourcePrecondition  CheckRuleType = 1
    90  	ResourcePostcondition CheckRuleType = 2
    91  	OutputPrecondition    CheckRuleType = 3
    92  	CheckDataResource     CheckRuleType = 4
    93  	CheckAssertion        CheckRuleType = 5
    94  	InputValidation       CheckRuleType = 6
    95  )
    96  
    97  // Description returns a human-readable description of the check type. This is
    98  // presented in the user interface through a diagnostic summary.
    99  func (c CheckRuleType) Description() string {
   100  	switch c {
   101  	case ResourcePrecondition:
   102  		return "Resource precondition"
   103  	case ResourcePostcondition:
   104  		return "Resource postcondition"
   105  	case OutputPrecondition:
   106  		return "Module output value precondition"
   107  	case CheckDataResource:
   108  		return "Check block data resource"
   109  	case CheckAssertion:
   110  		return "Check block assertion"
   111  	case InputValidation:
   112  		return "Input variable validation"
   113  	default:
   114  		// This should not happen
   115  		return "Condition"
   116  	}
   117  }