|
| 1 | +// Copyright (c) HashiCorp, Inc. |
| 2 | +// SPDX-License-Identifier: MPL-2.0 |
| 3 | + |
| 4 | +package framework |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + |
| 9 | + "github.com/hashicorp/terraform-plugin-framework-nettypes/iptypes" |
| 10 | + "github.com/hashicorp/terraform-plugin-framework-validators/setvalidator" |
| 11 | + "github.com/hashicorp/terraform-plugin-framework/attr" |
| 12 | + "github.com/hashicorp/terraform-plugin-framework/resource" |
| 13 | + "github.com/hashicorp/terraform-plugin-framework/resource/schema" |
| 14 | + "github.com/hashicorp/terraform-plugin-framework/schema/validator" |
| 15 | + "github.com/hashicorp/terraform-plugin-framework/types" |
| 16 | +) |
| 17 | + |
| 18 | +var _ resource.Resource = SetSemanticEqualityResource{} |
| 19 | + |
| 20 | +func NewSetSemanticEqualityResource() resource.Resource { |
| 21 | + return &SetSemanticEqualityResource{} |
| 22 | +} |
| 23 | + |
| 24 | +// This resource tests that semantic equality for elements inside of a set are correctly executed |
| 25 | +// Original bug: https://github.com/hashicorp/terraform-plugin-framework/issues/1061 |
| 26 | +type SetSemanticEqualityResource struct{} |
| 27 | + |
| 28 | +func (r SetSemanticEqualityResource) Metadata(_ context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) { |
| 29 | + resp.TypeName = req.ProviderTypeName + "_set_semantic_equality" |
| 30 | +} |
| 31 | + |
| 32 | +func (r SetSemanticEqualityResource) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) { |
| 33 | + resp.Schema = schema.Schema{ |
| 34 | + Attributes: map[string]schema.Attribute{ |
| 35 | + "set_of_ipv6": schema.SetAttribute{ |
| 36 | + ElementType: iptypes.IPv6AddressType{}, |
| 37 | + Required: true, |
| 38 | + }, |
| 39 | + }, |
| 40 | + Blocks: map[string]schema.Block{ |
| 41 | + "set_nested_block": schema.SetNestedBlock{ |
| 42 | + Validators: []validator.Set{ |
| 43 | + setvalidator.IsRequired(), |
| 44 | + }, |
| 45 | + NestedObject: schema.NestedBlockObject{ |
| 46 | + Attributes: map[string]schema.Attribute{ |
| 47 | + "ipv6": schema.StringAttribute{ |
| 48 | + CustomType: iptypes.IPv6AddressType{}, |
| 49 | + Required: true, |
| 50 | + }, |
| 51 | + }, |
| 52 | + }, |
| 53 | + }, |
| 54 | + }, |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +func (r SetSemanticEqualityResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) { |
| 59 | + var data SetSemanticEqualityResourceModel |
| 60 | + |
| 61 | + resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...) |
| 62 | + |
| 63 | + if resp.Diagnostics.HasError() { |
| 64 | + return |
| 65 | + } |
| 66 | + |
| 67 | + // Simulate remote API returning semantically equivalent IPv6 addresses |
| 68 | + data.shiftAndShorten() |
| 69 | + |
| 70 | + resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) |
| 71 | +} |
| 72 | + |
| 73 | +func (r SetSemanticEqualityResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) { |
| 74 | + var data SetSemanticEqualityResourceModel |
| 75 | + |
| 76 | + resp.Diagnostics.Append(req.State.Get(ctx, &data)...) |
| 77 | + |
| 78 | + if resp.Diagnostics.HasError() { |
| 79 | + return |
| 80 | + } |
| 81 | + |
| 82 | + // Simulate remote API returning semantically equivalent IPv6 addresses |
| 83 | + data.shiftAndShorten() |
| 84 | + |
| 85 | + resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) |
| 86 | +} |
| 87 | + |
| 88 | +func (r SetSemanticEqualityResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) { |
| 89 | + var data SetSemanticEqualityResourceModel |
| 90 | + |
| 91 | + resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...) |
| 92 | + |
| 93 | + if resp.Diagnostics.HasError() { |
| 94 | + return |
| 95 | + } |
| 96 | + |
| 97 | + // Simulate remote API returning semantically equivalent IPv6 addresses |
| 98 | + data.shiftAndShorten() |
| 99 | + |
| 100 | + resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) |
| 101 | +} |
| 102 | + |
| 103 | +func (r SetSemanticEqualityResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) { |
| 104 | +} |
| 105 | + |
| 106 | +type SetSemanticEqualityResourceModel struct { |
| 107 | + SetOfIPv6 types.Set `tfsdk:"set_of_ipv6"` |
| 108 | + SetNestedBlock types.Set `tfsdk:"set_nested_block"` |
| 109 | +} |
| 110 | + |
| 111 | +var setObjectWithIPv6 = types.ObjectType{ |
| 112 | + AttrTypes: map[string]attr.Type{ |
| 113 | + "ipv6": iptypes.IPv6AddressType{}, |
| 114 | + }, |
| 115 | +} |
| 116 | + |
| 117 | +// Shifts + switches data to shortened IPv6 addresses, but is semantically equal to test config |
| 118 | +func (m *SetSemanticEqualityResourceModel) shiftAndShorten() { |
| 119 | + m.SetOfIPv6 = types.SetValueMust(iptypes.IPv6AddressType{}, []attr.Value{ |
| 120 | + iptypes.NewIPv6AddressValue("2001:DB8::8:800:200C:417A"), |
| 121 | + iptypes.NewIPv6AddressValue("::FFFF:192.168.255.255"), |
| 122 | + iptypes.NewIPv6AddressValue("::"), |
| 123 | + iptypes.NewIPv6AddressValue("::101"), |
| 124 | + }) |
| 125 | + |
| 126 | + m.SetNestedBlock = types.SetValueMust(setObjectWithIPv6, []attr.Value{ |
| 127 | + types.ObjectValueMust(setObjectWithIPv6.AttributeTypes(), map[string]attr.Value{ |
| 128 | + "ipv6": iptypes.NewIPv6AddressValue("::FFFF:192.168.255.255"), |
| 129 | + }), |
| 130 | + types.ObjectValueMust(setObjectWithIPv6.AttributeTypes(), map[string]attr.Value{ |
| 131 | + "ipv6": iptypes.NewIPv6AddressValue("FF01::"), |
| 132 | + }), |
| 133 | + types.ObjectValueMust(setObjectWithIPv6.AttributeTypes(), map[string]attr.Value{ |
| 134 | + "ipv6": iptypes.NewIPv6AddressValue("2001:DB8::8:800:200C:417A"), |
| 135 | + }), |
| 136 | + }) |
| 137 | +} |
0 commit comments