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

     1  package newrelic
     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  }
    33  
    34  // NewCreateCommand returns a usable command registered under the parent.
    35  func NewCreateCommand(parent argparser.Registerer, g *global.Data) *CreateCommand {
    36  	c := CreateCommand{
    37  		Base: argparser.Base{
    38  			Globals: g,
    39  		},
    40  	}
    41  	c.CmdClause = parent.Command("create", "Create an New Relic logging endpoint attached to the specified service version").Alias("add")
    42  
    43  	// Required.
    44  	c.CmdClause.Flag("name", "The name for the real-time logging configuration").Action(c.name.Set).StringVar(&c.name.Value)
    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  	common.FormatVersion(c.CmdClause, &c.formatVersion)
    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("placement", "Where in the generated VCL the logging call should be placed").Action(c.placement.Set).StringVar(&c.placement.Value)
    61  	c.CmdClause.Flag("region", "The region to which to stream logs").Action(c.region.Set).StringVar(&c.region.Value)
    62  	c.CmdClause.Flag("response-condition", "The name of an existing condition in the configured endpoint").Action(c.responseCondition.Set).StringVar(&c.responseCondition.Value)
    63  	c.RegisterFlag(argparser.StringFlagOpts{
    64  		Name:        argparser.FlagServiceIDName,
    65  		Description: argparser.FlagServiceIDDesc,
    66  		Dst:         &g.Manifest.Flag.ServiceID,
    67  		Short:       's',
    68  	})
    69  	c.RegisterFlag(argparser.StringFlagOpts{
    70  		Action:      c.serviceName.Set,
    71  		Name:        argparser.FlagServiceName,
    72  		Description: argparser.FlagServiceDesc,
    73  		Dst:         &c.serviceName.Value,
    74  	})
    75  
    76  	return &c
    77  }
    78  
    79  // Exec invokes the application logic for the command.
    80  func (c *CreateCommand) Exec(_ io.Reader, out io.Writer) error {
    81  	serviceID, serviceVersion, err := argparser.ServiceDetails(argparser.ServiceDetailsOpts{
    82  		AutoCloneFlag:      c.autoClone,
    83  		APIClient:          c.Globals.APIClient,
    84  		Manifest:           *c.Globals.Manifest,
    85  		Out:                out,
    86  		ServiceNameFlag:    c.serviceName,
    87  		ServiceVersionFlag: c.serviceVersion,
    88  		VerboseMode:        c.Globals.Flags.Verbose,
    89  	})
    90  	if err != nil {
    91  		c.Globals.ErrLog.AddWithContext(err, map[string]any{
    92  			"Service ID":      serviceID,
    93  			"Service Version": errors.ServiceVersion(serviceVersion),
    94  		})
    95  		return err
    96  	}
    97  
    98  	input := c.constructInput(serviceID, fastly.ToValue(serviceVersion.Number))
    99  
   100  	l, err := c.Globals.APIClient.CreateNewRelic(input)
   101  	if err != nil {
   102  		c.Globals.ErrLog.AddWithContext(err, map[string]any{
   103  			"Service ID":      serviceID,
   104  			"Service Version": fastly.ToValue(serviceVersion.Number),
   105  		})
   106  		return err
   107  	}
   108  
   109  	text.Success(out,
   110  		"Created New Relic logging endpoint '%s' (service: %s, version: %d)",
   111  		fastly.ToValue(l.Name),
   112  		fastly.ToValue(l.ServiceID),
   113  		fastly.ToValue(l.ServiceVersion),
   114  	)
   115  	return nil
   116  }
   117  
   118  // constructInput transforms values parsed from CLI flags into an object to be used by the API client library.
   119  func (c *CreateCommand) constructInput(serviceID string, serviceVersion int) *fastly.CreateNewRelicInput {
   120  	var input fastly.CreateNewRelicInput
   121  
   122  	if c.name.WasSet {
   123  		input.Name = &c.name.Value
   124  	}
   125  	input.ServiceID = serviceID
   126  	input.ServiceVersion = serviceVersion
   127  	if c.key.WasSet {
   128  		input.Token = &c.key.Value
   129  	}
   130  
   131  	if c.format.WasSet {
   132  		input.Format = &c.format.Value
   133  	}
   134  
   135  	if c.formatVersion.WasSet {
   136  		input.FormatVersion = &c.formatVersion.Value
   137  	}
   138  
   139  	if c.placement.WasSet {
   140  		input.Placement = &c.placement.Value
   141  	}
   142  
   143  	if c.region.WasSet {
   144  		input.Region = &c.region.Value
   145  	}
   146  
   147  	if c.responseCondition.WasSet {
   148  		input.ResponseCondition = &c.responseCondition.Value
   149  	}
   150  
   151  	return &input
   152  }