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

     1  package cloudfiles
     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  	fsterr "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 Cloudfiles 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  	User              argparser.OptionalString
    30  	AccessKey         argparser.OptionalString
    31  	BucketName        argparser.OptionalString
    32  	Path              argparser.OptionalString
    33  	Region            argparser.OptionalString
    34  	Placement         argparser.OptionalString
    35  	Period            argparser.OptionalInt
    36  	GzipLevel         argparser.OptionalInt
    37  	Format            argparser.OptionalString
    38  	FormatVersion     argparser.OptionalInt
    39  	ResponseCondition argparser.OptionalString
    40  	MessageType       argparser.OptionalString
    41  	TimestampFormat   argparser.OptionalString
    42  	PublicKey         argparser.OptionalString
    43  	CompressionCodec  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 a Cloudfiles logging endpoint on a Fastly service version")
    54  
    55  	// Required.
    56  	c.CmdClause.Flag("name", "The name of the Cloudfiles 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  	c.CmdClause.Flag("access-key", "Your Cloudfile account access key").Action(c.AccessKey.Set).StringVar(&c.AccessKey.Value)
    70  	c.CmdClause.Flag("bucket", "The name of your Cloudfiles container").Action(c.BucketName.Set).StringVar(&c.BucketName.Value)
    71  	common.CompressionCodec(c.CmdClause, &c.CompressionCodec)
    72  	common.Format(c.CmdClause, &c.Format)
    73  	common.FormatVersion(c.CmdClause, &c.FormatVersion)
    74  	common.GzipLevel(c.CmdClause, &c.GzipLevel)
    75  	common.MessageType(c.CmdClause, &c.MessageType)
    76  	c.CmdClause.Flag("new-name", "New name of the Cloudfiles logging object").Action(c.NewName.Set).StringVar(&c.NewName.Value)
    77  	common.Path(c.CmdClause, &c.Path)
    78  	common.Period(c.CmdClause, &c.Period)
    79  	common.Placement(c.CmdClause, &c.Placement)
    80  	common.PublicKey(c.CmdClause, &c.PublicKey)
    81  	c.CmdClause.Flag("region", "The region to stream logs to. One of: DFW-Dallas, ORD-Chicago, IAD-Northern Virginia, LON-London, SYD-Sydney, HKG-Hong Kong").Action(c.Region.Set).StringVar(&c.Region.Value)
    82  	c.CmdClause.Flag("user", "The username for your Cloudfile account").Action(c.User.Set).StringVar(&c.User.Value)
    83  	common.ResponseCondition(c.CmdClause, &c.ResponseCondition)
    84  	c.RegisterFlag(argparser.StringFlagOpts{
    85  		Name:        argparser.FlagServiceIDName,
    86  		Description: argparser.FlagServiceIDDesc,
    87  		Dst:         &g.Manifest.Flag.ServiceID,
    88  		Short:       's',
    89  	})
    90  	c.RegisterFlag(argparser.StringFlagOpts{
    91  		Action:      c.ServiceName.Set,
    92  		Name:        argparser.FlagServiceName,
    93  		Description: argparser.FlagServiceDesc,
    94  		Dst:         &c.ServiceName.Value,
    95  	})
    96  	common.TimestampFormat(c.CmdClause, &c.TimestampFormat)
    97  	return &c
    98  }
    99  
   100  // ConstructInput transforms values parsed from CLI flags into an object to be used by the API client library.
   101  func (c *UpdateCommand) ConstructInput(serviceID string, serviceVersion int) (*fastly.UpdateCloudfilesInput, error) {
   102  	input := fastly.UpdateCloudfilesInput{
   103  		ServiceID:      serviceID,
   104  		ServiceVersion: serviceVersion,
   105  		Name:           c.EndpointName,
   106  	}
   107  
   108  	// Set new values if set by user.
   109  	if c.NewName.WasSet {
   110  		input.NewName = &c.NewName.Value
   111  	}
   112  
   113  	if c.User.WasSet {
   114  		input.User = &c.User.Value
   115  	}
   116  
   117  	if c.AccessKey.WasSet {
   118  		input.AccessKey = &c.AccessKey.Value
   119  	}
   120  
   121  	if c.BucketName.WasSet {
   122  		input.BucketName = &c.BucketName.Value
   123  	}
   124  
   125  	if c.Path.WasSet {
   126  		input.Path = &c.Path.Value
   127  	}
   128  
   129  	if c.Region.WasSet {
   130  		input.Region = &c.Region.Value
   131  	}
   132  
   133  	if c.Placement.WasSet {
   134  		input.Placement = &c.Placement.Value
   135  	}
   136  
   137  	if c.Period.WasSet {
   138  		input.Period = &c.Period.Value
   139  	}
   140  
   141  	if c.GzipLevel.WasSet {
   142  		input.GzipLevel = &c.GzipLevel.Value
   143  	}
   144  
   145  	if c.Format.WasSet {
   146  		input.Format = &c.Format.Value
   147  	}
   148  
   149  	if c.FormatVersion.WasSet {
   150  		input.FormatVersion = &c.FormatVersion.Value
   151  	}
   152  
   153  	if c.ResponseCondition.WasSet {
   154  		input.ResponseCondition = &c.ResponseCondition.Value
   155  	}
   156  
   157  	if c.MessageType.WasSet {
   158  		input.MessageType = &c.MessageType.Value
   159  	}
   160  
   161  	if c.TimestampFormat.WasSet {
   162  		input.TimestampFormat = &c.TimestampFormat.Value
   163  	}
   164  
   165  	if c.PublicKey.WasSet {
   166  		input.PublicKey = &c.PublicKey.Value
   167  	}
   168  
   169  	if c.CompressionCodec.WasSet {
   170  		input.CompressionCodec = &c.CompressionCodec.Value
   171  	}
   172  
   173  	return &input, nil
   174  }
   175  
   176  // Exec invokes the application logic for the command.
   177  func (c *UpdateCommand) Exec(_ io.Reader, out io.Writer) error {
   178  	serviceID, serviceVersion, err := argparser.ServiceDetails(argparser.ServiceDetailsOpts{
   179  		AutoCloneFlag:      c.AutoClone,
   180  		APIClient:          c.Globals.APIClient,
   181  		Manifest:           *c.Globals.Manifest,
   182  		Out:                out,
   183  		ServiceNameFlag:    c.ServiceName,
   184  		ServiceVersionFlag: c.ServiceVersion,
   185  		VerboseMode:        c.Globals.Flags.Verbose,
   186  	})
   187  	if err != nil {
   188  		c.Globals.ErrLog.AddWithContext(err, map[string]any{
   189  			"Service ID":      serviceID,
   190  			"Service Version": fsterr.ServiceVersion(serviceVersion),
   191  		})
   192  		return err
   193  	}
   194  
   195  	input, err := c.ConstructInput(serviceID, fastly.ToValue(serviceVersion.Number))
   196  	if err != nil {
   197  		c.Globals.ErrLog.AddWithContext(err, map[string]any{
   198  			"Service ID":      serviceID,
   199  			"Service Version": fastly.ToValue(serviceVersion.Number),
   200  		})
   201  		return err
   202  	}
   203  
   204  	cloudfiles, err := c.Globals.APIClient.UpdateCloudfiles(input)
   205  	if err != nil {
   206  		c.Globals.ErrLog.AddWithContext(err, map[string]any{
   207  			"Service ID":      serviceID,
   208  			"Service Version": fastly.ToValue(serviceVersion.Number),
   209  		})
   210  		return err
   211  	}
   212  
   213  	text.Success(out,
   214  		"Updated Cloudfiles logging endpoint %s (service %s version %d)",
   215  		fastly.ToValue(cloudfiles.Name),
   216  		fastly.ToValue(cloudfiles.ServiceID),
   217  		fastly.ToValue(cloudfiles.ServiceVersion),
   218  	)
   219  	return nil
   220  }