Skip to content

Commit

Permalink
[Feat.] added possibility to trigger alarm immediately in `openteleko…
Browse files Browse the repository at this point in the history
…mcloud_ces_alarmrule` (#2818)

[Feat.] added possibility to trigger alarm immediately in `opentelekomcloud_ces_alarmrule`

Summary of the Pull Request
To trigger an event immediately, simply set the "period" parameter to 0 in your API request.
Example API request to create an alarm rule with immediate triggering:
URL: https://ces.eu-de.otc.t-systems.com/V1.0/bdcc63e2876b454bb0f625c9153d3a3e/alarms
Body:
{
"alarm_name": "alarm-APIMON",
"metric": {
"namespace": "SYS.ECS",
"metric_name": "deleteServer",
"dimensions": []
},
"condition": {
"comparison_operator": ">=",
"count": 1,
"filter": "average",
"period": 0,
"unit": "count",
"value": 1
},
"alarm_enabled": true,
"alarm_action_enabled": false,
"alarm_level": 2,
"alarm_type": "EVENT.SYS"
}
Related documentation:
Creating an Alarm Rule
Please note that this information is currently missing from the documentation but will be included in the next update.
PR Checklist

 Refers to: #2817
 Tests added/passed.
 Documentation updated.
 Schema updated.
 Release notes added.

Acceptance Steps Performed
=== RUN   TestCESAlarmRule_basic
=== PAUSE TestCESAlarmRule_basic
=== CONT  TestCESAlarmRule_basic
--- PASS: TestCESAlarmRule_basic (171.22s)
=== RUN   TestCESAlarmRule_systemEvents
=== PAUSE TestCESAlarmRule_systemEvents
=== CONT  TestCESAlarmRule_systemEvents
--- PASS: TestCESAlarmRule_systemEvents (39.18s)
=== RUN   TestAccCheckCESV1AlarmValidation
--- PASS: TestAccCheckCESV1AlarmValidation (5.69s)
=== RUN   TestCESAlarmRule_slashes
=== PAUSE TestCESAlarmRule_slashes
=== CONT  TestCESAlarmRule_slashes
--- PASS: TestCESAlarmRule_slashes (86.65s)
PASS

Process finished with exit code 0

Reviewed-by: Artem Lifshits
Reviewed-by: Aloento
Reviewed-by: Muneeb H. Jan <[email protected]>
  • Loading branch information
anton-sidelnikov authored Feb 10, 2025
1 parent 3329063 commit e9a2b5f
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 84 deletions.
4 changes: 3 additions & 1 deletion docs/resources/ces_alarmrule.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,11 +153,13 @@ The `dimensions` block supports:
The `condition` block supports:

* `period` - (Required) Specifies the alarm checking period in seconds. The
value can be `1`, `300`, `1200`, `3600`, `14400`, and `86400`.
value can be `0`, `1`, `300`, `1200`, `3600`, `14400`, and `86400`.

-> If `period` is set to `1`, the raw metric data is used to determine
whether to generate an alarm.

-> To trigger an event immediately, simply set the `period` parameter to 0

* `filter` - (Required) Specifies the data rollup methods. The value can be
`max`, `min`, `average`, `sum`, and `variance`.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
golangsdk "github.com/opentelekomcloud/gophertelekomcloud"
"github.com/opentelekomcloud/gophertelekomcloud/openstack/ces/v1/alarms"
"github.com/opentelekomcloud/terraform-provider-opentelekomcloud/opentelekomcloud/acceptance/common/quotas"
ecs "github.com/opentelekomcloud/terraform-provider-opentelekomcloud/opentelekomcloud/acceptance/ecs"
Expand All @@ -18,8 +19,29 @@ import (

const resourceAlarmRuleName = "opentelekomcloud_ces_alarmrule.alarmrule_1"

func getAlarmRuleFunc(conf *cfg.Config, state *terraform.ResourceState) (interface{}, error) {
c, err := conf.CesV1Client(env.OS_REGION_NAME)
if err != nil {
return nil, fmt.Errorf("error creating OpenTelekomCloud APIG v2 client: %s", err)
}
a, err := alarms.ShowAlarm(c, state.Primary.ID)
if len(a) < 1 {
return nil, golangsdk.ErrDefault404{}
}
return a[0], err
}

func TestCESAlarmRule_basic(t *testing.T) {
var ar alarms.MetricAlarms
var (
ar alarms.MetricAlarms
rName = resourceAlarmRuleName
)

rc := common.InitResourceCheck(
rName,
&ar,
getAlarmRuleFunc,
)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() {
Expand All @@ -33,24 +55,40 @@ func TestCESAlarmRule_basic(t *testing.T) {
quotas.BookMany(t, qts)
},
ProviderFactories: common.TestAccProviderFactories,
CheckDestroy: testCESAlarmRuleDestroy,
CheckDestroy: rc.CheckResourceDestroy(),
Steps: []resource.TestStep{
{
Config: testCESAlarmRuleBasic,
Check: resource.ComposeTestCheckFunc(
testCESAlarmRuleExists(resourceAlarmRuleName, &ar),
rc.CheckResourceExists(),
),
},
{
Config: testCESAlarmRuleUpdate,
Check: resource.ComposeTestCheckFunc(),
Check: resource.ComposeTestCheckFunc(
rc.CheckResourceExists(),
),
},
{
ResourceName: resourceAlarmRuleName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func TestCESAlarmRule_systemEvents(t *testing.T) {
var ar alarms.MetricAlarms
var (
ar alarms.MetricAlarms
rName = resourceAlarmRuleName
)

rc := common.InitResourceCheck(
rName,
&ar,
getAlarmRuleFunc,
)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() {
Expand All @@ -64,41 +102,23 @@ func TestCESAlarmRule_systemEvents(t *testing.T) {
quotas.BookMany(t, qts)
},
ProviderFactories: common.TestAccProviderFactories,
CheckDestroy: testCESAlarmRuleDestroy,
CheckDestroy: rc.CheckResourceDestroy(),
Steps: []resource.TestStep{
{
Config: testCESAlarmRuleSystemEvents,
Check: resource.ComposeTestCheckFunc(
testCESAlarmRuleExists(resourceAlarmRuleName, &ar),
rc.CheckResourceExists(),
resource.TestCheckResourceAttr(resourceAlarmRuleName, "alarm_type", "EVENT.SYS"),
resource.TestCheckResourceAttr(resourceAlarmRuleName, "metric.0.namespace", "SYS.CBR"),
resource.TestCheckResourceAttr(resourceAlarmRuleName, "metric.0.metric_name", "backupFailed"),
resource.TestCheckResourceAttr(resourceAlarmRuleName, "condition.0.alarm_frequency", "300"),
resource.TestCheckResourceAttr(resourceAlarmRuleName, "condition.0.period", "0"),
),
},
},
})
}

func TestAccCESAlarmRules_importBasic(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { common.TestAccPreCheck(t) },
ProviderFactories: common.TestAccProviderFactories,
CheckDestroy: testCESAlarmRuleDestroy,
Steps: []resource.TestStep{
{
Config: testCESAlarmRuleBasic,
},

{
ResourceName: resourceAlarmRuleName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func TestAccCheckCESV1AlarmValidation(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { common.TestAccPreCheck(t) },
Expand All @@ -113,8 +133,16 @@ func TestAccCheckCESV1AlarmValidation(t *testing.T) {
}

func TestCESAlarmRule_slashes(t *testing.T) {
var ar alarms.MetricAlarms
resourceName := "opentelekomcloud_ces_alarmrule.alarmrule_s"
var (
ar alarms.MetricAlarms
rName = "opentelekomcloud_ces_alarmrule.alarmrule_s"
)

rc := common.InitResourceCheck(
rName,
&ar,
getAlarmRuleFunc,
)
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() {
common.TestAccPreCheck(t)
Expand All @@ -127,69 +155,18 @@ func TestCESAlarmRule_slashes(t *testing.T) {
quotas.BookMany(t, qts)
},
ProviderFactories: common.TestAccProviderFactories,
CheckDestroy: testCESAlarmRuleDestroy,
CheckDestroy: rc.CheckResourceDestroy(),
Steps: []resource.TestStep{
{
Config: testCESAlarmRuleSlashes,
Check: resource.ComposeTestCheckFunc(
testCESAlarmRuleExists(resourceName, &ar),
rc.CheckResourceExists(),
),
},
},
})
}

func testCESAlarmRuleDestroy(s *terraform.State) error {
config := common.TestAccProvider.Meta().(*cfg.Config)
client, err := config.CesV1Client(env.OS_REGION_NAME)
if err != nil {
return fmt.Errorf("error creating OpenTelekomCloud CESv1 client: %w", err)
}

for _, rs := range s.RootModule().Resources {
if rs.Type != "opentelekomcloud_ces_alarmrule" {
continue
}

id := rs.Primary.ID
_, err := alarms.ShowAlarm(client, id)
if err == nil {
return fmt.Errorf("alarm rule still exists")
}
}

return nil
}

func testCESAlarmRuleExists(n string, ar *alarms.MetricAlarms) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
if !ok {
return fmt.Errorf("not found: %s", n)
}

if rs.Primary.ID == "" {
return fmt.Errorf("no ID is set")
}

config := common.TestAccProvider.Meta().(*cfg.Config)
client, err := config.CesV1Client(env.OS_REGION_NAME)
if err != nil {
return fmt.Errorf("error creating OpenTelekomCloud CESv1 client: %w", err)
}

id := rs.Primary.ID
found, err := alarms.ShowAlarm(client, id)
if err != nil {
return err
}

*ar = found[0]

return nil
}
}

var testCESAlarmRuleBasic = fmt.Sprintf(`
%s
Expand Down Expand Up @@ -378,7 +355,7 @@ resource "opentelekomcloud_ces_alarmrule" "alarmrule_1" {
metric_name = "backupFailed"
}
condition {
period = 300
period = 0
filter = "average"
comparison_operator = ">"
value = 6
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ func ResourceAlarmRule() *schema.Resource {
Required: true,
ForceNew: true,
ValidateFunc: validation.IntInSlice([]int{
1, 300, 1200, 3600, 14400, 86400,
0, 1, 300, 1200, 3600, 14400, 86400,
}),
},
"filter": {
Expand Down
4 changes: 4 additions & 0 deletions releasenotes/notes/ces-alarmrule-period-8f5bed58bf5d8fea.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
enhancements:
- |
**[CES]** Update ``period`` validation in ``resource/opentelekomcloud_ces_alarmrule`` to trigger alarm immediately (`#2818 <https://github.com/opentelekomcloud/terraform-provider-opentelekomcloud/pull/2818>`_)

0 comments on commit e9a2b5f

Please sign in to comment.