Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 86 additions & 0 deletions docs/data-sources/cockpit_sources.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
---
subcategory: "Cockpit"
page_title: "Scaleway: scaleway_cockpit_sources"
---

# scaleway_cockpit_sources

Gets information about multiple Cockpit data sources.

## Example Usage

### List all sources in a project

```hcl
data "scaleway_cockpit_sources" "all" {
project_id = "11111111-1111-1111-1111-111111111111"
}
```

### Filter sources by type

```hcl
data "scaleway_cockpit_sources" "metrics" {
project_id = "11111111-1111-1111-1111-111111111111"
type = "metrics"
}
```

### Filter sources by name

```hcl
data "scaleway_cockpit_sources" "my_sources" {
project_id = "11111111-1111-1111-1111-111111111111"
name = "my-data-source"
}
```

### Filter sources by origin

```hcl
data "scaleway_cockpit_sources" "external" {
project_id = "11111111-1111-1111-1111-111111111111"
origin = "external"
}
```

### List default Scaleway sources

```hcl
data "scaleway_cockpit_sources" "default" {
project_id = "11111111-1111-1111-1111-111111111111"
origin = "scaleway"
}
```

## Argument Reference

The following arguments are supported:

- `project_id` - (Optional) The project ID the cockpit sources are associated with.
- `region` - (Optional) The region in which the cockpit sources are located.
- `name` - (Optional) Filter sources by name.
- `type` - (Optional) Filter sources by type. Possible values are: `metrics`, `logs`, `traces`.
- `origin` - (Optional) Filter sources by origin. Possible values are: `scaleway`, `external`, `custom`.

## Attributes Reference

In addition to all arguments above, the following attributes are exported:

- `sources` - List of cockpit sources.

Each `sources` block contains:

- `id` - The ID of the data source.
- `name` - Name of the datasource.
- `url` - The URL of the datasource.
- `type` - The type of the datasource.
- `origin` - The origin of the datasource.
- `synchronized_with_grafana` - Indicates whether the data source is synchronized with Grafana.
- `created_at` - The date and time of the creation of the cockpit datasource.
- `updated_at` - The date and time of the last update of the cockpit datasource.
- `retention_days` - The number of days to retain data.
- `push_url` - The URL endpoint used for pushing data to the cockpit data source.
- `region` - The region of the data source.
- `project_id` - The project ID of the data source.

1 change: 1 addition & 0 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@ func Provider(config *Config) plugin.ProviderFunc {
"scaleway_block_volume": block.DataSourceVolume(),
"scaleway_cockpit": cockpit.DataSourceCockpit(),
"scaleway_cockpit_source": cockpit.DataSourceCockpitSource(),
"scaleway_cockpit_sources": cockpit.DataSourceCockpitSources(),
"scaleway_config": scwconfig.DataSourceConfig(),
"scaleway_container": container.DataSourceContainer(),
"scaleway_container_namespace": container.DataSourceNamespace(),
Expand Down
119 changes: 119 additions & 0 deletions internal/services/cockpit/sources_data_source.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package cockpit

import (
"context"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/scaleway/scaleway-sdk-go/api/cockpit/v1"
"github.com/scaleway/scaleway-sdk-go/scw"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/datasource"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/locality/regional"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/meta"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/types"
)

func DataSourceCockpitSources() *schema.Resource {
dsSchema := datasource.SchemaFromResourceSchema(ResourceCockpitSource().Schema)

sourceElementSchema := datasource.SchemaFromResourceSchema(ResourceCockpitSource().Schema)
delete(sourceElementSchema, "sources")

sourceElementSchema["id"] = &schema.Schema{
Type: schema.TypeString,
Description: "The ID of the data source.",
Computed: true,
}

dsSchema["sources"] = &schema.Schema{
Type: schema.TypeList,
Description: "List of cockpit sources.",
Computed: true,
Elem: &schema.Resource{
Schema: sourceElementSchema,
},
}

datasource.FixDatasourceSchemaFlags(dsSchema, false, "name", "project_id")
datasource.AddOptionalFieldsToSchema(dsSchema, "region", "type", "origin")

return &schema.Resource{
ReadContext: dataSourceCockpitSourcesRead,
Schema: dsSchema,
}
}

func dataSourceCockpitSourcesRead(ctx context.Context, d *schema.ResourceData, m any) diag.Diagnostics {
var region scw.Region

var err error

if v, ok := d.GetOk("region"); ok && v.(string) != "" {
region, err = scw.ParseRegion(v.(string))
if err != nil {
return diag.FromErr(err)
}
} else {
_, region, err = cockpitAPIWithRegion(d, m)
if err != nil {
return diag.FromErr(err)
}
}

api := cockpit.NewRegionalAPI(meta.ExtractScwClient(m))

req := &cockpit.RegionalAPIListDataSourcesRequest{
Region: region,
ProjectID: d.Get("project_id").(string),
}

if v, ok := d.GetOk("type"); ok {
req.Types = []cockpit.DataSourceType{cockpit.DataSourceType(v.(string))}
}

if v, ok := d.GetOk("origin"); ok {
req.Origin = cockpit.DataSourceOrigin(v.(string))
}

res, err := api.ListDataSources(req, scw.WithContext(ctx), scw.WithAllPages())
if err != nil {
return diag.FromErr(err)
}

sources := []any(nil)

for _, ds := range res.DataSources {
if name, ok := d.GetOk("name"); ok {
if ds.Name != name.(string) {
continue
}
}

rawSource := flattenDataSourceToMap(ds)
sources = append(sources, rawSource)
}

d.SetId(region.String())
_ = d.Set("sources", sources)

return nil
}

func flattenDataSourceToMap(ds *cockpit.DataSource) map[string]any {
pushURL, _ := createCockpitPushURL(ds.Type, ds.URL)

return map[string]any{
"id": regional.NewIDString(ds.Region, ds.ID),
"project_id": ds.ProjectID,
"name": ds.Name,
"url": ds.URL,
"type": ds.Type.String(),
"origin": ds.Origin.String(),
"created_at": types.FlattenTime(ds.CreatedAt),
"updated_at": types.FlattenTime(ds.UpdatedAt),
"synchronized_with_grafana": ds.SynchronizedWithGrafana,
"retention_days": int(ds.RetentionDays),
"region": ds.Region.String(),
"push_url": pushURL,
}
}
Loading
Loading