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

     1  package syslog
     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 Syslog logging endpoint.
    17  type UpdateCommand struct {
    18  	argparser.Base
    19  	Manifest manifest.Data
    20  
    21  	// Required.
    22  	EndpointName   string
    23  	ServiceName    argparser.OptionalServiceNameID
    24  	ServiceVersion argparser.OptionalServiceVersion
    25  
    26  	// Optional.
    27  	AutoClone         argparser.OptionalAutoClone
    28  	NewName           argparser.OptionalString
    29  	Address           argparser.OptionalString
    30  	Port              argparser.OptionalInt
    31  	UseTLS            argparser.OptionalBool
    32  	TLSCACert         argparser.OptionalString
    33  	TLSHostname       argparser.OptionalString
    34  	TLSClientCert     argparser.OptionalString
    35  	TLSClientKey      argparser.OptionalString
    36  	Token             argparser.OptionalString
    37  	Format            argparser.OptionalString
    38  	FormatVersion     argparser.OptionalInt
    39  	MessageType       argparser.OptionalString
    40  	ResponseCondition argparser.OptionalString
    41  	Placement         argparser.OptionalString
    42  }
    43  
    44  // NewUpdateCommand returns a usable command registered under the parent.
    45  func NewUpdateCommand(parent argparser.Registerer, g *global.Data) *UpdateCommand {
    46  	c := UpdateCommand{
    47  		Base: argparser.Base{
    48  			Globals: g,
    49  		},
    50  	}
    51  	c.CmdClause = parent.Command("update", "Update a Syslog logging endpoint on a Fastly service version")
    52  
    53  	// Required.
    54  	c.CmdClause.Flag("name", "The name of the Syslog logging object").Short('n').Required().StringVar(&c.EndpointName)
    55  	c.RegisterFlag(argparser.StringFlagOpts{
    56  		Name:        argparser.FlagVersionName,
    57  		Description: argparser.FlagVersionDesc,
    58  		Dst:         &c.ServiceVersion.Value,
    59  		Required:    true,
    60  	})
    61  
    62  	// Optional.
    63  	c.CmdClause.Flag("address", "A hostname or IPv4 address").Action(c.Address.Set).StringVar(&c.Address.Value)
    64  	c.CmdClause.Flag("auth-token", "Whether to prepend each message with a specific token").Action(c.Token.Set).StringVar(&c.Token.Value)
    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("new-name", "New name of the Syslog logging object").Action(c.NewName.Set).StringVar(&c.NewName.Value)
    72  	common.MessageType(c.CmdClause, &c.MessageType)
    73  	common.Placement(c.CmdClause, &c.Placement)
    74  	c.CmdClause.Flag("port", "The port number").Action(c.Port.Set).IntVar(&c.Port.Value)
    75  	c.RegisterFlag(argparser.StringFlagOpts{
    76  		Name:        argparser.FlagServiceIDName,
    77  		Description: argparser.FlagServiceIDDesc,
    78  		Dst:         &g.Manifest.Flag.ServiceID,
    79  		Short:       's',
    80  	})
    81  	c.RegisterFlag(argparser.StringFlagOpts{
    82  		Action:      c.ServiceName.Set,
    83  		Name:        argparser.FlagServiceName,
    84  		Description: argparser.FlagServiceDesc,
    85  		Dst:         &c.ServiceName.Value,
    86  	})
    87  	common.ResponseCondition(c.CmdClause, &c.ResponseCondition)
    88  	common.TLSCACert(c.CmdClause, &c.TLSCACert)
    89  	common.TLSClientCert(c.CmdClause, &c.TLSClientCert)
    90  	common.TLSClientKey(c.CmdClause, &c.TLSClientKey)
    91  	c.CmdClause.Flag("tls-hostname", "Used during the TLS handshake to validate the certificate").Action(c.TLSHostname.Set).StringVar(&c.TLSHostname.Value)
    92  	c.CmdClause.Flag("use-tls", "Whether to use TLS for secure logging. Can be either true or false").Action(c.UseTLS.Set).BoolVar(&c.UseTLS.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 *UpdateCommand) ConstructInput(serviceID string, serviceVersion int) (*fastly.UpdateSyslogInput, error) {
    98  	input := fastly.UpdateSyslogInput{
    99  		ServiceID:      serviceID,
   100  		ServiceVersion: serviceVersion,
   101  		Name:           c.EndpointName,
   102  	}
   103  
   104  	// Set new values if set by user.
   105  	if c.NewName.WasSet {
   106  		input.NewName = &c.NewName.Value
   107  	}
   108  
   109  	if c.Address.WasSet {
   110  		input.Address = &c.Address.Value
   111  	}
   112  
   113  	if c.Port.WasSet {
   114  		input.Port = &c.Port.Value
   115  	}
   116  
   117  	if c.UseTLS.WasSet {
   118  		input.UseTLS = fastly.ToPointer(fastly.Compatibool(c.UseTLS.Value))
   119  	}
   120  
   121  	if c.TLSCACert.WasSet {
   122  		input.TLSCACert = &c.TLSCACert.Value
   123  	}
   124  
   125  	if c.TLSHostname.WasSet {
   126  		input.TLSHostname = &c.TLSHostname.Value
   127  	}
   128  
   129  	if c.TLSClientCert.WasSet {
   130  		input.TLSClientCert = &c.TLSClientCert.Value
   131  	}
   132  
   133  	if c.TLSClientKey.WasSet {
   134  		input.TLSClientKey = &c.TLSClientKey.Value
   135  	}
   136  
   137  	if c.Token.WasSet {
   138  		input.Token = &c.Token.Value
   139  	}
   140  
   141  	if c.Format.WasSet {
   142  		input.Format = &c.Format.Value
   143  	}
   144  
   145  	if c.FormatVersion.WasSet {
   146  		input.FormatVersion = &c.FormatVersion.Value
   147  	}
   148  
   149  	if c.MessageType.WasSet {
   150  		input.MessageType = &c.MessageType.Value
   151  	}
   152  
   153  	if c.ResponseCondition.WasSet {
   154  		input.ResponseCondition = &c.ResponseCondition.Value
   155  	}
   156  
   157  	if c.Placement.WasSet {
   158  		input.Placement = &c.Placement.Value
   159  	}
   160  
   161  	return &input, nil
   162  }
   163  
   164  // Exec invokes the application logic for the command.
   165  func (c *UpdateCommand) Exec(_ io.Reader, out io.Writer) error {
   166  	serviceID, serviceVersion, err := argparser.ServiceDetails(argparser.ServiceDetailsOpts{
   167  		AutoCloneFlag:      c.AutoClone,
   168  		APIClient:          c.Globals.APIClient,
   169  		Manifest:           *c.Globals.Manifest,
   170  		Out:                out,
   171  		ServiceNameFlag:    c.ServiceName,
   172  		ServiceVersionFlag: c.ServiceVersion,
   173  		VerboseMode:        c.Globals.Flags.Verbose,
   174  	})
   175  	if err != nil {
   176  		c.Globals.ErrLog.AddWithContext(err, map[string]any{
   177  			"Service ID":      serviceID,
   178  			"Service Version": errors.ServiceVersion(serviceVersion),
   179  		})
   180  		return err
   181  	}
   182  
   183  	input, err := c.ConstructInput(serviceID, fastly.ToValue(serviceVersion.Number))
   184  	if err != nil {
   185  		c.Globals.ErrLog.Add(err)
   186  		return err
   187  	}
   188  
   189  	syslog, err := c.Globals.APIClient.UpdateSyslog(input)
   190  	if err != nil {
   191  		c.Globals.ErrLog.Add(err)
   192  		return err
   193  	}
   194  
   195  	text.Success(out,
   196  		"Updated Syslog logging endpoint %s (service %s version %d)",
   197  		fastly.ToValue(syslog.Name),
   198  		fastly.ToValue(syslog.ServiceID),
   199  		fastly.ToValue(syslog.ServiceVersion),
   200  	)
   201  	return nil
   202  }