-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
Copy pathframework_data_source.go
51 lines (38 loc) · 1.44 KB
/
framework_data_source.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package sdk
import (
"context"
"fmt"
"time"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/features"
)
type DataSourceMetadata struct {
Client *clients.Client
SubscriptionId string
TimeoutRead time.Duration
Features features.UserFeatures
}
// Defaults configures the Data Source Metadata for client access, Provider Features, and subscriptionId.
func (r *DataSourceMetadata) Defaults(req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
if req.ProviderData == nil {
return
}
c, ok := req.ProviderData.(*clients.Client)
if !ok {
resp.Diagnostics.AddError("Client Provider Data Error", fmt.Sprintf("invalid provider data supplied, got %+v", req.ProviderData))
return
}
r.Client = c
r.SubscriptionId = c.Account.SubscriptionId
r.Features = c.Features
r.TimeoutRead = 5 * time.Minute
}
// DecodeRead is a helper function to populate the Data Source model from the user config and writes any diags back to the ReadResponse
// Returns true if there are no Error Diagnostics.
func (r *DataSourceMetadata) DecodeRead(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse, config interface{}) bool {
resp.Diagnostics.Append(req.Config.Get(ctx, config)...)
return !resp.Diagnostics.HasError()
}