github.com/fastly/cli@v1.7.2-0.20240304164155-9d0f1d77c3bf/pkg/commands/healthcheck/create.go (about) 1 package healthcheck 2 3 import ( 4 "io" 5 6 "github.com/fastly/go-fastly/v9/fastly" 7 8 "github.com/fastly/cli/pkg/argparser" 9 "github.com/fastly/cli/pkg/errors" 10 "github.com/fastly/cli/pkg/global" 11 "github.com/fastly/cli/pkg/text" 12 ) 13 14 // CreateCommand calls the Fastly API to create healthchecks. 15 type CreateCommand struct { 16 argparser.Base 17 18 // Required. 19 serviceVersion argparser.OptionalServiceVersion 20 21 // Optional. 22 autoClone argparser.OptionalAutoClone 23 checkInterval argparser.OptionalInt 24 comment argparser.OptionalString 25 expectedResponse argparser.OptionalInt 26 host argparser.OptionalString 27 httpVersion argparser.OptionalString 28 initial argparser.OptionalInt 29 method argparser.OptionalString 30 name argparser.OptionalString 31 path argparser.OptionalString 32 serviceName argparser.OptionalServiceNameID 33 threshold argparser.OptionalInt 34 timeout argparser.OptionalInt 35 window argparser.OptionalInt 36 } 37 38 // NewCreateCommand returns a usable command registered under the parent. 39 func NewCreateCommand(parent argparser.Registerer, g *global.Data) *CreateCommand { 40 c := CreateCommand{ 41 Base: argparser.Base{ 42 Globals: g, 43 }, 44 } 45 c.CmdClause = parent.Command("create", "Create a healthcheck on a Fastly service version").Alias("add") 46 47 // Required. 48 c.RegisterFlag(argparser.StringFlagOpts{ 49 Name: argparser.FlagVersionName, 50 Description: argparser.FlagVersionDesc, 51 Dst: &c.serviceVersion.Value, 52 Required: true, 53 }) 54 55 // Optional. 56 c.RegisterAutoCloneFlag(argparser.AutoCloneFlagOpts{ 57 Action: c.autoClone.Set, 58 Dst: &c.autoClone.Value, 59 }) 60 c.CmdClause.Flag("check-interval", "How often to run the healthcheck in milliseconds").Action(c.checkInterval.Set).IntVar(&c.checkInterval.Value) 61 c.CmdClause.Flag("comment", "A descriptive note").Action(c.comment.Set).StringVar(&c.comment.Value) 62 c.CmdClause.Flag("expected-response", "The status code expected from the host").Action(c.expectedResponse.Set).IntVar(&c.expectedResponse.Value) 63 c.CmdClause.Flag("host", "Which host to check").Action(c.host.Set).StringVar(&c.host.Value) 64 c.CmdClause.Flag("http-version", "Whether to use version 1.0 or 1.1 HTTP").Action(c.httpVersion.Set).StringVar(&c.httpVersion.Value) 65 c.CmdClause.Flag("initial", "When loading a config, the initial number of probes to be seen as OK").Action(c.initial.Set).IntVar(&c.initial.Value) 66 c.CmdClause.Flag("method", "Which HTTP method to use").Action(c.method.Set).StringVar(&c.method.Value) 67 c.CmdClause.Flag("name", "Healthcheck name").Short('n').Action(c.name.Set).StringVar(&c.name.Value) 68 c.CmdClause.Flag("path", "The path to check").Action(c.path.Set).StringVar(&c.path.Value) 69 c.RegisterFlag(argparser.StringFlagOpts{ 70 Name: argparser.FlagServiceIDName, 71 Description: argparser.FlagServiceIDDesc, 72 Dst: &g.Manifest.Flag.ServiceID, 73 Short: 's', 74 }) 75 c.RegisterFlag(argparser.StringFlagOpts{ 76 Action: c.serviceName.Set, 77 Name: argparser.FlagServiceName, 78 Description: argparser.FlagServiceDesc, 79 Dst: &c.serviceName.Value, 80 }) 81 c.CmdClause.Flag("threshold", "How many healthchecks must succeed to be considered healthy").Action(c.threshold.Set).IntVar(&c.threshold.Value) 82 c.CmdClause.Flag("timeout", "Timeout in milliseconds").Action(c.timeout.Set).IntVar(&c.timeout.Value) 83 c.CmdClause.Flag("window", "The number of most recent healthcheck queries to keep for this healthcheck").Action(c.window.Set).IntVar(&c.window.Value) 84 return &c 85 } 86 87 // Exec invokes the application logic for the command. 88 func (c *CreateCommand) Exec(_ io.Reader, out io.Writer) error { 89 serviceID, serviceVersion, err := argparser.ServiceDetails(argparser.ServiceDetailsOpts{ 90 AutoCloneFlag: c.autoClone, 91 APIClient: c.Globals.APIClient, 92 Manifest: *c.Globals.Manifest, 93 Out: out, 94 ServiceNameFlag: c.serviceName, 95 ServiceVersionFlag: c.serviceVersion, 96 VerboseMode: c.Globals.Flags.Verbose, 97 }) 98 if err != nil { 99 c.Globals.ErrLog.AddWithContext(err, map[string]any{ 100 "Service ID": serviceID, 101 "Service Version": errors.ServiceVersion(serviceVersion), 102 }) 103 return err 104 } 105 input := fastly.CreateHealthCheckInput{ 106 ServiceID: serviceID, 107 ServiceVersion: fastly.ToValue(serviceVersion.Number), 108 } 109 110 if c.name.WasSet { 111 input.Name = &c.name.Value 112 } 113 if c.comment.WasSet { 114 input.Comment = &c.comment.Value 115 } 116 if c.method.WasSet { 117 input.Method = &c.method.Value 118 } 119 if c.host.WasSet { 120 input.Host = &c.host.Value 121 } 122 if c.path.WasSet { 123 input.Path = &c.path.Value 124 } 125 if c.httpVersion.WasSet { 126 input.HTTPVersion = &c.httpVersion.Value 127 } 128 if c.timeout.WasSet { 129 input.Timeout = &c.timeout.Value 130 } 131 if c.checkInterval.WasSet { 132 input.CheckInterval = &c.checkInterval.Value 133 } 134 if c.expectedResponse.WasSet { 135 input.ExpectedResponse = &c.expectedResponse.Value 136 } 137 if c.window.WasSet { 138 input.Window = &c.window.Value 139 } 140 if c.threshold.WasSet { 141 input.Threshold = &c.threshold.Value 142 } 143 if c.initial.WasSet { 144 input.Initial = &c.initial.Value 145 } 146 147 h, err := c.Globals.APIClient.CreateHealthCheck(&input) 148 if err != nil { 149 c.Globals.ErrLog.AddWithContext(err, map[string]any{ 150 "Service ID": serviceID, 151 "Service Version": serviceVersion.Number, 152 }) 153 return err 154 } 155 156 text.Success(out, "Created healthcheck %s (service %s version %d)", fastly.ToValue(h.Name), fastly.ToValue(h.ServiceID), fastly.ToValue(h.ServiceVersion)) 157 return nil 158 }