Skip to content

Commit

Permalink
fix: default alert rule not being read (#151)
Browse files Browse the repository at this point in the history
fix: default alert rule not being read (#151)
  • Loading branch information
joaomper-TE authored Dec 12, 2023
1 parent 3351439 commit cc4cb04
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 85 deletions.
10 changes: 10 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,13 @@ jobs:
run: |
git diff --compact-summary --exit-code || \
(echo; echo "Unexpected difference in directories after code generation. Run 'go generate' command and commit."; exit 1)
unit:
name: Unit Tests
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-go@v4
with:
go-version-file: 'go.mod'
- run: go test -v -cover ./...
97 changes: 13 additions & 84 deletions thousandeyes/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ import (
type ResourceReadFunc func(client *thousandeyes.Client, id int64) (interface{}, error)

func IsNotFoundError(err error) bool {
notFoundPatterns := []string{"404", "not found"}
for _, pattern := range notFoundPatterns {
if strings.Contains(strings.ToLower(err.Error()), pattern) {
return true
}
}
return false
notFoundPatterns := []string{"404", "not found"}
for _, pattern := range notFoundPatterns {
if strings.Contains(strings.ToLower(err.Error()), pattern) {
return true
}
}
return false
}

func expandAgents(v interface{}) thousandeyes.Agents {
Expand Down Expand Up @@ -53,68 +53,6 @@ func expandAlertRules(alertRules *[]thousandeyes.AlertRule) *[]thousandeyes.Aler
return ret
}

func expandBGPMonitors(v interface{}) thousandeyes.BGPMonitors {
var bgpMonitors thousandeyes.BGPMonitors

for _, er := range v.([]interface{}) {
rer := er.(map[string]interface{})
bgpMonitor := &thousandeyes.BGPMonitor{
MonitorID: thousandeyes.Int64(rer["monitor_id"].(int64)),
}
bgpMonitors = append(bgpMonitors, *bgpMonitor)
}

return bgpMonitors
}

func expandDNSServers(v interface{}) []thousandeyes.Server {
var dnsServers []thousandeyes.Server

for _, er := range v.([]interface{}) {
rer := er.(map[string]interface{})
targetDNSServer := &thousandeyes.Server{
ServerName: thousandeyes.String(rer["server_name"].(string)),
}
dnsServers = append(dnsServers, *targetDNSServer)
}

return dnsServers
}

func unpackSIPAuthData(i interface{}) thousandeyes.SIPAuthData {
var m = i.(map[string]interface{})
var sipAuthData = thousandeyes.SIPAuthData{}

for k, v := range m {
if k == "auth_user" {
sipAuthData.AuthUser = thousandeyes.String(v.(string))
}
if k == "password" {
sipAuthData.Password = thousandeyes.String(v.(string))
}
if k == "port" {
port, err := strconv.Atoi(v.(string))
if err == nil {
sipAuthData.Port = thousandeyes.Int(port)
}
}
if k == "protocol" {
sipAuthData.Protocol = thousandeyes.String(v.(string))
}
if k == "sip_proxy" {
sipAuthData.SIPProxy = thousandeyes.String(v.(string))
}
if k == "sip_registrar" {
sipAuthData.SIPRegistrar = thousandeyes.String(v.(string))
}
if k == "user" {
sipAuthData.User = thousandeyes.String(v.(string))
}
}

return sipAuthData
}

// ResourceBuildStruct fills the struct at a given address by querying a
// schema.ResourceData object for the matching field. It discovers the
// matching value name by getting the JSON key from the struct field,
Expand All @@ -135,7 +73,7 @@ func ResourceBuildStruct(d *schema.ResourceData, structPtr interface{}) interfac
return resourceFixups(d, structPtr)
}

// resourceRead is a generic function for reading resources.
// GetResource is a generic function for reading resources.
func GetResource(d *schema.ResourceData, m interface{}, readFunc ResourceReadFunc) error {
client := m.(*thousandeyes.Client)

Expand Down Expand Up @@ -213,23 +151,14 @@ func FixReadValues(m interface{}, name string) (interface{}, error) {
}

// Remove all alert rule fields except for rule ID. Ignore default rules.
// Remove all alert rule fields except for rule ID.
case "alert_rules":
alert_rules := m.([]interface{})
// Edit the alert_rules slice in place, to return the same type.
i := 0
for i < len(alert_rules) {
rule := alert_rules[i].(map[string]interface{})
if is_default, ok := rule["default"]; ok && is_default != nil && *is_default.(*bool) {
// Remove this item from the slice
alert_rules = append(alert_rules[:i], alert_rules[i+1:]...)
} else {
alert_rules[i] = map[string]interface{}{
"rule_id": rule["rule_id"],
}
i = i + 1
for i, v := range m.([]interface{}) {
rule := v.(map[string]interface{})
m.([]interface{})[i] = map[string]interface{}{
"rule_id": rule["rule_id"],
}
}
m = alert_rules

// Remove all public BGP monitors. (ThousandEyes does not allow
// specifying individual public BGP monitors, and all available
Expand Down
13 changes: 12 additions & 1 deletion thousandeyes/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,9 @@ func TestFixReadValues(t *testing.T) {
map[string]interface{}{
"rule_id": thousandeyes.Int(2),
},
map[string]interface{}{
"rule_id": thousandeyes.Int(3),
},
}
output, err = FixReadValues(alertRulesInput, "alert_rules")
if err != nil {
Expand Down Expand Up @@ -317,11 +320,19 @@ func TestFixReadValues(t *testing.T) {
"test_id": "2",
},
}
testsTarget := []interface{}{
map[string]interface{}{
"test_id": "1",
},
map[string]interface{}{
"test_id": "2",
},
}
output, err = FixReadValues(testsInput, "tests")
if err != nil {
t.Errorf("tests input returned error: %s", err.Error())
}
if output != nil {
if reflect.DeepEqual(output, testsTarget) != true {
t.Errorf("Values not stripped correctly from tests input: Received %#v Expected %#v", output, nil)
}

Expand Down

0 comments on commit cc4cb04

Please sign in to comment.