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

     1  package digitalocean
     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 DigitalOcean Spaces 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  	BucketName        argparser.OptionalString
    30  	Domain            argparser.OptionalString
    31  	AccessKey         argparser.OptionalString
    32  	SecretKey         argparser.OptionalString
    33  	Path              argparser.OptionalString
    34  	Period            argparser.OptionalInt
    35  	GzipLevel         argparser.OptionalInt
    36  	Format            argparser.OptionalString
    37  	FormatVersion     argparser.OptionalInt
    38  	ResponseCondition argparser.OptionalString
    39  	MessageType       argparser.OptionalString
    40  	TimestampFormat   argparser.OptionalString
    41  	Placement         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 DigitalOcean Spaces logging endpoint on a Fastly service version")
    54  
    55  	// Required.
    56  	c.CmdClause.Flag("name", "The name of the DigitalOcean Spaces 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.CmdClause.Flag("access-key", "Your DigitalOcean Spaces account access key").Action(c.AccessKey.Set).StringVar(&c.AccessKey.Value)
    66  	c.RegisterAutoCloneFlag(argparser.AutoCloneFlagOpts{
    67  		Action: c.AutoClone.Set,
    68  		Dst:    &c.AutoClone.Value,
    69  	})
    70  	c.CmdClause.Flag("bucket", "The name of the DigitalOcean Space").Action(c.BucketName.Set).StringVar(&c.BucketName.Value)
    71  	common.CompressionCodec(c.CmdClause, &c.CompressionCodec)
    72  	c.CmdClause.Flag("domain", "The domain of the DigitalOcean Spaces endpoint (default 'nyc3.digitaloceanspaces.com')").Action(c.Domain.Set).StringVar(&c.Domain.Value)
    73  	common.Format(c.CmdClause, &c.Format)
    74  	common.FormatVersion(c.CmdClause, &c.FormatVersion)
    75  	common.GzipLevel(c.CmdClause, &c.GzipLevel)
    76  	common.MessageType(c.CmdClause, &c.MessageType)
    77  	c.CmdClause.Flag("new-name", "New name of the DigitalOcean Spaces logging object").Action(c.NewName.Set).StringVar(&c.NewName.Value)
    78  	common.Path(c.CmdClause, &c.Path)
    79  	common.Period(c.CmdClause, &c.Period)
    80  	common.Placement(c.CmdClause, &c.Placement)
    81  	common.PublicKey(c.CmdClause, &c.PublicKey)
    82  	common.ResponseCondition(c.CmdClause, &c.ResponseCondition)
    83  	c.CmdClause.Flag("secret-key", "Your DigitalOcean Spaces account secret key").Action(c.SecretKey.Set).StringVar(&c.SecretKey.Value)
    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.UpdateDigitalOceanInput, error) {
   102  	input := fastly.UpdateDigitalOceanInput{
   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.BucketName.WasSet {
   114  		input.BucketName = &c.BucketName.Value
   115  	}
   116  
   117  	if c.Domain.WasSet {
   118  		input.Domain = &c.Domain.Value
   119  	}
   120  
   121  	if c.AccessKey.WasSet {
   122  		input.AccessKey = &c.AccessKey.Value
   123  	}
   124  
   125  	if c.SecretKey.WasSet {
   126  		input.SecretKey = &c.SecretKey.Value
   127  	}
   128  
   129  	if c.Path.WasSet {
   130  		input.Path = &c.Path.Value
   131  	}
   132  
   133  	if c.Period.WasSet {
   134  		input.Period = &c.Period.Value
   135  	}
   136  
   137  	if c.GzipLevel.WasSet {
   138  		input.GzipLevel = &c.GzipLevel.Value
   139  	}
   140  
   141  	if c.Format.WasSet {
   142  		input.Format = &c.Format.Value
   143  	}
   144  
   145  	if c.FormatVersion.WasSet {
   146  		input.FormatVersion = &c.FormatVersion.Value
   147  	}
   148  
   149  	if c.ResponseCondition.WasSet {
   150  		input.ResponseCondition = &c.ResponseCondition.Value
   151  	}
   152  
   153  	if c.MessageType.WasSet {
   154  		input.MessageType = &c.MessageType.Value
   155  	}
   156  
   157  	if c.TimestampFormat.WasSet {
   158  		input.TimestampFormat = &c.TimestampFormat.Value
   159  	}
   160  
   161  	if c.Placement.WasSet {
   162  		input.Placement = &c.Placement.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": errors.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.Add(err)
   198  		return err
   199  	}
   200  
   201  	digitalocean, err := c.Globals.APIClient.UpdateDigitalOcean(input)
   202  	if err != nil {
   203  		c.Globals.ErrLog.Add(err)
   204  		return err
   205  	}
   206  
   207  	text.Success(out,
   208  		"Updated DigitalOcean Spaces logging endpoint %s (service %s version %d)",
   209  		fastly.ToValue(digitalocean.Name),
   210  		fastly.ToValue(digitalocean.ServiceID),
   211  		fastly.ToValue(digitalocean.ServiceVersion),
   212  	)
   213  	return nil
   214  }