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

     1  package newrelicotlp
     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/text"
    13  )
    14  
    15  // CreateCommand calls the Fastly API to create an appropriate resource.
    16  type CreateCommand struct {
    17  	argparser.Base
    18  
    19  	// Required.
    20  	serviceName    argparser.OptionalServiceNameID
    21  	serviceVersion argparser.OptionalServiceVersion
    22  
    23  	// Optional.
    24  	autoClone         argparser.OptionalAutoClone
    25  	format            argparser.OptionalString
    26  	formatVersion     argparser.OptionalInt
    27  	key               argparser.OptionalString
    28  	name              argparser.OptionalString
    29  	placement         argparser.OptionalString
    30  	region            argparser.OptionalString
    31  	responseCondition argparser.OptionalString
    32  	url               argparser.OptionalString
    33  }
    34  
    35  // NewCreateCommand returns a usable command registered under the parent.
    36  func NewCreateCommand(parent argparser.Registerer, g *global.Data) *CreateCommand {
    37  	c := CreateCommand{
    38  		Base: argparser.Base{
    39  			Globals: g,
    40  		},
    41  	}
    42  	c.CmdClause = parent.Command("create", "Create an New Relic logging endpoint attached to the specified service version").Alias("add")
    43  
    44  	// Required.
    45  	c.CmdClause.Flag("name", "The name for the real-time logging configuration").Action(c.name.Set).StringVar(&c.name.Value)
    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  	common.FormatVersion(c.CmdClause, &c.formatVersion)
    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("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.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)
    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  
    78  	return &c
    79  }
    80  
    81  // Exec invokes the application logic for the command.
    82  func (c *CreateCommand) Exec(_ io.Reader, out io.Writer) error {
    83  	serviceID, serviceVersion, err := argparser.ServiceDetails(argparser.ServiceDetailsOpts{
    84  		AutoCloneFlag:      c.autoClone,
    85  		APIClient:          c.Globals.APIClient,
    86  		Manifest:           *c.Globals.Manifest,
    87  		Out:                out,
    88  		ServiceNameFlag:    c.serviceName,
    89  		ServiceVersionFlag: c.serviceVersion,
    90  		VerboseMode:        c.Globals.Flags.Verbose,
    91  	})
    92  	if err != nil {
    93  		c.Globals.ErrLog.AddWithContext(err, map[string]any{
    94  			"Service ID":      serviceID,
    95  			"Service Version": errors.ServiceVersion(serviceVersion),
    96  		})
    97  		return err
    98  	}
    99  
   100  	input := c.constructInput(serviceID, fastly.ToValue(serviceVersion.Number))
   101  
   102  	l, err := c.Globals.APIClient.CreateNewRelicOTLP(input)
   103  	if err != nil {
   104  		c.Globals.ErrLog.AddWithContext(err, map[string]any{
   105  			"Service ID":      serviceID,
   106  			"Service Version": serviceVersion.Number,
   107  		})
   108  		return err
   109  	}
   110  
   111  	text.Success(out,
   112  		"Created New Relic OTLP logging endpoint '%s' (service: %s, version: %d)",
   113  		fastly.ToValue(l.Name),
   114  		fastly.ToValue(l.ServiceID),
   115  		fastly.ToValue(l.ServiceVersion),
   116  	)
   117  	return nil
   118  }
   119  
   120  // constructInput transforms values parsed from CLI flags into an object to be used by the API client library.
   121  func (c *CreateCommand) constructInput(serviceID string, serviceVersion int) *fastly.CreateNewRelicOTLPInput {
   122  	var input fastly.CreateNewRelicOTLPInput
   123  
   124  	if c.name.WasSet {
   125  		input.Name = &c.name.Value
   126  	}
   127  	input.ServiceID = serviceID
   128  	input.ServiceVersion = serviceVersion
   129  	if c.key.WasSet {
   130  		input.Token = &c.key.Value
   131  	}
   132  
   133  	if c.format.WasSet {
   134  		input.Format = &c.format.Value
   135  	}
   136  
   137  	if c.formatVersion.WasSet {
   138  		input.FormatVersion = &c.formatVersion.Value
   139  	}
   140  
   141  	if c.placement.WasSet {
   142  		input.Placement = &c.placement.Value
   143  	}
   144  
   145  	if c.region.WasSet {
   146  		input.Region = &c.region.Value
   147  	}
   148  
   149  	if c.responseCondition.WasSet {
   150  		input.ResponseCondition = &c.responseCondition.Value
   151  	}
   152  
   153  	if c.url.WasSet {
   154  		input.URL = &c.url.Value
   155  	}
   156  
   157  	return &input
   158  }