@@ -5,80 +5,30 @@ import (
55
66 "github.com/hashicorp/terraform-plugin-sdk/v2/diag"
77 "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
8- "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
98 "github.com/scaleway/scaleway-sdk-go/api/cockpit/v1"
109 "github.com/scaleway/scaleway-sdk-go/scw"
10+ "github.com/scaleway/terraform-provider-scaleway/v2/internal/datasource"
1111 "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 "
1313 "github.com/scaleway/terraform-provider-scaleway/v2/internal/types"
1414)
1515
1616func 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+
1729 return & schema.Resource {
1830 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 ,
8232 }
8333}
8434
@@ -113,12 +63,25 @@ func fetchDataSourceByID(ctx context.Context, d *schema.ResourceData, meta any)
11363 return nil
11464}
11565
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+ }
12081 }
12182
83+ api := cockpit .NewRegionalAPI (meta .ExtractScwClient (m ))
84+
12285 req := & cockpit.RegionalAPIListDataSourcesRequest {
12386 Region : region ,
12487 ProjectID : d .Get ("project_id" ).(string ),
0 commit comments