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

     1  package sumologic
     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 a Sumologic 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  	URL               argparser.OptionalString
    30  	Format            argparser.OptionalString
    31  	ResponseCondition argparser.OptionalString
    32  	MessageType       argparser.OptionalString
    33  	FormatVersion     argparser.OptionalInt // Inconsistent with other logging endpoints, but remaining as int to avoid breaking changes in fastly/go-fastly.
    34  	Placement         argparser.OptionalString
    35  }
    36  
    37  // NewUpdateCommand returns a usable command registered under the parent.
    38  func NewUpdateCommand(parent argparser.Registerer, g *global.Data) *UpdateCommand {
    39  	c := UpdateCommand{
    40  		Base: argparser.Base{
    41  			Globals: g,
    42  		},
    43  	}
    44  	c.CmdClause = parent.Command("update", "Update a Sumologic logging endpoint on a Fastly service version")
    45  
    46  	// Required.
    47  	c.CmdClause.Flag("name", "The name of the Sumologic logging object").Short('n').Required().StringVar(&c.EndpointName)
    48  	c.RegisterFlag(argparser.StringFlagOpts{
    49  		Name:        argparser.FlagVersionName,
    50  		Description: argparser.FlagVersionDesc,
    51  		Dst:         &c.ServiceVersion.Value,
    52  		Required:    true,
    53  	})
    54  
    55  	// Optional.
    56  	c.RegisterAutoCloneFlag(argparser.AutoCloneFlagOpts{
    57  		Action: c.AutoClone.Set,
    58  		Dst:    &c.AutoClone.Value,
    59  	})
    60  	common.Format(c.CmdClause, &c.Format)
    61  	c.CmdClause.Flag("format-version", "The version of the custom logging format used for the configured endpoint. Can be either 2 (the default, version 2 log format) or 1 (the version 1 log format). The logging call gets placed by default in vcl_log if format_version is set to 2 and in vcl_deliver if format_version is set to 1").Action(c.FormatVersion.Set).IntVar(&c.FormatVersion.Value)
    62  	common.MessageType(c.CmdClause, &c.MessageType)
    63  	c.CmdClause.Flag("new-name", "New name of the Sumologic logging object").Action(c.NewName.Set).StringVar(&c.NewName.Value)
    64  	common.Placement(c.CmdClause, &c.Placement)
    65  	common.ResponseCondition(c.CmdClause, &c.ResponseCondition)
    66  	c.RegisterFlag(argparser.StringFlagOpts{
    67  		Name:        argparser.FlagServiceIDName,
    68  		Description: argparser.FlagServiceIDDesc,
    69  		Dst:         &g.Manifest.Flag.ServiceID,
    70  		Short:       's',
    71  	})
    72  	c.RegisterFlag(argparser.StringFlagOpts{
    73  		Action:      c.ServiceName.Set,
    74  		Name:        argparser.FlagServiceName,
    75  		Description: argparser.FlagServiceDesc,
    76  		Dst:         &c.ServiceName.Value,
    77  	})
    78  	c.CmdClause.Flag("url", "The URL to POST to").Action(c.URL.Set).StringVar(&c.URL.Value)
    79  	return &c
    80  }
    81  
    82  // ConstructInput transforms values parsed from CLI flags into an object to be used by the API client library.
    83  func (c *UpdateCommand) ConstructInput(serviceID string, serviceVersion int) (*fastly.UpdateSumologicInput, error) {
    84  	input := fastly.UpdateSumologicInput{
    85  		ServiceID:      serviceID,
    86  		ServiceVersion: serviceVersion,
    87  		Name:           c.EndpointName,
    88  	}
    89  
    90  	// Set new values if set by user.
    91  	if c.NewName.WasSet {
    92  		input.NewName = &c.NewName.Value
    93  	}
    94  
    95  	if c.URL.WasSet {
    96  		input.URL = &c.URL.Value
    97  	}
    98  
    99  	if c.Format.WasSet {
   100  		input.Format = &c.Format.Value
   101  	}
   102  
   103  	if c.ResponseCondition.WasSet {
   104  		input.ResponseCondition = &c.ResponseCondition.Value
   105  	}
   106  
   107  	if c.MessageType.WasSet {
   108  		input.MessageType = &c.MessageType.Value
   109  	}
   110  
   111  	if c.FormatVersion.WasSet {
   112  		input.FormatVersion = &c.FormatVersion.Value
   113  	}
   114  
   115  	if c.Placement.WasSet {
   116  		input.Placement = &c.Placement.Value
   117  	}
   118  
   119  	return &input, nil
   120  }
   121  
   122  // Exec invokes the application logic for the command.
   123  func (c *UpdateCommand) Exec(_ io.Reader, out io.Writer) error {
   124  	serviceID, serviceVersion, err := argparser.ServiceDetails(argparser.ServiceDetailsOpts{
   125  		AutoCloneFlag:      c.AutoClone,
   126  		APIClient:          c.Globals.APIClient,
   127  		Manifest:           *c.Globals.Manifest,
   128  		Out:                out,
   129  		ServiceNameFlag:    c.ServiceName,
   130  		ServiceVersionFlag: c.ServiceVersion,
   131  		VerboseMode:        c.Globals.Flags.Verbose,
   132  	})
   133  	if err != nil {
   134  		c.Globals.ErrLog.AddWithContext(err, map[string]any{
   135  			"Service ID":      serviceID,
   136  			"Service Version": errors.ServiceVersion(serviceVersion),
   137  		})
   138  		return err
   139  	}
   140  
   141  	input, err := c.ConstructInput(serviceID, fastly.ToValue(serviceVersion.Number))
   142  	if err != nil {
   143  		c.Globals.ErrLog.Add(err)
   144  		return err
   145  	}
   146  	sumologic, err := c.Globals.APIClient.UpdateSumologic(input)
   147  	if err != nil {
   148  		c.Globals.ErrLog.Add(err)
   149  		return err
   150  	}
   151  
   152  	text.Success(out,
   153  		"Updated Sumologic logging endpoint %s (service %s version %d)",
   154  		fastly.ToValue(sumologic.Name),
   155  		fastly.ToValue(sumologic.ServiceID),
   156  		fastly.ToValue(sumologic.ServiceVersion),
   157  	)
   158  	return nil
   159  }