@@ -5,80 +5,30 @@ import (
5
5
6
6
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
7
7
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
8
- "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
9
8
"github.com/scaleway/scaleway-sdk-go/api/cockpit/v1"
10
9
"github.com/scaleway/scaleway-sdk-go/scw"
10
+ "github.com/scaleway/terraform-provider-scaleway/v2/internal/datasource"
11
11
"github.com/scaleway/terraform-provider-scaleway/v2/internal/locality/regional"
12
- "github.com/scaleway/terraform-provider-scaleway/v2/internal/services/account "
12
+ "github.com/scaleway/terraform-provider-scaleway/v2/internal/meta "
13
13
"github.com/scaleway/terraform-provider-scaleway/v2/internal/types"
14
14
)
15
15
16
16
func DataSourceCockpitSource () * schema.Resource {
17
+ dsSchema := datasource .SchemaFromResourceSchema (ResourceCockpitSource ().Schema )
18
+
19
+ dsSchema ["id" ] = & schema.Schema {
20
+ Type : schema .TypeString ,
21
+ Optional : true ,
22
+ Computed : true ,
23
+ Description : "ID of the data source." ,
24
+ }
25
+
26
+ datasource .FixDatasourceSchemaFlags (dsSchema , false , "id" , "name" , "project_id" )
27
+ datasource .AddOptionalFieldsToSchema (dsSchema , "region" , "type" , "origin" )
28
+
17
29
return & schema.Resource {
18
30
ReadContext : dataSourceCockpitSourceRead ,
19
- Schema : map [string ]* schema.Schema {
20
- "id" : {
21
- Type : schema .TypeString ,
22
- Optional : true ,
23
- Computed : true ,
24
- Description : "ID of the data source." ,
25
- },
26
- "region" : {
27
- Type : schema .TypeString ,
28
- Computed : true ,
29
- Description : "The region of the data source." ,
30
- },
31
- "project_id" : account .ProjectIDSchema (),
32
- "name" : {
33
- Type : schema .TypeString ,
34
- Optional : true ,
35
- Computed : true ,
36
- Description : "The name of the data source." ,
37
- },
38
- "type" : {
39
- Type : schema .TypeString ,
40
- Optional : true ,
41
- Computed : true ,
42
- Description : "The type of the data source (e.g., 'metrics', 'logs', 'traces')." ,
43
- ValidateFunc : validation .StringInSlice ([]string {
44
- "metrics" , "logs" , "traces" ,
45
- }, false ),
46
- },
47
- "origin" : {
48
- Type : schema .TypeString ,
49
- Optional : true ,
50
- Computed : true ,
51
- Description : "The origin of the data source (e.g., 'scaleway', 'external', 'custom')." ,
52
- ValidateFunc : validation .StringInSlice ([]string {
53
- "scaleway" , "external" , "custom" ,
54
- }, false ),
55
- },
56
- "url" : {
57
- Type : schema .TypeString ,
58
- Computed : true ,
59
- Description : "The URL of the data source." ,
60
- },
61
- "created_at" : {
62
- Type : schema .TypeString ,
63
- Computed : true ,
64
- Description : "The creation date of the data source." ,
65
- },
66
- "updated_at" : {
67
- Type : schema .TypeString ,
68
- Computed : true ,
69
- Description : "The last update date of the data source." ,
70
- },
71
- "synchronized_with_grafana" : {
72
- Type : schema .TypeBool ,
73
- Computed : true ,
74
- Description : "Whether the data source is synchronized with Grafana." ,
75
- },
76
- "retention_days" : {
77
- Type : schema .TypeInt ,
78
- Computed : true ,
79
- Description : "The retention period of the data source in days." ,
80
- },
81
- },
31
+ Schema : dsSchema ,
82
32
}
83
33
}
84
34
@@ -113,12 +63,25 @@ func fetchDataSourceByID(ctx context.Context, d *schema.ResourceData, meta any)
113
63
return nil
114
64
}
115
65
116
- func fetchDataSourceByFilters (ctx context.Context , d * schema.ResourceData , meta any ) diag.Diagnostics {
117
- api , region , err := cockpitAPIWithRegion (d , meta )
118
- if err != nil {
119
- return diag .FromErr (err )
66
+ func fetchDataSourceByFilters (ctx context.Context , d * schema.ResourceData , m any ) diag.Diagnostics {
67
+ var region scw.Region
68
+
69
+ var err error
70
+
71
+ if v , ok := d .GetOk ("region" ); ok && v .(string ) != "" {
72
+ region , err = scw .ParseRegion (v .(string ))
73
+ if err != nil {
74
+ return diag .FromErr (err )
75
+ }
76
+ } else {
77
+ _ , region , err = cockpitAPIWithRegion (d , m )
78
+ if err != nil {
79
+ return diag .FromErr (err )
80
+ }
120
81
}
121
82
83
+ api := cockpit .NewRegionalAPI (meta .ExtractScwClient (m ))
84
+
122
85
req := & cockpit.RegionalAPIListDataSourcesRequest {
123
86
Region : region ,
124
87
ProjectID : d .Get ("project_id" ).(string ),
0 commit comments