-
Notifications
You must be signed in to change notification settings - Fork 70
/
polling_recipe_validator.go
58 lines (46 loc) · 1.49 KB
/
polling_recipe_validator.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package validation
import (
"bytes"
"context"
"html/template"
"github.com/newrelic/newrelic-cli/internal/install/types"
"github.com/newrelic/newrelic-cli/internal/utils"
utilsValidation "github.com/newrelic/newrelic-cli/internal/utils/validation"
)
type contextKey int
// PollingRecipeValidator is an implementation of the RecipeValidator interface
// that polls NRDB to assert data is being reported for the given recipe.
type PollingRecipeValidator struct {
utilsValidation.PollingNRQLValidator
}
// NewPollingRecipeValidator returns a new instance of PollingRecipeValidator.
func NewPollingRecipeValidator(c utils.NRDBClient) *PollingRecipeValidator {
v := PollingRecipeValidator{
PollingNRQLValidator: *utilsValidation.NewPollingNRQLValidator(c),
}
return &v
}
// ValidateRecipe polls NRDB to assert data is being reported for the given recipe.
func (m *PollingRecipeValidator) ValidateRecipe(ctx context.Context, dm types.DiscoveryManifest, r types.OpenInstallationRecipe) (string, error) {
query, err := substituteHostname(dm, r)
if err != nil {
return "", err
}
return m.Validate(ctx, query)
}
func substituteHostname(dm types.DiscoveryManifest, r types.OpenInstallationRecipe) (string, error) {
tmpl, err := template.New("validationNRQL").Parse(string(r.ValidationNRQL))
if err != nil {
panic(err)
}
v := struct {
HOSTNAME string
}{
HOSTNAME: dm.Hostname,
}
var tpl bytes.Buffer
if err = tmpl.Execute(&tpl, v); err != nil {
return "", err
}
return tpl.String(), nil
}