|
4 | 4 | package sdk |
5 | 5 |
|
6 | 6 | import ( |
| 7 | + "context" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/hashicorp/go-azure-helpers/framework/typehelpers" |
| 11 | + "github.com/hashicorp/go-azure-helpers/resourcemanager/resourcegroups" |
7 | 12 | "github.com/hashicorp/terraform-plugin-framework/list" |
| 13 | + listschema "github.com/hashicorp/terraform-plugin-framework/list/schema" |
8 | 14 | "github.com/hashicorp/terraform-plugin-framework/resource" |
9 | | - "github.com/hashicorp/terraform-provider-azurerm/internal/clients" |
10 | | - "github.com/hashicorp/terraform-provider-azurerm/internal/features" |
| 15 | + "github.com/hashicorp/terraform-plugin-framework/schema/validator" |
| 16 | + "github.com/hashicorp/terraform-plugin-framework/types" |
| 17 | + "github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk" |
| 18 | + "github.com/hashicorp/terraform-provider-azurerm/internal/tf/validation" |
11 | 19 | ) |
12 | 20 |
|
13 | | -type ListResource interface { |
14 | | - list.ListResourceWithConfigure |
| 21 | +// FrameworkListResourceWrapper presents an interface to simplify the implementations for List Resources |
| 22 | +// it handles boilerplate code, and provides sensible defaults for the Azure Search APIs |
| 23 | +type FrameworkListResourceWrapper struct { |
| 24 | + ResourceMetadata |
| 25 | + |
| 26 | + FrameworkListWrappedResource |
15 | 27 | } |
16 | 28 |
|
17 | | -type ListResourceWithRawV5Schemas interface { |
18 | | - ListResource |
| 29 | +type DefaultListModel struct { |
| 30 | + ResourceGroupName types.String `tfsdk:"resource_group_name"` |
| 31 | + SubscriptionId types.String `tfsdk:"subscription_id"` |
| 32 | +} |
19 | 33 |
|
20 | | - list.ListResourceWithRawV5Schemas |
| 34 | +func (r *FrameworkListResourceWrapper) Metadata(ctx context.Context, request resource.MetadataRequest, response *resource.MetadataResponse) { |
| 35 | + r.FrameworkListWrappedResource.Metadata(ctx, request, response) |
21 | 36 | } |
22 | 37 |
|
23 | | -type ListResourceMetadata struct { |
24 | | - Client *clients.Client |
| 38 | +func (r *FrameworkListResourceWrapper) ListResourceConfigSchema(ctx context.Context, request list.ListResourceSchemaRequest, response *list.ListResourceSchemaResponse) { |
| 39 | + if l, ok := r.FrameworkListWrappedResource.(FrameworkListWrappedResourceWithConfig); ok { |
| 40 | + l.ListResourceConfigSchema(ctx, request, response) |
| 41 | + return |
| 42 | + } |
| 43 | + |
| 44 | + // most resources default to RG and Subscription, so unless we need to customise that above, we can default it here. |
| 45 | + response.Schema = listschema.Schema{ |
| 46 | + Attributes: map[string]listschema.Attribute{ |
| 47 | + "resource_group_name": listschema.StringAttribute{ |
| 48 | + Optional: true, |
| 49 | + Validators: []validator.String{ |
| 50 | + typehelpers.WrappedStringValidator{ |
| 51 | + Func: resourcegroups.ValidateName, |
| 52 | + }, |
| 53 | + }, |
| 54 | + }, |
| 55 | + "subscription_id": listschema.StringAttribute{ |
| 56 | + Optional: true, |
| 57 | + Validators: []validator.String{ |
| 58 | + typehelpers.WrappedStringValidator{ |
| 59 | + Func: validation.IsUUID, |
| 60 | + }, |
| 61 | + }, |
| 62 | + }, |
| 63 | + }, |
| 64 | + } |
| 65 | +} |
25 | 66 |
|
26 | | - SubscriptionId string |
| 67 | +func (r *FrameworkListResourceWrapper) List(ctx context.Context, request list.ListRequest, stream *list.ListResultsStream) { |
| 68 | + ctx, cancel := context.WithTimeout(ctx, time.Minute*60) // TODO - Custom Timeouts |
| 69 | + defer cancel() |
27 | 70 |
|
28 | | - Features features.UserFeatures |
| 71 | + r.FrameworkListWrappedResource.List(ctx, request, stream, r.ResourceMetadata) |
29 | 72 | } |
30 | 73 |
|
31 | | -func (r *ListResourceMetadata) Defaults(request resource.ConfigureRequest, response *resource.ConfigureResponse) { |
32 | | - if request.ProviderData == nil { |
33 | | - return |
34 | | - } |
| 74 | +func (r *FrameworkListResourceWrapper) RawV5Schemas(ctx context.Context, _ list.RawV5SchemaRequest, response *list.RawV5SchemaResponse) { |
| 75 | + res := r.FrameworkListWrappedResource.ResourceFunc() |
| 76 | + response.ProtoV5Schema = res.ProtoSchema(ctx)() |
| 77 | + response.ProtoV5IdentitySchema = res.ProtoIdentitySchema(ctx)() |
| 78 | +} |
35 | 79 |
|
36 | | - c, ok := request.ProviderData.(*clients.Client) |
37 | | - if !ok { |
38 | | - response.Diagnostics.AddError("Client Provider Data Error", "invalid provider data supplied") |
39 | | - return |
| 80 | +func (r *FrameworkListResourceWrapper) Resource() func() list.ListResource { |
| 81 | + return func() list.ListResource { |
| 82 | + return r |
40 | 83 | } |
| 84 | +} |
| 85 | + |
| 86 | +func (r *FrameworkListResourceWrapper) Configure(_ context.Context, request resource.ConfigureRequest, response *resource.ConfigureResponse) { |
| 87 | + r.Defaults(request, response) |
| 88 | +} |
| 89 | + |
| 90 | +var ( |
| 91 | + _ list.ListResourceWithConfigure = &FrameworkListResourceWrapper{} |
| 92 | + _ list.ListResourceWithRawV5Schemas = &FrameworkListResourceWrapper{} |
| 93 | +) |
| 94 | + |
| 95 | +type FrameworkListWrappedResource interface { |
| 96 | + Metadata(ctx context.Context, request resource.MetadataRequest, response *resource.MetadataResponse) |
| 97 | + |
| 98 | + List(ctx context.Context, request list.ListRequest, stream *list.ListResultsStream, metadata ResourceMetadata) |
| 99 | + |
| 100 | + // RawV5Schemas(ctx context.Context, request list.RawV5SchemaRequest, response *list.RawV5SchemaResponse) |
| 101 | + |
| 102 | + // ResourceFunc exposes the PluginSDKv2 resource function so that the RawV5Schema can be extracted |
| 103 | + // This should call the function that is used to register the resource in the provider that this List resource represents |
| 104 | + ResourceFunc() *pluginsdk.Resource |
| 105 | +} |
| 106 | + |
| 107 | +type FrameworkListWrappedResourceWithConfig interface { |
| 108 | + FrameworkListWrappedResource |
41 | 109 |
|
42 | | - r.Client = c |
43 | | - r.SubscriptionId = c.Account.SubscriptionId |
44 | | - r.Features = c.Features |
| 110 | + ListResourceConfigSchema(ctx context.Context, request list.ListResourceSchemaRequest, response *list.ListResourceSchemaResponse) |
45 | 111 | } |
0 commit comments