github.com/fastly/cli@v1.7.2-0.20240304164155-9d0f1d77c3bf/pkg/commands/logging/datadog/create.go (about) 1 package datadog 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 Datadog 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 Placement argparser.OptionalString 31 Region argparser.OptionalString 32 ResponseCondition argparser.OptionalString 33 Token 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 Datadog logging endpoint on a Fastly service version").Alias("add") 44 45 // Required. 46 c.CmdClause.Flag("name", "The name of the Datadog 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("auth-token", "The API key from your Datadog account").Action(c.Token.Set).StringVar(&c.Token.Value) 60 common.Format(c.CmdClause, &c.Format) 61 common.FormatVersion(c.CmdClause, &c.FormatVersion) 62 common.Placement(c.CmdClause, &c.Placement) 63 c.CmdClause.Flag("region", "The region that log data will be sent to. One of US, US3, US5, or EU. Defaults to US if undefined").Action(c.Region.Set).StringVar(&c.Region.Value) 64 common.ResponseCondition(c.CmdClause, &c.ResponseCondition) 65 c.RegisterFlag(argparser.StringFlagOpts{ 66 Name: argparser.FlagServiceIDName, 67 Description: argparser.FlagServiceIDDesc, 68 Dst: &g.Manifest.Flag.ServiceID, 69 Short: 's', 70 }) 71 c.RegisterFlag(argparser.StringFlagOpts{ 72 Action: c.ServiceName.Set, 73 Name: argparser.FlagServiceName, 74 Description: argparser.FlagServiceDesc, 75 Dst: &c.ServiceName.Value, 76 }) 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.CreateDatadogInput, error) { 82 var input fastly.CreateDatadogInput 83 84 input.ServiceID = serviceID 85 input.ServiceVersion = serviceVersion 86 if c.EndpointName.WasSet { 87 input.Name = &c.EndpointName.Value 88 } 89 if c.Token.WasSet { 90 input.Token = &c.Token.Value 91 } 92 93 if c.Region.WasSet { 94 input.Region = &c.Region.Value 95 } 96 97 if c.Format.WasSet { 98 input.Format = &c.Format.Value 99 } 100 101 if c.FormatVersion.WasSet { 102 input.FormatVersion = &c.FormatVersion.Value 103 } 104 105 if c.ResponseCondition.WasSet { 106 input.ResponseCondition = &c.ResponseCondition.Value 107 } 108 109 if c.Placement.WasSet { 110 input.Placement = &c.Placement.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.CreateDatadog(input) 142 if err != nil { 143 c.Globals.ErrLog.Add(err) 144 return err 145 } 146 147 text.Success(out, 148 "Created Datadog 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 }