Skip to content
This repository was archived by the owner on Jun 2, 2024. It is now read-only.

Commit

Permalink
#69: Fix incompatible change of equal condition operator
Browse files Browse the repository at this point in the history
  • Loading branch information
gessnerfl committed Oct 14, 2020
1 parent df9b77c commit 6a28a8b
Show file tree
Hide file tree
Showing 8 changed files with 533 additions and 115 deletions.
5 changes: 3 additions & 2 deletions docs/resources/instana_custom_event_spec_threshold_rule.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ resource "instana_custom_event_spec_threshold_rule" "custom_health_metrics_unhea
rule_metric_name = "metrics.gauges.myapp.healthIndicator"
rule_window = 10000
rule_aggregation = "avg"
rule_condition_operator = "=="
rule_condition_operator = "="
rule_condition_value = 1
}
```
Expand Down Expand Up @@ -110,6 +110,7 @@ placeholder string. Allowed values: `is`, `contains`, `any`, `startsWith`, `end
* `rule_aggregation` - Optional (depending on metric type) - the aggregation used to calculate the metric value for the given
time window and/or rollup. Supported value: `sum`, `avg`, `min`, `max`
* `rule_condition_operator` - Required - The condition operator used to check against the calculated metric value for the given
time window and/or rollup. Supported values: `==,` `!=,` `<=,` `<`, `>`, `=>`
time window and/or rollup. Supported values: `=` (`==` also supported as an alternative representation for equals), `!=`, `<=`,
`<`, `>`, `=>`
* `rule_condition_value` - Required - The numeric condition value used to check against the calculated metric value for the given
time window and/or rollup.
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func NewCustomEventSpecificationWithEntityVerificationRuleResourceHandle() *Reso
},
{
Type: customEventSpecificationWithEntityVerificationRuleSchemaV2().CoreConfigSchema().ImpliedType(),
Upgrade: migrateCustomEventConfigWithThresholdRuleToVersion3ByChangingMatchingOperatorToInstanaRepresentation,
Upgrade: migrateCustomEventConfigWithEntityVerificationRuleToVersion3ByChangingMatchingOperatorToInstanaRepresentation,
Version: 2,
},
},
Expand Down Expand Up @@ -150,7 +150,7 @@ func customEventSpecificationWithEntityVerificationRuleSchemaV2() *schema.Resour
}
}

func migrateCustomEventConfigWithThresholdRuleToVersion3ByChangingMatchingOperatorToInstanaRepresentation(rawState map[string]interface{}, meta interface{}) (map[string]interface{}, error) {
func migrateCustomEventConfigWithEntityVerificationRuleToVersion3ByChangingMatchingOperatorToInstanaRepresentation(rawState map[string]interface{}, meta interface{}) (map[string]interface{}, error) {
v, ok := rawState[EntityVerificationRuleFieldMatchingOperator]
if ok {
operator, err := restapi.SupportedMatchingOperators.FromTerraformValue(v.(string))
Expand Down
48 changes: 42 additions & 6 deletions instana/resource-custom-event-specficiation-threshold-rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,12 @@ var thresholdRuleSchemaFields = map[string]*schema.Schema{
ThresholdRuleFieldConditionOperator: {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice(restapi.SupportedConditionOperatorTypes.ToStringSlice(), false),
Description: "The condition operator (e.g >, <)",
ValidateFunc: validation.StringInSlice(restapi.SupportedConditionOperators.TerrafromSupportedValues(), false),
StateFunc: func(val interface{}) string {
operator, _ := restapi.SupportedConditionOperators.FromTerraformValue(val.(string))
return operator.InstanaAPIValue()
},
Description: "The condition operator (e.g >, <)",
},
ThresholdRuleFieldConditionValue: {
Type: schema.TypeFloat,
Expand Down Expand Up @@ -109,7 +113,7 @@ func NewCustomEventSpecificationWithThresholdRuleResourceHandle() *ResourceHandl
return &ResourceHandle{
ResourceName: ResourceInstanaCustomEventSpecificationThresholdRule,
Schema: mergeSchemaMap(defaultCustomEventSchemaFields, thresholdRuleSchemaFields),
SchemaVersion: 2,
SchemaVersion: 3,
StateUpgraders: []schema.StateUpgrader{
{
Type: customEventSpecificationWithThresholdRuleSchemaV0().CoreConfigSchema().ImpliedType(),
Expand All @@ -121,6 +125,11 @@ func NewCustomEventSpecificationWithThresholdRuleResourceHandle() *ResourceHandl
Upgrade: migrateCustomEventConfigFullStateFromV1toV2AndRemoveDownstreamConfiguration,
Version: 1,
},
{
Type: customEventSpecificationWithThresholdRuleSchemaV2().CoreConfigSchema().ImpliedType(),
Upgrade: migrateCustomEventConfigWithThreasholdRuleToVersion3ByChangingConditionOperatorToInstanaRepresentation,
Version: 2,
},
},
RestResourceFactory: func(api restapi.InstanaAPI) restapi.RestResource { return api.CustomEventSpecifications() },
UpdateState: updateStateForCustomEventSpecificationWithThresholdRule,
Expand All @@ -136,14 +145,18 @@ func updateStateForCustomEventSpecificationWithThresholdRule(d *schema.ResourceD
if err != nil {
return err
}
conditionOperator, err := ruleSpec.ConditionOperatorType()
if err != nil {
return err
}

updateStateForBasicCustomEventSpecification(d, customEventSpecification)
d.Set(CustomEventSpecificationRuleSeverity, severity)
d.Set(ThresholdRuleFieldMetricName, ruleSpec.MetricName)
d.Set(ThresholdRuleFieldRollup, ruleSpec.Rollup)
d.Set(ThresholdRuleFieldWindow, ruleSpec.Window)
d.Set(ThresholdRuleFieldAggregation, ruleSpec.Aggregation)
d.Set(ThresholdRuleFieldConditionOperator, ruleSpec.ConditionOperator)
d.Set(ThresholdRuleFieldConditionOperator, conditionOperator.InstanaAPIValue())
d.Set(ThresholdRuleFieldConditionValue, ruleSpec.ConditionValue)

if ruleSpec.MetricPattern != nil {
Expand All @@ -161,7 +174,12 @@ func mapStateToDataObjectForCustomEventSpecificationWithThresholdRule(d *schema.
return restapi.CustomEventSpecification{}, err
}
metricName := d.Get(ThresholdRuleFieldMetricName).(string)
conditionOperator := restapi.ConditionOperatorType(d.Get(ThresholdRuleFieldConditionOperator).(string))
conditionOperatorString := d.Get(ThresholdRuleFieldConditionOperator).(string)
conditionOperator, err := restapi.SupportedConditionOperators.FromTerraformValue(conditionOperatorString)
if err != nil {
return restapi.CustomEventSpecification{}, err
}
conditionOperatorInstanaValue := conditionOperator.InstanaAPIValue()

rule := restapi.RuleSpecification{
DType: restapi.ThresholdRuleType,
Expand All @@ -170,7 +188,7 @@ func mapStateToDataObjectForCustomEventSpecificationWithThresholdRule(d *schema.
Rollup: GetIntPointerFromResourceData(d, ThresholdRuleFieldRollup),
Window: GetIntPointerFromResourceData(d, ThresholdRuleFieldWindow),
Aggregation: getAggregationTypePointerFromResourceData(d, ThresholdRuleFieldAggregation),
ConditionOperator: &conditionOperator,
ConditionOperator: &conditionOperatorInstanaValue,
ConditionValue: GetFloat64PointerFromResourceData(d, ThresholdRuleFieldConditionValue),
}

Expand Down Expand Up @@ -210,3 +228,21 @@ func customEventSpecificationWithThresholdRuleSchemaV1() *schema.Resource {
Schema: mergeSchemaMap(defaultCustomEventSchemaFieldsV1, thresholdRuleSchemaFields),
}
}

func customEventSpecificationWithThresholdRuleSchemaV2() *schema.Resource {
return &schema.Resource{
Schema: mergeSchemaMap(defaultCustomEventSchemaFieldsV1, thresholdRuleSchemaFields),
}
}

func migrateCustomEventConfigWithThreasholdRuleToVersion3ByChangingConditionOperatorToInstanaRepresentation(rawState map[string]interface{}, meta interface{}) (map[string]interface{}, error) {
v, ok := rawState[ThresholdRuleFieldConditionOperator]
if ok {
operator, err := restapi.SupportedConditionOperators.FromTerraformValue(v.(string))
if err != nil {
return rawState, err
}
rawState[ThresholdRuleFieldConditionOperator] = operator.InstanaAPIValue()
}
return rawState, nil
}
Loading

0 comments on commit 6a28a8b

Please sign in to comment.