Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cockpit): add retention days in resource source #2846

Merged
merged 5 commits into from
Dec 12, 2024
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
2 changes: 2 additions & 0 deletions docs/resources/cockpit_source.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ resource "scaleway_cockpit_source" "main" {
project_id = scaleway_account_project.project.id
name = "my-data-source"
type = "metrics"
retention_days = 6
}
```

Expand All @@ -35,6 +36,7 @@ This section lists the arguments that are supported:
- `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`.
- `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.
- `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.
- `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.

## Attributes Reference

Expand Down
3 changes: 3 additions & 0 deletions internal/services/cockpit/cockpit_data_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,21 @@ func TestAccDataSourceCockpit_Basic(t *testing.T) {
project_id = scaleway_account_project.project.id
name = "my-data-source-metrics"
type = "metrics"
retention_days = 31
}

resource "scaleway_cockpit_source" "logs" {
project_id = scaleway_account_project.project.id
name = "my-data-source-logs"
type = "logs"
retention_days = 7
}

resource "scaleway_cockpit_source" "traces" {
project_id = scaleway_account_project.project.id
name = "my-data-source-traces"
type = "traces"
retention_days = 7
}

resource "scaleway_cockpit_alert_manager" "alert_manager" {
Expand Down
50 changes: 46 additions & 4 deletions internal/services/cockpit/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/scaleway/scaleway-sdk-go/api/cockpit/v1"
"github.com/scaleway/scaleway-sdk-go/scw"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/httperrors"
Expand All @@ -18,6 +19,7 @@ func ResourceCockpitSource() *schema.Resource {
return &schema.Resource{
CreateContext: ResourceCockpitSourceCreate,
ReadContext: ResourceCockpitSourceRead,
UpdateContext: ResourceCockpitSourceUpdate,
DeleteContext: ResourceCockpitSourceDelete,
Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(DefaultCockpitTimeout),
Expand All @@ -42,6 +44,12 @@ func ResourceCockpitSource() *schema.Resource {
Description: "The type of the datasource",
ValidateDiagFunc: verify.ValidateEnum[cockpit.DataSourceType](),
},
"retention_days": {
Type: schema.TypeInt,
Required: true,
ValidateFunc: validation.IntBetween(1, 365),
Description: "The number of days to retain data, must be between 1 and 365.",
},
// computed
"url": {
Type: schema.TypeString,
Expand Down Expand Up @@ -85,11 +93,13 @@ func ResourceCockpitSourceCreate(ctx context.Context, d *schema.ResourceData, me
return diag.FromErr(err)
}

retentionDays := uint32(d.Get("retention_days").(int))
res, err := api.CreateDataSource(&cockpit.RegionalAPICreateDataSourceRequest{
Region: region,
ProjectID: d.Get("project_id").(string),
Name: d.Get("name").(string),
Type: cockpit.DataSourceType(d.Get("type").(string)),
Region: region,
ProjectID: d.Get("project_id").(string),
Name: d.Get("name").(string),
Type: cockpit.DataSourceType(d.Get("type").(string)),
RetentionDays: &retentionDays,
}, scw.WithContext(ctx))
if err != nil {
return diag.FromErr(err)
Expand Down Expand Up @@ -132,6 +142,38 @@ func ResourceCockpitSourceRead(ctx context.Context, d *schema.ResourceData, meta
_ = d.Set("updated_at", types.FlattenTime(res.UpdatedAt))
_ = d.Set("project_id", res.ProjectID)
_ = d.Set("push_url", pushURL)
_ = d.Set("retention_days", int(res.RetentionDays))

return nil
}

func ResourceCockpitSourceUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
api, region, id, err := NewAPIWithRegionAndID(meta, d.Id())
if err != nil {
return diag.FromErr(err)
}

updateRequest := &cockpit.RegionalAPIUpdateDataSourceRequest{
DataSourceID: id,
Region: region,
}

if d.HasChange("name") {
name := d.Get("name").(string)
updateRequest.Name = &name
}

if d.HasChange("retention_days") {
retentionDays := uint32(d.Get("retention_days").(int))
updateRequest.RetentionDays = &retentionDays
}

if d.HasChanges("retention_days", "name") {
_, err = api.UpdateDataSource(updateRequest, scw.WithContext(ctx))
if err != nil {
return diag.FromErr(err)
}
}

return nil
}
Expand Down
115 changes: 115 additions & 0 deletions internal/services/cockpit/source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,15 @@ func TestAccCockpitSource_Basic_metrics(t *testing.T) {
project_id = scaleway_account_project.project.id
name = "my-source"
type = "metrics"
retention_days = 31
}
`,
Check: resource.ComposeTestCheckFunc(
isSourcePresent(tt, "scaleway_cockpit_source.main"),
resource.TestCheckResourceAttr("scaleway_cockpit_source.main", "name", "my-source"),
resource.TestCheckResourceAttr("scaleway_cockpit_source.main", "type", "metrics"),
resource.TestCheckResourceAttr("scaleway_cockpit_source.main", "region", "fr-par"),
resource.TestCheckResourceAttr("scaleway_cockpit_source.main", "retention_days", "31"),
resource.TestCheckResourceAttrSet("scaleway_cockpit_source.main", "url"),
resource.TestCheckResourceAttrSet("scaleway_cockpit_source.main", "push_url"),
resource.TestCheckResourceAttrSet("scaleway_cockpit_source.main", "origin"),
Expand Down Expand Up @@ -70,13 +72,57 @@ func TestAccCockpitSource_Basic_logs(t *testing.T) {
project_id = scaleway_account_project.project.id
name = "my-source"
type = "logs"
retention_days = 31

}
`,
Check: resource.ComposeTestCheckFunc(
isSourcePresent(tt, "scaleway_cockpit_source.main"),
resource.TestCheckResourceAttr("scaleway_cockpit_source.main", "name", "my-source"),
resource.TestCheckResourceAttr("scaleway_cockpit_source.main", "type", "logs"),
resource.TestCheckResourceAttr("scaleway_cockpit_source.main", "region", "fr-par"),
resource.TestCheckResourceAttrSet("scaleway_cockpit_source.main", "url"),
resource.TestCheckResourceAttrSet("scaleway_cockpit_source.main", "push_url"),
resource.TestCheckResourceAttrSet("scaleway_cockpit_source.main", "origin"),
resource.TestCheckResourceAttrSet("scaleway_cockpit_source.main", "created_at"),
resource.TestCheckResourceAttrSet("scaleway_cockpit_source.main", "updated_at"),
resource.TestCheckResourceAttrSet("scaleway_cockpit_source.main", "synchronized_with_grafana"),
resource.TestCheckResourceAttrPair("scaleway_cockpit_source.main", "project_id", "scaleway_account_project.project", "id"),
),
},
},
})
}

func TestAccCockpitSource_retention_days(t *testing.T) {
tt := acctest.NewTestTools(t)
defer tt.Cleanup()

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(t) },
ProviderFactories: tt.ProviderFactories,
CheckDestroy: isSourceDestroyed(tt),
Steps: []resource.TestStep{
{
Config: `
resource "scaleway_account_project" "project" {
name = "tf_tests_cockpit_datasource_basic"
}

resource "scaleway_cockpit_source" "main" {
project_id = scaleway_account_project.project.id
name = "my-source"
type = "logs"
retention_days = 13

}
`,
Check: resource.ComposeTestCheckFunc(
isSourcePresent(tt, "scaleway_cockpit_source.main"),
resource.TestCheckResourceAttr("scaleway_cockpit_source.main", "name", "my-source"),
resource.TestCheckResourceAttr("scaleway_cockpit_source.main", "type", "logs"),
resource.TestCheckResourceAttr("scaleway_cockpit_source.main", "region", "fr-par"),
resource.TestCheckResourceAttr("scaleway_cockpit_source.main", "retention_days", "13"),
resource.TestCheckResourceAttrSet("scaleway_cockpit_source.main", "url"),
resource.TestCheckResourceAttrSet("scaleway_cockpit_source.main", "push_url"),
resource.TestCheckResourceAttrSet("scaleway_cockpit_source.main", "origin"),
Expand All @@ -90,6 +136,75 @@ func TestAccCockpitSource_Basic_logs(t *testing.T) {
})
}

func TestAccCockpitSource_Update(t *testing.T) {
tt := acctest.NewTestTools(t)
defer tt.Cleanup()

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(t) },
ProviderFactories: tt.ProviderFactories,
CheckDestroy: isSourceDestroyed(tt),
Steps: []resource.TestStep{
// Initial creation
{
Config: `
resource "scaleway_account_project" "project" {
name = "tf_tests_cockpit_source_update"
}

resource "scaleway_cockpit_source" "main" {
project_id = scaleway_account_project.project.id
name = "initial-name"
type = "logs"
retention_days = 10
}
`,
Check: resource.ComposeTestCheckFunc(
isSourcePresent(tt, "scaleway_cockpit_source.main"),
resource.TestCheckResourceAttr("scaleway_cockpit_source.main", "name", "initial-name"),
resource.TestCheckResourceAttr("scaleway_cockpit_source.main", "retention_days", "10"),
),
},
{
Config: `
resource "scaleway_account_project" "project" {
name = "tf_tests_cockpit_source_update"
}

resource "scaleway_cockpit_source" "main" {
project_id = scaleway_account_project.project.id
name = "initial-name"
type = "logs"
retention_days = 20
}
`,
Check: resource.ComposeTestCheckFunc(
isSourcePresent(tt, "scaleway_cockpit_source.main"),
resource.TestCheckResourceAttr("scaleway_cockpit_source.main", "retention_days", "20"),
),
},
{
Config: `
resource "scaleway_account_project" "project" {
name = "tf_tests_cockpit_source_update"
}

resource "scaleway_cockpit_source" "main" {
project_id = scaleway_account_project.project.id
name = "updated-name"
type = "logs"
retention_days = 20
}
`,
Check: resource.ComposeTestCheckFunc(
isSourcePresent(tt, "scaleway_cockpit_source.main"),
resource.TestCheckResourceAttr("scaleway_cockpit_source.main", "name", "updated-name"),
),
},
},
})
}

func isSourcePresent(tt *acctest.TestTools, n string) resource.TestCheckFunc {
return func(state *terraform.State) error {
rs, ok := state.RootModule().Resources[n]
Expand Down
Loading
Loading