github.com/fastly/cli@v1.7.2-0.20240304164155-9d0f1d77c3bf/pkg/commands/logging/bigquery/create.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  // CreateCommand calls the Fastly API to create a BigQuery logging endpoint.
    17  type CreateCommand struct {
    18  	argparser.Base
    19  	Manifest manifest.Data
    20  
    21  	// Required.
    22  	ServiceName    argparser.OptionalServiceNameID
    23  	ServiceVersion argparser.OptionalServiceVersion
    24  
    25  	// Optional.
    26  	AccountName       argparser.OptionalString
    27  	AutoClone         argparser.OptionalAutoClone
    28  	Dataset           argparser.OptionalString
    29  	EndpointName      argparser.OptionalString // Can't shadow argparser.Base method Name().
    30  	Format            argparser.OptionalString
    31  	FormatVersion     argparser.OptionalInt
    32  	Placement         argparser.OptionalString
    33  	ProjectID         argparser.OptionalString
    34  	ResponseCondition argparser.OptionalString
    35  	SecretKey         argparser.OptionalString
    36  	Table             argparser.OptionalString
    37  	Template          argparser.OptionalString
    38  	User              argparser.OptionalString
    39  }
    40  
    41  // NewCreateCommand returns a usable command registered under the parent.
    42  func NewCreateCommand(parent argparser.Registerer, g *global.Data) *CreateCommand {
    43  	c := CreateCommand{
    44  		Base: argparser.Base{
    45  			Globals: g,
    46  		},
    47  	}
    48  	c.CmdClause = parent.Command("create", "Create a BigQuery logging endpoint on a Fastly service version").Alias("add")
    49  
    50  	// Required.
    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  	c.CmdClause.Flag("dataset", "Your BigQuery dataset").Action(c.Dataset.Set).StringVar(&c.Dataset.Value)
    65  	common.Format(c.CmdClause, &c.Format)
    66  	common.FormatVersion(c.CmdClause, &c.FormatVersion)
    67  	c.CmdClause.Flag("name", "The name of the BigQuery logging object. Used as a primary key for API access").Short('n').Action(c.EndpointName.Set).StringVar(&c.EndpointName.Value)
    68  	common.Placement(c.CmdClause, &c.Placement)
    69  	c.CmdClause.Flag("project-id", "Your Google Cloud Platform project ID").Action(c.ProjectID.Set).StringVar(&c.ProjectID.Value)
    70  	common.ResponseCondition(c.CmdClause, &c.ResponseCondition)
    71  	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)
    72  	c.RegisterFlag(argparser.StringFlagOpts{
    73  		Name:        argparser.FlagServiceIDName,
    74  		Description: argparser.FlagServiceIDDesc,
    75  		Dst:         &g.Manifest.Flag.ServiceID,
    76  		Short:       's',
    77  	})
    78  	c.RegisterFlag(argparser.StringFlagOpts{
    79  		Action:      c.ServiceName.Set,
    80  		Name:        argparser.FlagServiceName,
    81  		Description: argparser.FlagServiceDesc,
    82  		Dst:         &c.ServiceName.Value,
    83  	})
    84  	c.CmdClause.Flag("table", "Your BigQuery table").Action(c.Table.Set).StringVar(&c.Table.Value)
    85  	c.CmdClause.Flag("template-suffix", "BigQuery table name suffix template").Action(c.Template.Set).StringVar(&c.Template.Value)
    86  	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)
    87  	return &c
    88  }
    89  
    90  // ConstructInput transforms values parsed from CLI flags into an object to be used by the API client library.
    91  func (c *CreateCommand) ConstructInput(serviceID string, serviceVersion int) (*fastly.CreateBigQueryInput, error) {
    92  	input := fastly.CreateBigQueryInput{
    93  		ServiceID:      serviceID,
    94  		ServiceVersion: serviceVersion,
    95  	}
    96  
    97  	if c.AccountName.WasSet {
    98  		input.AccountName = &c.AccountName.Value
    99  	}
   100  	if c.Dataset.WasSet {
   101  		input.Dataset = &c.Dataset.Value
   102  	}
   103  	if c.EndpointName.WasSet {
   104  		input.Name = &c.EndpointName.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.Placement.WasSet {
   113  		input.Placement = &c.Placement.Value
   114  	}
   115  	if c.ProjectID.WasSet {
   116  		input.ProjectID = &c.ProjectID.Value
   117  	}
   118  	if c.ResponseCondition.WasSet {
   119  		input.ResponseCondition = &c.ResponseCondition.Value
   120  	}
   121  	if c.SecretKey.WasSet {
   122  		input.SecretKey = &c.SecretKey.Value
   123  	}
   124  	if c.Table.WasSet {
   125  		input.Table = &c.Table.Value
   126  	}
   127  	if c.Template.WasSet {
   128  		input.Template = &c.Template.Value
   129  	}
   130  	if c.User.WasSet {
   131  		input.User = &c.User.Value
   132  	}
   133  
   134  	return &input, nil
   135  }
   136  
   137  // Exec invokes the application logic for the command.
   138  func (c *CreateCommand) Exec(_ io.Reader, out io.Writer) error {
   139  	serviceID, serviceVersion, err := argparser.ServiceDetails(argparser.ServiceDetailsOpts{
   140  		AutoCloneFlag:      c.AutoClone,
   141  		APIClient:          c.Globals.APIClient,
   142  		Manifest:           *c.Globals.Manifest,
   143  		Out:                out,
   144  		ServiceNameFlag:    c.ServiceName,
   145  		ServiceVersionFlag: c.ServiceVersion,
   146  		VerboseMode:        c.Globals.Flags.Verbose,
   147  	})
   148  	if err != nil {
   149  		c.Globals.ErrLog.AddWithContext(err, map[string]any{
   150  			"Service ID":      serviceID,
   151  			"Service Version": fsterr.ServiceVersion(serviceVersion),
   152  		})
   153  		return err
   154  	}
   155  
   156  	input, err := c.ConstructInput(serviceID, fastly.ToValue(serviceVersion.Number))
   157  	if err != nil {
   158  		c.Globals.ErrLog.AddWithContext(err, map[string]any{
   159  			"Service ID":      serviceID,
   160  			"Service Version": fastly.ToValue(serviceVersion.Number),
   161  		})
   162  		return err
   163  	}
   164  
   165  	d, err := c.Globals.APIClient.CreateBigQuery(input)
   166  	if err != nil {
   167  		c.Globals.ErrLog.AddWithContext(err, map[string]any{
   168  			"Service ID":      serviceID,
   169  			"Service Version": fastly.ToValue(serviceVersion.Number),
   170  		})
   171  		return err
   172  	}
   173  
   174  	text.Success(out,
   175  		"Created BigQuery logging endpoint %s (service %s version %d)",
   176  		fastly.ToValue(d.Name),
   177  		fastly.ToValue(d.ServiceID),
   178  		fastly.ToValue(d.ServiceVersion),
   179  	)
   180  	return nil
   181  }