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

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