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

     1  package splunk
     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  // CreateCommand calls the Fastly API to create a Splunk logging endpoint.
    17  type CreateCommand struct {
    18  	argparser.Base
    19  	Manifest manifest.Data
    20  
    21  	// Required.
    22  	ServiceName    argparser.OptionalServiceNameID
    23  	ServiceVersion argparser.OptionalServiceVersion
    24  
    25  	// Optional.
    26  	AutoClone         argparser.OptionalAutoClone
    27  	EndpointName      argparser.OptionalString // Can't shadow argparser.Base method Name().
    28  	Format            argparser.OptionalString
    29  	FormatVersion     argparser.OptionalInt
    30  	Placement         argparser.OptionalString
    31  	ResponseCondition argparser.OptionalString
    32  	TimestampFormat   argparser.OptionalString
    33  	TLSCACert         argparser.OptionalString
    34  	TLSClientCert     argparser.OptionalString
    35  	TLSClientKey      argparser.OptionalString
    36  	TLSHostname       argparser.OptionalString
    37  	Token             argparser.OptionalString
    38  	URL               argparser.OptionalString
    39  }
    40  
    41  // NewCreateCommand returns a usable command registered under the parent.
    42  func NewCreateCommand(parent argparser.Registerer, g *global.Data) *CreateCommand {
    43  	c := CreateCommand{
    44  		Base: argparser.Base{
    45  			Globals: g,
    46  		},
    47  	}
    48  	c.CmdClause = parent.Command("create", "Create a Splunk logging endpoint on a Fastly service version").Alias("add")
    49  
    50  	// Required.
    51  	c.CmdClause.Flag("name", "The name of the Splunk logging object. Used as a primary key for API access").Short('n').Action(c.EndpointName.Set).StringVar(&c.EndpointName.Value)
    52  	c.RegisterFlag(argparser.StringFlagOpts{
    53  		Name:        argparser.FlagVersionName,
    54  		Description: argparser.FlagVersionDesc,
    55  		Dst:         &c.ServiceVersion.Value,
    56  		Required:    true,
    57  	})
    58  
    59  	// Optional.
    60  	c.CmdClause.Flag("auth-token", "A Splunk token for use in posting logs over HTTP to your collector").Action(c.Token.Set).StringVar(&c.Token.Value)
    61  	c.RegisterAutoCloneFlag(argparser.AutoCloneFlagOpts{
    62  		Action: c.AutoClone.Set,
    63  		Dst:    &c.AutoClone.Value,
    64  	})
    65  	common.Format(c.CmdClause, &c.Format)
    66  	common.FormatVersion(c.CmdClause, &c.FormatVersion)
    67  	common.Placement(c.CmdClause, &c.Placement)
    68  	common.ResponseCondition(c.CmdClause, &c.ResponseCondition)
    69  	c.RegisterFlag(argparser.StringFlagOpts{
    70  		Name:        argparser.FlagServiceIDName,
    71  		Description: argparser.FlagServiceIDDesc,
    72  		Dst:         &g.Manifest.Flag.ServiceID,
    73  		Short:       's',
    74  	})
    75  	c.RegisterFlag(argparser.StringFlagOpts{
    76  		Action:      c.ServiceName.Set,
    77  		Name:        argparser.FlagServiceName,
    78  		Description: argparser.FlagServiceDesc,
    79  		Dst:         &c.ServiceName.Value,
    80  	})
    81  	common.TLSCACert(c.CmdClause, &c.TLSCACert)
    82  	common.TLSClientCert(c.CmdClause, &c.TLSClientCert)
    83  	common.TLSClientKey(c.CmdClause, &c.TLSClientKey)
    84  	common.TLSHostname(c.CmdClause, &c.TLSHostname)
    85  	c.CmdClause.Flag("url", "The URL to POST to").Action(c.URL.Set).StringVar(&c.URL.Value)
    86  	return &c
    87  }
    88  
    89  // ConstructInput transforms values parsed from CLI flags into an object to be used by the API client library.
    90  func (c *CreateCommand) ConstructInput(serviceID string, serviceVersion int) (*fastly.CreateSplunkInput, error) {
    91  	var input fastly.CreateSplunkInput
    92  
    93  	input.ServiceID = serviceID
    94  	input.ServiceVersion = serviceVersion
    95  	if c.EndpointName.WasSet {
    96  		input.Name = &c.EndpointName.Value
    97  	}
    98  	if c.URL.WasSet {
    99  		input.URL = &c.URL.Value
   100  	}
   101  
   102  	if c.TLSHostname.WasSet {
   103  		input.TLSHostname = &c.TLSHostname.Value
   104  	}
   105  
   106  	if c.TLSCACert.WasSet {
   107  		input.TLSCACert = &c.TLSCACert.Value
   108  	}
   109  
   110  	if c.TLSClientCert.WasSet {
   111  		input.TLSClientCert = &c.TLSClientCert.Value
   112  	}
   113  
   114  	if c.TLSClientKey.WasSet {
   115  		input.TLSClientKey = &c.TLSClientKey.Value
   116  	}
   117  
   118  	if c.Format.WasSet {
   119  		input.Format = &c.Format.Value
   120  	}
   121  
   122  	if c.FormatVersion.WasSet {
   123  		input.FormatVersion = &c.FormatVersion.Value
   124  	}
   125  
   126  	if c.ResponseCondition.WasSet {
   127  		input.ResponseCondition = &c.ResponseCondition.Value
   128  	}
   129  
   130  	if c.Token.WasSet {
   131  		input.Token = &c.Token.Value
   132  	}
   133  
   134  	if c.Placement.WasSet {
   135  		input.Placement = &c.Placement.Value
   136  	}
   137  
   138  	return &input, nil
   139  }
   140  
   141  // Exec invokes the application logic for the command.
   142  func (c *CreateCommand) Exec(_ io.Reader, out io.Writer) error {
   143  	serviceID, serviceVersion, err := argparser.ServiceDetails(argparser.ServiceDetailsOpts{
   144  		AutoCloneFlag:      c.AutoClone,
   145  		APIClient:          c.Globals.APIClient,
   146  		Manifest:           *c.Globals.Manifest,
   147  		Out:                out,
   148  		ServiceNameFlag:    c.ServiceName,
   149  		ServiceVersionFlag: c.ServiceVersion,
   150  		VerboseMode:        c.Globals.Flags.Verbose,
   151  	})
   152  	if err != nil {
   153  		c.Globals.ErrLog.AddWithContext(err, map[string]any{
   154  			"Service ID":      serviceID,
   155  			"Service Version": errors.ServiceVersion(serviceVersion),
   156  		})
   157  		return err
   158  	}
   159  
   160  	input, err := c.ConstructInput(serviceID, fastly.ToValue(serviceVersion.Number))
   161  	if err != nil {
   162  		c.Globals.ErrLog.Add(err)
   163  		return err
   164  	}
   165  
   166  	d, err := c.Globals.APIClient.CreateSplunk(input)
   167  	if err != nil {
   168  		c.Globals.ErrLog.Add(err)
   169  		return err
   170  	}
   171  
   172  	text.Success(out,
   173  		"Created Splunk logging endpoint %s (service %s version %d)",
   174  		fastly.ToValue(d.Name),
   175  		fastly.ToValue(d.ServiceID),
   176  		fastly.ToValue(d.ServiceVersion),
   177  	)
   178  	return nil
   179  }