Skip to content

Commit

Permalink
Bugfix: Add validation call for Read in case alert replace call doesn…
Browse files Browse the repository at this point in the history
…'t work (#23)
  • Loading branch information
ruokei authored Jul 18, 2023
1 parent 1f5cc62 commit 5aa97ab
Showing 1 changed file with 26 additions and 21 deletions.
47 changes: 26 additions & 21 deletions alicloud/resource_cms_composite_group_metric_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,18 +184,23 @@ func (r *cmsAlarmRuleResource) Read(ctx context.Context, req resource.ReadReques

totalRules, _ := strconv.ParseInt(*alarmRuleResponse.Body.Total, 10, 64)

if totalRules > 0 {
if totalRules > 0 &&
alarmRuleResponse.Body.Alarms.Alarm[0].CompositeExpression.ExpressionRaw != nil &&
alarmRuleResponse.Body.Alarms.Alarm[0].CompositeExpression.Level != nil &&
alarmRuleResponse.Body.Alarms.Alarm[0].CompositeExpression.Times != nil {

alarm := alarmRuleResponse.Body.Alarms.Alarm[0]
groupId, _ := strconv.ParseInt(*alarmRuleResponse.Body.Alarms.Alarm[0].GroupId, 10, 64)

state.RuleName = types.StringValue(*alarmRuleResponse.Body.Alarms.Alarm[0].RuleName)
state.Namespace = types.StringValue(*alarmRuleResponse.Body.Alarms.Alarm[0].Namespace)
state.MetricName = types.StringValue(*alarmRuleResponse.Body.Alarms.Alarm[0].MetricName)
state.ContactGroups = types.StringValue(*alarmRuleResponse.Body.Alarms.Alarm[0].ContactGroups)
state.RuleName = types.StringValue(*alarm.RuleName)
state.Namespace = types.StringValue(*alarm.Namespace)
state.MetricName = types.StringValue(*alarm.MetricName)
state.ContactGroups = types.StringValue(*alarm.ContactGroups)
state.GroupId = types.Int64Value(groupId)

state.CompositeExpression.ExpressionRaw = types.StringValue(*alarmRuleResponse.Body.Alarms.Alarm[0].CompositeExpression.ExpressionRaw)
state.CompositeExpression.Level = types.StringValue(*alarmRuleResponse.Body.Alarms.Alarm[0].CompositeExpression.Level)
state.CompositeExpression.Times = types.Int64Value(int64(*alarmRuleResponse.Body.Alarms.Alarm[0].CompositeExpression.Times))
state.CompositeExpression.ExpressionRaw = types.StringValue(*alarm.CompositeExpression.ExpressionRaw)
state.CompositeExpression.Level = types.StringValue(*alarm.CompositeExpression.Level)
state.CompositeExpression.Times = types.Int64Value(int64(*alarm.CompositeExpression.Times))

// Set refreshed state
setStateDiags := resp.State.Set(ctx, &state)
Expand Down Expand Up @@ -343,6 +348,19 @@ func (r *cmsAlarmRuleResource) setRule(ctx context.Context, plan *cmsAlarmRuleRe
},
}

_, _err := r.client.CreateGroupMetricRulesWithOptions(createGroupMetricRulesRequest, runtime)
if _err != nil {
if _t, ok := _err.(*tea.SDKError); ok {
if isAbleToRetry(*_t.Code) {
return _err
} else {
return backoff.Permanent(_err)
}
} else {
return _err
}
}

putResourceMetricRuleRequest := &alicloudCmsClient.PutResourceMetricRuleRequest{
RuleId: tea.String(ruleId),
RuleName: tea.String(plan.RuleName.ValueString()),
Expand All @@ -357,19 +375,6 @@ func (r *cmsAlarmRuleResource) setRule(ctx context.Context, plan *cmsAlarmRuleRe
},
}

_, _err := r.client.CreateGroupMetricRulesWithOptions(createGroupMetricRulesRequest, runtime)
if _err != nil {
if _t, ok := _err.(*tea.SDKError); ok {
if isAbleToRetry(*_t.Code) {
return _err
} else {
return backoff.Permanent(_err)
}
} else {
return _err
}
}

_, err := r.client.PutResourceMetricRuleWithOptions(putResourceMetricRuleRequest, runtime)
if err != nil {
if _t, ok := err.(*tea.SDKError); ok {
Expand Down

0 comments on commit 5aa97ab

Please sign in to comment.