Skip to content

Commit a4dc79d

Browse files
authored
Merge pull request #867 from ovh/dev/aamstutz/datasource-storage
feat: Add s3 storage datasources
2 parents 570e1e3 + 8147795 commit a4dc79d

7 files changed

+2063
-0
lines changed

Diff for: ovh/data_cloud_project_storage.go

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package ovh
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"net/url"
7+
"strconv"
8+
9+
"github.com/hashicorp/terraform-plugin-framework/datasource"
10+
)
11+
12+
var _ datasource.DataSourceWithConfigure = (*cloudProjectStorageDataSource)(nil)
13+
14+
func NewCloudProjectStorageDataSource() datasource.DataSource {
15+
return &cloudProjectStorageDataSource{}
16+
}
17+
18+
type cloudProjectStorageDataSource struct {
19+
config *Config
20+
}
21+
22+
func (d *cloudProjectStorageDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
23+
resp.TypeName = req.ProviderTypeName + "_cloud_project_storage"
24+
}
25+
26+
func (d *cloudProjectStorageDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
27+
if req.ProviderData == nil {
28+
return
29+
}
30+
31+
config, ok := req.ProviderData.(*Config)
32+
if !ok {
33+
resp.Diagnostics.AddError(
34+
"Unexpected Data Source Configure Type",
35+
fmt.Sprintf("Expected *Config, got: %T. Please report this issue to the provider developers.", req.ProviderData),
36+
)
37+
return
38+
}
39+
40+
d.config = config
41+
}
42+
43+
func (d *cloudProjectStorageDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
44+
resp.Schema = CloudProjectStorageDataSourceSchema(ctx)
45+
}
46+
47+
func (d *cloudProjectStorageDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
48+
var data CloudProjectStorageModel
49+
50+
// Read Terraform configuration data into the model
51+
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
52+
53+
if resp.Diagnostics.HasError() {
54+
return
55+
}
56+
57+
queryParams := url.Values{}
58+
if !data.Limit.IsNull() && !data.Limit.IsUnknown() {
59+
queryParams.Add("limit", strconv.FormatInt(data.Limit.ValueInt64(), 10))
60+
}
61+
if !data.Marker.IsNull() && !data.Marker.IsUnknown() {
62+
queryParams.Add("marker", data.Marker.ValueString())
63+
}
64+
if !data.Prefix.IsNull() && !data.Prefix.IsUnknown() {
65+
queryParams.Add("prefix", data.Prefix.ValueString())
66+
}
67+
68+
// Read API call logic
69+
endpoint := "/cloud/project/" + url.PathEscape(data.ServiceName.ValueString()) + "/region/" + url.PathEscape(data.RegionName.ValueString()) + "/storage/" + url.PathEscape(data.Name.ValueString()) + "?" + queryParams.Encode()
70+
if err := d.config.OVHClient.Get(endpoint, &data); err != nil {
71+
resp.Diagnostics.AddError(
72+
fmt.Sprintf("Error calling Get %s", endpoint),
73+
err.Error(),
74+
)
75+
return
76+
}
77+
78+
// Save data into Terraform state
79+
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
80+
}

0 commit comments

Comments
 (0)