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

     1  package elasticsearch
     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 an Elasticsearch 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  	Index             argparser.OptionalString
    31  	Password          argparser.OptionalString
    32  	Pipeline          argparser.OptionalString
    33  	Placement         argparser.OptionalString
    34  	RequestMaxBytes   argparser.OptionalInt
    35  	RequestMaxEntries argparser.OptionalInt
    36  	ResponseCondition argparser.OptionalString
    37  	TLSCACert         argparser.OptionalString
    38  	TLSClientCert     argparser.OptionalString
    39  	TLSClientKey      argparser.OptionalString
    40  	TLSHostname       argparser.OptionalString
    41  	URL               argparser.OptionalString
    42  	User              argparser.OptionalString
    43  }
    44  
    45  // NewCreateCommand returns a usable command registered under the parent.
    46  func NewCreateCommand(parent argparser.Registerer, g *global.Data) *CreateCommand {
    47  	c := CreateCommand{
    48  		Base: argparser.Base{
    49  			Globals: g,
    50  		},
    51  	}
    52  	c.CmdClause = parent.Command("create", "Create an Elasticsearch logging endpoint on a Fastly service version").Alias("add")
    53  
    54  	// Required.
    55  	c.CmdClause.Flag("name", "The name of the Elasticsearch logging object. Used as a primary key for API access").Short('n').Action(c.EndpointName.Set).StringVar(&c.EndpointName.Value)
    56  	c.RegisterFlag(argparser.StringFlagOpts{
    57  		Name:        argparser.FlagVersionName,
    58  		Description: argparser.FlagVersionDesc,
    59  		Dst:         &c.ServiceVersion.Value,
    60  		Required:    true,
    61  	})
    62  
    63  	// Optional.
    64  	c.RegisterAutoCloneFlag(argparser.AutoCloneFlagOpts{
    65  		Action: c.AutoClone.Set,
    66  		Dst:    &c.AutoClone.Value,
    67  	})
    68  	common.Format(c.CmdClause, &c.Format)
    69  	common.FormatVersion(c.CmdClause, &c.FormatVersion)
    70  	c.CmdClause.Flag("index", `The name of the Elasticsearch index to send documents (logs) to. The index must follow the Elasticsearch index format rules (https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-create-index.html). We support strftime (http://man7.org/linux/man-pages/man3/strftime.3.html) interpolated variables inside braces prefixed with a pound symbol. For example, #{%F} will interpolate as YYYY-MM-DD with today's date`).Action(c.Index.Set).StringVar(&c.Index.Value)
    71  	common.Placement(c.CmdClause, &c.Placement)
    72  	c.CmdClause.Flag("pipeline", "The ID of the Elasticsearch ingest pipeline to apply pre-process transformations to before indexing. For example my_pipeline_id. Learn more about creating a pipeline in the Elasticsearch docs (https://www.elastic.co/guide/en/elasticsearch/reference/current/ingest.html)").Action(c.Password.Set).StringVar(&c.Pipeline.Value)
    73  	c.CmdClause.Flag("request-max-bytes", "Maximum size of log batch, if non-zero. Defaults to 100MB").Action(c.RequestMaxBytes.Set).IntVar(&c.RequestMaxBytes.Value)
    74  	c.CmdClause.Flag("request-max-entries", "Maximum number of logs to append to a batch, if non-zero. Defaults to 10k").Action(c.RequestMaxEntries.Set).IntVar(&c.RequestMaxEntries.Value)
    75  	common.ResponseCondition(c.CmdClause, &c.ResponseCondition)
    76  	c.RegisterFlag(argparser.StringFlagOpts{
    77  		Name:        argparser.FlagServiceIDName,
    78  		Description: argparser.FlagServiceIDDesc,
    79  		Dst:         &g.Manifest.Flag.ServiceID,
    80  		Short:       's',
    81  	})
    82  	c.RegisterFlag(argparser.StringFlagOpts{
    83  		Action:      c.ServiceName.Set,
    84  		Name:        argparser.FlagServiceName,
    85  		Description: argparser.FlagServiceDesc,
    86  		Dst:         &c.ServiceName.Value,
    87  	})
    88  	common.TLSCACert(c.CmdClause, &c.TLSCACert)
    89  	common.TLSClientCert(c.CmdClause, &c.TLSClientCert)
    90  	common.TLSClientKey(c.CmdClause, &c.TLSClientKey)
    91  	common.TLSHostname(c.CmdClause, &c.TLSHostname)
    92  	c.CmdClause.Flag("url", "The URL to stream logs to. Must use HTTPS.").Action(c.URL.Set).StringVar(&c.URL.Value)
    93  	return &c
    94  }
    95  
    96  // ConstructInput transforms values parsed from CLI flags into an object to be used by the API client library.
    97  func (c *CreateCommand) ConstructInput(serviceID string, serviceVersion int) (*fastly.CreateElasticsearchInput, error) {
    98  	var input fastly.CreateElasticsearchInput
    99  
   100  	input.ServiceID = serviceID
   101  	input.ServiceVersion = serviceVersion
   102  	if c.EndpointName.WasSet {
   103  		input.Name = &c.EndpointName.Value
   104  	}
   105  	if c.Index.WasSet {
   106  		input.Index = &c.Index.Value
   107  	}
   108  	if c.URL.WasSet {
   109  		input.URL = &c.URL.Value
   110  	}
   111  
   112  	if c.Pipeline.WasSet {
   113  		input.Pipeline = &c.Pipeline.Value
   114  	}
   115  
   116  	if c.RequestMaxEntries.WasSet {
   117  		input.RequestMaxEntries = &c.RequestMaxEntries.Value
   118  	}
   119  
   120  	if c.RequestMaxBytes.WasSet {
   121  		input.RequestMaxBytes = &c.RequestMaxBytes.Value
   122  	}
   123  
   124  	if c.User.WasSet {
   125  		input.User = &c.User.Value
   126  	}
   127  
   128  	if c.Password.WasSet {
   129  		input.Password = &c.Password.Value
   130  	}
   131  
   132  	if c.TLSCACert.WasSet {
   133  		input.TLSCACert = &c.TLSCACert.Value
   134  	}
   135  
   136  	if c.TLSClientCert.WasSet {
   137  		input.TLSClientCert = &c.TLSClientCert.Value
   138  	}
   139  
   140  	if c.TLSClientKey.WasSet {
   141  		input.TLSClientKey = &c.TLSClientKey.Value
   142  	}
   143  
   144  	if c.TLSHostname.WasSet {
   145  		input.TLSHostname = &c.TLSHostname.Value
   146  	}
   147  
   148  	if c.Format.WasSet {
   149  		input.Format = &c.Format.Value
   150  	}
   151  
   152  	if c.FormatVersion.WasSet {
   153  		input.FormatVersion = &c.FormatVersion.Value
   154  	}
   155  
   156  	if c.ResponseCondition.WasSet {
   157  		input.ResponseCondition = &c.ResponseCondition.Value
   158  	}
   159  
   160  	if c.Placement.WasSet {
   161  		input.Placement = &c.Placement.Value
   162  	}
   163  
   164  	return &input, nil
   165  }
   166  
   167  // Exec invokes the application logic for the command.
   168  func (c *CreateCommand) Exec(_ io.Reader, out io.Writer) error {
   169  	serviceID, serviceVersion, err := argparser.ServiceDetails(argparser.ServiceDetailsOpts{
   170  		AutoCloneFlag:      c.AutoClone,
   171  		APIClient:          c.Globals.APIClient,
   172  		Manifest:           *c.Globals.Manifest,
   173  		Out:                out,
   174  		ServiceNameFlag:    c.ServiceName,
   175  		ServiceVersionFlag: c.ServiceVersion,
   176  		VerboseMode:        c.Globals.Flags.Verbose,
   177  	})
   178  	if err != nil {
   179  		c.Globals.ErrLog.AddWithContext(err, map[string]any{
   180  			"Service ID":      serviceID,
   181  			"Service Version": errors.ServiceVersion(serviceVersion),
   182  		})
   183  		return err
   184  	}
   185  
   186  	input, err := c.ConstructInput(serviceID, fastly.ToValue(serviceVersion.Number))
   187  	if err != nil {
   188  		c.Globals.ErrLog.Add(err)
   189  		return err
   190  	}
   191  
   192  	d, err := c.Globals.APIClient.CreateElasticsearch(input)
   193  	if err != nil {
   194  		c.Globals.ErrLog.Add(err)
   195  		return err
   196  	}
   197  
   198  	text.Success(out,
   199  		"Created Elasticsearch logging endpoint %s (service %s version %d)",
   200  		fastly.ToValue(d.Name),
   201  		fastly.ToValue(d.ServiceID),
   202  		fastly.ToValue(d.ServiceVersion),
   203  	)
   204  	return nil
   205  }