|
| 1 | +package analyzer |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "net" |
| 6 | + |
| 7 | + "github.com/pkg/errors" |
| 8 | + troubleshootv1beta2 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta2" |
| 9 | +) |
| 10 | + |
| 11 | +type AnalyzeHostSubnetContainsIP struct { |
| 12 | + hostAnalyzer *troubleshootv1beta2.SubnetContainsIPAnalyze |
| 13 | +} |
| 14 | + |
| 15 | +func (a *AnalyzeHostSubnetContainsIP) Title() string { |
| 16 | + return hostAnalyzerTitleOrDefault(a.hostAnalyzer.AnalyzeMeta, "Subnet Contains IP") |
| 17 | +} |
| 18 | + |
| 19 | +func (a *AnalyzeHostSubnetContainsIP) IsExcluded() (bool, error) { |
| 20 | + return isExcluded(a.hostAnalyzer.Exclude) |
| 21 | +} |
| 22 | + |
| 23 | +func (a *AnalyzeHostSubnetContainsIP) Analyze( |
| 24 | + getCollectedFileContents func(string) ([]byte, error), findFiles getChildCollectedFileContents, |
| 25 | +) ([]*AnalyzeResult, error) { |
| 26 | + _, ipNet, err := net.ParseCIDR(a.hostAnalyzer.CIDR) |
| 27 | + if err != nil { |
| 28 | + return nil, errors.Wrapf(err, "failed to parse CIDR %s", a.hostAnalyzer.CIDR) |
| 29 | + } |
| 30 | + |
| 31 | + ip := net.ParseIP(a.hostAnalyzer.IP) |
| 32 | + if ip == nil { |
| 33 | + return nil, errors.Errorf("failed to parse IP address %s", a.hostAnalyzer.IP) |
| 34 | + } |
| 35 | + |
| 36 | + contains := fmt.Sprintf("%t", ipNet.Contains(ip)) |
| 37 | + |
| 38 | + results, err := analyzeHostCollectorResults([]collectedContent{{Data: []byte(contains)}}, a.hostAnalyzer.Outcomes, a.CheckCondition, a.Title()) |
| 39 | + if err != nil { |
| 40 | + return nil, errors.Wrap(err, "failed to analyze Subnet Contains IP") |
| 41 | + } |
| 42 | + |
| 43 | + return results, nil |
| 44 | +} |
| 45 | + |
| 46 | +func (a *AnalyzeHostSubnetContainsIP) CheckCondition(when string, data []byte) (bool, error) { |
| 47 | + switch when { |
| 48 | + case "true": |
| 49 | + return string(data) == "true", nil |
| 50 | + case "false": |
| 51 | + return string(data) == "false", nil |
| 52 | + } |
| 53 | + |
| 54 | + return false, errors.Errorf("unknown condition: %q", when) |
| 55 | +} |
0 commit comments