Skip to content

Commit d325307

Browse files
committed
feat(cockpit): add retention days in resource source (#2846)
* feat(cockpit): add retention days in resource source * feat(cockpit): add update in source * feat(cockpit): required for retention_days * feat(cockpit): add validate func for retention days * feat(cockpit): refacto update source
1 parent 6e06215 commit d325307

9 files changed

+2412
-576
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/cockpit_data_source_test.go

+3
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,21 @@ func TestAccDataSourceCockpit_Basic(t *testing.T) {
2525
project_id = scaleway_account_project.project.id
2626
name = "my-data-source-metrics"
2727
type = "metrics"
28+
retention_days = 31
2829
}
2930
3031
resource "scaleway_cockpit_source" "logs" {
3132
project_id = scaleway_account_project.project.id
3233
name = "my-data-source-logs"
3334
type = "logs"
35+
retention_days = 7
3436
}
3537
3638
resource "scaleway_cockpit_source" "traces" {
3739
project_id = scaleway_account_project.project.id
3840
name = "my-data-source-traces"
3941
type = "traces"
42+
retention_days = 7
4043
}
4144
4245
resource "scaleway_cockpit_alert_manager" "alert_manager" {

internal/services/cockpit/source.go

+46-4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ 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"
89
"github.com/scaleway/scaleway-sdk-go/api/cockpit/v1"
910
"github.com/scaleway/scaleway-sdk-go/scw"
1011
"github.com/scaleway/terraform-provider-scaleway/v2/internal/httperrors"
@@ -18,6 +19,7 @@ func ResourceCockpitSource() *schema.Resource {
1819
return &schema.Resource{
1920
CreateContext: ResourceCockpitSourceCreate,
2021
ReadContext: ResourceCockpitSourceRead,
22+
UpdateContext: ResourceCockpitSourceUpdate,
2123
DeleteContext: ResourceCockpitSourceDelete,
2224
Timeouts: &schema.ResourceTimeout{
2325
Create: schema.DefaultTimeout(DefaultCockpitTimeout),
@@ -42,6 +44,12 @@ func ResourceCockpitSource() *schema.Resource {
4244
Description: "The type of the datasource",
4345
ValidateDiagFunc: verify.ValidateEnum[cockpit.DataSourceType](),
4446
},
47+
"retention_days": {
48+
Type: schema.TypeInt,
49+
Required: true,
50+
ValidateFunc: validation.IntBetween(1, 365),
51+
Description: "The number of days to retain data, must be between 1 and 365.",
52+
},
4553
// computed
4654
"url": {
4755
Type: schema.TypeString,
@@ -85,11 +93,13 @@ func ResourceCockpitSourceCreate(ctx context.Context, d *schema.ResourceData, me
8593
return diag.FromErr(err)
8694
}
8795

96+
retentionDays := uint32(d.Get("retention_days").(int))
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))
94104
if err != nil {
95105
return diag.FromErr(err)
@@ -132,6 +142,38 @@ func ResourceCockpitSourceRead(ctx context.Context, d *schema.ResourceData, meta
132142
_ = d.Set("updated_at", types.FlattenTime(res.UpdatedAt))
133143
_ = d.Set("project_id", res.ProjectID)
134144
_ = d.Set("push_url", pushURL)
145+
_ = d.Set("retention_days", int(res.RetentionDays))
146+
147+
return nil
148+
}
149+
150+
func ResourceCockpitSourceUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
151+
api, region, id, err := NewAPIWithRegionAndID(meta, d.Id())
152+
if err != nil {
153+
return diag.FromErr(err)
154+
}
155+
156+
updateRequest := &cockpit.RegionalAPIUpdateDataSourceRequest{
157+
DataSourceID: id,
158+
Region: region,
159+
}
160+
161+
if d.HasChange("name") {
162+
name := d.Get("name").(string)
163+
updateRequest.Name = &name
164+
}
165+
166+
if d.HasChange("retention_days") {
167+
retentionDays := uint32(d.Get("retention_days").(int))
168+
updateRequest.RetentionDays = &retentionDays
169+
}
170+
171+
if d.HasChanges("retention_days", "name") {
172+
_, err = api.UpdateDataSource(updateRequest, scw.WithContext(ctx))
173+
if err != nil {
174+
return diag.FromErr(err)
175+
}
176+
}
135177

136178
return nil
137179
}

internal/services/cockpit/source_test.go

+115
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,15 @@ func TestAccCockpitSource_Basic_metrics(t *testing.T) {
3131
project_id = scaleway_account_project.project.id
3232
name = "my-source"
3333
type = "metrics"
34+
retention_days = 31
3435
}
3536
`,
3637
Check: resource.ComposeTestCheckFunc(
3738
isSourcePresent(tt, "scaleway_cockpit_source.main"),
3839
resource.TestCheckResourceAttr("scaleway_cockpit_source.main", "name", "my-source"),
3940
resource.TestCheckResourceAttr("scaleway_cockpit_source.main", "type", "metrics"),
4041
resource.TestCheckResourceAttr("scaleway_cockpit_source.main", "region", "fr-par"),
42+
resource.TestCheckResourceAttr("scaleway_cockpit_source.main", "retention_days", "31"),
4143
resource.TestCheckResourceAttrSet("scaleway_cockpit_source.main", "url"),
4244
resource.TestCheckResourceAttrSet("scaleway_cockpit_source.main", "push_url"),
4345
resource.TestCheckResourceAttrSet("scaleway_cockpit_source.main", "origin"),
@@ -70,13 +72,57 @@ func TestAccCockpitSource_Basic_logs(t *testing.T) {
7072
project_id = scaleway_account_project.project.id
7173
name = "my-source"
7274
type = "logs"
75+
retention_days = 31
76+
77+
}
78+
`,
79+
Check: resource.ComposeTestCheckFunc(
80+
isSourcePresent(tt, "scaleway_cockpit_source.main"),
81+
resource.TestCheckResourceAttr("scaleway_cockpit_source.main", "name", "my-source"),
82+
resource.TestCheckResourceAttr("scaleway_cockpit_source.main", "type", "logs"),
83+
resource.TestCheckResourceAttr("scaleway_cockpit_source.main", "region", "fr-par"),
84+
resource.TestCheckResourceAttrSet("scaleway_cockpit_source.main", "url"),
85+
resource.TestCheckResourceAttrSet("scaleway_cockpit_source.main", "push_url"),
86+
resource.TestCheckResourceAttrSet("scaleway_cockpit_source.main", "origin"),
87+
resource.TestCheckResourceAttrSet("scaleway_cockpit_source.main", "created_at"),
88+
resource.TestCheckResourceAttrSet("scaleway_cockpit_source.main", "updated_at"),
89+
resource.TestCheckResourceAttrSet("scaleway_cockpit_source.main", "synchronized_with_grafana"),
90+
resource.TestCheckResourceAttrPair("scaleway_cockpit_source.main", "project_id", "scaleway_account_project.project", "id"),
91+
),
92+
},
93+
},
94+
})
95+
}
96+
97+
func TestAccCockpitSource_retention_days(t *testing.T) {
98+
tt := acctest.NewTestTools(t)
99+
defer tt.Cleanup()
100+
101+
resource.ParallelTest(t, resource.TestCase{
102+
PreCheck: func() { acctest.PreCheck(t) },
103+
ProviderFactories: tt.ProviderFactories,
104+
CheckDestroy: isSourceDestroyed(tt),
105+
Steps: []resource.TestStep{
106+
{
107+
Config: `
108+
resource "scaleway_account_project" "project" {
109+
name = "tf_tests_cockpit_datasource_basic"
110+
}
111+
112+
resource "scaleway_cockpit_source" "main" {
113+
project_id = scaleway_account_project.project.id
114+
name = "my-source"
115+
type = "logs"
116+
retention_days = 13
117+
73118
}
74119
`,
75120
Check: resource.ComposeTestCheckFunc(
76121
isSourcePresent(tt, "scaleway_cockpit_source.main"),
77122
resource.TestCheckResourceAttr("scaleway_cockpit_source.main", "name", "my-source"),
78123
resource.TestCheckResourceAttr("scaleway_cockpit_source.main", "type", "logs"),
79124
resource.TestCheckResourceAttr("scaleway_cockpit_source.main", "region", "fr-par"),
125+
resource.TestCheckResourceAttr("scaleway_cockpit_source.main", "retention_days", "13"),
80126
resource.TestCheckResourceAttrSet("scaleway_cockpit_source.main", "url"),
81127
resource.TestCheckResourceAttrSet("scaleway_cockpit_source.main", "push_url"),
82128
resource.TestCheckResourceAttrSet("scaleway_cockpit_source.main", "origin"),
@@ -90,6 +136,75 @@ func TestAccCockpitSource_Basic_logs(t *testing.T) {
90136
})
91137
}
92138

139+
func TestAccCockpitSource_Update(t *testing.T) {
140+
tt := acctest.NewTestTools(t)
141+
defer tt.Cleanup()
142+
143+
resource.ParallelTest(t, resource.TestCase{
144+
PreCheck: func() { acctest.PreCheck(t) },
145+
ProviderFactories: tt.ProviderFactories,
146+
CheckDestroy: isSourceDestroyed(tt),
147+
Steps: []resource.TestStep{
148+
// Initial creation
149+
{
150+
Config: `
151+
resource "scaleway_account_project" "project" {
152+
name = "tf_tests_cockpit_source_update"
153+
}
154+
155+
resource "scaleway_cockpit_source" "main" {
156+
project_id = scaleway_account_project.project.id
157+
name = "initial-name"
158+
type = "logs"
159+
retention_days = 10
160+
}
161+
`,
162+
Check: resource.ComposeTestCheckFunc(
163+
isSourcePresent(tt, "scaleway_cockpit_source.main"),
164+
resource.TestCheckResourceAttr("scaleway_cockpit_source.main", "name", "initial-name"),
165+
resource.TestCheckResourceAttr("scaleway_cockpit_source.main", "retention_days", "10"),
166+
),
167+
},
168+
{
169+
Config: `
170+
resource "scaleway_account_project" "project" {
171+
name = "tf_tests_cockpit_source_update"
172+
}
173+
174+
resource "scaleway_cockpit_source" "main" {
175+
project_id = scaleway_account_project.project.id
176+
name = "initial-name"
177+
type = "logs"
178+
retention_days = 20
179+
}
180+
`,
181+
Check: resource.ComposeTestCheckFunc(
182+
isSourcePresent(tt, "scaleway_cockpit_source.main"),
183+
resource.TestCheckResourceAttr("scaleway_cockpit_source.main", "retention_days", "20"),
184+
),
185+
},
186+
{
187+
Config: `
188+
resource "scaleway_account_project" "project" {
189+
name = "tf_tests_cockpit_source_update"
190+
}
191+
192+
resource "scaleway_cockpit_source" "main" {
193+
project_id = scaleway_account_project.project.id
194+
name = "updated-name"
195+
type = "logs"
196+
retention_days = 20
197+
}
198+
`,
199+
Check: resource.ComposeTestCheckFunc(
200+
isSourcePresent(tt, "scaleway_cockpit_source.main"),
201+
resource.TestCheckResourceAttr("scaleway_cockpit_source.main", "name", "updated-name"),
202+
),
203+
},
204+
},
205+
})
206+
}
207+
93208
func isSourcePresent(tt *acctest.TestTools, n string) resource.TestCheckFunc {
94209
return func(state *terraform.State) error {
95210
rs, ok := state.RootModule().Resources[n]

0 commit comments

Comments
 (0)