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

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