forked from sensu/sensu-go
/
create.go
103 lines (89 loc) · 3.65 KB
/
create.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package check
import (
"errors"
"fmt"
"github.com/sensu/sensu-go/cli"
"github.com/sensu/sensu-go/cli/commands/flags"
"github.com/sensu/sensu-go/cli/commands/helpers"
"github.com/sensu/sensu-go/types"
"github.com/spf13/cobra"
)
// CreateCommand adds command that allows user to create new checks
func CreateCommand(cli *cli.SensuCli) *cobra.Command {
cmd := &cobra.Command{
Use: "create [NAME]",
Short: "create new checks",
SilenceUsage: true,
PreRun: func(cmd *cobra.Command, args []string) {
isInteractive, _ := cmd.Flags().GetBool(flags.Interactive)
if !isInteractive {
// Mark flags are required for bash-completions
_ = cmd.MarkFlagRequired("command")
_ = cmd.MarkFlagRequired("subscriptions")
}
},
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) > 1 {
_ = cmd.Help()
return errors.New("invalid argument(s) received")
}
isInteractive, _ := cmd.Flags().GetBool(flags.Interactive)
opts := newCheckOpts()
if len(args) > 0 {
opts.Name = args[0]
}
opts.Namespace = cli.Config.Namespace()
if isInteractive {
if err := opts.administerQuestionnaire(false); err != nil {
return err
}
} else {
opts.withFlags(cmd.Flags())
if opts.Interval != "" && opts.Cron != "" {
return fmt.Errorf("cannot specify --interval and --cron at the same time")
}
if opts.Interval == "" && opts.Cron == "" {
return fmt.Errorf("must specify --interval or --cron")
}
}
// Apply given arguments to check
check := types.CheckConfig{}
opts.Copy(&check)
if err := check.Validate(); err != nil {
if !isInteractive {
cmd.SilenceUsage = false
}
return err
}
//
// TODO:
//
// Current validation is a bit too laissez faire. For usability we should
// determine whether there are assets / handlers / mutators associated w/
// the check and warn the user if they do not exist yet.
if err := cli.Client.CreateCheck(&check); err != nil {
return err
}
fmt.Fprintln(cmd.OutOrStdout(), "Created")
return nil
},
}
cmd.Flags().StringP("command", "c", "", "the command the check should run")
cmd.Flags().String("cron", "", "the cron schedule at which the check is run")
cmd.Flags().String("handlers", "", "comma separated list of handlers to invoke when check fails")
cmd.Flags().StringP("interval", "i", "", "interval, in seconds, at which the check is run")
cmd.Flags().StringP("runtime-assets", "r", "", "comma separated list of assets this check depends on")
cmd.Flags().String("proxy-entity-name", "", "the check proxy entity, used to create a proxy entity for an external resource")
cmd.Flags().BoolP("publish", "p", true, "publish check requests")
cmd.Flags().BoolP("stdin", "", false, "accept event data via STDIN")
cmd.Flags().StringP("subscriptions", "s", "", "comma separated list of topics check requests will be sent to")
cmd.Flags().StringP("timeout", "t", "", "timeout, in seconds, at which the check has to run")
cmd.Flags().String("ttl", "", "time to live in seconds for which a check result is valid")
cmd.Flags().String("high-flap-threshold", "", "flap detection high threshold (percent state change) for the check")
cmd.Flags().String("low-flap-threshold", "", "flap detection low threshold (percent state change) for the check")
cmd.Flags().String("output-metric-handlers", "", "comma separated list of handlers to set on output check metrics")
cmd.Flags().String("output-metric-format", "", "the output metric format to be used to parse check output for metric extraction")
cmd.Flags().Bool("round-robin", false, "enable round-robin scheduling")
helpers.AddInteractiveFlag(cmd.Flags())
return cmd
}