github.com/fastly/cli@v1.7.2-0.20240304164155-9d0f1d77c3bf/pkg/commands/logging/https/update.go (about) 1 package https 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/commands/logging/common" 10 "github.com/fastly/cli/pkg/errors" 11 "github.com/fastly/cli/pkg/global" 12 "github.com/fastly/cli/pkg/manifest" 13 "github.com/fastly/cli/pkg/text" 14 ) 15 16 // UpdateCommand calls the Fastly API to update an HTTPS logging endpoint. 17 type UpdateCommand struct { 18 argparser.Base 19 Manifest manifest.Data 20 21 // Required. 22 EndpointName string // Can't shadow argparser.Base method Name(). 23 ServiceName argparser.OptionalServiceNameID 24 ServiceVersion argparser.OptionalServiceVersion 25 26 // Optional. 27 AutoClone argparser.OptionalAutoClone 28 NewName argparser.OptionalString 29 URL argparser.OptionalString 30 RequestMaxEntries argparser.OptionalInt 31 RequestMaxBytes argparser.OptionalInt 32 TLSCACert argparser.OptionalString 33 TLSClientCert argparser.OptionalString 34 TLSClientKey argparser.OptionalString 35 TLSHostname argparser.OptionalString 36 MessageType argparser.OptionalString 37 ContentType argparser.OptionalString 38 HeaderName argparser.OptionalString 39 HeaderValue argparser.OptionalString 40 Method argparser.OptionalString 41 JSONFormat argparser.OptionalString 42 Format argparser.OptionalString 43 FormatVersion argparser.OptionalInt 44 Placement argparser.OptionalString 45 ResponseCondition argparser.OptionalString 46 } 47 48 // NewUpdateCommand returns a usable command registered under the parent. 49 func NewUpdateCommand(parent argparser.Registerer, g *global.Data) *UpdateCommand { 50 c := UpdateCommand{ 51 Base: argparser.Base{ 52 Globals: g, 53 }, 54 } 55 c.CmdClause = parent.Command("update", "Update an HTTPS logging endpoint on a Fastly service version") 56 57 // Required. 58 c.CmdClause.Flag("name", "The name of the HTTPS logging object").Short('n').Required().StringVar(&c.EndpointName) 59 c.RegisterFlag(argparser.StringFlagOpts{ 60 Name: argparser.FlagVersionName, 61 Description: argparser.FlagVersionDesc, 62 Dst: &c.ServiceVersion.Value, 63 Required: true, 64 }) 65 66 // Optional. 67 c.RegisterAutoCloneFlag(argparser.AutoCloneFlagOpts{ 68 Action: c.AutoClone.Set, 69 Dst: &c.AutoClone.Value, 70 }) 71 c.CmdClause.Flag("content-type", "Content type of the header sent with the request").Action(c.ContentType.Set).StringVar(&c.ContentType.Value) 72 common.Format(c.CmdClause, &c.Format) 73 common.FormatVersion(c.CmdClause, &c.FormatVersion) 74 c.CmdClause.Flag("header-name", "Name of the custom header sent with the request").Action(c.HeaderName.Set).StringVar(&c.HeaderName.Value) 75 c.CmdClause.Flag("header-value", "Value of the custom header sent with the request").Action(c.HeaderValue.Set).StringVar(&c.HeaderValue.Value) 76 c.CmdClause.Flag("json-format", "Enforces valid JSON formatting for log entries. Can be disabled 0, array of json (wraps JSON log batches in an array) 1, or newline delimited json (places each JSON log entry onto a new line in a batch) 2").Action(c.JSONFormat.Set).StringVar(&c.JSONFormat.Value) 77 common.MessageType(c.CmdClause, &c.MessageType) 78 c.CmdClause.Flag("method", "HTTP method used for request. Can be POST or PUT. Defaults to POST if not specified").Action(c.Method.Set).StringVar(&c.Method.Value) 79 c.CmdClause.Flag("new-name", "New name of the HTTPS logging object").Action(c.NewName.Set).StringVar(&c.NewName.Value) 80 common.Placement(c.CmdClause, &c.Placement) 81 c.CmdClause.Flag("request-max-bytes", "Maximum size of log batch, if non-zero. Defaults to 100MB").Action(c.RequestMaxBytes.Set).IntVar(&c.RequestMaxBytes.Value) 82 c.CmdClause.Flag("request-max-entries", "Maximum number of logs to append to a batch, if non-zero. Defaults to 10k").Action(c.RequestMaxEntries.Set).IntVar(&c.RequestMaxEntries.Value) 83 common.ResponseCondition(c.CmdClause, &c.ResponseCondition) 84 c.RegisterFlag(argparser.StringFlagOpts{ 85 Name: argparser.FlagServiceIDName, 86 Description: argparser.FlagServiceIDDesc, 87 Dst: &g.Manifest.Flag.ServiceID, 88 Short: 's', 89 }) 90 c.RegisterFlag(argparser.StringFlagOpts{ 91 Action: c.ServiceName.Set, 92 Name: argparser.FlagServiceName, 93 Description: argparser.FlagServiceDesc, 94 Dst: &c.ServiceName.Value, 95 }) 96 common.TLSCACert(c.CmdClause, &c.TLSCACert) 97 common.TLSClientCert(c.CmdClause, &c.TLSClientCert) 98 common.TLSClientKey(c.CmdClause, &c.TLSClientKey) 99 common.TLSHostname(c.CmdClause, &c.TLSHostname) 100 c.CmdClause.Flag("url", "URL that log data will be sent to. Must use the https protocol").Action(c.URL.Set).StringVar(&c.URL.Value) 101 return &c 102 } 103 104 // ConstructInput transforms values parsed from CLI flags into an object to be used by the API client library. 105 func (c *UpdateCommand) ConstructInput(serviceID string, serviceVersion int) (*fastly.UpdateHTTPSInput, error) { 106 input := fastly.UpdateHTTPSInput{ 107 ServiceID: serviceID, 108 ServiceVersion: serviceVersion, 109 Name: c.EndpointName, 110 } 111 112 if c.NewName.WasSet { 113 input.NewName = &c.NewName.Value 114 } 115 116 if c.URL.WasSet { 117 input.URL = &c.URL.Value 118 } 119 120 if c.ContentType.WasSet { 121 input.ContentType = &c.ContentType.Value 122 } 123 124 if c.JSONFormat.WasSet { 125 input.JSONFormat = &c.JSONFormat.Value 126 } 127 128 if c.HeaderName.WasSet { 129 input.HeaderName = &c.HeaderName.Value 130 } 131 132 if c.HeaderValue.WasSet { 133 input.HeaderValue = &c.HeaderValue.Value 134 } 135 136 if c.Method.WasSet { 137 input.Method = &c.Method.Value 138 } 139 140 if c.RequestMaxEntries.WasSet { 141 input.RequestMaxEntries = &c.RequestMaxEntries.Value 142 } 143 144 if c.RequestMaxBytes.WasSet { 145 input.RequestMaxBytes = &c.RequestMaxBytes.Value 146 } 147 148 if c.TLSCACert.WasSet { 149 input.TLSCACert = &c.TLSCACert.Value 150 } 151 152 if c.TLSClientCert.WasSet { 153 input.TLSClientCert = &c.TLSClientCert.Value 154 } 155 156 if c.TLSClientKey.WasSet { 157 input.TLSClientKey = &c.TLSClientKey.Value 158 } 159 160 if c.TLSHostname.WasSet { 161 input.TLSHostname = &c.TLSHostname.Value 162 } 163 164 if c.Format.WasSet { 165 input.Format = &c.Format.Value 166 } 167 168 if c.FormatVersion.WasSet { 169 input.FormatVersion = &c.FormatVersion.Value 170 } 171 172 if c.ResponseCondition.WasSet { 173 input.ResponseCondition = &c.ResponseCondition.Value 174 } 175 176 if c.Placement.WasSet { 177 input.Placement = &c.Placement.Value 178 } 179 180 if c.MessageType.WasSet { 181 input.MessageType = &c.MessageType.Value 182 } 183 184 return &input, nil 185 } 186 187 // Exec invokes the application logic for the command. 188 func (c *UpdateCommand) Exec(_ io.Reader, out io.Writer) error { 189 serviceID, serviceVersion, err := argparser.ServiceDetails(argparser.ServiceDetailsOpts{ 190 AutoCloneFlag: c.AutoClone, 191 APIClient: c.Globals.APIClient, 192 Manifest: *c.Globals.Manifest, 193 Out: out, 194 ServiceNameFlag: c.ServiceName, 195 ServiceVersionFlag: c.ServiceVersion, 196 VerboseMode: c.Globals.Flags.Verbose, 197 }) 198 if err != nil { 199 c.Globals.ErrLog.AddWithContext(err, map[string]any{ 200 "Service ID": serviceID, 201 "Service Version": errors.ServiceVersion(serviceVersion), 202 }) 203 return err 204 } 205 206 input, err := c.ConstructInput(serviceID, fastly.ToValue(serviceVersion.Number)) 207 if err != nil { 208 c.Globals.ErrLog.Add(err) 209 return err 210 } 211 212 https, err := c.Globals.APIClient.UpdateHTTPS(input) 213 if err != nil { 214 c.Globals.ErrLog.Add(err) 215 return err 216 } 217 218 text.Success(out, 219 "Updated HTTPS logging endpoint %s (service %s version %d)", 220 fastly.ToValue(https.Name), 221 fastly.ToValue(https.ServiceID), 222 fastly.ToValue(https.ServiceVersion), 223 ) 224 return nil 225 }