|
| 1 | +package internal |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "strings" |
| 7 | + |
| 8 | + "github.com/hashicorp/terraform-plugin-framework/attr" |
| 9 | + "github.com/hashicorp/terraform-plugin-framework/diag" |
| 10 | + "github.com/hashicorp/terraform-plugin-framework/path" |
| 11 | + "github.com/hashicorp/terraform-plugin-framework/schema/validator" |
| 12 | + "github.com/hashicorp/terraform-plugin-framework/tfsdk" |
| 13 | +) |
| 14 | + |
| 15 | +const oneOfWithDescriptionIfAttributeIsOneOfValidatorDescription = "Value must be one of:" |
| 16 | + |
| 17 | +// This type of validator must satisfy all types. |
| 18 | +var ( |
| 19 | + _ validator.Float64 = OneOfWithDescriptionValidator{} |
| 20 | + _ validator.Int64 = OneOfWithDescriptionValidator{} |
| 21 | + _ validator.List = OneOfWithDescriptionValidator{} |
| 22 | + _ validator.Map = OneOfWithDescriptionValidator{} |
| 23 | + _ validator.Number = OneOfWithDescriptionValidator{} |
| 24 | + _ validator.Set = OneOfWithDescriptionValidator{} |
| 25 | + _ validator.String = OneOfWithDescriptionValidator{} |
| 26 | +) |
| 27 | + |
| 28 | +type OneOfWithDescriptionIfAttributeIsOneOf struct { |
| 29 | + Value attr.Value |
| 30 | + Description string |
| 31 | +} |
| 32 | + |
| 33 | +// OneOfWithDescriptionValidator validates that the value matches one of expected values. |
| 34 | +type OneOfWithDescriptionIfAttributeIsOneOfValidator struct { |
| 35 | + PathExpression path.Expression |
| 36 | + Values []OneOfWithDescriptionIfAttributeIsOneOf |
| 37 | + ExceptedValues []attr.Value |
| 38 | +} |
| 39 | + |
| 40 | +type OneOfWithDescriptionIfAttributeIsOneOfValidatorRequest struct { |
| 41 | + Config tfsdk.Config |
| 42 | + ConfigValue attr.Value |
| 43 | + Path path.Path |
| 44 | + PathExpression path.Expression |
| 45 | + Values []OneOfWithDescriptionIfAttributeIsOneOf |
| 46 | + ExceptedValues []attr.Value |
| 47 | +} |
| 48 | + |
| 49 | +type OneOfWithDescriptionIfAttributeIsOneOfValidatorResponse struct { |
| 50 | + Diagnostics diag.Diagnostics |
| 51 | +} |
| 52 | + |
| 53 | +func (v OneOfWithDescriptionIfAttributeIsOneOfValidator) Description(_ context.Context) string { |
| 54 | + var expectedValueDescritpion string |
| 55 | + for i, expectedValue := range v.ExceptedValues { |
| 56 | + // remove the quotes around the string |
| 57 | + if i == len(v.ExceptedValues)-1 { |
| 58 | + expectedValueDescritpion += expectedValue.String() |
| 59 | + break |
| 60 | + } |
| 61 | + expectedValueDescritpion += fmt.Sprintf("%s, ", expectedValue.String()) |
| 62 | + } |
| 63 | + |
| 64 | + var valuesDescription string |
| 65 | + for i, value := range v.Values { |
| 66 | + if i == len(v.Values)-1 { |
| 67 | + valuesDescription += fmt.Sprintf("%s (%s)", value.Value.String(), value.Description) |
| 68 | + break |
| 69 | + } |
| 70 | + valuesDescription += fmt.Sprintf("%s (%s), ", value.Value.String(), value.Description) |
| 71 | + } |
| 72 | + |
| 73 | + switch len(v.ExceptedValues) { |
| 74 | + case 1: |
| 75 | + return fmt.Sprintf("If the value of attribute %s is %s the allowed values are : %s", v.PathExpression.String(), expectedValueDescritpion, valuesDescription) |
| 76 | + default: |
| 77 | + return fmt.Sprintf("If the value of attribute %s is one of %s the allowed are : %s", v.PathExpression.String(), expectedValueDescritpion, valuesDescription) |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +func (v OneOfWithDescriptionIfAttributeIsOneOfValidator) MarkdownDescription(_ context.Context) string { |
| 82 | + var expectedValueDescritpion string |
| 83 | + for i, expectedValue := range v.ExceptedValues { |
| 84 | + // remove the quotes around the string |
| 85 | + x := strings.Trim(expectedValue.String(), "\"") |
| 86 | + |
| 87 | + switch i { |
| 88 | + case len(v.ExceptedValues) - 1: |
| 89 | + expectedValueDescritpion += fmt.Sprintf("`%s`", x) |
| 90 | + case len(v.ExceptedValues) - 2: |
| 91 | + expectedValueDescritpion += fmt.Sprintf("`%s` or ", x) |
| 92 | + default: |
| 93 | + expectedValueDescritpion += fmt.Sprintf("`%s`, ", x) |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + valuesDescription := "" |
| 98 | + for _, value := range v.Values { |
| 99 | + valuesDescription += fmt.Sprintf("- `%s` - %s<br>", value.Value.String(), value.Description) |
| 100 | + } |
| 101 | + |
| 102 | + switch len(v.ExceptedValues) { |
| 103 | + case 1: |
| 104 | + return fmt.Sprintf("\n\n-> **If the value of the attribute [`%s`](#%s) is %s the value is one of** %s", v.PathExpression, v.PathExpression, expectedValueDescritpion, valuesDescription) |
| 105 | + default: |
| 106 | + return fmt.Sprintf("\n\n-> **If the value of the attribute [`%s`](#%s) is one of %s** : %s", v.PathExpression, v.PathExpression, expectedValueDescritpion, valuesDescription) |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +func (v OneOfWithDescriptionIfAttributeIsOneOfValidator) Validate(ctx context.Context, req OneOfWithDescriptionIfAttributeIsOneOfValidatorRequest, res *OneOfWithDescriptionIfAttributeIsOneOfValidatorResponse) { |
| 111 | + // Here attribute configuration is null or unknown, so we need to check if attribute in the path |
| 112 | + // is equal to one of the excepted values |
| 113 | + paths, diags := req.Config.PathMatches(ctx, req.PathExpression.Merge(v.PathExpression)) |
| 114 | + if diags.HasError() { |
| 115 | + res.Diagnostics.Append(diags...) |
| 116 | + return |
| 117 | + } |
| 118 | + |
| 119 | + if len(paths) == 0 { |
| 120 | + res.Diagnostics.AddError( |
| 121 | + fmt.Sprintf("Invalid configuration for attribute %s", req.Path), |
| 122 | + "Path must be set", |
| 123 | + ) |
| 124 | + return |
| 125 | + } |
| 126 | + |
| 127 | + res.Diagnostics.AddWarning("Paths", fmt.Sprintf("%v", paths)) |
| 128 | + |
| 129 | + path := paths[0] |
| 130 | + |
| 131 | + // mpVal is the value of the attribute in the path |
| 132 | + var mpVal attr.Value |
| 133 | + res.Diagnostics.Append(req.Config.GetAttribute(ctx, path, &mpVal)...) |
| 134 | + if res.Diagnostics.HasError() { |
| 135 | + res.Diagnostics.AddError( |
| 136 | + fmt.Sprintf("Invalid configuration for attribute %s", req.Path), |
| 137 | + fmt.Sprintf("Unable to retrieve attribute path: %q", path), |
| 138 | + ) |
| 139 | + return |
| 140 | + } |
| 141 | + |
| 142 | + // If the target attribute configuration is unknown or null, there is nothing else to validate |
| 143 | + if mpVal.IsNull() || mpVal.IsUnknown() { |
| 144 | + return |
| 145 | + } |
| 146 | + |
| 147 | + for _, expectedValue := range v.ExceptedValues { |
| 148 | + // If the value of the target attribute is equal to one of the expected values, we need to validate the value of the current attribute |
| 149 | + if mpVal.Equal(expectedValue) || mpVal.String() == expectedValue.String() { |
| 150 | + if req.ConfigValue.IsNull() || req.ConfigValue.IsUnknown() { |
| 151 | + res.Diagnostics.AddAttributeError( |
| 152 | + path, |
| 153 | + fmt.Sprintf("Invalid configuration for attribute %s", req.Path), |
| 154 | + fmt.Sprintf("Value is empty. %s", v.Description(ctx)), |
| 155 | + ) |
| 156 | + return |
| 157 | + } |
| 158 | + |
| 159 | + for _, value := range v.Values { |
| 160 | + if req.ConfigValue.Equal(value.Value) { |
| 161 | + // Ok the value is valid |
| 162 | + return |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + // The value is not valid |
| 167 | + res.Diagnostics.AddAttributeError( |
| 168 | + path, |
| 169 | + fmt.Sprintf("Invalid configuration for attribute %s", req.Path), |
| 170 | + fmt.Sprintf("Invalid value %s. %s", req.ConfigValue.String(), v.Description(ctx)), |
| 171 | + ) |
| 172 | + return |
| 173 | + } |
| 174 | + } |
| 175 | +} |
| 176 | + |
| 177 | +func (v OneOfWithDescriptionIfAttributeIsOneOfValidator) ValidateString(ctx context.Context, req validator.StringRequest, resp *validator.StringResponse) { |
| 178 | + validateReq := OneOfWithDescriptionIfAttributeIsOneOfValidatorRequest{ |
| 179 | + Config: req.Config, |
| 180 | + ConfigValue: req.ConfigValue, |
| 181 | + Path: req.Path, |
| 182 | + PathExpression: req.PathExpression, |
| 183 | + } |
| 184 | + validateResp := &OneOfWithDescriptionIfAttributeIsOneOfValidatorResponse{} |
| 185 | + |
| 186 | + v.Validate(ctx, validateReq, validateResp) |
| 187 | + |
| 188 | + resp.Diagnostics.Append(validateResp.Diagnostics...) |
| 189 | +} |
| 190 | + |
| 191 | +// Float64 validates that the value matches one of expected values. |
| 192 | +func (v OneOfWithDescriptionIfAttributeIsOneOfValidator) ValidateFloat64(ctx context.Context, req validator.Float64Request, resp *validator.Float64Response) { |
| 193 | + validateReq := OneOfWithDescriptionIfAttributeIsOneOfValidatorRequest{ |
| 194 | + Config: req.Config, |
| 195 | + ConfigValue: req.ConfigValue, |
| 196 | + Path: req.Path, |
| 197 | + } |
| 198 | + validateResp := &OneOfWithDescriptionIfAttributeIsOneOfValidatorResponse{} |
| 199 | + |
| 200 | + v.Validate(ctx, validateReq, validateResp) |
| 201 | + |
| 202 | + resp.Diagnostics.Append(validateResp.Diagnostics...) |
| 203 | +} |
| 204 | + |
| 205 | +// Int64 validates that the value matches one of expected values. |
| 206 | +func (v OneOfWithDescriptionIfAttributeIsOneOfValidator) ValidateInt64(ctx context.Context, req validator.Int64Request, resp *validator.Int64Response) { |
| 207 | + validateReq := OneOfWithDescriptionIfAttributeIsOneOfValidatorRequest{ |
| 208 | + Config: req.Config, |
| 209 | + ConfigValue: req.ConfigValue, |
| 210 | + Path: req.Path, |
| 211 | + } |
| 212 | + validateResp := &OneOfWithDescriptionIfAttributeIsOneOfValidatorResponse{} |
| 213 | + |
| 214 | + v.Validate(ctx, validateReq, validateResp) |
| 215 | + |
| 216 | + resp.Diagnostics.Append(validateResp.Diagnostics...) |
| 217 | +} |
| 218 | + |
| 219 | +// Number validates that the value matches one of expected values. |
| 220 | +func (v OneOfWithDescriptionIfAttributeIsOneOfValidator) ValidateNumber(ctx context.Context, req validator.NumberRequest, resp *validator.NumberResponse) { |
| 221 | + validateReq := OneOfWithDescriptionIfAttributeIsOneOfValidatorRequest{ |
| 222 | + Config: req.Config, |
| 223 | + ConfigValue: req.ConfigValue, |
| 224 | + Path: req.Path, |
| 225 | + } |
| 226 | + validateResp := &OneOfWithDescriptionIfAttributeIsOneOfValidatorResponse{} |
| 227 | + |
| 228 | + v.Validate(ctx, validateReq, validateResp) |
| 229 | + |
| 230 | + resp.Diagnostics.Append(validateResp.Diagnostics...) |
| 231 | +} |
| 232 | + |
| 233 | +// List validates that the value matches one of expected values. |
| 234 | +func (v OneOfWithDescriptionIfAttributeIsOneOfValidator) ValidateList(ctx context.Context, req validator.ListRequest, resp *validator.ListResponse) { |
| 235 | + validateReq := OneOfWithDescriptionIfAttributeIsOneOfValidatorRequest{ |
| 236 | + Config: req.Config, |
| 237 | + ConfigValue: req.ConfigValue, |
| 238 | + Path: req.Path, |
| 239 | + } |
| 240 | + validateResp := &OneOfWithDescriptionIfAttributeIsOneOfValidatorResponse{} |
| 241 | + |
| 242 | + v.Validate(ctx, validateReq, validateResp) |
| 243 | + |
| 244 | + resp.Diagnostics.Append(validateResp.Diagnostics...) |
| 245 | +} |
| 246 | + |
| 247 | +// Set validates that the value matches one of expected values. |
| 248 | +func (v OneOfWithDescriptionIfAttributeIsOneOfValidator) ValidateSet(ctx context.Context, req validator.SetRequest, resp *validator.SetResponse) { |
| 249 | + validateReq := OneOfWithDescriptionIfAttributeIsOneOfValidatorRequest{ |
| 250 | + Config: req.Config, |
| 251 | + ConfigValue: req.ConfigValue, |
| 252 | + Path: req.Path, |
| 253 | + } |
| 254 | + validateResp := &OneOfWithDescriptionIfAttributeIsOneOfValidatorResponse{} |
| 255 | + |
| 256 | + v.Validate(ctx, validateReq, validateResp) |
| 257 | + |
| 258 | + resp.Diagnostics.Append(validateResp.Diagnostics...) |
| 259 | +} |
| 260 | + |
| 261 | +// Map validates that the value matches one of expected values. |
| 262 | +func (v OneOfWithDescriptionIfAttributeIsOneOfValidator) ValidateMap(ctx context.Context, req validator.MapRequest, resp *validator.MapResponse) { |
| 263 | + validateReq := OneOfWithDescriptionIfAttributeIsOneOfValidatorRequest{ |
| 264 | + Config: req.Config, |
| 265 | + ConfigValue: req.ConfigValue, |
| 266 | + Path: req.Path, |
| 267 | + } |
| 268 | + validateResp := &OneOfWithDescriptionIfAttributeIsOneOfValidatorResponse{} |
| 269 | + |
| 270 | + v.Validate(ctx, validateReq, validateResp) |
| 271 | + |
| 272 | + resp.Diagnostics.Append(validateResp.Diagnostics...) |
| 273 | +} |
0 commit comments