Skip to content

Commit 9ef8485

Browse files
committed
feat(cockpit): add retention days in resource source
1 parent 36a1f88 commit 9ef8485

File tree

5 files changed

+636
-86
lines changed

5 files changed

+636
-86
lines changed

docs/resources/cockpit_source.md

+2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ resource "scaleway_cockpit_source" "main" {
2424
project_id = scaleway_account_project.project.id
2525
name = "my-data-source"
2626
type = "metrics"
27+
retention_days = 6
2728
}
2829
```
2930

@@ -35,6 +36,7 @@ This section lists the arguments that are supported:
3536
- `type` - (Required) The [type](https://www.scaleway.com/en/docs/observability/cockpit/concepts/#data-types) of data source. Possible values are: `metrics`, `logs`, or `traces`.
3637
- `region` - (Defaults to the region specified in the [provider configuration](../index.md#region)) The [region](../guides/regions_and_zones.md#regions) where the data source is located.
3738
- `project_id` - (Defaults to the Project ID specified in the [provider configuration](../index.md#project_id)) The ID of the Project the data source is associated with.
39+
- `retention_days` - (Optional, Defaults to 6) The number of days to retain data in the data source. Must be a value between 1 and 365. Changes to this field will force the creation of a new resource.
3840

3941
## Attributes Reference
4042

internal/services/cockpit/source.go

+16-4
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,13 @@ func ResourceCockpitSource() *schema.Resource {
4242
Description: "The type of the datasource",
4343
ValidateDiagFunc: verify.ValidateEnum[cockpit.DataSourceType](),
4444
},
45+
"retention_days": {
46+
Type: schema.TypeInt,
47+
Optional: true,
48+
Default: 6,
49+
ForceNew: true,
50+
Description: "The number of days to retain data, must be between 1 and 365.",
51+
},
4552
// computed
4653
"url": {
4754
Type: schema.TypeString,
@@ -85,12 +92,16 @@ func ResourceCockpitSourceCreate(ctx context.Context, d *schema.ResourceData, me
8592
return diag.FromErr(err)
8693
}
8794

95+
retentionDays := uint32(d.Get("retention_days").(int))
96+
8897
res, err := api.CreateDataSource(&cockpit.RegionalAPICreateDataSourceRequest{
89-
Region: region,
90-
ProjectID: d.Get("project_id").(string),
91-
Name: d.Get("name").(string),
92-
Type: cockpit.DataSourceType(d.Get("type").(string)),
98+
Region: region,
99+
ProjectID: d.Get("project_id").(string),
100+
Name: d.Get("name").(string),
101+
Type: cockpit.DataSourceType(d.Get("type").(string)),
102+
RetentionDays: &retentionDays,
93103
}, scw.WithContext(ctx))
104+
94105
if err != nil {
95106
return diag.FromErr(err)
96107
}
@@ -132,6 +143,7 @@ func ResourceCockpitSourceRead(ctx context.Context, d *schema.ResourceData, meta
132143
_ = d.Set("updated_at", types.FlattenTime(res.UpdatedAt))
133144
_ = d.Set("project_id", res.ProjectID)
134145
_ = d.Set("push_url", pushURL)
146+
_ = d.Set("retention_days", res.RetentionDays)
135147

136148
return nil
137149
}

internal/services/cockpit/source_test.go

+43
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ func TestAccCockpitSource_Basic_metrics(t *testing.T) {
3838
resource.TestCheckResourceAttr("scaleway_cockpit_source.main", "name", "my-source"),
3939
resource.TestCheckResourceAttr("scaleway_cockpit_source.main", "type", "metrics"),
4040
resource.TestCheckResourceAttr("scaleway_cockpit_source.main", "region", "fr-par"),
41+
resource.TestCheckResourceAttr("scaleway_cockpit_source.main", "retention_days", "6"),
4142
resource.TestCheckResourceAttrSet("scaleway_cockpit_source.main", "url"),
4243
resource.TestCheckResourceAttrSet("scaleway_cockpit_source.main", "push_url"),
4344
resource.TestCheckResourceAttrSet("scaleway_cockpit_source.main", "origin"),
@@ -90,6 +91,48 @@ func TestAccCockpitSource_Basic_logs(t *testing.T) {
9091
})
9192
}
9293

94+
func TestAccCockpitSource_retention_days(t *testing.T) {
95+
tt := acctest.NewTestTools(t)
96+
defer tt.Cleanup()
97+
98+
resource.ParallelTest(t, resource.TestCase{
99+
PreCheck: func() { acctest.PreCheck(t) },
100+
ProviderFactories: tt.ProviderFactories,
101+
CheckDestroy: isSourceDestroyed(tt),
102+
Steps: []resource.TestStep{
103+
{
104+
Config: `
105+
resource "scaleway_account_project" "project" {
106+
name = "tf_tests_cockpit_datasource_basic"
107+
}
108+
109+
resource "scaleway_cockpit_source" "main" {
110+
project_id = scaleway_account_project.project.id
111+
name = "my-source"
112+
type = "logs"
113+
retention_days = 13
114+
115+
}
116+
`,
117+
Check: resource.ComposeTestCheckFunc(
118+
isSourcePresent(tt, "scaleway_cockpit_source.main"),
119+
resource.TestCheckResourceAttr("scaleway_cockpit_source.main", "name", "my-source"),
120+
resource.TestCheckResourceAttr("scaleway_cockpit_source.main", "type", "logs"),
121+
resource.TestCheckResourceAttr("scaleway_cockpit_source.main", "region", "fr-par"),
122+
resource.TestCheckResourceAttr("scaleway_cockpit_source.main", "retention_days", "13"),
123+
resource.TestCheckResourceAttrSet("scaleway_cockpit_source.main", "url"),
124+
resource.TestCheckResourceAttrSet("scaleway_cockpit_source.main", "push_url"),
125+
resource.TestCheckResourceAttrSet("scaleway_cockpit_source.main", "origin"),
126+
resource.TestCheckResourceAttrSet("scaleway_cockpit_source.main", "created_at"),
127+
resource.TestCheckResourceAttrSet("scaleway_cockpit_source.main", "updated_at"),
128+
resource.TestCheckResourceAttrSet("scaleway_cockpit_source.main", "synchronized_with_grafana"),
129+
resource.TestCheckResourceAttrPair("scaleway_cockpit_source.main", "project_id", "scaleway_account_project.project", "id"),
130+
),
131+
},
132+
},
133+
})
134+
}
135+
93136
func isSourcePresent(tt *acctest.TestTools, n string) resource.TestCheckFunc {
94137
return func(state *terraform.State) error {
95138
rs, ok := state.RootModule().Resources[n]

0 commit comments

Comments
 (0)