|
6 | 6 | "testing" |
7 | 7 |
|
8 | 8 | "github.com/hashicorp/terraform-plugin-framework/resource/schema" |
| 9 | + "github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier" |
9 | 10 | "github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier" |
10 | 11 | "github.com/hashicorp/terraform-plugin-framework/schema/validator" |
11 | 12 | "github.com/hashicorp/terraform-plugin-framework/types" |
@@ -175,3 +176,219 @@ func TestCustomizeSchema_SetComputed_PanicOnBlock(t *testing.T) { |
175 | 176 | }) |
176 | 177 | }) |
177 | 178 | } |
| 179 | + |
| 180 | +type mockPlanModifier struct{} |
| 181 | + |
| 182 | +// Description implements planmodifier.List. |
| 183 | +func (m mockPlanModifier) Description(context.Context) string { |
| 184 | + panic("unimplemented") |
| 185 | +} |
| 186 | + |
| 187 | +// MarkdownDescription implements planmodifier.List. |
| 188 | +func (m mockPlanModifier) MarkdownDescription(context.Context) string { |
| 189 | + panic("unimplemented") |
| 190 | +} |
| 191 | + |
| 192 | +// PlanModifyList implements planmodifier.List. |
| 193 | +func (m mockPlanModifier) PlanModifyList(context.Context, planmodifier.ListRequest, *planmodifier.ListResponse) { |
| 194 | + panic("unimplemented") |
| 195 | +} |
| 196 | + |
| 197 | +// PlanModifyList implements planmodifier.List. |
| 198 | +func (m mockPlanModifier) PlanModifyObject(context.Context, planmodifier.ObjectRequest, *planmodifier.ObjectResponse) { |
| 199 | + panic("unimplemented") |
| 200 | +} |
| 201 | + |
| 202 | +var _ planmodifier.List = mockPlanModifier{} |
| 203 | +var _ planmodifier.Object = mockPlanModifier{} |
| 204 | + |
| 205 | +type mockValidator struct{} |
| 206 | + |
| 207 | +// Description implements validator.List. |
| 208 | +func (m mockValidator) Description(context.Context) string { |
| 209 | + panic("unimplemented") |
| 210 | +} |
| 211 | + |
| 212 | +// MarkdownDescription implements validator.List. |
| 213 | +func (m mockValidator) MarkdownDescription(context.Context) string { |
| 214 | + panic("unimplemented") |
| 215 | +} |
| 216 | + |
| 217 | +// ValidateList implements validator.List. |
| 218 | +func (m mockValidator) ValidateList(context.Context, validator.ListRequest, *validator.ListResponse) { |
| 219 | + panic("unimplemented") |
| 220 | +} |
| 221 | + |
| 222 | +// ValidateList implements validator.Object. |
| 223 | +func (m mockValidator) ValidateObject(context.Context, validator.ObjectRequest, *validator.ObjectResponse) { |
| 224 | + panic("unimplemented") |
| 225 | +} |
| 226 | + |
| 227 | +var _ validator.List = mockValidator{} |
| 228 | +var _ validator.Object = mockValidator{} |
| 229 | + |
| 230 | +func TestCustomizeSchema_ConvertToAttribute(t *testing.T) { |
| 231 | + v := mockValidator{} |
| 232 | + pm := mockPlanModifier{} |
| 233 | + testCases := []struct { |
| 234 | + name string |
| 235 | + baseSchema NestedBlockObject |
| 236 | + path []string |
| 237 | + want NestedBlockObject |
| 238 | + expectPanic bool |
| 239 | + }{ |
| 240 | + { |
| 241 | + name: "ListNestedBlock", |
| 242 | + baseSchema: NestedBlockObject{ |
| 243 | + Blocks: map[string]BlockBuilder{ |
| 244 | + "list": ListNestedBlockBuilder{ |
| 245 | + NestedObject: NestedBlockObject{ |
| 246 | + Attributes: map[string]AttributeBuilder{ |
| 247 | + "attr": StringAttributeBuilder{}, |
| 248 | + }, |
| 249 | + }, |
| 250 | + DeprecationMessage: "deprecated", |
| 251 | + Validators: []validator.List{v}, |
| 252 | + PlanModifiers: []planmodifier.List{pm}, |
| 253 | + }, |
| 254 | + }, |
| 255 | + }, |
| 256 | + path: []string{"list"}, |
| 257 | + want: NestedBlockObject{ |
| 258 | + Attributes: map[string]AttributeBuilder{ |
| 259 | + "list": ListNestedAttributeBuilder{ |
| 260 | + NestedObject: NestedAttributeObject{ |
| 261 | + Attributes: map[string]AttributeBuilder{ |
| 262 | + "attr": StringAttributeBuilder{}, |
| 263 | + }, |
| 264 | + }, |
| 265 | + DeprecationMessage: "deprecated", |
| 266 | + Validators: []validator.List{v}, |
| 267 | + PlanModifiers: []planmodifier.List{pm}, |
| 268 | + }, |
| 269 | + }, |
| 270 | + }, |
| 271 | + }, |
| 272 | + { |
| 273 | + name: "ListNestedBlock/CalledOnInnerBlock", |
| 274 | + baseSchema: NestedBlockObject{ |
| 275 | + Blocks: map[string]BlockBuilder{ |
| 276 | + "list": ListNestedBlockBuilder{ |
| 277 | + NestedObject: NestedBlockObject{ |
| 278 | + Blocks: map[string]BlockBuilder{ |
| 279 | + "nested_block": ListNestedBlockBuilder{ |
| 280 | + NestedObject: NestedBlockObject{ |
| 281 | + Attributes: map[string]AttributeBuilder{ |
| 282 | + "attr": StringAttributeBuilder{}, |
| 283 | + }, |
| 284 | + }, |
| 285 | + }, |
| 286 | + }, |
| 287 | + }, |
| 288 | + }, |
| 289 | + }, |
| 290 | + }, |
| 291 | + path: []string{"list", "nested_block"}, |
| 292 | + want: NestedBlockObject{ |
| 293 | + Blocks: map[string]BlockBuilder{ |
| 294 | + "list": ListNestedBlockBuilder{ |
| 295 | + NestedObject: NestedBlockObject{ |
| 296 | + Attributes: map[string]AttributeBuilder{ |
| 297 | + "nested_block": ListNestedAttributeBuilder{ |
| 298 | + NestedObject: NestedAttributeObject{ |
| 299 | + Attributes: map[string]AttributeBuilder{ |
| 300 | + "attr": StringAttributeBuilder{}, |
| 301 | + }, |
| 302 | + }, |
| 303 | + }, |
| 304 | + }, |
| 305 | + }, |
| 306 | + }, |
| 307 | + }, |
| 308 | + }, |
| 309 | + }, |
| 310 | + { |
| 311 | + name: "SingleNestedBlock", |
| 312 | + baseSchema: NestedBlockObject{ |
| 313 | + Blocks: map[string]BlockBuilder{ |
| 314 | + "single": SingleNestedBlockBuilder{ |
| 315 | + NestedObject: NestedBlockObject{ |
| 316 | + Attributes: map[string]AttributeBuilder{ |
| 317 | + "attr": StringAttributeBuilder{}, |
| 318 | + }, |
| 319 | + }, |
| 320 | + DeprecationMessage: "deprecated", |
| 321 | + Validators: []validator.Object{v}, |
| 322 | + PlanModifiers: []planmodifier.Object{pm}, |
| 323 | + }, |
| 324 | + }, |
| 325 | + }, |
| 326 | + path: []string{"single"}, |
| 327 | + want: NestedBlockObject{ |
| 328 | + Attributes: map[string]AttributeBuilder{ |
| 329 | + "single": SingleNestedAttributeBuilder{ |
| 330 | + Attributes: map[string]AttributeBuilder{ |
| 331 | + "attr": StringAttributeBuilder{}, |
| 332 | + }, |
| 333 | + DeprecationMessage: "deprecated", |
| 334 | + Validators: []validator.Object{v}, |
| 335 | + PlanModifiers: []planmodifier.Object{pm}, |
| 336 | + }, |
| 337 | + }, |
| 338 | + }, |
| 339 | + }, |
| 340 | + { |
| 341 | + name: "SingleNestedBlock/RecursiveBlocks", |
| 342 | + baseSchema: NestedBlockObject{ |
| 343 | + Blocks: map[string]BlockBuilder{ |
| 344 | + "single": SingleNestedBlockBuilder{ |
| 345 | + NestedObject: NestedBlockObject{ |
| 346 | + Blocks: map[string]BlockBuilder{ |
| 347 | + "nested_block": ListNestedBlockBuilder{ |
| 348 | + NestedObject: NestedBlockObject{ |
| 349 | + Attributes: map[string]AttributeBuilder{ |
| 350 | + "attr": StringAttributeBuilder{}, |
| 351 | + }, |
| 352 | + }, |
| 353 | + }, |
| 354 | + }, |
| 355 | + }, |
| 356 | + }, |
| 357 | + }, |
| 358 | + }, |
| 359 | + path: []string{"single"}, |
| 360 | + want: NestedBlockObject{ |
| 361 | + Attributes: map[string]AttributeBuilder{ |
| 362 | + "single": SingleNestedAttributeBuilder{ |
| 363 | + Attributes: map[string]AttributeBuilder{ |
| 364 | + "nested_block": ListNestedAttributeBuilder{ |
| 365 | + NestedObject: NestedAttributeObject{ |
| 366 | + Attributes: map[string]AttributeBuilder{ |
| 367 | + "attr": StringAttributeBuilder{}, |
| 368 | + }, |
| 369 | + }, |
| 370 | + }, |
| 371 | + }, |
| 372 | + }, |
| 373 | + }, |
| 374 | + }, |
| 375 | + }, |
| 376 | + { |
| 377 | + name: "PanicOnEmptyPath", |
| 378 | + path: nil, |
| 379 | + expectPanic: true, |
| 380 | + }, |
| 381 | + } |
| 382 | + for _, c := range testCases { |
| 383 | + t.Run(c.name, func(t *testing.T) { |
| 384 | + if c.expectPanic { |
| 385 | + assert.Panics(t, func() { |
| 386 | + ConstructCustomizableSchema(c.baseSchema).ConvertToAttribute(c.path...) |
| 387 | + }) |
| 388 | + } else { |
| 389 | + got := ConstructCustomizableSchema(c.baseSchema).ConvertToAttribute(c.path...) |
| 390 | + assert.Equal(t, c.want, got.attr.(SingleNestedBlockBuilder).NestedObject) |
| 391 | + } |
| 392 | + }) |
| 393 | + } |
| 394 | +} |
0 commit comments