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

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