|
| 1 | +// Copyright (c) HashiCorp, Inc. |
| 2 | +// SPDX-License-Identifier: MPL-2.0 |
| 3 | + |
| 4 | +package framework |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "unique" |
| 9 | + |
| 10 | + "github.com/hashicorp/terraform-plugin-framework/diag" |
| 11 | + "github.com/hashicorp/terraform-plugin-framework/list" |
| 12 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 13 | + "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" |
| 14 | + "github.com/hashicorp/terraform-provider-aws/internal/conns" |
| 15 | + inttypes "github.com/hashicorp/terraform-provider-aws/internal/types" |
| 16 | + tfunique "github.com/hashicorp/terraform-provider-aws/internal/unique" |
| 17 | + "github.com/hashicorp/terraform-provider-aws/names" |
| 18 | +) |
| 19 | + |
| 20 | +type WithRegionSpec interface { |
| 21 | + SetRegionSpec(regionSpec unique.Handle[inttypes.ServicePackageResourceRegion]) |
| 22 | +} |
| 23 | + |
| 24 | +type ListResourceWithSDKv2Resource struct { |
| 25 | + resourceSchema *schema.Resource |
| 26 | + identitySpec inttypes.Identity |
| 27 | + identitySchema *schema.ResourceIdentity |
| 28 | + regionSpec unique.Handle[inttypes.ServicePackageResourceRegion] |
| 29 | +} |
| 30 | + |
| 31 | +func (l *ListResourceWithSDKv2Resource) SetRegionSpec(regionSpec unique.Handle[inttypes.ServicePackageResourceRegion]) { |
| 32 | + l.regionSpec = regionSpec |
| 33 | + |
| 34 | + var isRegionOverrideEnabled bool |
| 35 | + if !tfunique.IsHandleNil(regionSpec) && regionSpec.Value().IsOverrideEnabled { |
| 36 | + isRegionOverrideEnabled = true |
| 37 | + } |
| 38 | + |
| 39 | + if isRegionOverrideEnabled { |
| 40 | + if _, ok := l.resourceSchema.SchemaMap()[names.AttrRegion]; !ok { |
| 41 | + // TODO: Use standard shared `region` attribute |
| 42 | + l.resourceSchema.SchemaMap()[names.AttrRegion] = &schema.Schema{ |
| 43 | + Type: schema.TypeString, |
| 44 | + Optional: true, |
| 45 | + Computed: true, |
| 46 | + } |
| 47 | + } |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +func (l *ListResourceWithSDKv2Resource) SetIdentitySpec(identitySpec inttypes.Identity) { |
| 52 | + out := make(map[string]*schema.Schema) |
| 53 | + for _, v := range identitySpec.Attributes { |
| 54 | + out[v.Name()] = &schema.Schema{ |
| 55 | + Type: schema.TypeString, |
| 56 | + } |
| 57 | + if v.Required() { |
| 58 | + out[v.Name()].Required = true |
| 59 | + } else { |
| 60 | + out[v.Name()].Optional = true |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + identitySchema := schema.ResourceIdentity{ |
| 65 | + SchemaFunc: func() map[string]*schema.Schema { |
| 66 | + return out |
| 67 | + }, |
| 68 | + } |
| 69 | + |
| 70 | + l.identitySchema = &identitySchema |
| 71 | + l.resourceSchema.Identity = &identitySchema |
| 72 | + l.identitySpec = identitySpec |
| 73 | +} |
| 74 | + |
| 75 | +func (l *ListResourceWithSDKv2Resource) RawV5Schemas(ctx context.Context, _ list.RawV5SchemaRequest, response *list.RawV5SchemaResponse) { |
| 76 | + response.ProtoV5Schema = l.resourceSchema.ProtoSchema(ctx)() |
| 77 | + response.ProtoV5IdentitySchema = l.resourceSchema.ProtoIdentitySchema(ctx)() |
| 78 | +} |
| 79 | + |
| 80 | +func (l *ListResourceWithSDKv2Resource) SetResourceSchema(resource *schema.Resource) { |
| 81 | + l.resourceSchema = resource |
| 82 | +} |
| 83 | + |
| 84 | +func (l *ListResourceWithSDKv2Resource) ResourceData() *schema.ResourceData { |
| 85 | + return l.resourceSchema.Data(&terraform.InstanceState{}) |
| 86 | +} |
| 87 | + |
| 88 | +func (l *ListResourceWithSDKv2Resource) setResourceIdentity(ctx context.Context, client *conns.AWSClient, d *schema.ResourceData) error { |
| 89 | + identity, err := d.Identity() |
| 90 | + if err != nil { |
| 91 | + return err |
| 92 | + } |
| 93 | + |
| 94 | + for _, attr := range l.identitySpec.Attributes { |
| 95 | + switch attr.Name() { |
| 96 | + case names.AttrAccountID: |
| 97 | + if err := identity.Set(attr.Name(), client.AccountID(ctx)); err != nil { |
| 98 | + return err |
| 99 | + } |
| 100 | + |
| 101 | + case names.AttrRegion: |
| 102 | + if err := identity.Set(attr.Name(), client.Region(ctx)); err != nil { |
| 103 | + return err |
| 104 | + } |
| 105 | + |
| 106 | + default: |
| 107 | + val, ok := getAttributeOk(d, attr.ResourceAttributeName()) |
| 108 | + if !ok { |
| 109 | + continue |
| 110 | + } |
| 111 | + if err := identity.Set(attr.Name(), val); err != nil { |
| 112 | + return err |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + return nil |
| 118 | +} |
| 119 | + |
| 120 | +type resourceData interface { |
| 121 | + Id() string |
| 122 | + GetOk(string) (any, bool) |
| 123 | +} |
| 124 | + |
| 125 | +func getAttributeOk(d resourceData, name string) (string, bool) { |
| 126 | + if name == "id" { |
| 127 | + return d.Id(), true |
| 128 | + } |
| 129 | + if v, ok := d.GetOk(name); !ok { |
| 130 | + return "", false |
| 131 | + } else { |
| 132 | + return v.(string), true |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +func (l *ListResourceWithSDKv2Resource) SetResult(ctx context.Context, awsClient *conns.AWSClient, includeResource bool, result *list.ListResult, rd *schema.ResourceData) { |
| 137 | + err := l.setResourceIdentity(ctx, awsClient, rd) |
| 138 | + if err != nil { |
| 139 | + result.Diagnostics.Append(diag.NewErrorDiagnostic( |
| 140 | + "Error Listing Remote Resources", |
| 141 | + "An unexpected error occurred setting resource identity. "+ |
| 142 | + "This is always an error in the provider. "+ |
| 143 | + "Please report the following to the provider developer:\n\n"+ |
| 144 | + "Error: "+err.Error(), |
| 145 | + )) |
| 146 | + return |
| 147 | + } |
| 148 | + |
| 149 | + tfTypeIdentity, err := rd.TfTypeIdentityState() |
| 150 | + if err != nil { |
| 151 | + result.Diagnostics.Append(diag.NewErrorDiagnostic( |
| 152 | + "Error Listing Remote Resources", |
| 153 | + "An unexpected error occurred converting identity state. "+ |
| 154 | + "This is always an error in the provider. "+ |
| 155 | + "Please report the following to the provider developer:\n\n"+ |
| 156 | + "Error: "+err.Error(), |
| 157 | + )) |
| 158 | + return |
| 159 | + } |
| 160 | + |
| 161 | + result.Diagnostics.Append(result.Identity.Set(ctx, *tfTypeIdentity)...) |
| 162 | + if result.Diagnostics.HasError() { |
| 163 | + return |
| 164 | + } |
| 165 | + |
| 166 | + if includeResource { |
| 167 | + if !tfunique.IsHandleNil(l.regionSpec) && l.regionSpec.Value().IsOverrideEnabled { |
| 168 | + if err := rd.Set(names.AttrRegion, awsClient.Region(ctx)); err != nil { |
| 169 | + result.Diagnostics.Append(diag.NewErrorDiagnostic( |
| 170 | + "Error Listing Remote Resources", |
| 171 | + "An unexpected error occurred. "+ |
| 172 | + "This is always an error in the provider. "+ |
| 173 | + "Please report the following to the provider developer:\n\n"+ |
| 174 | + "Error: "+err.Error(), |
| 175 | + )) |
| 176 | + return |
| 177 | + } |
| 178 | + } |
| 179 | + |
| 180 | + tfTypeResource, err := rd.TfTypeResourceState() |
| 181 | + if err != nil { |
| 182 | + result.Diagnostics.Append(diag.NewErrorDiagnostic( |
| 183 | + "Error Listing Remote Resources", |
| 184 | + "An unexpected error occurred converting resource state. "+ |
| 185 | + "This is always an error in the provider. "+ |
| 186 | + "Please report the following to the provider developer:\n\n"+ |
| 187 | + "Error: "+err.Error(), |
| 188 | + )) |
| 189 | + return |
| 190 | + } |
| 191 | + |
| 192 | + result.Diagnostics.Append(result.Resource.Set(ctx, *tfTypeResource)...) |
| 193 | + if result.Diagnostics.HasError() { |
| 194 | + return |
| 195 | + } |
| 196 | + } |
| 197 | +} |
0 commit comments