github.com/opentofu/opentofu@v1.7.1/internal/command/jsonformat/computed/renderers/sensitive_block.go (about)

     1  // Copyright (c) The OpenTofu Authors
     2  // SPDX-License-Identifier: MPL-2.0
     3  // Copyright (c) 2023 HashiCorp, Inc.
     4  // SPDX-License-Identifier: MPL-2.0
     5  
     6  package renderers
     7  
     8  import (
     9  	"fmt"
    10  
    11  	"github.com/opentofu/opentofu/internal/command/jsonformat/computed"
    12  	"github.com/opentofu/opentofu/internal/plans"
    13  )
    14  
    15  func SensitiveBlock(diff computed.Diff, beforeSensitive, afterSensitive bool) computed.DiffRenderer {
    16  	return &sensitiveBlockRenderer{
    17  		inner:           diff,
    18  		beforeSensitive: beforeSensitive,
    19  		afterSensitive:  afterSensitive,
    20  	}
    21  }
    22  
    23  type sensitiveBlockRenderer struct {
    24  	inner computed.Diff
    25  
    26  	afterSensitive  bool
    27  	beforeSensitive bool
    28  }
    29  
    30  func (renderer sensitiveBlockRenderer) RenderHuman(diff computed.Diff, indent int, opts computed.RenderHumanOpts) string {
    31  	cachedLinePrefix := fmt.Sprintf("%s%s", formatIndent(indent), writeDiffActionSymbol(plans.NoOp, opts))
    32  	return fmt.Sprintf("{%s\n%s  # At least one attribute in this block is (or was) sensitive,\n%s  # so its contents will not be displayed.\n%s}",
    33  		forcesReplacement(diff.Replace, opts), cachedLinePrefix, cachedLinePrefix, cachedLinePrefix)
    34  }
    35  
    36  func (renderer sensitiveBlockRenderer) WarningsHuman(diff computed.Diff, indent int, opts computed.RenderHumanOpts) []string {
    37  	if (renderer.beforeSensitive == renderer.afterSensitive) || renderer.inner.Action == plans.Create || renderer.inner.Action == plans.Delete {
    38  		// Only display warnings for sensitive values if they are changing from
    39  		// being sensitive or to being sensitive and if they are not being
    40  		// destroyed or created.
    41  		return []string{}
    42  	}
    43  
    44  	if renderer.beforeSensitive {
    45  		return []string{opts.Colorize.Color(fmt.Sprintf("  # [yellow]Warning[reset]: this block will no longer be marked as sensitive\n%s  # after applying this change.", formatIndent(indent)))}
    46  	} else {
    47  		return []string{opts.Colorize.Color(fmt.Sprintf("  # [yellow]Warning[reset]: this block will be marked as sensitive and will not\n%s  # display in UI output after applying this change.", formatIndent(indent)))}
    48  	}
    49  }