|
| 1 | +package analyzer |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "path/filepath" |
| 6 | + "reflect" |
| 7 | + "strconv" |
| 8 | + |
| 9 | + "github.com/pkg/errors" |
| 10 | + troubleshootv1beta2 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta2" |
| 11 | + iutils "github.com/replicatedhq/troubleshoot/pkg/interfaceutils" |
| 12 | +) |
| 13 | + |
| 14 | +func analyzeJsonCompare(analyzer *troubleshootv1beta2.JsonCompare, getCollectedFileContents func(string) ([]byte, error)) (*AnalyzeResult, error) { |
| 15 | + fullPath := filepath.Join(analyzer.CollectorName, analyzer.FileName) |
| 16 | + collected, err := getCollectedFileContents(fullPath) |
| 17 | + if err != nil { |
| 18 | + return nil, errors.Wrapf(err, "failed to read collected file name: %s", fullPath) |
| 19 | + } |
| 20 | + |
| 21 | + var actual interface{} |
| 22 | + err = json.Unmarshal(collected, &actual) |
| 23 | + if err != nil { |
| 24 | + return nil, errors.Wrap(err, "failed to parse collected data as json") |
| 25 | + } |
| 26 | + |
| 27 | + if analyzer.Path != "" { |
| 28 | + actual, err = iutils.GetAtPath(actual, analyzer.Path) |
| 29 | + if err != nil { |
| 30 | + return nil, errors.Wrapf(err, "failed to get object at path: %s", analyzer.Path) |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + var expected interface{} |
| 35 | + err = json.Unmarshal([]byte(analyzer.Value), &expected) |
| 36 | + if err != nil { |
| 37 | + return nil, errors.Wrap(err, "failed to parse expected value as json") |
| 38 | + } |
| 39 | + |
| 40 | + title := analyzer.CheckName |
| 41 | + if title == "" { |
| 42 | + title = analyzer.CollectorName |
| 43 | + } |
| 44 | + |
| 45 | + result := &AnalyzeResult{ |
| 46 | + Title: title, |
| 47 | + IconKey: "kubernetes_text_analyze", |
| 48 | + IconURI: "https://troubleshoot.sh/images/analyzer-icons/text-analyze.svg", |
| 49 | + } |
| 50 | + |
| 51 | + equal := reflect.DeepEqual(actual, expected) |
| 52 | + |
| 53 | + for _, outcome := range analyzer.Outcomes { |
| 54 | + if outcome.Fail != nil { |
| 55 | + when := false |
| 56 | + if outcome.Fail.When != "" { |
| 57 | + when, err = strconv.ParseBool(outcome.Fail.When) |
| 58 | + if err != nil { |
| 59 | + return nil, errors.Wrapf(err, "failed to process when statement: %s", outcome.Fail.When) |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + if when == equal { |
| 64 | + result.IsFail = true |
| 65 | + result.Message = outcome.Fail.Message |
| 66 | + result.URI = outcome.Fail.URI |
| 67 | + |
| 68 | + return result, nil |
| 69 | + } |
| 70 | + } else if outcome.Warn != nil { |
| 71 | + when := false |
| 72 | + if outcome.Warn.When != "" { |
| 73 | + when, err = strconv.ParseBool(outcome.Warn.When) |
| 74 | + if err != nil { |
| 75 | + return nil, errors.Wrapf(err, "failed to process when statement: %s", outcome.Warn.When) |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + if when == equal { |
| 80 | + result.IsWarn = true |
| 81 | + result.Message = outcome.Warn.Message |
| 82 | + result.URI = outcome.Warn.URI |
| 83 | + |
| 84 | + return result, nil |
| 85 | + } |
| 86 | + } else if outcome.Pass != nil { |
| 87 | + when := true // default to passing when values are equal |
| 88 | + if outcome.Pass.When != "" { |
| 89 | + when, err = strconv.ParseBool(outcome.Pass.When) |
| 90 | + if err != nil { |
| 91 | + return nil, errors.Wrapf(err, "failed to process when statement: %s", outcome.Pass.When) |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + if when == equal { |
| 96 | + result.IsPass = true |
| 97 | + result.Message = outcome.Pass.Message |
| 98 | + result.URI = outcome.Pass.URI |
| 99 | + |
| 100 | + return result, nil |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + return &AnalyzeResult{ |
| 106 | + Title: title, |
| 107 | + IconKey: "kubernetes_text_analyze", |
| 108 | + IconURI: "https://troubleshoot.sh/images/analyzer-icons/text-analyze.svg", |
| 109 | + IsFail: true, |
| 110 | + Message: "Invalid analyzer", |
| 111 | + }, nil |
| 112 | +} |
0 commit comments