Skip to content

Commit f0e4b54

Browse files
committed
feat: add Cockpit TriggerTestAlert action
1 parent 72cb51a commit f0e4b54

File tree

3 files changed

+39
-16
lines changed

3 files changed

+39
-16
lines changed

internal/services/cockpit/action_trigger_test_alert_action.go

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ var (
1919

2020
type TriggerTestAlertAction struct {
2121
regionalAPI *cockpit.RegionalAPI
22+
meta *meta.Meta
2223
}
2324

2425
func (a *TriggerTestAlertAction) Configure(ctx context.Context, req action.ConfigureRequest, resp *action.ConfigureResponse) {
@@ -38,6 +39,7 @@ func (a *TriggerTestAlertAction) Configure(ctx context.Context, req action.Confi
3839

3940
client := m.ScwClient()
4041
a.regionalAPI = cockpit.NewRegionalAPI(client)
42+
a.meta = m
4143
}
4244

4345
func (a *TriggerTestAlertAction) Metadata(ctx context.Context, req action.MetadataRequest, resp *action.MetadataResponse) {
@@ -61,8 +63,8 @@ func (a *TriggerTestAlertAction) Schema(ctx context.Context, req action.SchemaRe
6163
Description: "ID of the Project",
6264
},
6365
"region": schema.StringAttribute{
64-
Required: true,
65-
Description: "Region to target",
66+
Optional: true,
67+
Description: "Region to target. If not provided, will use the default region from the provider configuration",
6668
},
6769
},
6870
}
@@ -96,18 +98,27 @@ func (a *TriggerTestAlertAction) Invoke(ctx context.Context, req action.InvokeRe
9698
return
9799
}
98100

99-
if data.Region.IsNull() || data.Region.ValueString() == "" {
100-
resp.Diagnostics.AddError(
101-
"Missing region",
102-
"The region attribute is required to trigger a test alert.",
103-
)
104-
105-
return
101+
var region scw.Region
102+
if !data.Region.IsNull() && data.Region.ValueString() != "" {
103+
region = scw.Region(data.Region.ValueString())
104+
} else {
105+
// Use default region from provider configuration
106+
defaultRegion, exists := a.meta.ScwClient().GetDefaultRegion()
107+
if !exists {
108+
resp.Diagnostics.AddError(
109+
"Missing region",
110+
"The region attribute is required to trigger a test alert. Please provide it explicitly or configure a default region in the provider.",
111+
)
112+
113+
return
114+
}
115+
116+
region = defaultRegion
106117
}
107118

108119
err := a.regionalAPI.TriggerTestAlert(&cockpit.RegionalAPITriggerTestAlertRequest{
109120
ProjectID: data.ProjectID.ValueString(),
110-
Region: scw.Region(data.Region.ValueString()),
121+
Region: region,
111122
}, scw.WithContext(ctx))
112123

113124
if err != nil {
@@ -119,4 +130,3 @@ func (a *TriggerTestAlertAction) Invoke(ctx context.Context, req action.InvokeRe
119130
return
120131
}
121132
}
122-

internal/services/cockpit/action_trigger_test_alert_action_test.go

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,27 @@ func TestAccActionCockpitTriggerTestAlert_Basic(t *testing.T) {
2929
3030
resource "scaleway_cockpit_alert_manager" "main" {
3131
project_id = scaleway_account_project.project.id
32+
}
33+
34+
resource "scaleway_cockpit_source" "metrics" {
35+
project_id = scaleway_account_project.project.id
36+
name = "test-metrics-source"
37+
type = "metrics"
38+
retention_days = 31
3239
3340
lifecycle {
3441
action_trigger {
3542
events = [after_create]
3643
actions = [action.scaleway_cockpit_trigger_test_alert_action.main]
3744
}
3845
}
46+
47+
depends_on = [scaleway_cockpit_alert_manager.main]
3948
}
4049
4150
action "scaleway_cockpit_trigger_test_alert_action" "main" {
4251
config {
4352
project_id = scaleway_account_project.project.id
44-
region = scaleway_cockpit_alert_manager.main.region
4553
}
4654
}
4755
`,
@@ -54,19 +62,27 @@ func TestAccActionCockpitTriggerTestAlert_Basic(t *testing.T) {
5462
5563
resource "scaleway_cockpit_alert_manager" "main" {
5664
project_id = scaleway_account_project.project.id
65+
}
66+
67+
resource "scaleway_cockpit_source" "metrics" {
68+
project_id = scaleway_account_project.project.id
69+
name = "test-metrics-source"
70+
type = "metrics"
71+
retention_days = 31
5772
5873
lifecycle {
5974
action_trigger {
6075
events = [after_create]
6176
actions = [action.scaleway_cockpit_trigger_test_alert_action.main]
6277
}
6378
}
79+
80+
depends_on = [scaleway_cockpit_alert_manager.main]
6481
}
6582
6683
action "scaleway_cockpit_trigger_test_alert_action" "main" {
6784
config {
6885
project_id = scaleway_account_project.project.id
69-
region = scaleway_cockpit_alert_manager.main.region
7086
}
7187
}
7288
@@ -100,4 +116,3 @@ func TestAccActionCockpitTriggerTestAlert_Basic(t *testing.T) {
100116
},
101117
})
102118
}
103-

provider/framework.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,6 @@ func (p *ScalewayProvider) Actions(_ context.Context) []func() action.Action {
140140

141141
res = append(res, instance.NewServerAction)
142142
res = append(res, cockpit.NewTriggerTestAlertAction)
143-
res = append(res, cockpit.NewResetGrafanaUserPasswordAction)
144-
res = append(res, cockpit.NewSyncGrafanaDataSourcesAction)
145143

146144
return res
147145
}

0 commit comments

Comments
 (0)