Skip to content

Commit

Permalink
fix(CIRC-10300): Ensure SNMP check OID settings do not swap places (#119
Browse files Browse the repository at this point in the history
)

* fix(CIRC-10300): Ensure SNMP check OID settings do not swap places

Change the type of the OID configurations for SNMP checks to use
TypeSet, rather than TypeList. This ensures that the state is always
ordered in the same way, regardless of how the API returns the
configuration keys.

This should resolve the issue of OID's changing positions in the state
when applying SNMP checks.

* Update changelog
  • Loading branch information
dhaifley authored May 25, 2023
1 parent 6ec9e95 commit dfb2294
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 16 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
## 0.12.15 (May 25, 2023)

CHANGES:

* fix(CIRC-10300): Change the type of the OID configurations for SNMP checks
from TypeList to TypeSet. This ensures the state is always ordered the same
regardless of how the API returns the configuration keys. This should resolve
issues of OID's changing positions in the state when applying SNMP checks.

## 0.12.14 (December 6, 2022)

CHANGES:
Expand Down
42 changes: 30 additions & 12 deletions circonus/resource_circonus_check_snmp.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package circonus

import (
"bytes"
"fmt"
"log"
"sort"
"strconv"
"strings"

"github.com/circonus-labs/go-apiclient/config"
"github.com/circonus-labs/terraform-provider-circonus/internal/hashcode"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

Expand Down Expand Up @@ -131,9 +132,10 @@ var schemaCheckSNMP = &schema.Schema{
ValidateFunc: validateRegexp(checkSNMPVersion, `(1|2c|3)`),
},
checkSNMPOID: {
Type: schema.TypeList,
Type: schema.TypeSet,
Required: true,
MinItems: 1,
Set: hashCheckSNMPOID,
Elem: &schema.Resource{
Schema: convertToHelperSchema(checkSNMPOIDDescriptions, map[schemaAttr]*schema.Schema{
checkSNMPOIDName: {
Expand All @@ -158,6 +160,27 @@ var schemaCheckSNMP = &schema.Schema{
},
}

// hashCheckSNMPOID creates a stable hash of the normalized OID values.
func hashCheckSNMPOID(v interface{}) int {
m := v.(map[string]interface{})

b := &bytes.Buffer{}

b.Grow(defaultHashBufSize)

writeString := func(attrName schemaAttr) {
if v, ok := m[string(attrName)]; ok && v.(string) != "" {
fmt.Fprint(b, strings.TrimSpace(v.(string)))
}
}

writeString(checkSNMPOIDName)

s := b.String()

return hashcode.String(s)
}

// checkAPIToStateSNMP reads the Config data out of circonusCheck.CheckBundle into the
// statefile.
func checkAPIToStateSNMP(c *circonusCheck, d *schema.ResourceData) error {
Expand Down Expand Up @@ -246,15 +269,8 @@ func checkAPIToStateSNMP(c *circonusCheck, d *schema.ResourceData) error {
}
}

sort.Slice(oidList, func(i, j int) bool {
if oidList[i] != nil && oidList[j] != nil {
y := oidList[i].(map[string]interface{})
z := oidList[j].(map[string]interface{})
return y[string(checkSNMPOIDName)].(string) < z[string(checkSNMPOIDName)].(string)
}
return true
})
snmpConfig[string(checkSNMPOID)] = oidList
snmpConfig[string(checkSNMPOID)] = schema.NewSet(hashCheckSNMPOID,
oidList)

whitelistedConfigKeys := map[config.Key]struct{}{
config.ReverseSecretKey: {},
Expand Down Expand Up @@ -333,7 +349,9 @@ func checkConfigToAPISNMP(c *circonusCheck, l interfaceList) error { //nolint:un
}

if v, found := snmpConfig[checkSNMPOID]; found {
m := v.([]interface{})
ss := v.(*schema.Set)

m := ss.List()
for _, ll := range m {
if ll == nil {
continue
Expand Down
8 changes: 4 additions & 4 deletions circonus/resource_circonus_check_snmp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,16 @@ resource "circonus_check" "snmp" {
security_level = "authNoPriv"
oid {
name = "upsBatCapacity"
path = ".1.3.6.1.4.1.318.1.1.1.2.2.1.0"
name = "upsBatVoltage"
path = ".1.3.6.1.4.1.318.1.1.1.2.2.8.0"
}
oid {
name = "upsBatTimeRemaining"
path = ".1.3.6.1.4.1.318.1.1.1.2.2.3.0"
}
oid {
name = "upsBatVoltage"
path = ".1.3.6.1.4.1.318.1.1.1.2.2.8.0"
name = "upsBatCapacity"
path = ".1.3.6.1.4.1.318.1.1.1.2.2.1.0"
}
}
Expand Down

0 comments on commit dfb2294

Please sign in to comment.