github.com/fastly/cli@v1.7.2-0.20240304164155-9d0f1d77c3bf/pkg/commands/logging/newrelic/update.go (about) 1 package newrelic 2 3 import ( 4 "fmt" 5 "io" 6 7 "github.com/fastly/go-fastly/v9/fastly" 8 9 "github.com/fastly/cli/pkg/argparser" 10 "github.com/fastly/cli/pkg/commands/logging/common" 11 "github.com/fastly/cli/pkg/errors" 12 "github.com/fastly/cli/pkg/global" 13 "github.com/fastly/cli/pkg/text" 14 ) 15 16 // UpdateCommand calls the Fastly API to update an appropriate resource. 17 type UpdateCommand struct { 18 argparser.Base 19 20 endpointName string 21 serviceName argparser.OptionalServiceNameID 22 serviceVersion argparser.OptionalServiceVersion 23 24 autoClone argparser.OptionalAutoClone 25 format argparser.OptionalString 26 formatVersion argparser.OptionalInt 27 key argparser.OptionalString 28 newName argparser.OptionalString 29 placement argparser.OptionalString 30 region argparser.OptionalString 31 responseCondition argparser.OptionalString 32 } 33 34 // NewUpdateCommand returns a usable command registered under the parent. 35 func NewUpdateCommand(parent argparser.Registerer, g *global.Data) *UpdateCommand { 36 c := UpdateCommand{ 37 Base: argparser.Base{ 38 Globals: g, 39 }, 40 } 41 c.CmdClause = parent.Command("update", "Update a New Relic Logs logging object for a particular service and version") 42 43 // Required. 44 c.CmdClause.Flag("name", "The name for the real-time logging configuration to update").Required().StringVar(&c.endpointName) 45 c.RegisterFlag(argparser.StringFlagOpts{ 46 Name: argparser.FlagVersionName, 47 Description: argparser.FlagVersionDesc, 48 Dst: &c.serviceVersion.Value, 49 Required: true, 50 }) 51 52 // Optional. 53 c.RegisterAutoCloneFlag(argparser.AutoCloneFlagOpts{ 54 Action: c.autoClone.Set, 55 Dst: &c.autoClone.Value, 56 }) 57 common.Format(c.CmdClause, &c.format) 58 c.CmdClause.Flag("format-version", "The version of the custom logging format used for the configured endpoint").Action(c.formatVersion.Set).IntVar(&c.formatVersion.Value) 59 c.CmdClause.Flag("key", "The Insert API key from the Account page of your New Relic account").Action(c.key.Set).StringVar(&c.key.Value) 60 c.CmdClause.Flag("new-name", "The name for the real-time logging configuration").Action(c.newName.Set).StringVar(&c.newName.Value) 61 c.CmdClause.Flag("placement", "Where in the generated VCL the logging call should be placed").Action(c.placement.Set).StringVar(&c.placement.Value) 62 c.CmdClause.Flag("region", "The region to which to stream logs").Action(c.region.Set).StringVar(&c.region.Value) 63 c.CmdClause.Flag("response-condition", "The name of an existing condition in the configured endpoint").Action(c.responseCondition.Set).StringVar(&c.responseCondition.Value) 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 77 return &c 78 } 79 80 // Exec invokes the application logic for the command. 81 func (c *UpdateCommand) Exec(_ io.Reader, out io.Writer) error { 82 serviceID, serviceVersion, err := argparser.ServiceDetails(argparser.ServiceDetailsOpts{ 83 AutoCloneFlag: c.autoClone, 84 APIClient: c.Globals.APIClient, 85 Manifest: *c.Globals.Manifest, 86 Out: out, 87 ServiceNameFlag: c.serviceName, 88 ServiceVersionFlag: c.serviceVersion, 89 VerboseMode: c.Globals.Flags.Verbose, 90 }) 91 if err != nil { 92 c.Globals.ErrLog.AddWithContext(err, map[string]any{ 93 "Service ID": serviceID, 94 "Service Version": errors.ServiceVersion(serviceVersion), 95 }) 96 return err 97 } 98 99 input := c.constructInput(serviceID, fastly.ToValue(serviceVersion.Number)) 100 101 l, err := c.Globals.APIClient.UpdateNewRelic(input) 102 if err != nil { 103 c.Globals.ErrLog.AddWithContext(err, map[string]any{ 104 "Service ID": serviceID, 105 "Service Version": fastly.ToValue(serviceVersion.Number), 106 }) 107 return err 108 } 109 110 var prev string 111 if c.newName.WasSet { 112 prev = fmt.Sprintf("previously: %s, ", c.endpointName) 113 } 114 115 text.Success(out, 116 "Updated New Relic logging endpoint '%s' (%sservice: %s, version: %d)", 117 fastly.ToValue(l.Name), 118 prev, 119 fastly.ToValue(l.ServiceID), 120 fastly.ToValue(l.ServiceVersion), 121 ) 122 return nil 123 } 124 125 // constructInput transforms values parsed from CLI flags into an object to be used by the API client library. 126 func (c *UpdateCommand) constructInput(serviceID string, serviceVersion int) *fastly.UpdateNewRelicInput { 127 var input fastly.UpdateNewRelicInput 128 129 input.Name = c.endpointName 130 input.ServiceID = serviceID 131 input.ServiceVersion = serviceVersion 132 133 if c.format.WasSet { 134 input.Format = &c.format.Value 135 } 136 if c.formatVersion.WasSet { 137 input.FormatVersion = &c.formatVersion.Value 138 } 139 if c.key.WasSet { 140 input.Token = &c.key.Value 141 } 142 if c.newName.WasSet { 143 input.NewName = &c.newName.Value 144 } 145 if c.placement.WasSet { 146 input.Placement = &c.placement.Value 147 } 148 if c.region.WasSet { 149 input.Region = &c.region.Value 150 } 151 if c.responseCondition.WasSet { 152 input.ResponseCondition = &c.responseCondition.Value 153 } 154 155 return &input 156 }