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