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

     1  package gcs
     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 GCS 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  	AccountName       argparser.OptionalString
    28  	AutoClone         argparser.OptionalAutoClone
    29  	Bucket            argparser.OptionalString
    30  	CompressionCodec  argparser.OptionalString
    31  	Format            argparser.OptionalString
    32  	FormatVersion     argparser.OptionalInt
    33  	GzipLevel         argparser.OptionalInt
    34  	MessageType       argparser.OptionalString
    35  	NewName           argparser.OptionalString
    36  	Path              argparser.OptionalString
    37  	Period            argparser.OptionalInt
    38  	Placement         argparser.OptionalString
    39  	ResponseCondition argparser.OptionalString
    40  	SecretKey         argparser.OptionalString
    41  	TimestampFormat   argparser.OptionalString
    42  	User              argparser.OptionalString
    43  }
    44  
    45  // NewUpdateCommand returns a usable command registered under the parent.
    46  func NewUpdateCommand(parent argparser.Registerer, g *global.Data) *UpdateCommand {
    47  	c := UpdateCommand{
    48  		Base: argparser.Base{
    49  			Globals: g,
    50  		},
    51  	}
    52  	c.CmdClause = parent.Command("update", "Update a GCS logging endpoint on a Fastly service version")
    53  
    54  	// Required.
    55  	c.CmdClause.Flag("name", "The name of the GCS logging object").Short('n').Required().StringVar(&c.EndpointName)
    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  	common.AccountName(c.CmdClause, &c.AccountName)
    65  	c.RegisterAutoCloneFlag(argparser.AutoCloneFlagOpts{
    66  		Action: c.AutoClone.Set,
    67  		Dst:    &c.AutoClone.Value,
    68  	})
    69  	c.CmdClause.Flag("bucket", "The bucket of the GCS bucket").Action(c.Bucket.Set).StringVar(&c.Bucket.Value)
    70  	common.CompressionCodec(c.CmdClause, &c.CompressionCodec)
    71  	common.Format(c.CmdClause, &c.Format)
    72  	common.FormatVersion(c.CmdClause, &c.FormatVersion)
    73  	common.GzipLevel(c.CmdClause, &c.GzipLevel)
    74  	common.MessageType(c.CmdClause, &c.MessageType)
    75  	c.CmdClause.Flag("new-name", "New name of the GCS logging object").Action(c.NewName.Set).StringVar(&c.NewName.Value)
    76  	c.CmdClause.Flag("path", "The path to upload logs to (default '/')").Action(c.Path.Set).StringVar(&c.Path.Value)
    77  	common.Period(c.CmdClause, &c.Period)
    78  	common.Placement(c.CmdClause, &c.Placement)
    79  	common.ResponseCondition(c.CmdClause, &c.ResponseCondition)
    80  	c.CmdClause.Flag("secret-key", "Your GCS account secret key. The private_key field in your service account authentication JSON").Action(c.SecretKey.Set).StringVar(&c.SecretKey.Value)
    81  	c.RegisterFlag(argparser.StringFlagOpts{
    82  		Name:        argparser.FlagServiceIDName,
    83  		Description: argparser.FlagServiceIDDesc,
    84  		Dst:         &g.Manifest.Flag.ServiceID,
    85  		Short:       's',
    86  	})
    87  	c.RegisterFlag(argparser.StringFlagOpts{
    88  		Action:      c.ServiceName.Set,
    89  		Name:        argparser.FlagServiceName,
    90  		Description: argparser.FlagServiceDesc,
    91  		Dst:         &c.ServiceName.Value,
    92  	})
    93  	c.CmdClause.Flag("user", "Your GCS service account email address. The client_email field in your service account authentication JSON").Action(c.User.Set).StringVar(&c.User.Value)
    94  	common.TimestampFormat(c.CmdClause, &c.TimestampFormat)
    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.UpdateGCSInput, error) {
   100  	input := fastly.UpdateGCSInput{
   101  		ServiceID:      serviceID,
   102  		ServiceVersion: serviceVersion,
   103  		Name:           c.EndpointName,
   104  	}
   105  
   106  	if c.AccountName.WasSet {
   107  		input.AccountName = &c.AccountName.Value
   108  	}
   109  	if c.Bucket.WasSet {
   110  		input.Bucket = &c.Bucket.Value
   111  	}
   112  	if c.CompressionCodec.WasSet {
   113  		input.CompressionCodec = &c.CompressionCodec.Value
   114  	}
   115  	if c.Format.WasSet {
   116  		input.Format = &c.Format.Value
   117  	}
   118  	if c.FormatVersion.WasSet {
   119  		input.FormatVersion = &c.FormatVersion.Value
   120  	}
   121  	if c.GzipLevel.WasSet {
   122  		input.GzipLevel = &c.GzipLevel.Value
   123  	}
   124  	if c.MessageType.WasSet {
   125  		input.MessageType = &c.MessageType.Value
   126  	}
   127  	if c.NewName.WasSet {
   128  		input.NewName = &c.NewName.Value
   129  	}
   130  	if c.Path.WasSet {
   131  		input.Path = &c.Path.Value
   132  	}
   133  	if c.Period.WasSet {
   134  		input.Period = &c.Period.Value
   135  	}
   136  	if c.Placement.WasSet {
   137  		input.Placement = &c.Placement.Value
   138  	}
   139  	if c.ResponseCondition.WasSet {
   140  		input.ResponseCondition = &c.ResponseCondition.Value
   141  	}
   142  	if c.SecretKey.WasSet {
   143  		input.SecretKey = &c.SecretKey.Value
   144  	}
   145  	if c.TimestampFormat.WasSet {
   146  		input.TimestampFormat = &c.TimestampFormat.Value
   147  	}
   148  	if c.User.WasSet {
   149  		input.User = &c.User.Value
   150  	}
   151  
   152  	return &input, nil
   153  }
   154  
   155  // Exec invokes the application logic for the command.
   156  func (c *UpdateCommand) Exec(_ io.Reader, out io.Writer) error {
   157  	serviceID, serviceVersion, err := argparser.ServiceDetails(argparser.ServiceDetailsOpts{
   158  		AutoCloneFlag:      c.AutoClone,
   159  		APIClient:          c.Globals.APIClient,
   160  		Manifest:           *c.Globals.Manifest,
   161  		Out:                out,
   162  		ServiceNameFlag:    c.ServiceName,
   163  		ServiceVersionFlag: c.ServiceVersion,
   164  		VerboseMode:        c.Globals.Flags.Verbose,
   165  	})
   166  	if err != nil {
   167  		c.Globals.ErrLog.AddWithContext(err, map[string]any{
   168  			"Service ID":      serviceID,
   169  			"Service Version": errors.ServiceVersion(serviceVersion),
   170  		})
   171  		return err
   172  	}
   173  
   174  	input, err := c.ConstructInput(serviceID, fastly.ToValue(serviceVersion.Number))
   175  	if err != nil {
   176  		c.Globals.ErrLog.Add(err)
   177  		return err
   178  	}
   179  
   180  	gcs, err := c.Globals.APIClient.UpdateGCS(input)
   181  	if err != nil {
   182  		c.Globals.ErrLog.Add(err)
   183  		return err
   184  	}
   185  
   186  	text.Success(out,
   187  		"Updated GCS logging endpoint %s (service %s version %d)",
   188  		fastly.ToValue(gcs.Name),
   189  		fastly.ToValue(gcs.ServiceID),
   190  		fastly.ToValue(gcs.ServiceVersion),
   191  	)
   192  	return nil
   193  }