github.com/kevinklinger/open_terraform@v1.3.6/noninternal/checks/status.go (about)

     1  package checks
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/zclconf/go-cty/cty"
     7  )
     8  
     9  // Status represents the status of an individual check associated with a
    10  // checkable object.
    11  type Status rune
    12  
    13  //go:generate go run golang.org/x/tools/cmd/stringer -type=Status
    14  
    15  const (
    16  	// StatusUnknown represents that there is not yet a conclusive result
    17  	// for the check, either because we haven't yet visited its associated
    18  	// object or because the check condition itself depends on a value not
    19  	// yet known during planning.
    20  	StatusUnknown Status = 0
    21  	// NOTE: Our implementation relies on StatusUnknown being the zero value
    22  	// of Status.
    23  
    24  	// StatusPass represents that Terraform Core has evaluated the check's
    25  	// condition and it returned true, indicating success.
    26  	StatusPass Status = 'P'
    27  
    28  	// StatusFail represents that Terraform Core has evaluated the check's
    29  	// condition and it returned false, indicating failure.
    30  	StatusFail Status = 'F'
    31  
    32  	// StatusError represents that Terraform Core tried to evaluate the check's
    33  	// condition but encountered an error while evaluating the check expression.
    34  	//
    35  	// This is different than StatusFail because StatusFail indiciates that
    36  	// the condition was valid and returned false, whereas StatusError
    37  	// indicates that the condition was not valid at all.
    38  	StatusError Status = 'E'
    39  )
    40  
    41  // StatusForCtyValue returns the Status value corresponding to the given
    42  // cty Value, which must be one of either cty.True, cty.False, or
    43  // cty.UnknownVal(cty.Bool) or else this function will panic.
    44  //
    45  // The current behavior of this function is:
    46  //
    47  //	cty.True                  StatusPass
    48  //	cty.False                 StatusFail
    49  //	cty.UnknownVal(cty.Bool)  StatusUnknown
    50  //
    51  // Any other input will panic. Note that there's no value that can produce
    52  // StatusError, because in case of a condition error there will not typically
    53  // be a result value at all.
    54  func StatusForCtyValue(v cty.Value) Status {
    55  	if !v.Type().Equals(cty.Bool) {
    56  		panic(fmt.Sprintf("cannot use %s as check status", v.Type().FriendlyName()))
    57  	}
    58  	if v.IsNull() {
    59  		panic("cannot use null as check status")
    60  	}
    61  
    62  	switch {
    63  	case v == cty.True:
    64  		return StatusPass
    65  	case v == cty.False:
    66  		return StatusFail
    67  	case !v.IsKnown():
    68  		return StatusUnknown
    69  	default:
    70  		// Should be impossible to get here unless something particularly
    71  		// weird is going on, like a marked condition result.
    72  		panic(fmt.Sprintf("cannot use %#v as check status", v))
    73  	}
    74  }