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

     1  package s3
     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 an Amazon S3 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  	Address                      argparser.OptionalString
    30  	BucketName                   argparser.OptionalString
    31  	AccessKey                    argparser.OptionalString
    32  	SecretKey                    argparser.OptionalString
    33  	IAMRole                      argparser.OptionalString
    34  	Domain                       argparser.OptionalString
    35  	Path                         argparser.OptionalString
    36  	Period                       argparser.OptionalInt
    37  	GzipLevel                    argparser.OptionalInt
    38  	FileMaxBytes                 argparser.OptionalInt
    39  	Format                       argparser.OptionalString
    40  	FormatVersion                argparser.OptionalInt
    41  	MessageType                  argparser.OptionalString
    42  	ResponseCondition            argparser.OptionalString
    43  	TimestampFormat              argparser.OptionalString
    44  	Placement                    argparser.OptionalString
    45  	PublicKey                    argparser.OptionalString
    46  	Redundancy                   argparser.OptionalString
    47  	ServerSideEncryption         argparser.OptionalString
    48  	ServerSideEncryptionKMSKeyID argparser.OptionalString
    49  	CompressionCodec             argparser.OptionalString
    50  }
    51  
    52  // NewUpdateCommand returns a usable command registered under the parent.
    53  func NewUpdateCommand(parent argparser.Registerer, g *global.Data) *UpdateCommand {
    54  	c := UpdateCommand{
    55  		Base: argparser.Base{
    56  			Globals: g,
    57  		},
    58  	}
    59  	c.CmdClause = parent.Command("update", "Update a S3 logging endpoint on a Fastly service version")
    60  
    61  	// Required.
    62  	c.CmdClause.Flag("name", "The name of the S3 logging object").Short('n').Required().StringVar(&c.EndpointName)
    63  	c.RegisterFlag(argparser.StringFlagOpts{
    64  		Name:        argparser.FlagVersionName,
    65  		Description: argparser.FlagVersionDesc,
    66  		Dst:         &c.ServiceVersion.Value,
    67  		Required:    true,
    68  	})
    69  
    70  	// Optional.
    71  	c.RegisterAutoCloneFlag(argparser.AutoCloneFlagOpts{
    72  		Action: c.AutoClone.Set,
    73  		Dst:    &c.AutoClone.Value,
    74  	})
    75  	c.CmdClause.Flag("access-key", "Your S3 account access key").Action(c.AccessKey.Set).StringVar(&c.AccessKey.Value)
    76  	c.CmdClause.Flag("bucket", "Your S3 bucket name").Action(c.BucketName.Set).StringVar(&c.BucketName.Value)
    77  	common.CompressionCodec(c.CmdClause, &c.CompressionCodec)
    78  	c.CmdClause.Flag("domain", "The domain of the S3 endpoint").Action(c.Domain.Set).StringVar(&c.Domain.Value)
    79  	c.CmdClause.Flag("file-max-bytes", "The maximum size of a log file in bytes").Action(c.FileMaxBytes.Set).IntVar(&c.FileMaxBytes.Value)
    80  	common.Format(c.CmdClause, &c.Format)
    81  	common.FormatVersion(c.CmdClause, &c.FormatVersion)
    82  	common.GzipLevel(c.CmdClause, &c.GzipLevel)
    83  	c.CmdClause.Flag("iam-role", "The IAM role ARN for logging").Action(c.IAMRole.Set).StringVar(&c.IAMRole.Value)
    84  	common.MessageType(c.CmdClause, &c.MessageType)
    85  	c.CmdClause.Flag("new-name", "New name of the S3 logging object").Action(c.NewName.Set).StringVar(&c.NewName.Value)
    86  	common.Path(c.CmdClause, &c.Path)
    87  	common.Period(c.CmdClause, &c.Period)
    88  	common.Placement(c.CmdClause, &c.Placement)
    89  	common.PublicKey(c.CmdClause, &c.PublicKey)
    90  	c.CmdClause.Flag("redundancy", "The S3 storage class. One of: standard, intelligent_tiering, standard_ia, onezone_ia, glacier, glacier_ir, deep_archive, or reduced_redundancy").Action(c.Redundancy.Set).EnumVar(&c.Redundancy.Value, string(fastly.S3RedundancyStandard), string(fastly.S3RedundancyIntelligentTiering), string(fastly.S3RedundancyStandardIA), string(fastly.S3RedundancyOneZoneIA), string(fastly.S3RedundancyGlacierFlexibleRetrieval), string(fastly.S3RedundancyGlacierInstantRetrieval), string(fastly.S3RedundancyGlacierDeepArchive), string(fastly.S3RedundancyReduced))
    91  	common.ResponseCondition(c.CmdClause, &c.ResponseCondition)
    92  	c.CmdClause.Flag("secret-key", "Your S3 account secret key").Action(c.SecretKey.Set).StringVar(&c.SecretKey.Value)
    93  	c.CmdClause.Flag("server-side-encryption", "Set to enable S3 Server Side Encryption. Can be either AES256 or aws:kms").Action(c.ServerSideEncryption.Set).EnumVar(&c.ServerSideEncryption.Value, string(fastly.S3ServerSideEncryptionAES), string(fastly.S3ServerSideEncryptionKMS))
    94  	c.CmdClause.Flag("server-side-encryption-kms-key-id", "Server-side KMS Key ID. Must be set if server-side-encryption is set to aws:kms").Action(c.ServerSideEncryptionKMSKeyID.Set).StringVar(&c.ServerSideEncryptionKMSKeyID.Value)
    95  	c.RegisterFlag(argparser.StringFlagOpts{
    96  		Name:        argparser.FlagServiceIDName,
    97  		Description: argparser.FlagServiceIDDesc,
    98  		Dst:         &g.Manifest.Flag.ServiceID,
    99  		Short:       's',
   100  	})
   101  	c.RegisterFlag(argparser.StringFlagOpts{
   102  		Action:      c.ServiceName.Set,
   103  		Name:        argparser.FlagServiceName,
   104  		Description: argparser.FlagServiceDesc,
   105  		Dst:         &c.ServiceName.Value,
   106  	})
   107  	common.TimestampFormat(c.CmdClause, &c.TimestampFormat)
   108  	return &c
   109  }
   110  
   111  // ConstructInput transforms values parsed from CLI flags into an object to be used by the API client library.
   112  func (c *UpdateCommand) ConstructInput(serviceID string, serviceVersion int) (*fastly.UpdateS3Input, error) {
   113  	input := fastly.UpdateS3Input{
   114  		ServiceID:      serviceID,
   115  		ServiceVersion: serviceVersion,
   116  		Name:           c.EndpointName,
   117  	}
   118  
   119  	if c.NewName.WasSet {
   120  		input.NewName = &c.NewName.Value
   121  	}
   122  
   123  	if c.BucketName.WasSet {
   124  		input.BucketName = &c.BucketName.Value
   125  	}
   126  
   127  	if c.AccessKey.WasSet {
   128  		input.AccessKey = &c.AccessKey.Value
   129  	}
   130  
   131  	if c.SecretKey.WasSet {
   132  		input.SecretKey = &c.SecretKey.Value
   133  	}
   134  
   135  	if c.IAMRole.WasSet {
   136  		input.IAMRole = &c.IAMRole.Value
   137  	}
   138  
   139  	if c.Domain.WasSet {
   140  		input.Domain = &c.Domain.Value
   141  	}
   142  
   143  	if c.Path.WasSet {
   144  		input.Path = &c.Path.Value
   145  	}
   146  
   147  	if c.Period.WasSet {
   148  		input.Period = &c.Period.Value
   149  	}
   150  
   151  	if c.GzipLevel.WasSet {
   152  		input.GzipLevel = &c.GzipLevel.Value
   153  	}
   154  
   155  	if c.FileMaxBytes.WasSet {
   156  		input.FileMaxBytes = &c.FileMaxBytes.Value
   157  	}
   158  
   159  	if c.Format.WasSet {
   160  		input.Format = &c.Format.Value
   161  	}
   162  
   163  	if c.FormatVersion.WasSet {
   164  		input.FormatVersion = &c.FormatVersion.Value
   165  	}
   166  
   167  	if c.MessageType.WasSet {
   168  		input.MessageType = &c.MessageType.Value
   169  	}
   170  
   171  	if c.ResponseCondition.WasSet {
   172  		input.ResponseCondition = &c.ResponseCondition.Value
   173  	}
   174  
   175  	if c.TimestampFormat.WasSet {
   176  		input.TimestampFormat = &c.TimestampFormat.Value
   177  	}
   178  
   179  	if c.Placement.WasSet {
   180  		input.Placement = &c.Placement.Value
   181  	}
   182  
   183  	if c.PublicKey.WasSet {
   184  		input.PublicKey = &c.PublicKey.Value
   185  	}
   186  
   187  	if c.ServerSideEncryptionKMSKeyID.WasSet {
   188  		input.ServerSideEncryptionKMSKeyID = &c.ServerSideEncryptionKMSKeyID.Value
   189  	}
   190  
   191  	if c.CompressionCodec.WasSet {
   192  		input.CompressionCodec = &c.CompressionCodec.Value
   193  	}
   194  
   195  	if c.Redundancy.WasSet {
   196  		redundancy, err := ValidateRedundancy(c.Redundancy.Value)
   197  		if err == nil {
   198  			input.Redundancy = fastly.ToPointer(redundancy)
   199  		}
   200  	}
   201  
   202  	if c.ServerSideEncryption.WasSet {
   203  		switch c.ServerSideEncryption.Value {
   204  		case string(fastly.S3ServerSideEncryptionAES):
   205  			input.ServerSideEncryption = fastly.ToPointer(fastly.S3ServerSideEncryptionAES)
   206  		case string(fastly.S3ServerSideEncryptionKMS):
   207  			input.ServerSideEncryption = fastly.ToPointer(fastly.S3ServerSideEncryptionKMS)
   208  		}
   209  	}
   210  
   211  	return &input, nil
   212  }
   213  
   214  // Exec invokes the application logic for the command.
   215  func (c *UpdateCommand) Exec(_ io.Reader, out io.Writer) error {
   216  	serviceID, serviceVersion, err := argparser.ServiceDetails(argparser.ServiceDetailsOpts{
   217  		AutoCloneFlag:      c.AutoClone,
   218  		APIClient:          c.Globals.APIClient,
   219  		Manifest:           *c.Globals.Manifest,
   220  		Out:                out,
   221  		ServiceNameFlag:    c.ServiceName,
   222  		ServiceVersionFlag: c.ServiceVersion,
   223  		VerboseMode:        c.Globals.Flags.Verbose,
   224  	})
   225  	if err != nil {
   226  		c.Globals.ErrLog.AddWithContext(err, map[string]any{
   227  			"Service ID":      serviceID,
   228  			"Service Version": errors.ServiceVersion(serviceVersion),
   229  		})
   230  		return err
   231  	}
   232  
   233  	input, err := c.ConstructInput(serviceID, fastly.ToValue(serviceVersion.Number))
   234  	if err != nil {
   235  		c.Globals.ErrLog.Add(err)
   236  		return err
   237  	}
   238  
   239  	s3, err := c.Globals.APIClient.UpdateS3(input)
   240  	if err != nil {
   241  		c.Globals.ErrLog.Add(err)
   242  		return err
   243  	}
   244  
   245  	text.Success(out,
   246  		"Updated S3 logging endpoint %s (service %s version %d)",
   247  		fastly.ToValue(s3.Name),
   248  		fastly.ToValue(s3.ServiceID),
   249  		fastly.ToValue(s3.ServiceVersion),
   250  	)
   251  	return nil
   252  }