github.com/fastly/cli@v1.7.2-0.20240304164155-9d0f1d77c3bf/pkg/commands/logging/splunk/update.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  // UpdateCommand calls the Fastly API to update a Splunk 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  	FormatVersion     argparser.OptionalInt
    32  	ResponseCondition argparser.OptionalString
    33  	Placement         argparser.OptionalString
    34  	Token             argparser.OptionalString
    35  	TLSCACert         argparser.OptionalString
    36  	TLSHostname       argparser.OptionalString
    37  	TLSClientCert     argparser.OptionalString
    38  	TLSClientKey      argparser.OptionalString
    39  }
    40  
    41  // NewUpdateCommand returns a usable command registered under the parent.
    42  func NewUpdateCommand(parent argparser.Registerer, g *global.Data) *UpdateCommand {
    43  	c := UpdateCommand{
    44  		Base: argparser.Base{
    45  			Globals: g,
    46  		},
    47  	}
    48  	c.CmdClause = parent.Command("update", "Update a Splunk logging endpoint on a Fastly service version")
    49  
    50  	// Required.
    51  	c.CmdClause.Flag("name", "The name of the Splunk logging object").Short('n').Required().StringVar(&c.EndpointName)
    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", "").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  	c.CmdClause.Flag("new-name", "New name of the Splunk logging object").Action(c.NewName.Set).StringVar(&c.NewName.Value)
    66  	common.Format(c.CmdClause, &c.Format)
    67  	common.FormatVersion(c.CmdClause, &c.FormatVersion)
    68  	c.CmdClause.Flag("placement", "	Where in the generated VCL the logging call should be placed, overriding any format_version default. Can be none or waf_debug. This field is not required and has no default value").Action(c.Placement.Set).StringVar(&c.Placement.Value)
    69  	common.ResponseCondition(c.CmdClause, &c.ResponseCondition)
    70  	c.RegisterFlag(argparser.StringFlagOpts{
    71  		Name:        argparser.FlagServiceIDName,
    72  		Description: argparser.FlagServiceIDDesc,
    73  		Dst:         &g.Manifest.Flag.ServiceID,
    74  		Short:       's',
    75  	})
    76  	c.RegisterFlag(argparser.StringFlagOpts{
    77  		Action:      c.ServiceName.Set,
    78  		Name:        argparser.FlagServiceName,
    79  		Description: argparser.FlagServiceDesc,
    80  		Dst:         &c.ServiceName.Value,
    81  	})
    82  	common.TLSCACert(c.CmdClause, &c.TLSCACert)
    83  	common.TLSClientCert(c.CmdClause, &c.TLSClientCert)
    84  	common.TLSClientKey(c.CmdClause, &c.TLSClientKey)
    85  	common.TLSHostname(c.CmdClause, &c.TLSHostname)
    86  	c.CmdClause.Flag("url", "The URL to POST to.").Action(c.URL.Set).StringVar(&c.URL.Value)
    87  	return &c
    88  }
    89  
    90  // ConstructInput transforms values parsed from CLI flags into an object to be used by the API client library.
    91  func (c *UpdateCommand) ConstructInput(serviceID string, serviceVersion int) (*fastly.UpdateSplunkInput, error) {
    92  	input := fastly.UpdateSplunkInput{
    93  		ServiceID:      serviceID,
    94  		ServiceVersion: serviceVersion,
    95  		Name:           c.EndpointName,
    96  	}
    97  
    98  	// Set new values if set by user.
    99  	if c.NewName.WasSet {
   100  		input.NewName = &c.NewName.Value
   101  	}
   102  
   103  	if c.URL.WasSet {
   104  		input.URL = &c.URL.Value
   105  	}
   106  
   107  	if c.Format.WasSet {
   108  		input.Format = &c.Format.Value
   109  	}
   110  
   111  	if c.FormatVersion.WasSet {
   112  		input.FormatVersion = &c.FormatVersion.Value
   113  	}
   114  
   115  	if c.ResponseCondition.WasSet {
   116  		input.ResponseCondition = &c.ResponseCondition.Value
   117  	}
   118  
   119  	if c.Placement.WasSet {
   120  		input.Placement = &c.Placement.Value
   121  	}
   122  
   123  	if c.Token.WasSet {
   124  		input.Token = &c.Token.Value
   125  	}
   126  
   127  	if c.TLSCACert.WasSet {
   128  		input.TLSCACert = &c.TLSCACert.Value
   129  	}
   130  
   131  	if c.TLSHostname.WasSet {
   132  		input.TLSHostname = &c.TLSHostname.Value
   133  	}
   134  
   135  	if c.TLSClientCert.WasSet {
   136  		input.TLSClientCert = &c.TLSClientCert.Value
   137  	}
   138  
   139  	if c.TLSClientKey.WasSet {
   140  		input.TLSClientKey = &c.TLSClientKey.Value
   141  	}
   142  
   143  	return &input, nil
   144  }
   145  
   146  // Exec invokes the application logic for the command.
   147  func (c *UpdateCommand) Exec(_ io.Reader, out io.Writer) error {
   148  	serviceID, serviceVersion, err := argparser.ServiceDetails(argparser.ServiceDetailsOpts{
   149  		AutoCloneFlag:      c.AutoClone,
   150  		APIClient:          c.Globals.APIClient,
   151  		Manifest:           *c.Globals.Manifest,
   152  		Out:                out,
   153  		ServiceNameFlag:    c.ServiceName,
   154  		ServiceVersionFlag: c.ServiceVersion,
   155  		VerboseMode:        c.Globals.Flags.Verbose,
   156  	})
   157  	if err != nil {
   158  		c.Globals.ErrLog.AddWithContext(err, map[string]any{
   159  			"Service ID":      serviceID,
   160  			"Service Version": errors.ServiceVersion(serviceVersion),
   161  		})
   162  		return err
   163  	}
   164  
   165  	input, err := c.ConstructInput(serviceID, fastly.ToValue(serviceVersion.Number))
   166  	if err != nil {
   167  		c.Globals.ErrLog.Add(err)
   168  		return err
   169  	}
   170  
   171  	splunk, err := c.Globals.APIClient.UpdateSplunk(input)
   172  	if err != nil {
   173  		c.Globals.ErrLog.Add(err)
   174  		return err
   175  	}
   176  
   177  	text.Success(out,
   178  		"Updated Splunk logging endpoint %s (service %s version %d)",
   179  		fastly.ToValue(splunk.Name),
   180  		fastly.ToValue(splunk.ServiceID),
   181  		fastly.ToValue(splunk.ServiceVersion),
   182  	)
   183  	return nil
   184  }