Skip to content

Commit 588a0e7

Browse files
committed
feat(cockpit): add update in source
1 parent 9ef8485 commit 588a0e7

5 files changed

+1392
-116
lines changed

internal/services/cockpit/source.go

+28-2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ func ResourceCockpitSource() *schema.Resource {
1818
return &schema.Resource{
1919
CreateContext: ResourceCockpitSourceCreate,
2020
ReadContext: ResourceCockpitSourceRead,
21+
UpdateContext: ResourceCockpitSourceUpdate,
2122
DeleteContext: ResourceCockpitSourceDelete,
2223
Timeouts: &schema.ResourceTimeout{
2324
Create: schema.DefaultTimeout(DefaultCockpitTimeout),
@@ -46,7 +47,6 @@ func ResourceCockpitSource() *schema.Resource {
4647
Type: schema.TypeInt,
4748
Optional: true,
4849
Default: 6,
49-
ForceNew: true,
5050
Description: "The number of days to retain data, must be between 1 and 365.",
5151
},
5252
// computed
@@ -101,7 +101,6 @@ func ResourceCockpitSourceCreate(ctx context.Context, d *schema.ResourceData, me
101101
Type: cockpit.DataSourceType(d.Get("type").(string)),
102102
RetentionDays: &retentionDays,
103103
}, scw.WithContext(ctx))
104-
105104
if err != nil {
106105
return diag.FromErr(err)
107106
}
@@ -148,6 +147,33 @@ func ResourceCockpitSourceRead(ctx context.Context, d *schema.ResourceData, meta
148147
return nil
149148
}
150149

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+
name := d.Get("name").(string)
157+
updateRequest := &cockpit.RegionalAPIUpdateDataSourceRequest{
158+
DataSourceID: id,
159+
Region: region,
160+
Name: &name,
161+
}
162+
163+
if d.HasChange("retention_days") {
164+
retentionDays := uint32(d.Get("retention_days").(int))
165+
updateRequest.RetentionDays = &retentionDays
166+
}
167+
168+
if d.HasChange("name") || d.HasChange("retention_days") {
169+
_, err = api.UpdateDataSource(updateRequest, scw.WithContext(ctx))
170+
if err != nil {
171+
return diag.FromErr(err)
172+
}
173+
}
174+
return nil
175+
}
176+
151177
func ResourceCockpitSourceDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
152178
api, region, id, err := NewAPIWithRegionAndID(meta, d.Id())
153179
if err != nil {

internal/services/cockpit/source_test.go

+69
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,75 @@ func TestAccCockpitSource_retention_days(t *testing.T) {
133133
})
134134
}
135135

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

0 commit comments

Comments
 (0)