|
| 1 | +// Copyright (c) HashiCorp, Inc. |
| 2 | +// SPDX-License-Identifier: MPL-2.0 |
| 3 | + |
| 4 | +package schema |
| 5 | + |
| 6 | +import ( |
| 7 | + "github.com/hashicorp/terraform-plugin-go/tftypes" |
| 8 | + |
| 9 | + "github.com/hashicorp/terraform-plugin-framework/attr" |
| 10 | + "github.com/hashicorp/terraform-plugin-framework/internal/fwschema" |
| 11 | + "github.com/hashicorp/terraform-plugin-framework/types" |
| 12 | + "github.com/hashicorp/terraform-plugin-framework/types/basetypes" |
| 13 | +) |
| 14 | + |
| 15 | +// Ensure the implementation satisifies the desired interfaces. |
| 16 | +var ( |
| 17 | + _ Attribute = DynamicAttribute{} |
| 18 | +) |
| 19 | + |
| 20 | +// DynamicAttribute represents a schema attribute that is a dynamic, rather |
| 21 | +// than a single static type. Static types are always preferable over dynamic |
| 22 | +// types in Terraform as practitioners will receive less helpful configuration |
| 23 | +// assistance from validation error diagnostics and editor integrations. When |
| 24 | +// retrieving the value for this attribute, use types.Dynamic as the value type |
| 25 | +// unless the CustomType field is set. |
| 26 | +// |
| 27 | +// The concrete value type for a dynamic is determined at runtime in this order: |
| 28 | +// 1. By Terraform, if defined in the configuration (if Required or Optional). |
| 29 | +// 2. By the provider (if Computed). |
| 30 | +// |
| 31 | +// Once the concrete value type has been determined, it must remain consistent between |
| 32 | +// plan and apply or Terraform will return an error. |
| 33 | +type DynamicAttribute struct { |
| 34 | + // CustomType enables the use of a custom attribute type in place of the |
| 35 | + // default basetypes.DynamicType. When retrieving data, the basetypes.DynamicValuable |
| 36 | + // associated with this custom type must be used in place of types.Dynamic. |
| 37 | + CustomType basetypes.DynamicTypable |
| 38 | + |
| 39 | + // Required indicates whether the practitioner must enter a value for |
| 40 | + // this attribute or not. Required and Optional cannot both be true. |
| 41 | + Required bool |
| 42 | + |
| 43 | + // Optional indicates whether the practitioner can choose to enter a value |
| 44 | + // for this attribute or not. Optional and Required cannot both be true. |
| 45 | + Optional bool |
| 46 | + |
| 47 | + // Description is used in various tooling, like the language server, to |
| 48 | + // give practitioners more information about what this attribute is, |
| 49 | + // what it's for, and how it should be used. It should be written as |
| 50 | + // plain text, with no special formatting. |
| 51 | + Description string |
| 52 | + |
| 53 | + // MarkdownDescription is used in various tooling, like the |
| 54 | + // documentation generator, to give practitioners more information |
| 55 | + // about what this attribute is, what it's for, and how it should be |
| 56 | + // used. It should be formatted using Markdown. |
| 57 | + MarkdownDescription string |
| 58 | + |
| 59 | + // DeprecationMessage defines warning diagnostic details to display when |
| 60 | + // practitioner configurations use this Attribute. The warning diagnostic |
| 61 | + // summary is automatically set to "Attribute Deprecated" along with |
| 62 | + // configuration source file and line information. |
| 63 | + // |
| 64 | + // Set this field to a practitioner actionable message such as: |
| 65 | + // |
| 66 | + // - "Configure other_attribute instead. This attribute will be removed |
| 67 | + // in the next major version of the provider." |
| 68 | + // - "Remove this attribute's configuration as it no longer is used and |
| 69 | + // the attribute will be removed in the next major version of the |
| 70 | + // provider." |
| 71 | + // |
| 72 | + // In Terraform 1.2.7 and later, this warning diagnostic is displayed any |
| 73 | + // time a practitioner attempts to configure a value for this attribute and |
| 74 | + // certain scenarios where this attribute is referenced. |
| 75 | + // |
| 76 | + // In Terraform 1.2.6 and earlier, this warning diagnostic is only |
| 77 | + // displayed when the Attribute is Required or Optional, and if the |
| 78 | + // practitioner configuration sets the value to a known or unknown value |
| 79 | + // (which may eventually be null). |
| 80 | + // |
| 81 | + // Across any Terraform version, there are no warnings raised for |
| 82 | + // practitioner configuration values set directly to null, as there is no |
| 83 | + // way for the framework to differentiate between an unset and null |
| 84 | + // configuration due to how Terraform sends configuration information |
| 85 | + // across the protocol. |
| 86 | + // |
| 87 | + // Additional information about deprecation enhancements for read-only |
| 88 | + // attributes can be found in: |
| 89 | + // |
| 90 | + // - https://github.com/hashicorp/terraform/issues/7569 |
| 91 | + // |
| 92 | + DeprecationMessage string |
| 93 | +} |
| 94 | + |
| 95 | +// ApplyTerraform5AttributePathStep always returns an error as it is not |
| 96 | +// possible to step further into a DynamicAttribute. |
| 97 | +func (a DynamicAttribute) ApplyTerraform5AttributePathStep(step tftypes.AttributePathStep) (interface{}, error) { |
| 98 | + return a.GetType().ApplyTerraform5AttributePathStep(step) |
| 99 | +} |
| 100 | + |
| 101 | +// Equal returns true if the given Attribute is a DynamicAttribute |
| 102 | +// and all fields are equal. |
| 103 | +func (a DynamicAttribute) Equal(o fwschema.Attribute) bool { |
| 104 | + if _, ok := o.(DynamicAttribute); !ok { |
| 105 | + return false |
| 106 | + } |
| 107 | + |
| 108 | + return fwschema.AttributesEqual(a, o) |
| 109 | +} |
| 110 | + |
| 111 | +// GetDeprecationMessage returns the DeprecationMessage field value. |
| 112 | +func (a DynamicAttribute) GetDeprecationMessage() string { |
| 113 | + return a.DeprecationMessage |
| 114 | +} |
| 115 | + |
| 116 | +// GetDescription returns the Description field value. |
| 117 | +func (a DynamicAttribute) GetDescription() string { |
| 118 | + return a.Description |
| 119 | +} |
| 120 | + |
| 121 | +// GetMarkdownDescription returns the MarkdownDescription field value. |
| 122 | +func (a DynamicAttribute) GetMarkdownDescription() string { |
| 123 | + return a.MarkdownDescription |
| 124 | +} |
| 125 | + |
| 126 | +// GetType returns types.DynamicType or the CustomType field value if defined. |
| 127 | +func (a DynamicAttribute) GetType() attr.Type { |
| 128 | + if a.CustomType != nil { |
| 129 | + return a.CustomType |
| 130 | + } |
| 131 | + |
| 132 | + return types.DynamicType |
| 133 | +} |
| 134 | + |
| 135 | +// IsComputed always returns false as action schema attributes cannot be Computed. |
| 136 | +func (a DynamicAttribute) IsComputed() bool { |
| 137 | + return false |
| 138 | +} |
| 139 | + |
| 140 | +// IsOptional returns the Optional field value. |
| 141 | +func (a DynamicAttribute) IsOptional() bool { |
| 142 | + return a.Optional |
| 143 | +} |
| 144 | + |
| 145 | +// IsRequired returns the Required field value. |
| 146 | +func (a DynamicAttribute) IsRequired() bool { |
| 147 | + return a.Required |
| 148 | +} |
| 149 | + |
| 150 | +// IsSensitive always returns false as action schema attributes cannot be Sensitive. |
| 151 | +func (a DynamicAttribute) IsSensitive() bool { |
| 152 | + return false |
| 153 | +} |
| 154 | + |
| 155 | +// IsWriteOnly always returns false as action schema attributes cannot be WriteOnly. |
| 156 | +func (a DynamicAttribute) IsWriteOnly() bool { |
| 157 | + return false |
| 158 | +} |
| 159 | + |
| 160 | +// IsRequiredForImport returns false as this behavior is only relevant |
| 161 | +// for managed resource identity schema attributes. |
| 162 | +func (a DynamicAttribute) IsRequiredForImport() bool { |
| 163 | + return false |
| 164 | +} |
| 165 | + |
| 166 | +// IsOptionalForImport returns false as this behavior is only relevant |
| 167 | +// for managed resource identity schema attributes. |
| 168 | +func (a DynamicAttribute) IsOptionalForImport() bool { |
| 169 | + return false |
| 170 | +} |
0 commit comments