github.com/fastly/cli@v1.7.2-0.20240304164155-9d0f1d77c3bf/pkg/commands/logging/elasticsearch/update.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  // UpdateCommand calls the Fastly API to update an Elasticsearch logging endpoint.
    17  type UpdateCommand struct {
    18  	argparser.Base
    19  	Manifest manifest.Data
    20  
    21  	// Required.
    22  	EndpointName   string // Can't shadow argparser.Base method Name().
    23  	ServiceName    argparser.OptionalServiceNameID
    24  	ServiceVersion argparser.OptionalServiceVersion
    25  
    26  	// Optional.
    27  	AutoClone         argparser.OptionalAutoClone
    28  	NewName           argparser.OptionalString
    29  	Index             argparser.OptionalString
    30  	URL               argparser.OptionalString
    31  	Pipeline          argparser.OptionalString
    32  	RequestMaxEntries argparser.OptionalInt
    33  	RequestMaxBytes   argparser.OptionalInt
    34  	User              argparser.OptionalString
    35  	Password          argparser.OptionalString
    36  	TLSCACert         argparser.OptionalString
    37  	TLSClientCert     argparser.OptionalString
    38  	TLSClientKey      argparser.OptionalString
    39  	TLSHostname       argparser.OptionalString
    40  	Format            argparser.OptionalString
    41  	FormatVersion     argparser.OptionalInt
    42  	Placement         argparser.OptionalString
    43  	ResponseCondition argparser.OptionalString
    44  }
    45  
    46  // NewUpdateCommand returns a usable command registered under the parent.
    47  func NewUpdateCommand(parent argparser.Registerer, g *global.Data) *UpdateCommand {
    48  	c := UpdateCommand{
    49  		Base: argparser.Base{
    50  			Globals: g,
    51  		},
    52  	}
    53  	c.CmdClause = parent.Command("update", "Update an Elasticsearch logging endpoint on a Fastly service version")
    54  
    55  	// Required.
    56  	c.CmdClause.Flag("name", "The name of the Elasticsearch logging object").Short('n').Required().StringVar(&c.EndpointName)
    57  	c.RegisterFlag(argparser.StringFlagOpts{
    58  		Name:        argparser.FlagVersionName,
    59  		Description: argparser.FlagVersionDesc,
    60  		Dst:         &c.ServiceVersion.Value,
    61  		Required:    true,
    62  	})
    63  
    64  	// Optional.
    65  	c.RegisterAutoCloneFlag(argparser.AutoCloneFlagOpts{
    66  		Action: c.AutoClone.Set,
    67  		Dst:    &c.AutoClone.Value,
    68  	})
    69  	common.Format(c.CmdClause, &c.Format)
    70  	common.FormatVersion(c.CmdClause, &c.FormatVersion)
    71  	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)
    72  	c.CmdClause.Flag("new-name", "New name of the Elasticsearch logging object").Action(c.NewName.Set).StringVar(&c.NewName.Value)
    73  	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)
    74  	common.Placement(c.CmdClause, &c.Placement)
    75  	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)
    76  	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)
    77  	common.ResponseCondition(c.CmdClause, &c.ResponseCondition)
    78  	c.RegisterFlag(argparser.StringFlagOpts{
    79  		Name:        argparser.FlagServiceIDName,
    80  		Description: argparser.FlagServiceIDDesc,
    81  		Dst:         &g.Manifest.Flag.ServiceID,
    82  		Short:       's',
    83  	})
    84  	c.RegisterFlag(argparser.StringFlagOpts{
    85  		Action:      c.ServiceName.Set,
    86  		Name:        argparser.FlagServiceName,
    87  		Description: argparser.FlagServiceDesc,
    88  		Dst:         &c.ServiceName.Value,
    89  	})
    90  	common.TLSCACert(c.CmdClause, &c.TLSCACert)
    91  	common.TLSClientCert(c.CmdClause, &c.TLSClientCert)
    92  	common.TLSClientKey(c.CmdClause, &c.TLSClientKey)
    93  	common.TLSHostname(c.CmdClause, &c.TLSHostname)
    94  	c.CmdClause.Flag("url", "The URL to stream logs to. Must use HTTPS.").Action(c.URL.Set).StringVar(&c.URL.Value)
    95  	return &c
    96  }
    97  
    98  // ConstructInput transforms values parsed from CLI flags into an object to be used by the API client library.
    99  func (c *UpdateCommand) ConstructInput(serviceID string, serviceVersion int) (*fastly.UpdateElasticsearchInput, error) {
   100  	input := fastly.UpdateElasticsearchInput{
   101  		ServiceID:      serviceID,
   102  		ServiceVersion: serviceVersion,
   103  		Name:           c.EndpointName,
   104  	}
   105  
   106  	if c.NewName.WasSet {
   107  		input.NewName = &c.NewName.Value
   108  	}
   109  
   110  	if c.Index.WasSet {
   111  		input.Index = &c.Index.Value
   112  	}
   113  
   114  	if c.URL.WasSet {
   115  		input.URL = &c.URL.Value
   116  	}
   117  
   118  	if c.Pipeline.WasSet {
   119  		input.Pipeline = &c.Pipeline.Value
   120  	}
   121  
   122  	if c.RequestMaxEntries.WasSet {
   123  		input.RequestMaxEntries = &c.RequestMaxEntries.Value
   124  	}
   125  
   126  	if c.RequestMaxBytes.WasSet {
   127  		input.RequestMaxBytes = &c.RequestMaxBytes.Value
   128  	}
   129  
   130  	if c.User.WasSet {
   131  		input.User = &c.User.Value
   132  	}
   133  
   134  	if c.Password.WasSet {
   135  		input.Password = &c.Password.Value
   136  	}
   137  
   138  	if c.TLSCACert.WasSet {
   139  		input.TLSCACert = &c.TLSCACert.Value
   140  	}
   141  
   142  	if c.TLSClientCert.WasSet {
   143  		input.TLSClientCert = &c.TLSClientCert.Value
   144  	}
   145  
   146  	if c.TLSClientKey.WasSet {
   147  		input.TLSClientKey = &c.TLSClientKey.Value
   148  	}
   149  
   150  	if c.TLSHostname.WasSet {
   151  		input.TLSHostname = &c.TLSHostname.Value
   152  	}
   153  
   154  	if c.Format.WasSet {
   155  		input.Format = &c.Format.Value
   156  	}
   157  
   158  	if c.FormatVersion.WasSet {
   159  		input.FormatVersion = &c.FormatVersion.Value
   160  	}
   161  
   162  	if c.ResponseCondition.WasSet {
   163  		input.ResponseCondition = &c.ResponseCondition.Value
   164  	}
   165  
   166  	if c.Placement.WasSet {
   167  		input.Placement = &c.Placement.Value
   168  	}
   169  
   170  	return &input, nil
   171  }
   172  
   173  // Exec invokes the application logic for the command.
   174  func (c *UpdateCommand) Exec(_ io.Reader, out io.Writer) error {
   175  	serviceID, serviceVersion, err := argparser.ServiceDetails(argparser.ServiceDetailsOpts{
   176  		AutoCloneFlag:      c.AutoClone,
   177  		APIClient:          c.Globals.APIClient,
   178  		Manifest:           *c.Globals.Manifest,
   179  		Out:                out,
   180  		ServiceNameFlag:    c.ServiceName,
   181  		ServiceVersionFlag: c.ServiceVersion,
   182  		VerboseMode:        c.Globals.Flags.Verbose,
   183  	})
   184  	if err != nil {
   185  		c.Globals.ErrLog.AddWithContext(err, map[string]any{
   186  			"Service ID":      serviceID,
   187  			"Service Version": errors.ServiceVersion(serviceVersion),
   188  		})
   189  		return err
   190  	}
   191  
   192  	input, err := c.ConstructInput(serviceID, fastly.ToValue(serviceVersion.Number))
   193  	if err != nil {
   194  		c.Globals.ErrLog.Add(err)
   195  		return err
   196  	}
   197  
   198  	elasticsearch, err := c.Globals.APIClient.UpdateElasticsearch(input)
   199  	if err != nil {
   200  		c.Globals.ErrLog.Add(err)
   201  		return err
   202  	}
   203  
   204  	text.Success(out,
   205  		"Updated Elasticsearch logging endpoint %s (service %s version %d)",
   206  		fastly.ToValue(elasticsearch.Name),
   207  		fastly.ToValue(elasticsearch.ServiceID),
   208  		fastly.ToValue(elasticsearch.ServiceVersion),
   209  	)
   210  	return nil
   211  }