/
bucket_website_configuration_datasource.go
95 lines (75 loc) · 3.02 KB
/
bucket_website_configuration_datasource.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// Package s3 provides a Terraform datasource.
package s3
import (
"context"
"fmt"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/datasource"
v1 "github.com/orange-cloudavenue/cloudavenue-sdk-go/v1"
"github.com/orange-cloudavenue/terraform-provider-cloudavenue/internal/client"
"github.com/orange-cloudavenue/terraform-provider-cloudavenue/internal/metrics"
)
var (
_ datasource.DataSource = &BucketWebsiteConfigurationDataSource{}
_ datasource.DataSourceWithConfigure = &BucketWebsiteConfigurationDataSource{}
)
func NewBucketWebsiteConfigurationDataSource() datasource.DataSource {
return &BucketWebsiteConfigurationDataSource{}
}
type BucketWebsiteConfigurationDataSource struct {
client *client.CloudAvenue
s3Client v1.S3Client
}
// Init Initializes the data source.
func (d *BucketWebsiteConfigurationDataSource) Init(ctx context.Context, dm *BucketWebsiteConfigurationDataSourceModel) (diags diag.Diagnostics) {
d.s3Client = d.client.CAVSDK.V1.S3()
return
}
func (d *BucketWebsiteConfigurationDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_" + categoryName + "_bucket_website_configuration"
}
func (d *BucketWebsiteConfigurationDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = bucketWebsiteConfigurationSchema(ctx).GetDataSource(ctx)
}
func (d *BucketWebsiteConfigurationDataSource) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
// Prevent panic if the provider has not been configured.
if req.ProviderData == nil {
return
}
client, ok := req.ProviderData.(*client.CloudAvenue)
if !ok {
resp.Diagnostics.AddError(
"Unexpected Data Source Configure Type",
fmt.Sprintf("Expected *client.CloudAvenue, got: %T. Please report this issue to the provider developers.", req.ProviderData),
)
return
}
d.client = client
}
func (d *BucketWebsiteConfigurationDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
defer metrics.New("data.cloudavenue_s3_bucket_website_configuration", d.client.GetOrgName(), metrics.Read)()
config := &BucketWebsiteConfigurationDataSourceModel{}
// Read Terraform configuration data into the model
resp.Diagnostics.Append(req.Config.Get(ctx, config)...)
if resp.Diagnostics.HasError() {
return
}
// Init the resource
resp.Diagnostics.Append(d.Init(ctx, config)...)
if resp.Diagnostics.HasError() {
return
}
/*
Implement the data source read logic here.
*/
data, _, diags := genericReadWebsiteConfiguration(ctx, &readWebsiteConfigurationConfig[*BucketWebsiteConfigurationDataSourceModel]{
Client: d.s3Client.S3,
BucketName: config.Bucket.GetPtr(),
}, config)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
// Save data into Terraform state
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}