Skip to content

Commit 60741d4

Browse files
committed
feat(cockpit): add scaleway_cockpit_sources data source
1 parent b5ff068 commit 60741d4

10 files changed

+4288
-0
lines changed
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
---
2+
subcategory: "Cockpit"
3+
page_title: "Scaleway: scaleway_cockpit_sources"
4+
---
5+
6+
# scaleway_cockpit_sources
7+
8+
Gets information about multiple Cockpit data sources.
9+
10+
## Example Usage
11+
12+
### List all sources in a project
13+
14+
```hcl
15+
data "scaleway_cockpit_sources" "all" {
16+
project_id = "11111111-1111-1111-1111-111111111111"
17+
}
18+
```
19+
20+
### Filter sources by type
21+
22+
```hcl
23+
data "scaleway_cockpit_sources" "metrics" {
24+
project_id = "11111111-1111-1111-1111-111111111111"
25+
type = "metrics"
26+
}
27+
```
28+
29+
### Filter sources by name
30+
31+
```hcl
32+
data "scaleway_cockpit_sources" "my_sources" {
33+
project_id = "11111111-1111-1111-1111-111111111111"
34+
name = "my-data-source"
35+
}
36+
```
37+
38+
### Filter sources by origin
39+
40+
```hcl
41+
data "scaleway_cockpit_sources" "external" {
42+
project_id = "11111111-1111-1111-1111-111111111111"
43+
origin = "external"
44+
}
45+
```
46+
47+
### List default Scaleway sources
48+
49+
```hcl
50+
data "scaleway_cockpit_sources" "default" {
51+
project_id = "11111111-1111-1111-1111-111111111111"
52+
origin = "scaleway"
53+
}
54+
```
55+
56+
## Argument Reference
57+
58+
The following arguments are supported:
59+
60+
- `project_id` - (Optional) The project ID the cockpit sources are associated with.
61+
- `region` - (Optional) The region in which the cockpit sources are located.
62+
- `name` - (Optional) Filter sources by name.
63+
- `type` - (Optional) Filter sources by type. Possible values are: `metrics`, `logs`, `traces`.
64+
- `origin` - (Optional) Filter sources by origin. Possible values are: `scaleway`, `external`, `custom`.
65+
66+
## Attributes Reference
67+
68+
In addition to all arguments above, the following attributes are exported:
69+
70+
- `sources` - List of cockpit sources.
71+
72+
Each `sources` block contains:
73+
74+
- `id` - The ID of the data source.
75+
- `name` - Name of the datasource.
76+
- `url` - The URL of the datasource.
77+
- `type` - The type of the datasource.
78+
- `origin` - The origin of the datasource.
79+
- `synchronized_with_grafana` - Indicates whether the data source is synchronized with Grafana.
80+
- `created_at` - The date and time of the creation of the cockpit datasource.
81+
- `updated_at` - The date and time of the last update of the cockpit datasource.
82+
- `retention_days` - The number of days to retain data.
83+
- `push_url` - The URL endpoint used for pushing data to the cockpit data source.
84+
- `region` - The region of the data source.
85+
- `project_id` - The project ID of the data source.
86+

internal/provider/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,7 @@ func Provider(config *Config) plugin.ProviderFunc {
268268
"scaleway_block_volume": block.DataSourceVolume(),
269269
"scaleway_cockpit": cockpit.DataSourceCockpit(),
270270
"scaleway_cockpit_source": cockpit.DataSourceCockpitSource(),
271+
"scaleway_cockpit_sources": cockpit.DataSourceCockpitSources(),
271272
"scaleway_config": scwconfig.DataSourceConfig(),
272273
"scaleway_container": container.DataSourceContainer(),
273274
"scaleway_container_namespace": container.DataSourceNamespace(),
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
package cockpit
2+
3+
import (
4+
"context"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
8+
"github.com/scaleway/scaleway-sdk-go/api/cockpit/v1"
9+
"github.com/scaleway/scaleway-sdk-go/scw"
10+
"github.com/scaleway/terraform-provider-scaleway/v2/internal/datasource"
11+
"github.com/scaleway/terraform-provider-scaleway/v2/internal/locality/regional"
12+
"github.com/scaleway/terraform-provider-scaleway/v2/internal/meta"
13+
"github.com/scaleway/terraform-provider-scaleway/v2/internal/types"
14+
)
15+
16+
func DataSourceCockpitSources() *schema.Resource {
17+
dsSchema := datasource.SchemaFromResourceSchema(ResourceCockpitSource().Schema)
18+
19+
sourceElementSchema := datasource.SchemaFromResourceSchema(ResourceCockpitSource().Schema)
20+
delete(sourceElementSchema, "sources")
21+
22+
sourceElementSchema["id"] = &schema.Schema{
23+
Type: schema.TypeString,
24+
Description: "The ID of the data source.",
25+
Computed: true,
26+
}
27+
28+
dsSchema["sources"] = &schema.Schema{
29+
Type: schema.TypeList,
30+
Description: "List of cockpit sources.",
31+
Computed: true,
32+
Elem: &schema.Resource{
33+
Schema: sourceElementSchema,
34+
},
35+
}
36+
37+
datasource.FixDatasourceSchemaFlags(dsSchema, false, "name", "project_id")
38+
datasource.AddOptionalFieldsToSchema(dsSchema, "region", "type", "origin")
39+
40+
return &schema.Resource{
41+
ReadContext: dataSourceCockpitSourcesRead,
42+
Schema: dsSchema,
43+
}
44+
}
45+
46+
func dataSourceCockpitSourcesRead(ctx context.Context, d *schema.ResourceData, m any) diag.Diagnostics {
47+
var region scw.Region
48+
49+
var err error
50+
51+
if v, ok := d.GetOk("region"); ok && v.(string) != "" {
52+
region, err = scw.ParseRegion(v.(string))
53+
if err != nil {
54+
return diag.FromErr(err)
55+
}
56+
} else {
57+
_, region, err = cockpitAPIWithRegion(d, m)
58+
if err != nil {
59+
return diag.FromErr(err)
60+
}
61+
}
62+
63+
api := cockpit.NewRegionalAPI(meta.ExtractScwClient(m))
64+
65+
req := &cockpit.RegionalAPIListDataSourcesRequest{
66+
Region: region,
67+
ProjectID: d.Get("project_id").(string),
68+
}
69+
70+
if v, ok := d.GetOk("type"); ok {
71+
req.Types = []cockpit.DataSourceType{cockpit.DataSourceType(v.(string))}
72+
}
73+
74+
if v, ok := d.GetOk("origin"); ok {
75+
req.Origin = cockpit.DataSourceOrigin(v.(string))
76+
}
77+
78+
res, err := api.ListDataSources(req, scw.WithContext(ctx), scw.WithAllPages())
79+
if err != nil {
80+
return diag.FromErr(err)
81+
}
82+
83+
sources := []any(nil)
84+
85+
for _, ds := range res.DataSources {
86+
if name, ok := d.GetOk("name"); ok {
87+
if ds.Name != name.(string) {
88+
continue
89+
}
90+
}
91+
92+
rawSource := flattenDataSourceToMap(ds)
93+
sources = append(sources, rawSource)
94+
}
95+
96+
d.SetId(region.String())
97+
_ = d.Set("sources", sources)
98+
99+
return nil
100+
}
101+
102+
func flattenDataSourceToMap(ds *cockpit.DataSource) map[string]any {
103+
pushURL, _ := createCockpitPushURL(ds.Type, ds.URL)
104+
105+
return map[string]any{
106+
"id": regional.NewIDString(ds.Region, ds.ID),
107+
"project_id": ds.ProjectID,
108+
"name": ds.Name,
109+
"url": ds.URL,
110+
"type": ds.Type.String(),
111+
"origin": ds.Origin.String(),
112+
"created_at": types.FlattenTime(ds.CreatedAt),
113+
"updated_at": types.FlattenTime(ds.UpdatedAt),
114+
"synchronized_with_grafana": ds.SynchronizedWithGrafana,
115+
"retention_days": int(ds.RetentionDays),
116+
"region": ds.Region.String(),
117+
"push_url": pushURL,
118+
}
119+
}

0 commit comments

Comments
 (0)