github.com/fastly/cli@v1.7.2-0.20240304164155-9d0f1d77c3bf/pkg/commands/logging/sumologic/create.go (about) 1 package sumologic 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 a Sumologic 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 EndpointName argparser.OptionalString // Can't shadow argparser.Base method Name(). 28 Format argparser.OptionalString 29 FormatVersion argparser.OptionalInt 30 MessageType argparser.OptionalString 31 Placement argparser.OptionalString 32 ResponseCondition argparser.OptionalString 33 URL argparser.OptionalString 34 } 35 36 // NewCreateCommand returns a usable command registered under the parent. 37 func NewCreateCommand(parent argparser.Registerer, g *global.Data) *CreateCommand { 38 c := CreateCommand{ 39 Base: argparser.Base{ 40 Globals: g, 41 }, 42 } 43 c.CmdClause = parent.Command("create", "Create a Sumologic logging endpoint on a Fastly service version").Alias("add") 44 45 // Required. 46 c.CmdClause.Flag("name", "The name of the Sumologic logging object. Used as a primary key for API access").Short('n').Action(c.EndpointName.Set).StringVar(&c.EndpointName.Value) 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("format-version", "The version of the custom logging format used for the configured endpoint. Can be either 2 (the default, version 2 log format) or 1 (the version 1 log format). The logging call gets placed by default in vcl_log if format_version is set to 2 and in vcl_deliver if format_version is set to 1").Action(c.FormatVersion.Set).IntVar(&c.FormatVersion.Value) 60 common.Format(c.CmdClause, &c.Format) 61 common.MessageType(c.CmdClause, &c.MessageType) 62 common.Placement(c.CmdClause, &c.Placement) 63 common.ResponseCondition(c.CmdClause, &c.ResponseCondition) 64 c.RegisterFlag(argparser.StringFlagOpts{ 65 Name: argparser.FlagServiceIDName, 66 Description: argparser.FlagServiceIDDesc, 67 Dst: &g.Manifest.Flag.ServiceID, 68 Short: 's', 69 }) 70 c.RegisterFlag(argparser.StringFlagOpts{ 71 Action: c.ServiceName.Set, 72 Name: argparser.FlagServiceName, 73 Description: argparser.FlagServiceDesc, 74 Dst: &c.ServiceName.Value, 75 }) 76 c.CmdClause.Flag("url", "The URL to POST to").Action(c.URL.Set).StringVar(&c.URL.Value) 77 return &c 78 } 79 80 // ConstructInput transforms values parsed from CLI flags into an object to be used by the API client library. 81 func (c *CreateCommand) ConstructInput(serviceID string, serviceVersion int) (*fastly.CreateSumologicInput, error) { 82 var input fastly.CreateSumologicInput 83 84 input.ServiceID = serviceID 85 input.ServiceVersion = serviceVersion 86 if c.EndpointName.WasSet { 87 input.Name = &c.EndpointName.Value 88 } 89 if c.URL.WasSet { 90 input.URL = &c.URL.Value 91 } 92 93 if c.Format.WasSet { 94 input.Format = &c.Format.Value 95 } 96 97 if c.FormatVersion.WasSet { 98 input.FormatVersion = &c.FormatVersion.Value 99 } 100 101 if c.ResponseCondition.WasSet { 102 input.ResponseCondition = &c.ResponseCondition.Value 103 } 104 105 if c.Placement.WasSet { 106 input.Placement = &c.Placement.Value 107 } 108 109 if c.MessageType.WasSet { 110 input.MessageType = &c.MessageType.Value 111 } 112 113 return &input, nil 114 } 115 116 // Exec invokes the application logic for the command. 117 func (c *CreateCommand) Exec(_ io.Reader, out io.Writer) error { 118 serviceID, serviceVersion, err := argparser.ServiceDetails(argparser.ServiceDetailsOpts{ 119 AutoCloneFlag: c.AutoClone, 120 APIClient: c.Globals.APIClient, 121 Manifest: *c.Globals.Manifest, 122 Out: out, 123 ServiceNameFlag: c.ServiceName, 124 ServiceVersionFlag: c.ServiceVersion, 125 VerboseMode: c.Globals.Flags.Verbose, 126 }) 127 if err != nil { 128 c.Globals.ErrLog.AddWithContext(err, map[string]any{ 129 "Service ID": serviceID, 130 "Service Version": errors.ServiceVersion(serviceVersion), 131 }) 132 return err 133 } 134 135 input, err := c.ConstructInput(serviceID, fastly.ToValue(serviceVersion.Number)) 136 if err != nil { 137 c.Globals.ErrLog.Add(err) 138 return err 139 } 140 141 d, err := c.Globals.APIClient.CreateSumologic(input) 142 if err != nil { 143 c.Globals.ErrLog.Add(err) 144 return err 145 } 146 147 text.Success(out, 148 "Created Sumologic logging endpoint %s (service %s version %d)", 149 fastly.ToValue(d.Name), 150 fastly.ToValue(d.ServiceID), 151 fastly.ToValue(d.ServiceVersion), 152 ) 153 return nil 154 }