github.com/fastly/cli@v1.7.2-0.20240304164155-9d0f1d77c3bf/pkg/commands/logging/loggly/update.go (about) 1 package loggly 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 a Loggly 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 Format argparser.OptionalString 30 FormatVersion argparser.OptionalInt 31 Token argparser.OptionalString 32 ResponseCondition argparser.OptionalString 33 Placement argparser.OptionalString 34 } 35 36 // NewUpdateCommand returns a usable command registered under the parent. 37 func NewUpdateCommand(parent argparser.Registerer, g *global.Data) *UpdateCommand { 38 c := UpdateCommand{ 39 Base: argparser.Base{ 40 Globals: g, 41 }, 42 } 43 c.CmdClause = parent.Command("update", "Update a Loggly logging endpoint on a Fastly service version") 44 45 // Required. 46 c.CmdClause.Flag("name", "The name of the Loggly logging object").Short('n').Required().StringVar(&c.EndpointName) 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 token to use for authentication (https://www.loggly.com/docs/customer-token-authentication-token/)").Action(c.Token.Set).StringVar(&c.Token.Value) 60 common.Format(c.CmdClause, &c.Format) 61 common.FormatVersion(c.CmdClause, &c.FormatVersion) 62 c.CmdClause.Flag("new-name", "New name of the Loggly logging object").Action(c.NewName.Set).StringVar(&c.NewName.Value) 63 common.Placement(c.CmdClause, &c.Placement) 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 *UpdateCommand) ConstructInput(serviceID string, serviceVersion int) (*fastly.UpdateLogglyInput, error) { 82 input := fastly.UpdateLogglyInput{ 83 ServiceID: serviceID, 84 ServiceVersion: serviceVersion, 85 Name: c.EndpointName, 86 } 87 88 if c.NewName.WasSet { 89 input.NewName = &c.NewName.Value 90 } 91 92 if c.Format.WasSet { 93 input.Format = &c.Format.Value 94 } 95 96 if c.FormatVersion.WasSet { 97 input.FormatVersion = &c.FormatVersion.Value 98 } 99 100 if c.Token.WasSet { 101 input.Token = &c.Token.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 *UpdateCommand) 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 loggly, err := c.Globals.APIClient.UpdateLoggly(input) 141 if err != nil { 142 c.Globals.ErrLog.Add(err) 143 return err 144 } 145 146 text.Success(out, 147 "Updated Loggly logging endpoint %s (service %s version %d)", 148 fastly.ToValue(loggly.Name), 149 fastly.ToValue(loggly.ServiceID), 150 fastly.ToValue(loggly.ServiceVersion), 151 ) 152 return nil 153 }