github.com/opentofu/opentofu@v1.7.1/internal/configs/configschema/schema.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 configschema 7 8 import ( 9 "github.com/zclconf/go-cty/cty" 10 ) 11 12 type StringKind int 13 14 const ( 15 StringPlain StringKind = iota 16 StringMarkdown 17 ) 18 19 // Block represents a configuration block. 20 // 21 // "Block" here is a logical grouping construct, though it happens to map 22 // directly onto the physical block syntax of OpenTofu's native configuration 23 // syntax. It may be a more a matter of convention in other syntaxes, such as 24 // JSON. 25 // 26 // When converted to a value, a Block always becomes an instance of an object 27 // type derived from its defined attributes and nested blocks 28 type Block struct { 29 // Attributes describes any attributes that may appear directly inside 30 // the block. 31 Attributes map[string]*Attribute 32 33 // BlockTypes describes any nested block types that may appear directly 34 // inside the block. 35 BlockTypes map[string]*NestedBlock 36 37 Description string 38 DescriptionKind StringKind 39 40 Deprecated bool 41 } 42 43 // Attribute represents a configuration attribute, within a block. 44 type Attribute struct { 45 // Type is a type specification that the attribute's value must conform to. 46 // It conflicts with NestedType. 47 Type cty.Type 48 49 // NestedType indicates that the attribute is a NestedBlock-style object. 50 // This field conflicts with Type. 51 NestedType *Object 52 53 // Description is an English-language description of the purpose and 54 // usage of the attribute. A description should be concise and use only 55 // one or two sentences, leaving full definition to longer-form 56 // documentation defined elsewhere. 57 Description string 58 DescriptionKind StringKind 59 60 // Required, if set to true, specifies that an omitted or null value is 61 // not permitted. 62 Required bool 63 64 // Optional, if set to true, specifies that an omitted or null value is 65 // permitted. This field conflicts with Required. 66 Optional bool 67 68 // Computed, if set to true, specifies that the value comes from the 69 // provider rather than from configuration. If combined with Optional, 70 // then the config may optionally provide an overridden value. 71 Computed bool 72 73 // Sensitive, if set to true, indicates that an attribute may contain 74 // sensitive information. 75 // 76 // At present nothing is done with this information, but callers are 77 // encouraged to set it where appropriate so that it may be used in the 78 // future to help OpenTofu mask sensitive information. (OpenTofu 79 // currently achieves this in a limited sense via other mechanisms.) 80 Sensitive bool 81 82 Deprecated bool 83 } 84 85 // Object represents the embedding of a structural object inside an Attribute. 86 type Object struct { 87 // Attributes describes the nested attributes which may appear inside the 88 // Object. 89 Attributes map[string]*Attribute 90 91 // Nesting provides the nesting mode for this Object, which determines how 92 // many instances of the Object are allowed, how many labels it expects, and 93 // how the resulting data will be converted into a data structure. 94 Nesting NestingMode 95 } 96 97 // NestedBlock represents the embedding of one block within another. 98 type NestedBlock struct { 99 // Block is the description of the block that's nested. 100 Block 101 102 // Nesting provides the nesting mode for the child block, which determines 103 // how many instances of the block are allowed, how many labels it expects, 104 // and how the resulting data will be converted into a data structure. 105 Nesting NestingMode 106 107 // MinItems and MaxItems set, for the NestingList and NestingSet nesting 108 // modes, lower and upper limits on the number of child blocks allowed 109 // of the given type. If both are left at zero, no limit is applied. 110 // 111 // As a special case, both values can be set to 1 for NestingSingle in 112 // order to indicate that a particular single block is required. 113 // 114 // These fields are ignored for other nesting modes and must both be left 115 // at zero. 116 MinItems, MaxItems int 117 } 118 119 // NestingMode is an enumeration of modes for nesting blocks inside other 120 // blocks. 121 type NestingMode int 122 123 // Object represents the embedding of a NestedBl 124 125 //go:generate go run golang.org/x/tools/cmd/stringer -type=NestingMode 126 127 const ( 128 nestingModeInvalid NestingMode = iota 129 130 // NestingSingle indicates that only a single instance of a given 131 // block type is permitted, with no labels, and its content should be 132 // provided directly as an object value. 133 NestingSingle 134 135 // NestingGroup is similar to NestingSingle in that it calls for only a 136 // single instance of a given block type with no labels, but it additonally 137 // guarantees that its result will never be null, even if the block is 138 // absent, and instead the nested attributes and blocks will be treated 139 // as absent in that case. (Any required attributes or blocks within the 140 // nested block are not enforced unless the block is explicitly present 141 // in the configuration, so they are all effectively optional when the 142 // block is not present.) 143 // 144 // This is useful for the situation where a remote API has a feature that 145 // is always enabled but has a group of settings related to that feature 146 // that themselves have default values. By using NestingGroup instead of 147 // NestingSingle in that case, generated plans will show the block as 148 // present even when not present in configuration, thus allowing any 149 // default values within to be displayed to the user. 150 NestingGroup 151 152 // NestingList indicates that multiple blocks of the given type are 153 // permitted, with no labels, and that their corresponding objects should 154 // be provided in a list. 155 NestingList 156 157 // NestingSet indicates that multiple blocks of the given type are 158 // permitted, with no labels, and that their corresponding objects should 159 // be provided in a set. 160 NestingSet 161 162 // NestingMap indicates that multiple blocks of the given type are 163 // permitted, each with a single label, and that their corresponding 164 // objects should be provided in a map whose keys are the labels. 165 // 166 // It's an error, therefore, to use the same label value on multiple 167 // blocks. 168 NestingMap 169 )