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

     1  package azureblob
     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 Azure Blob Storage 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  	AccountName       argparser.OptionalString
    30  	Container         argparser.OptionalString
    31  	SASToken          argparser.OptionalString
    32  	Path              argparser.OptionalString
    33  	Period            argparser.OptionalInt
    34  	GzipLevel         argparser.OptionalInt
    35  	MessageType       argparser.OptionalString
    36  	Format            argparser.OptionalString
    37  	FormatVersion     argparser.OptionalInt
    38  	ResponseCondition argparser.OptionalString
    39  	TimestampFormat   argparser.OptionalString
    40  	Placement         argparser.OptionalString
    41  	PublicKey         argparser.OptionalString
    42  	FileMaxBytes      argparser.OptionalInt
    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 an Azure Blob Storage logging endpoint on a Fastly service version")
    54  
    55  	// Required.
    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  	c.RegisterAutoCloneFlag(argparser.AutoCloneFlagOpts{
    65  		Action: c.AutoClone.Set,
    66  		Dst:    &c.AutoClone.Value,
    67  	})
    68  	c.CmdClause.Flag("name", "The name of the Azure Blob Storage logging object").Short('n').Required().StringVar(&c.EndpointName)
    69  
    70  	// Optional.
    71  	c.CmdClause.Flag("account-name", "The unique Azure Blob Storage namespace in which your data objects are stored").Action(c.AccountName.Set).StringVar(&c.AccountName.Value)
    72  	common.CompressionCodec(c.CmdClause, &c.CompressionCodec)
    73  	c.CmdClause.Flag("container", "The name of the Azure Blob Storage container in which to store logs").Action(c.Container.Set).StringVar(&c.Container.Value)
    74  	c.CmdClause.Flag("file-max-bytes", "The maximum size of a log file in bytes").Action(c.FileMaxBytes.Set).IntVar(&c.FileMaxBytes.Value)
    75  	common.Format(c.CmdClause, &c.Format)
    76  	common.FormatVersion(c.CmdClause, &c.FormatVersion)
    77  	common.GzipLevel(c.CmdClause, &c.GzipLevel)
    78  	common.MessageType(c.CmdClause, &c.MessageType)
    79  	c.CmdClause.Flag("new-name", "New name of the Azure Blob Storage logging object").Action(c.NewName.Set).StringVar(&c.NewName.Value)
    80  	common.Path(c.CmdClause, &c.Path)
    81  	common.Period(c.CmdClause, &c.Period)
    82  	common.Placement(c.CmdClause, &c.Placement)
    83  	common.PublicKey(c.CmdClause, &c.PublicKey)
    84  	common.ResponseCondition(c.CmdClause, &c.ResponseCondition)
    85  	c.CmdClause.Flag("sas-token", "The Azure shared access signature providing write access to the blob service objects. Be sure to update your token before it expires or the logging functionality will not work").Action(c.SASToken.Set).StringVar(&c.SASToken.Value)
    86  	c.RegisterFlag(argparser.StringFlagOpts{
    87  		Name:        argparser.FlagServiceIDName,
    88  		Description: argparser.FlagServiceIDDesc,
    89  		Dst:         &g.Manifest.Flag.ServiceID,
    90  		Short:       's',
    91  	})
    92  	c.RegisterFlag(argparser.StringFlagOpts{
    93  		Action:      c.ServiceName.Set,
    94  		Name:        argparser.FlagServiceName,
    95  		Description: argparser.FlagServiceDesc,
    96  		Dst:         &c.ServiceName.Value,
    97  	})
    98  	common.TimestampFormat(c.CmdClause, &c.TimestampFormat)
    99  	return &c
   100  }
   101  
   102  // ConstructInput transforms values parsed from CLI flags into an object to be used by the API client library.
   103  func (c *UpdateCommand) ConstructInput(serviceID string, serviceVersion int) (*fastly.UpdateBlobStorageInput, error) {
   104  	input := fastly.UpdateBlobStorageInput{
   105  		ServiceID:      serviceID,
   106  		ServiceVersion: serviceVersion,
   107  		Name:           c.EndpointName,
   108  	}
   109  
   110  	// Set new values if set by user.
   111  	if c.NewName.WasSet {
   112  		input.NewName = &c.NewName.Value
   113  	}
   114  	if c.Path.WasSet {
   115  		input.Path = &c.Path.Value
   116  	}
   117  	if c.AccountName.WasSet {
   118  		input.AccountName = &c.AccountName.Value
   119  	}
   120  	if c.Container.WasSet {
   121  		input.Container = &c.Container.Value
   122  	}
   123  	if c.SASToken.WasSet {
   124  		input.SASToken = &c.SASToken.Value
   125  	}
   126  	if c.Period.WasSet {
   127  		input.Period = &c.Period.Value
   128  	}
   129  	if c.GzipLevel.WasSet {
   130  		input.GzipLevel = &c.GzipLevel.Value
   131  	}
   132  	if c.Format.WasSet {
   133  		input.Format = &c.Format.Value
   134  	}
   135  	if c.FormatVersion.WasSet {
   136  		input.FormatVersion = &c.FormatVersion.Value
   137  	}
   138  	if c.ResponseCondition.WasSet {
   139  		input.ResponseCondition = &c.ResponseCondition.Value
   140  	}
   141  	if c.MessageType.WasSet {
   142  		input.MessageType = &c.MessageType.Value
   143  	}
   144  	if c.TimestampFormat.WasSet {
   145  		input.TimestampFormat = &c.TimestampFormat.Value
   146  	}
   147  	if c.Placement.WasSet {
   148  		input.Placement = &c.Placement.Value
   149  	}
   150  	if c.PublicKey.WasSet {
   151  		input.PublicKey = &c.PublicKey.Value
   152  	}
   153  	if c.FileMaxBytes.WasSet {
   154  		input.FileMaxBytes = &c.FileMaxBytes.Value
   155  	}
   156  	if c.CompressionCodec.WasSet {
   157  		input.CompressionCodec = &c.CompressionCodec.Value
   158  	}
   159  
   160  	return &input, nil
   161  }
   162  
   163  // Exec invokes the application logic for the command.
   164  func (c *UpdateCommand) Exec(_ io.Reader, out io.Writer) error {
   165  	serviceID, serviceVersion, err := argparser.ServiceDetails(argparser.ServiceDetailsOpts{
   166  		AutoCloneFlag:      c.AutoClone,
   167  		APIClient:          c.Globals.APIClient,
   168  		Manifest:           *c.Globals.Manifest,
   169  		Out:                out,
   170  		ServiceNameFlag:    c.ServiceName,
   171  		ServiceVersionFlag: c.ServiceVersion,
   172  		VerboseMode:        c.Globals.Flags.Verbose,
   173  	})
   174  	if err != nil {
   175  		c.Globals.ErrLog.AddWithContext(err, map[string]any{
   176  			"Service ID":      serviceID,
   177  			"Service Version": errors.ServiceVersion(serviceVersion),
   178  		})
   179  		return err
   180  	}
   181  
   182  	input, err := c.ConstructInput(serviceID, fastly.ToValue(serviceVersion.Number))
   183  	if err != nil {
   184  		c.Globals.ErrLog.AddWithContext(err, map[string]any{
   185  			"Service ID":      serviceID,
   186  			"Service Version": fastly.ToValue(serviceVersion.Number),
   187  		})
   188  		return err
   189  	}
   190  
   191  	azureblob, err := c.Globals.APIClient.UpdateBlobStorage(input)
   192  	if err != nil {
   193  		c.Globals.ErrLog.AddWithContext(err, map[string]any{
   194  			"Service ID":      serviceID,
   195  			"Service Version": fastly.ToValue(serviceVersion.Number),
   196  		})
   197  		return err
   198  	}
   199  
   200  	text.Success(out,
   201  		"Updated Azure Blob Storage logging endpoint %s (service %s version %d)",
   202  		fastly.ToValue(azureblob.Name),
   203  		fastly.ToValue(azureblob.ServiceID),
   204  		fastly.ToValue(azureblob.ServiceVersion),
   205  	)
   206  	return nil
   207  }