Skip to content

Commit

Permalink
feat(cae): add new data source to query event notification rules
Browse files Browse the repository at this point in the history
  • Loading branch information
wuzhuanhong committed Jan 9, 2025
1 parent c2be21c commit 36528a0
Show file tree
Hide file tree
Showing 4 changed files with 553 additions and 2 deletions.
118 changes: 118 additions & 0 deletions docs/data-sources/cae_notification_rules.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
---
subcategory: "Cloud Application Engine (CAE)"
layout: "huaweicloud"
page_title: "HuaweiCloud: huaweicloud_cae_notification_rules"
description: |-
Use this data source to get the list of the event notification rules within HuaweiCloud.
---

# huaweicloud_cae_notification_rules

Use this data source to get the list of the event notification rules within HuaweiCloud.

## Example Usage

```hcl
data "huaweicloud_cae_notification_rules" "test" {}
```

## Argument Reference

The following arguments are supported:

* `region` - (Optional, String) Specifies the region in which to query the resource.
If omitted, the provider-level region will be used.

## Attribute Reference

In addition to all arguments above, the following attributes are exported:

* `id` - The data source ID.

* `rules` - The list of the event notification rules.

The [rules](#rules_struct) structure is documented below.

<a name="rules_struct"></a>
The `rules` block supports:

* `id` - The ID of the event notification rule.

* `name` - The name of the event notification rule.

* `event_name` - The trigger event of the event notification.
The valid values are as follows:
+ **Healthy**: Healthy checked.
+ **Unhealthy**: Healthy check failed.
+ **Pulled**: Image pulled.
+ **FailedPullImage**: Pull image failed.
+ **Started**: Container started up.
+ **BackOffStart**: Container startup failed.
+ **SuccessfulMountVolume**: Volume mounted.
+ **FailedMount**: Attach volume failed.

* `scope` - The scope in which event notification rule takes effect.

The [scope](#rules_scope_struct) structure is documented below.

* `trigger_policy` - The trigger policy of the event notification rule.

The [trigger_policy](#rules_trigger_policy_struct) structure is documented below.

* `notification` - The configuration of the event notification.

The [notification](#rules_notification_struct) structure is documented below.

* `enabled` - Whether the event notification rule is enabled.

<a name="rules_scope_struct"></a>
The `scope` block supports:

* `type` - The type to which the event notification rule takes effect.
The valid values are as follows:
+ **environments**: The rule takes effect for all components in the environment.
+ **applications**: The rule takes effect for all components in the application.
+ **components**: The rule takes effect for the specified components.

* `environments` - The list of the environment IDs.

* `applications` - The list of the application IDs.

* `components` - The list of the component IDs.

<a name="rules_trigger_policy_struct"></a>
The `trigger_policy` block supports:

* `type` - The type of the trigger.
The valid values are as follows:
+ **accumulative**
+ **immediately**

* `period` - The trigger period of the event.
The valid values are as follows:
+ **300**
+ **1200**
+ **3600**
+ **14400**
+ **86400**

* `count` - The number of times the event occurred.

* `operator` - The condition of the event notification.
The valid values are **>** and **>=**.

<a name="rules_notification_struct"></a>
The `notification` block supports:

* `protocol` - The protocol of the event notification.
The valid values are as follows:
+ **sms**
+ **email**
+ **wechat**

* `endpoint` - The endpoint of the event notification.

* `template` - The template language of the event notification.
The valid values are as follows:
+ **EN**
+ **ZH**
5 changes: 3 additions & 2 deletions huaweicloud/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -481,8 +481,9 @@ func Provider() *schema.Provider {
"huaweicloud_bms_flavors": bms.DataSourceBmsFlavors(),
"huaweicloud_bms_instances": bms.DataSourceBmsInstances(),

"huaweicloud_cae_environments": cae.DataSourceEnvironments(),
"huaweicloud_cae_applications": cae.DataSourceApplications(),
"huaweicloud_cae_applications": cae.DataSourceApplications(),
"huaweicloud_cae_environments": cae.DataSourceEnvironments(),
"huaweicloud_cae_notification_rules": cae.DataSourceCaeNotificationRules(),

"huaweicloud_cbr_backup": cbr.DataSourceBackup(),
"huaweicloud_cbr_vaults": cbr.DataSourceVaults(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
package cae

import (
"fmt"
"regexp"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"

"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/services/acceptance"
)

func TestAccNotificationRules_basic(t *testing.T) {
dataSource := "data.huaweicloud_cae_notification_rules.test"
rName := acceptance.RandomAccResourceNameWithDash()
dc := acceptance.InitDataSourceCheck(dataSource)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() {
acceptance.TestAccPreCheck(t)
acceptance.TestAccPreCheckCaeEnvironment(t)
acceptance.TestAccPreCheckCaeApplication(t)
},
ProviderFactories: acceptance.TestAccProviderFactories,
Steps: []resource.TestStep{
{
Config: testDataSourceNotificationRules_basic(rName),
Check: resource.ComposeTestCheckFunc(
dc.CheckResourceExists(),
resource.TestMatchResourceAttr(dataSource, "rules.#", regexp.MustCompile(`[1-9]\d*`)),
resource.TestCheckOutput("is_with_env_name_set", "true"),
resource.TestCheckOutput("is_with_env_event_name_set", "true"),
resource.TestCheckOutput("is_with_env_scope_environments_set", "true"),
resource.TestCheckOutput("is_with_env_scope_app_and_com_empty", "true"),
resource.TestCheckOutput("is_with_env_trigger_policy_type_set", "true"),
resource.TestCheckOutput("is_with_env_trigger_policy_period_set", "true"),
resource.TestCheckOutput("is_with_env_trigger_policy_count_set", "true"),
resource.TestCheckOutput("is_with_env_trigger_policy_operator_set", "true"),
resource.TestCheckOutput("is_with_env_scope_type_set", "true"),
resource.TestCheckOutput("is_with_env_notification_endpoint_set", "true"),
resource.TestCheckOutput("is_with_env_notification_protocol_set", "true"),
resource.TestCheckOutput("is_with_env_notification_template_set", "true"),
resource.TestCheckOutput("is_with_env_enabled_set", "true"),
resource.TestCheckOutput("is_with_app_name_set", "true"),
resource.TestCheckOutput("is_with_app_scope_type_set", "true"),
resource.TestCheckOutput("is_with_app_scope_applications_set", "true"),
resource.TestCheckOutput("is_with_app_scope_env_and_com_empty", "true"),
resource.TestCheckOutput("is_with_app_trigger_policy_type_set", "true"),
resource.TestCheckOutput("is_with_app_enabled_set", "true"),
resource.TestCheckOutput("is_with_com_scope_type_set", "true"),
resource.TestCheckOutput("is_with_com_scope_components_set", "true"),
resource.TestCheckOutput("is_with_com_scope_env_and_app_empty", "true"),
),
},
},
})
}

func testDataSourceNotificationRules_basic(name string) string {
return fmt.Sprintf(`
%[1]s
data "huaweicloud_cae_notification_rules" "test" {
depends_on = [
huaweicloud_cae_notification_rule.env,
huaweicloud_cae_notification_rule.app,
huaweicloud_cae_notification_rule.com
]
}
locals {
with_env_filter_result = try([
for v in data.huaweicloud_cae_notification_rules.test.rules : v if v.id == huaweicloud_cae_notification_rule.env.id
][0], [])
}
output "is_with_env_name_set" {
value = try(local.with_env_filter_result.name == huaweicloud_cae_notification_rule.env.name, false)
}
output "is_with_env_event_name_set" {
value = try(local.with_env_filter_result.event_name == huaweicloud_cae_notification_rule.env.event_name, false)
}
output "is_with_env_scope_environments_set" {
value = try(local.with_env_filter_result.scope[0].environments[0] ==
huaweicloud_cae_notification_rule.env.scope[0].environments[0], false)
}
output "is_with_env_scope_app_and_com_empty" {
value = try(length(local.with_env_filter_result.scope[0].applications) == 0 &&
length(local.with_env_filter_result.scope[0].components) == 0, false)
}
output "is_with_env_trigger_policy_type_set" {
value = try(local.with_env_filter_result.trigger_policy[0].type ==
huaweicloud_cae_notification_rule.env.trigger_policy[0].type, false)
}
output "is_with_env_trigger_policy_period_set" {
value = try(local.with_env_filter_result.trigger_policy[0].period ==
huaweicloud_cae_notification_rule.env.trigger_policy[0].period, false)
}
output "is_with_env_trigger_policy_count_set" {
value = try(local.with_env_filter_result.trigger_policy[0].count ==
huaweicloud_cae_notification_rule.env.trigger_policy[0].count, false)
}
output "is_with_env_trigger_policy_operator_set" {
value = try(local.with_env_filter_result.trigger_policy[0].operator ==
huaweicloud_cae_notification_rule.env.trigger_policy[0].operator, false)
}
output "is_with_env_scope_type_set" {
value = try(local.with_env_filter_result.scope[0].type == huaweicloud_cae_notification_rule.env.scope[0].type,
false)
}
output "is_with_env_notification_endpoint_set" {
value = try(local.with_env_filter_result.notification[0].endpoint == huaweicloud_cae_notification_rule.env.notification[0].endpoint,
false)
}
output "is_with_env_notification_protocol_set" {
value = try(local.with_env_filter_result.notification[0].protocol == huaweicloud_cae_notification_rule.env.notification[0].protocol,
false)
}
output "is_with_env_notification_template_set" {
value = try(local.with_env_filter_result.notification[0].template == huaweicloud_cae_notification_rule.env.notification[0].template,
false)
}
output "is_with_env_enabled_set" {
value = try(local.with_env_filter_result.enabled == huaweicloud_cae_notification_rule.env.enabled, false)
}
locals {
with_app_filter_result = try([
for v in data.huaweicloud_cae_notification_rules.test.rules : v if v.id == huaweicloud_cae_notification_rule.app.id
][0], [])
}
output "is_with_app_name_set" {
value = try(local.with_app_filter_result.name == huaweicloud_cae_notification_rule.app.name, false)
}
output "is_with_app_scope_type_set" {
value = try(local.with_app_filter_result.scope[0].type == huaweicloud_cae_notification_rule.app.scope[0].type, false)
}
output "is_with_app_scope_applications_set" {
value = try(local.with_app_filter_result.scope[0].applications[0] ==
huaweicloud_cae_notification_rule.app.scope[0].applications[0], false)
}
output "is_with_app_scope_env_and_com_empty" {
value = try(length(local.with_app_filter_result.scope[0].environments) == 0 &&
length(local.with_app_filter_result.scope[0].components) == 0, false)
}
output "is_with_app_trigger_policy_type_set" {
value = try(local.with_app_filter_result.trigger_policy[0].type ==
huaweicloud_cae_notification_rule.app.trigger_policy[0].type, false)
}
output "is_with_app_enabled_set" {
value = try(local.with_app_filter_result.enabled == huaweicloud_cae_notification_rule.app.enabled, false)
}
locals {
with_com_filter_result = try([
for v in data.huaweicloud_cae_notification_rules.test.rules : v if v.id == huaweicloud_cae_notification_rule.com.id
][0], [])
}
output "is_with_com_scope_type_set" {
value = try(local.with_com_filter_result.scope[0].type == huaweicloud_cae_notification_rule.com.scope[0].type, false)
}
output "is_with_com_scope_components_set" {
value = try(length(local.with_com_filter_result.scope[0].components) ==
length(huaweicloud_cae_notification_rule.com.scope[0].components), false)
}
output "is_with_com_scope_env_and_app_empty" {
value = try(length(local.with_com_filter_result.scope[0].environments) == 0 &&
length(local.with_com_filter_result.scope[0].applications) == 0, false)
}
`, testAccResourceNotificationRule_basic_step1(name))
}
Loading

0 comments on commit 36528a0

Please sign in to comment.