|
9 | 9 | "github.com/hashicorp/terraform-plugin-framework/attr"
|
10 | 10 | "github.com/hashicorp/terraform-plugin-framework/path"
|
11 | 11 | "github.com/hashicorp/terraform-plugin-framework/resource"
|
| 12 | + "github.com/hashicorp/terraform-plugin-framework/tfsdk" |
| 13 | + "github.com/hashicorp/terraform-plugin-framework/types" |
12 | 14 | inttypes "github.com/hashicorp/terraform-provider-aws/internal/types"
|
13 | 15 | "github.com/hashicorp/terraform-provider-aws/names"
|
14 | 16 | )
|
@@ -56,6 +58,41 @@ func (r identityInterceptor) create(ctx context.Context, opts interceptorOptions
|
56 | 58 | }
|
57 | 59 | }
|
58 | 60 | }
|
| 61 | + case OnError: |
| 62 | + identity := response.Identity |
| 63 | + if identity == nil { |
| 64 | + break |
| 65 | + } |
| 66 | + |
| 67 | + if identityIsFullyNull(ctx, identity, r.attributes) { |
| 68 | + for _, att := range r.attributes { |
| 69 | + switch att.Name() { |
| 70 | + case names.AttrAccountID: |
| 71 | + opts.response.Diagnostics.Append(identity.SetAttribute(ctx, path.Root(att.Name()), awsClient.AccountID(ctx))...) |
| 72 | + if opts.response.Diagnostics.HasError() { |
| 73 | + return |
| 74 | + } |
| 75 | + |
| 76 | + case names.AttrRegion: |
| 77 | + opts.response.Diagnostics.Append(identity.SetAttribute(ctx, path.Root(att.Name()), awsClient.Region(ctx))...) |
| 78 | + if opts.response.Diagnostics.HasError() { |
| 79 | + return |
| 80 | + } |
| 81 | + |
| 82 | + default: |
| 83 | + var attrVal attr.Value |
| 84 | + opts.response.Diagnostics.Append(response.State.GetAttribute(ctx, path.Root(att.ResourceAttributeName()), &attrVal)...) |
| 85 | + if opts.response.Diagnostics.HasError() { |
| 86 | + return |
| 87 | + } |
| 88 | + |
| 89 | + opts.response.Diagnostics.Append(identity.SetAttribute(ctx, path.Root(att.Name()), attrVal)...) |
| 90 | + if opts.response.Diagnostics.HasError() { |
| 91 | + return |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + } |
59 | 96 | }
|
60 | 97 | }
|
61 | 98 |
|
@@ -103,11 +140,109 @@ func (r identityInterceptor) read(ctx context.Context, opts interceptorOptions[r
|
103 | 140 | }
|
104 | 141 |
|
105 | 142 | func (r identityInterceptor) update(ctx context.Context, opts interceptorOptions[resource.UpdateRequest, resource.UpdateResponse]) {
|
| 143 | + awsClient := opts.c |
| 144 | + |
| 145 | + switch response, when := opts.response, opts.when; when { |
| 146 | + case After: |
| 147 | + if response.State.Raw.IsNull() { |
| 148 | + break |
| 149 | + } |
| 150 | + identity := response.Identity |
| 151 | + if identity == nil { |
| 152 | + break |
| 153 | + } |
| 154 | + |
| 155 | + for _, att := range r.attributes { |
| 156 | + switch att.Name() { |
| 157 | + case names.AttrAccountID: |
| 158 | + opts.response.Diagnostics.Append(identity.SetAttribute(ctx, path.Root(att.Name()), awsClient.AccountID(ctx))...) |
| 159 | + if opts.response.Diagnostics.HasError() { |
| 160 | + return |
| 161 | + } |
| 162 | + |
| 163 | + case names.AttrRegion: |
| 164 | + opts.response.Diagnostics.Append(identity.SetAttribute(ctx, path.Root(att.Name()), awsClient.Region(ctx))...) |
| 165 | + if opts.response.Diagnostics.HasError() { |
| 166 | + return |
| 167 | + } |
| 168 | + |
| 169 | + default: |
| 170 | + var attrVal attr.Value |
| 171 | + opts.response.Diagnostics.Append(response.State.GetAttribute(ctx, path.Root(att.ResourceAttributeName()), &attrVal)...) |
| 172 | + if opts.response.Diagnostics.HasError() { |
| 173 | + return |
| 174 | + } |
| 175 | + |
| 176 | + opts.response.Diagnostics.Append(identity.SetAttribute(ctx, path.Root(att.Name()), attrVal)...) |
| 177 | + if opts.response.Diagnostics.HasError() { |
| 178 | + return |
| 179 | + } |
| 180 | + } |
| 181 | + } |
| 182 | + case OnError: |
| 183 | + if response.State.Raw.IsNull() { |
| 184 | + break |
| 185 | + } |
| 186 | + identity := response.Identity |
| 187 | + if identity == nil { |
| 188 | + break |
| 189 | + } |
| 190 | + |
| 191 | + if identityIsFullyNull(ctx, identity, r.attributes) { |
| 192 | + for _, att := range r.attributes { |
| 193 | + switch att.Name() { |
| 194 | + case names.AttrAccountID: |
| 195 | + opts.response.Diagnostics.Append(identity.SetAttribute(ctx, path.Root(att.Name()), awsClient.AccountID(ctx))...) |
| 196 | + if opts.response.Diagnostics.HasError() { |
| 197 | + return |
| 198 | + } |
| 199 | + |
| 200 | + case names.AttrRegion: |
| 201 | + opts.response.Diagnostics.Append(identity.SetAttribute(ctx, path.Root(att.Name()), awsClient.Region(ctx))...) |
| 202 | + if opts.response.Diagnostics.HasError() { |
| 203 | + return |
| 204 | + } |
| 205 | + |
| 206 | + default: |
| 207 | + var attrVal attr.Value |
| 208 | + opts.response.Diagnostics.Append(response.State.GetAttribute(ctx, path.Root(att.ResourceAttributeName()), &attrVal)...) |
| 209 | + if opts.response.Diagnostics.HasError() { |
| 210 | + return |
| 211 | + } |
| 212 | + |
| 213 | + opts.response.Diagnostics.Append(identity.SetAttribute(ctx, path.Root(att.Name()), attrVal)...) |
| 214 | + if opts.response.Diagnostics.HasError() { |
| 215 | + return |
| 216 | + } |
| 217 | + } |
| 218 | + } |
| 219 | + } |
| 220 | + } |
106 | 221 | }
|
107 | 222 |
|
108 | 223 | func (r identityInterceptor) delete(ctx context.Context, opts interceptorOptions[resource.DeleteRequest, resource.DeleteResponse]) {
|
109 | 224 | }
|
110 | 225 |
|
| 226 | +// identityIsFullyNull returns true if a resource supports identity and |
| 227 | +// all attributes are set to null values |
| 228 | +func identityIsFullyNull(ctx context.Context, identity *tfsdk.ResourceIdentity, attributes []inttypes.IdentityAttribute) bool { |
| 229 | + if identity == nil { |
| 230 | + return true |
| 231 | + } |
| 232 | + |
| 233 | + for _, attr := range attributes { |
| 234 | + var attrVal types.String |
| 235 | + if diags := identity.GetAttribute(ctx, path.Root(attr.Name()), &attrVal); diags.HasError() { |
| 236 | + return false |
| 237 | + } |
| 238 | + if !attrVal.IsNull() && attrVal.ValueString() != "" { |
| 239 | + return false |
| 240 | + } |
| 241 | + } |
| 242 | + |
| 243 | + return true |
| 244 | +} |
| 245 | + |
111 | 246 | func newIdentityInterceptor(attributes []inttypes.IdentityAttribute) identityInterceptor {
|
112 | 247 | return identityInterceptor{
|
113 | 248 | attributes: attributes,
|
|
0 commit comments