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