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

     1  package papertrail
     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 Papertrail 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  	Address           argparser.OptionalString
    30  	Port              argparser.OptionalInt
    31  	FormatVersion     argparser.OptionalInt
    32  	Format            argparser.OptionalString
    33  	ResponseCondition argparser.OptionalString
    34  	Placement         argparser.OptionalString
    35  }
    36  
    37  // NewUpdateCommand returns a usable command registered under the parent.
    38  func NewUpdateCommand(parent argparser.Registerer, g *global.Data) *UpdateCommand {
    39  	c := UpdateCommand{
    40  		Base: argparser.Base{
    41  			Globals: g,
    42  		},
    43  	}
    44  	c.CmdClause = parent.Command("update", "Update a Papertrail logging endpoint on a Fastly service version")
    45  
    46  	// Required.
    47  	c.CmdClause.Flag("name", "The name of the Papertrail logging object").Short('n').Required().StringVar(&c.EndpointName)
    48  	c.RegisterFlag(argparser.StringFlagOpts{
    49  		Name:        argparser.FlagVersionName,
    50  		Description: argparser.FlagVersionDesc,
    51  		Dst:         &c.ServiceVersion.Value,
    52  		Required:    true,
    53  	})
    54  
    55  	// Optional.
    56  	c.RegisterAutoCloneFlag(argparser.AutoCloneFlagOpts{
    57  		Action: c.AutoClone.Set,
    58  		Dst:    &c.AutoClone.Value,
    59  	})
    60  	c.CmdClause.Flag("address", "A hostname or IPv4 address").Action(c.Address.Set).StringVar(&c.Address.Value)
    61  	common.Format(c.CmdClause, &c.Format)
    62  	common.FormatVersion(c.CmdClause, &c.FormatVersion)
    63  	c.CmdClause.Flag("new-name", "New name of the Papertrail logging object").Action(c.NewName.Set).StringVar(&c.NewName.Value)
    64  	common.Placement(c.CmdClause, &c.Placement)
    65  	c.CmdClause.Flag("port", "The port number").Action(c.Port.Set).IntVar(&c.Port.Value)
    66  	common.ResponseCondition(c.CmdClause, &c.ResponseCondition)
    67  	c.RegisterFlag(argparser.StringFlagOpts{
    68  		Name:        argparser.FlagServiceIDName,
    69  		Description: argparser.FlagServiceIDDesc,
    70  		Dst:         &g.Manifest.Flag.ServiceID,
    71  		Short:       's',
    72  	})
    73  	c.RegisterFlag(argparser.StringFlagOpts{
    74  		Action:      c.ServiceName.Set,
    75  		Name:        argparser.FlagServiceName,
    76  		Description: argparser.FlagServiceDesc,
    77  		Dst:         &c.ServiceName.Value,
    78  	})
    79  	return &c
    80  }
    81  
    82  // ConstructInput transforms values parsed from CLI flags into an object to be used by the API client library.
    83  func (c *UpdateCommand) ConstructInput(serviceID string, serviceVersion int) (*fastly.UpdatePapertrailInput, error) {
    84  	input := fastly.UpdatePapertrailInput{
    85  		ServiceID:      serviceID,
    86  		ServiceVersion: serviceVersion,
    87  		Name:           c.EndpointName,
    88  	}
    89  
    90  	// Set new values if set by user.
    91  	if c.NewName.WasSet {
    92  		input.NewName = &c.NewName.Value
    93  	}
    94  
    95  	if c.NewName.WasSet {
    96  		input.NewName = &c.NewName.Value
    97  	}
    98  
    99  	if c.Address.WasSet {
   100  		input.Address = &c.Address.Value
   101  	}
   102  
   103  	if c.Port.WasSet {
   104  		input.Port = &c.Port.Value
   105  	}
   106  
   107  	if c.FormatVersion.WasSet {
   108  		input.FormatVersion = &c.FormatVersion.Value
   109  	}
   110  
   111  	if c.Format.WasSet {
   112  		input.Format = &c.Format.Value
   113  	}
   114  
   115  	if c.ResponseCondition.WasSet {
   116  		input.ResponseCondition = &c.ResponseCondition.Value
   117  	}
   118  
   119  	if c.Placement.WasSet {
   120  		input.Placement = &c.Placement.Value
   121  	}
   122  
   123  	return &input, nil
   124  }
   125  
   126  // Exec invokes the application logic for the command.
   127  func (c *UpdateCommand) Exec(_ io.Reader, out io.Writer) error {
   128  	serviceID, serviceVersion, err := argparser.ServiceDetails(argparser.ServiceDetailsOpts{
   129  		AutoCloneFlag:      c.AutoClone,
   130  		APIClient:          c.Globals.APIClient,
   131  		Manifest:           *c.Globals.Manifest,
   132  		Out:                out,
   133  		ServiceNameFlag:    c.ServiceName,
   134  		ServiceVersionFlag: c.ServiceVersion,
   135  		VerboseMode:        c.Globals.Flags.Verbose,
   136  	})
   137  	if err != nil {
   138  		c.Globals.ErrLog.AddWithContext(err, map[string]any{
   139  			"Service ID":      serviceID,
   140  			"Service Version": errors.ServiceVersion(serviceVersion),
   141  		})
   142  		return err
   143  	}
   144  
   145  	input, err := c.ConstructInput(serviceID, fastly.ToValue(serviceVersion.Number))
   146  	if err != nil {
   147  		c.Globals.ErrLog.Add(err)
   148  		return err
   149  	}
   150  
   151  	papertrail, err := c.Globals.APIClient.UpdatePapertrail(input)
   152  	if err != nil {
   153  		c.Globals.ErrLog.Add(err)
   154  		return err
   155  	}
   156  
   157  	text.Success(out,
   158  		"Updated Papertrail logging endpoint %s (service %s version %d)",
   159  		fastly.ToValue(papertrail.Name),
   160  		fastly.ToValue(papertrail.ServiceID),
   161  		fastly.ToValue(papertrail.ServiceVersion),
   162  	)
   163  	return nil
   164  }