Skip to content

Commit

Permalink
support to update parameters of ces alarm rule (#1116)
Browse files Browse the repository at this point in the history
  • Loading branch information
ShiChangkuo authored May 11, 2021
1 parent 2f2a385 commit 9ff76cd
Show file tree
Hide file tree
Showing 11 changed files with 143 additions and 70 deletions.
33 changes: 17 additions & 16 deletions docs/resources/ces_alarmrule.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ The following arguments are supported:
be a string of 1 to 128 characters that can consist of numbers, lowercase letters,
uppercase letters, underscores (_), or hyphens (-).

* `metric` - (Required, List) Specifies the alarm metrics. The structure is described
below.
* `metric` - (Required, List, ForceNew) Specifies the alarm metrics. The structure is described
below. Changing this creates a new resource.

* `condition` - (Required, List) Specifies the alarm triggering condition. The structure
is described below.
Expand All @@ -57,22 +57,22 @@ The following arguments are supported:
* `alarm_enabled` - (Optional, Bool) Specifies whether to enable the alarm. The default
value is true.

* `alarm_type` - (Optional, Int, ForceNew) Specifies the alarm severity. The value can be
1, 2, 3 or 4, which indicates *critical*, *major*, *minor*, and *informational*, respectively.
The default value is 2. Changing this creates a new resource.
* `alarm_level` - (Optional, Int) Specifies the alarm severity. The value can be 1, 2, 3 or 4,
which indicates *critical*, *major*, *minor*, and *informational*, respectively.
The default value is 2.

* `alarm_actions` - (Optional, List) Specifies the action triggered by an alarm. The
structure is described below.
* `alarm_actions` - (Optional, List, ForceNew) Specifies the action triggered by an alarm. The
structure is described below. Changing this creates a new resource.

* `ok_actions` - (Optional, List) Specifies the action triggered by the clearing of
an alarm. The structure is described below.
* `ok_actions` - (Optional, List, ForceNew) Specifies the action triggered by the clearing of
an alarm. The structure is described below. Changing this creates a new resource.

* `alarm_action_enabled` - (Optional, Bool) Specifies whether to enable the action
to be triggered by an alarm. The default value is true.

-> **Note** If alarm_action_enabled is set to true, either alarm_actions or
-> **Note** If alarm_action_enabled is set to true, either alarm_actions or
ok_actions cannot be empty. If alarm_actions and ok_actions coexist, their
corresponding notification_list must be of the same value.
corresponding notification_list must be of the **same value**.

The `metric` block supports:

Expand All @@ -97,13 +97,13 @@ The `dimensions` block supports:

* `value` - (Required, String) Specifies the dimension value. The value can be a string
of 1 to 64 characters that must start with a letter or a number and can consists
of uppercase letters, lowercase letters, numbers, underscores (_), or hyphens
(-).
of uppercase letters, lowercase letters, numbers, underscores (_), or hyphens (-).

The `condition` block supports:

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

Note: If period is set to 1, the raw metric data is used to determine
whether to generate an alarm.

Expand All @@ -130,10 +130,11 @@ the `alarm_actions` block supports:

* `notification_list` - (Optional, List) specifies the list of objects to be notified
if the alarm status changes, the maximum length is 5.
if type is set to *notification*, the value of notification_list cannot be empty.
if type is set to *autoscaling*, the value of notification_list must be **[]**
If `type` is set to *notification*, the value of notification_list cannot be empty.
If `type` is set to *autoscaling*, the value of notification_list must be **[]**
and the value of namespace must be *SYS.AS*.
Note: to enable the autoscaling alarm rules take effect, you must bind scaling

Note: to enable the *autoscaling* alarm rules take effect, you must bind scaling
policies.

the `ok_actions` block supports:
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require (
github.com/hashicorp/errwrap v1.0.0
github.com/hashicorp/go-multierror v1.0.0
github.com/hashicorp/terraform-plugin-sdk v1.16.0
github.com/huaweicloud/golangsdk v0.0.0-20210507120834-b552827e340b
github.com/huaweicloud/golangsdk v0.0.0-20210510122110-66430a62b255
github.com/jen20/awspolicyequivalence v1.1.0
github.com/smartystreets/goconvey v0.0.0-20190222223459-a17d461953aa // indirect
github.com/stretchr/testify v1.4.0
Expand Down
6 changes: 2 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -204,10 +204,8 @@ github.com/hashicorp/terraform-svchost v0.0.0-20191011084731-65d371908596/go.mod
github.com/hashicorp/yamux v0.0.0-20180604194846-3520598351bb/go.mod h1:+NfK9FKeTrX5uv1uIXGdwYDTeHna2qgaIlx54MXqjAM=
github.com/hashicorp/yamux v0.0.0-20181012175058-2f1d1f20f75d h1:kJCB4vdITiW1eC1vq2e6IsrXKrZit1bv/TDYFGMp4BQ=
github.com/hashicorp/yamux v0.0.0-20181012175058-2f1d1f20f75d/go.mod h1:+NfK9FKeTrX5uv1uIXGdwYDTeHna2qgaIlx54MXqjAM=
github.com/huaweicloud/golangsdk v0.0.0-20210429115535-b33d1a7b88e5 h1:dTowzMwLUi1BmweYzwLUsgvU+zrCCWt5oQa0ky55e8g=
github.com/huaweicloud/golangsdk v0.0.0-20210429115535-b33d1a7b88e5/go.mod h1:fcOI5u+0f62JtJd7zkCch/Z57BNC6bhqb32TKuiF4r0=
github.com/huaweicloud/golangsdk v0.0.0-20210507120834-b552827e340b h1:OeZOWN5e0rgLXwu+4x9NHFVzyO5eI87oTzOWWoZ6Nx4=
github.com/huaweicloud/golangsdk v0.0.0-20210507120834-b552827e340b/go.mod h1:fcOI5u+0f62JtJd7zkCch/Z57BNC6bhqb32TKuiF4r0=
github.com/huaweicloud/golangsdk v0.0.0-20210510122110-66430a62b255 h1:2iW/eqhVyyvnFjXX9++3oKO3FPKGkFl49e/xnYaNUwc=
github.com/huaweicloud/golangsdk v0.0.0-20210510122110-66430a62b255/go.mod h1:fcOI5u+0f62JtJd7zkCch/Z57BNC6bhqb32TKuiF4r0=
github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/imdario/mergo v0.3.9 h1:UauaLniWCFHWd+Jp9oCEkTBj8VO/9DKg3PV3VCNMDIg=
github.com/imdario/mergo v0.3.9/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJh5FfA=
Expand Down
64 changes: 49 additions & 15 deletions huaweicloud/resource_huaweicloud_ces_alarmrule.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,23 @@ const nameCESAR = "CES-AlarmRule"
var cesAlarmActions = schema.Schema{
Type: schema.TypeList,
Optional: true,
ForceNew: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"type": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringInSlice([]string{
"notification", "autoscaling",
}, false),
},

"notification_list": {
Type: schema.TypeList,
Required: true,
MaxItems: 5,
Required: true,
ForceNew: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
Expand Down Expand Up @@ -75,6 +78,7 @@ func resourceAlarmRule() *schema.Resource {
"metric": {
Type: schema.TypeList,
Required: true,
ForceNew: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
Expand Down Expand Up @@ -152,6 +156,7 @@ func resourceAlarmRule() *schema.Resource {
"unit": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
},
},
Expand All @@ -170,7 +175,6 @@ func resourceAlarmRule() *schema.Resource {
Type: schema.TypeInt,
Optional: true,
Default: 2,
ForceNew: true,
ValidateFunc: validation.IntBetween(1, 4),
},

Expand Down Expand Up @@ -367,24 +371,54 @@ func resourceAlarmRuleUpdate(d *schema.ResourceData, meta interface{}) error {

arId := d.Id()

if !d.HasChange("alarm_enabled") {
log.Printf("[WARN] %s Nothing will be updated", nameCESAR)
return nil
if d.HasChange("alarm_enabled") {
enabled := d.Get("alarm_enabled").(bool)
enableOpts := alarmrule.EnableOpts{
AlarmEnabled: enabled,
}
log.Printf("[DEBUG] Updating %s %s to %#v", nameCESAR, arId, enabled)

timeout := d.Timeout(schema.TimeoutUpdate)
//lintignore:R006
err = resource.Retry(timeout, func() *resource.RetryError {
err := alarmrule.Enable(client, arId, enableOpts).ExtractErr()
if err != nil {
return checkForRetryableError(err)
}
return nil
})
if err != nil {
return fmt.Errorf("Error updating %s %s: %s", nameCESAR, arId, err)
}
}
updateOpts := alarmrule.UpdateOpts{AlarmEnabled: d.Get("alarm_enabled").(bool)}
log.Printf("[DEBUG] Updating %s %s with options: %#v", nameCESAR, arId, updateOpts)

timeout := d.Timeout(schema.TimeoutUpdate)
//lintignore:R006
err = resource.Retry(timeout, func() *resource.RetryError {
updateOpts := alarmrule.UpdateOpts{}
changed := false
if d.HasChanges("alarm_name", "alarm_description", "alarm_level", "alarm_action_enabled") {
description := d.Get("alarm_description").(string)
actionEnabled := d.Get("alarm_action_enabled").(bool)

updateOpts.Name = d.Get("alarm_name").(string)
updateOpts.AlarmLevel = d.Get("alarm_level").(int)
updateOpts.Description = &description
updateOpts.ActionEnabled = &actionEnabled
changed = true
}

if d.HasChange("condition") {
condition := getAlarmCondition(d)
// unit field is not supported in Update
condition.Unit = ""
updateOpts.Condition = &condition
changed = true
}

if changed {
log.Printf("[DEBUG] Updating %s %s opts: %#v", nameCESAR, arId, updateOpts)
err := alarmrule.Update(client, arId, updateOpts).ExtractErr()
if err != nil {
return checkForRetryableError(err)
return fmt.Errorf("Error updating %s %s: %s", nameCESAR, arId, err)
}
return nil
})
if err != nil {
return fmt.Errorf("Error updating %s %s: %s", nameCESAR, arId, err)
}

return resourceAlarmRuleRead(d, meta)
Expand Down
10 changes: 8 additions & 2 deletions huaweicloud/resource_huaweicloud_ces_alarmrule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@ func TestAccCESAlarmRule_basic(t *testing.T) {
Config: testCESAlarmRule_basic(rName),
Check: resource.ComposeTestCheckFunc(
testCESAlarmRuleExists(resourceName, &ar),
resource.TestCheckResourceAttr(resourceName, "alarm_name", fmt.Sprintf("rule-%s", rName)),
resource.TestCheckResourceAttr(resourceName, "alarm_enabled", "true"),
resource.TestCheckResourceAttr(resourceName, "alarm_action_enabled", "true"),
resource.TestCheckResourceAttr(resourceName, "alarm_level", "2"),
resource.TestCheckResourceAttr(resourceName, "condition.0.value", "6"),
),
},
{
Expand All @@ -38,7 +40,10 @@ func TestAccCESAlarmRule_basic(t *testing.T) {
{
Config: testCESAlarmRule_update(rName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "alarm_name", fmt.Sprintf("rule-%s-update", rName)),
resource.TestCheckResourceAttr(resourceName, "alarm_enabled", "false"),
resource.TestCheckResourceAttr(resourceName, "alarm_level", "3"),
resource.TestCheckResourceAttr(resourceName, "condition.0.value", "60"),
),
},
},
Expand Down Expand Up @@ -174,9 +179,10 @@ func testCESAlarmRule_update(rName string) string {
%s
resource "huaweicloud_ces_alarmrule" "alarmrule_1" {
alarm_name = "rule-%s"
alarm_name = "rule-%s-update"
alarm_action_enabled = true
alarm_enabled = false
alarm_level = 3
metric {
namespace = "SYS.ECS"
Expand All @@ -190,7 +196,7 @@ resource "huaweicloud_ces_alarmrule" "alarmrule_1" {
period = 300
filter = "average"
comparison_operator = ">"
value = 6
value = 60
unit = "B/s"
count = 1
}
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 9ff76cd

Please sign in to comment.