github.com/fastly/cli@v1.7.2-0.20240304164155-9d0f1d77c3bf/pkg/commands/logging/heroku/create.go (about) 1 package heroku 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 Heroku 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 ResponseCondition argparser.OptionalString 32 Token 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 Heroku logging endpoint on a Fastly service version").Alias("add") 44 45 // Required. 46 c.CmdClause.Flag("name", "The name of the Heroku 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.CmdClause.Flag("auth-token", "The token to use for authentication (https://devcenter.heroku.com/articles/add-on-partner-log-integration)").Action(c.Token.Set).StringVar(&c.Token.Value) 56 c.RegisterAutoCloneFlag(argparser.AutoCloneFlagOpts{ 57 Action: c.AutoClone.Set, 58 Dst: &c.AutoClone.Value, 59 }) 60 common.Format(c.CmdClause, &c.Format) 61 common.FormatVersion(c.CmdClause, &c.FormatVersion) 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 stream logs 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.CreateHerokuInput, error) { 82 var input fastly.CreateHerokuInput 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 if c.URL.WasSet { 93 input.URL = &c.URL.Value 94 } 95 96 if c.Format.WasSet { 97 input.Format = &c.Format.Value 98 } 99 100 if c.FormatVersion.WasSet { 101 input.FormatVersion = &c.FormatVersion.Value 102 } 103 104 if c.ResponseCondition.WasSet { 105 input.ResponseCondition = &c.ResponseCondition.Value 106 } 107 108 if c.Placement.WasSet { 109 input.Placement = &c.Placement.Value 110 } 111 112 return &input, nil 113 } 114 115 // Exec invokes the application logic for the command. 116 func (c *CreateCommand) Exec(_ io.Reader, out io.Writer) error { 117 serviceID, serviceVersion, err := argparser.ServiceDetails(argparser.ServiceDetailsOpts{ 118 AutoCloneFlag: c.AutoClone, 119 APIClient: c.Globals.APIClient, 120 Manifest: *c.Globals.Manifest, 121 Out: out, 122 ServiceNameFlag: c.ServiceName, 123 ServiceVersionFlag: c.ServiceVersion, 124 VerboseMode: c.Globals.Flags.Verbose, 125 }) 126 if err != nil { 127 c.Globals.ErrLog.AddWithContext(err, map[string]any{ 128 "Service ID": serviceID, 129 "Service Version": errors.ServiceVersion(serviceVersion), 130 }) 131 return err 132 } 133 134 input, err := c.ConstructInput(serviceID, fastly.ToValue(serviceVersion.Number)) 135 if err != nil { 136 c.Globals.ErrLog.Add(err) 137 return err 138 } 139 140 d, err := c.Globals.APIClient.CreateHeroku(input) 141 if err != nil { 142 c.Globals.ErrLog.Add(err) 143 return err 144 } 145 146 text.Success(out, 147 "Created Heroku logging endpoint %s (service %s version %d)", 148 fastly.ToValue(d.Name), 149 fastly.ToValue(d.ServiceID), 150 fastly.ToValue(d.ServiceVersion), 151 ) 152 return nil 153 }