github.com/fastly/cli@v1.7.2-0.20240304164155-9d0f1d77c3bf/pkg/commands/logging/newrelicotlp/update.go (about)

     1  package newrelicotlp
     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  	url               argparser.OptionalString
    33  }
    34  
    35  // NewUpdateCommand returns a usable command registered under the parent.
    36  func NewUpdateCommand(parent argparser.Registerer, g *global.Data) *UpdateCommand {
    37  	c := UpdateCommand{
    38  		Base: argparser.Base{
    39  			Globals: g,
    40  		},
    41  	}
    42  	c.CmdClause = parent.Command("update", "Update a New Relic Logs logging object for a particular service and version")
    43  
    44  	// Required.
    45  	c.CmdClause.Flag("name", "The name for the real-time logging configuration to update").Required().StringVar(&c.endpointName)
    46  	c.RegisterFlag(argparser.StringFlagOpts{
    47  		Name:        argparser.FlagVersionName,
    48  		Description: argparser.FlagVersionDesc,
    49  		Dst:         &c.serviceVersion.Value,
    50  		Required:    true,
    51  	})
    52  
    53  	// Optional.
    54  	c.RegisterAutoCloneFlag(argparser.AutoCloneFlagOpts{
    55  		Action: c.autoClone.Set,
    56  		Dst:    &c.autoClone.Value,
    57  	})
    58  	common.Format(c.CmdClause, &c.format)
    59  	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)
    60  	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)
    61  	c.CmdClause.Flag("new-name", "The name for the real-time logging configuration").Action(c.newName.Set).StringVar(&c.newName.Value)
    62  	c.CmdClause.Flag("placement", "Where in the generated VCL the logging call should be placed").Action(c.placement.Set).StringVar(&c.placement.Value)
    63  	c.CmdClause.Flag("region", "The region to which to stream logs").Action(c.region.Set).StringVar(&c.region.Value)
    64  	c.CmdClause.Flag("response-condition", "The name of an existing condition in the configured endpoint").Action(c.responseCondition.Set).StringVar(&c.responseCondition.Value)
    65  	c.CmdClause.Flag("url", "URL of the New Relic Trace Observer, if you are using New Relic Infinite Tracing").Action(c.url.Set).StringVar(&c.url.Value)
    66  	c.RegisterFlag(argparser.StringFlagOpts{
    67  		Name:        argparser.FlagServiceIDName,
    68  		Description: argparser.FlagServiceIDDesc,
    69  		Dst:         &g.Manifest.Flag.ServiceID,
    70  		Short:       's',
    71  	})
    72  	c.RegisterFlag(argparser.StringFlagOpts{
    73  		Action:      c.serviceName.Set,
    74  		Name:        argparser.FlagServiceName,
    75  		Description: argparser.FlagServiceDesc,
    76  		Dst:         &c.serviceName.Value,
    77  	})
    78  
    79  	return &c
    80  }
    81  
    82  // Exec invokes the application logic for the command.
    83  func (c *UpdateCommand) Exec(_ io.Reader, out io.Writer) error {
    84  	serviceID, serviceVersion, err := argparser.ServiceDetails(argparser.ServiceDetailsOpts{
    85  		AutoCloneFlag:      c.autoClone,
    86  		APIClient:          c.Globals.APIClient,
    87  		Manifest:           *c.Globals.Manifest,
    88  		Out:                out,
    89  		ServiceNameFlag:    c.serviceName,
    90  		ServiceVersionFlag: c.serviceVersion,
    91  		VerboseMode:        c.Globals.Flags.Verbose,
    92  	})
    93  	if err != nil {
    94  		c.Globals.ErrLog.AddWithContext(err, map[string]any{
    95  			"Service ID":      serviceID,
    96  			"Service Version": errors.ServiceVersion(serviceVersion),
    97  		})
    98  		return err
    99  	}
   100  
   101  	input := c.constructInput(serviceID, fastly.ToValue(serviceVersion.Number))
   102  
   103  	l, err := c.Globals.APIClient.UpdateNewRelicOTLP(input)
   104  	if err != nil {
   105  		c.Globals.ErrLog.AddWithContext(err, map[string]any{
   106  			"Service ID":      serviceID,
   107  			"Service Version": fastly.ToValue(serviceVersion.Number),
   108  		})
   109  		return err
   110  	}
   111  
   112  	var prev string
   113  	if c.newName.WasSet {
   114  		prev = fmt.Sprintf("previously: %s, ", c.endpointName)
   115  	}
   116  
   117  	text.Success(out,
   118  		"Updated New Relic OTLP logging endpoint '%s' (%sservice: %s, version: %d)",
   119  		fastly.ToValue(l.Name),
   120  		prev,
   121  		fastly.ToValue(l.ServiceID),
   122  		fastly.ToValue(l.ServiceVersion),
   123  	)
   124  	return nil
   125  }
   126  
   127  // constructInput transforms values parsed from CLI flags into an object to be used by the API client library.
   128  func (c *UpdateCommand) constructInput(serviceID string, serviceVersion int) *fastly.UpdateNewRelicOTLPInput {
   129  	var input fastly.UpdateNewRelicOTLPInput
   130  
   131  	input.Name = c.endpointName
   132  	input.ServiceID = serviceID
   133  	input.ServiceVersion = serviceVersion
   134  
   135  	if c.format.WasSet {
   136  		input.Format = &c.format.Value
   137  	}
   138  	if c.formatVersion.WasSet {
   139  		input.FormatVersion = &c.formatVersion.Value
   140  	}
   141  	if c.key.WasSet {
   142  		input.Token = &c.key.Value
   143  	}
   144  	if c.newName.WasSet {
   145  		input.NewName = &c.newName.Value
   146  	}
   147  	if c.placement.WasSet {
   148  		input.Placement = &c.placement.Value
   149  	}
   150  	if c.region.WasSet {
   151  		input.Region = &c.region.Value
   152  	}
   153  	if c.responseCondition.WasSet {
   154  		input.ResponseCondition = &c.responseCondition.Value
   155  	}
   156  	if c.url.WasSet {
   157  		input.URL = &c.url.Value
   158  	}
   159  
   160  	return &input
   161  }