Skip to content

Commit 7349d5e

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

7 files changed

+1615
-2190
lines changed

internal/services/cockpit/source.go

+36-3
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
@@ -93,6 +93,9 @@ func ResourceCockpitSourceCreate(ctx context.Context, d *schema.ResourceData, me
9393
}
9494

9595
retentionDays := uint32(d.Get("retention_days").(int))
96+
if retentionDays == 0 {
97+
retentionDays = 6
98+
}
9699

97100
res, err := api.CreateDataSource(&cockpit.RegionalAPICreateDataSourceRequest{
98101
Region: region,
@@ -101,7 +104,6 @@ func ResourceCockpitSourceCreate(ctx context.Context, d *schema.ResourceData, me
101104
Type: cockpit.DataSourceType(d.Get("type").(string)),
102105
RetentionDays: &retentionDays,
103106
}, scw.WithContext(ctx))
104-
105107
if err != nil {
106108
return diag.FromErr(err)
107109
}
@@ -143,7 +145,38 @@ func ResourceCockpitSourceRead(ctx context.Context, d *schema.ResourceData, meta
143145
_ = d.Set("updated_at", types.FlattenTime(res.UpdatedAt))
144146
_ = d.Set("project_id", res.ProjectID)
145147
_ = d.Set("push_url", pushURL)
146-
_ = d.Set("retention_days", res.RetentionDays)
148+
_ = d.Set("retention_days", int(res.RetentionDays))
149+
150+
return nil
151+
}
152+
153+
func ResourceCockpitSourceUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
154+
api, region, id, err := NewAPIWithRegionAndID(meta, d.Id())
155+
if err != nil {
156+
return diag.FromErr(err)
157+
}
158+
159+
updateRequest := &cockpit.RegionalAPIUpdateDataSourceRequest{
160+
DataSourceID: id,
161+
Region: region,
162+
}
163+
164+
if d.HasChange("name") {
165+
name := d.Get("name").(string)
166+
updateRequest.Name = &name
167+
}
168+
169+
if d.HasChange("retention_days") {
170+
retentionDays := uint32(d.Get("retention_days").(int))
171+
updateRequest.RetentionDays = &retentionDays
172+
}
173+
174+
if updateRequest.Name != nil || updateRequest.RetentionDays != nil {
175+
_, err = api.UpdateDataSource(updateRequest, scw.WithContext(ctx))
176+
if err != nil {
177+
return diag.FromErr(err)
178+
}
179+
}
147180

148181
return nil
149182
}

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)