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

     1  package azureblob
     2  
     3  import (
     4  	"fmt"
     5  	"io"
     6  
     7  	"github.com/fastly/go-fastly/v9/fastly"
     8  
     9  	"github.com/fastly/cli/pkg/argparser"
    10  	"github.com/fastly/cli/pkg/commands/logging/common"
    11  	"github.com/fastly/cli/pkg/errors"
    12  	"github.com/fastly/cli/pkg/global"
    13  	"github.com/fastly/cli/pkg/manifest"
    14  	"github.com/fastly/cli/pkg/text"
    15  )
    16  
    17  // CreateCommand calls the Fastly API to create an Azure Blob Storage logging endpoint.
    18  type CreateCommand struct {
    19  	argparser.Base
    20  	Manifest manifest.Data
    21  
    22  	// Required.
    23  	ServiceName    argparser.OptionalServiceNameID
    24  	ServiceVersion argparser.OptionalServiceVersion
    25  
    26  	// Optional.
    27  	EndpointName      argparser.OptionalString
    28  	Container         argparser.OptionalString
    29  	AccountName       argparser.OptionalString
    30  	SASToken          argparser.OptionalString
    31  	AutoClone         argparser.OptionalAutoClone
    32  	Path              argparser.OptionalString
    33  	Period            argparser.OptionalInt
    34  	GzipLevel         argparser.OptionalInt
    35  	MessageType       argparser.OptionalString
    36  	Format            argparser.OptionalString
    37  	FormatVersion     argparser.OptionalInt
    38  	ResponseCondition argparser.OptionalString
    39  	TimestampFormat   argparser.OptionalString
    40  	Placement         argparser.OptionalString
    41  	PublicKey         argparser.OptionalString
    42  	FileMaxBytes      argparser.OptionalInt
    43  	CompressionCodec  argparser.OptionalString
    44  }
    45  
    46  // NewCreateCommand returns a usable command registered under the parent.
    47  func NewCreateCommand(parent argparser.Registerer, g *global.Data) *CreateCommand {
    48  	c := CreateCommand{
    49  		Base: argparser.Base{
    50  			Globals: g,
    51  		},
    52  	}
    53  	c.CmdClause = parent.Command("create", "Create an Azure Blob Storage logging endpoint on a Fastly service version").Alias("add")
    54  
    55  	// Required.
    56  	c.RegisterFlag(argparser.StringFlagOpts{
    57  		Name:        argparser.FlagVersionName,
    58  		Description: argparser.FlagVersionDesc,
    59  		Dst:         &c.ServiceVersion.Value,
    60  		Required:    true,
    61  	})
    62  
    63  	// Optional.
    64  	c.CmdClause.Flag("account-name", "The unique Azure Blob Storage namespace in which your data objects are stored").Action(c.AccountName.Set).StringVar(&c.AccountName.Value)
    65  	c.RegisterAutoCloneFlag(argparser.AutoCloneFlagOpts{
    66  		Action: c.AutoClone.Set,
    67  		Dst:    &c.AutoClone.Value,
    68  	})
    69  	common.CompressionCodec(c.CmdClause, &c.CompressionCodec)
    70  	c.CmdClause.Flag("container", "The name of the Azure Blob Storage container in which to store logs").Action(c.Container.Set).StringVar(&c.Container.Value)
    71  	c.CmdClause.Flag("file-max-bytes", "The maximum size of a log file in bytes").Action(c.FileMaxBytes.Set).IntVar(&c.FileMaxBytes.Value)
    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("name", "The name of the Azure Blob Storage logging object. Used as a primary key for API access").Short('n').Action(c.EndpointName.Set).StringVar(&c.EndpointName.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.CmdClause.Flag("sas-token", "The Azure shared access signature providing write access to the blob service objects. Be sure to update your token before it expires or the logging functionality will not work").Action(c.SASToken.Set).StringVar(&c.SASToken.Value)
    83  	c.RegisterFlag(argparser.StringFlagOpts{
    84  		Name:        argparser.FlagServiceIDName,
    85  		Description: argparser.FlagServiceIDDesc,
    86  		Dst:         &g.Manifest.Flag.ServiceID,
    87  		Short:       's',
    88  	})
    89  	c.RegisterFlag(argparser.StringFlagOpts{
    90  		Action:      c.ServiceName.Set,
    91  		Name:        argparser.FlagServiceName,
    92  		Description: argparser.FlagServiceDesc,
    93  		Dst:         &c.ServiceName.Value,
    94  	})
    95  	common.TimestampFormat(c.CmdClause, &c.TimestampFormat)
    96  	return &c
    97  }
    98  
    99  // ConstructInput transforms values parsed from CLI flags into an object to be used by the API client library.
   100  func (c *CreateCommand) ConstructInput(serviceID string, serviceVersion int) (*fastly.CreateBlobStorageInput, error) {
   101  	var input fastly.CreateBlobStorageInput
   102  
   103  	input.ServiceID = serviceID
   104  	input.ServiceVersion = serviceVersion
   105  
   106  	if c.EndpointName.WasSet {
   107  		input.Name = &c.EndpointName.Value
   108  	}
   109  	if c.Container.WasSet {
   110  		input.Container = &c.Container.Value
   111  	}
   112  	if c.AccountName.WasSet {
   113  		input.AccountName = &c.AccountName.Value
   114  	}
   115  	if c.SASToken.WasSet {
   116  		input.SASToken = &c.SASToken.Value
   117  	}
   118  
   119  	// The following blocks enforces the mutual exclusivity of the
   120  	// CompressionCodec and GzipLevel flags.
   121  	if c.CompressionCodec.WasSet && c.GzipLevel.WasSet {
   122  		return nil, fmt.Errorf("error parsing arguments: the --compression-codec flag is mutually exclusive with the --gzip-level flag")
   123  	}
   124  
   125  	if c.Path.WasSet {
   126  		input.Path = &c.Path.Value
   127  	}
   128  	if c.Period.WasSet {
   129  		input.Period = &c.Period.Value
   130  	}
   131  	if c.GzipLevel.WasSet {
   132  		input.GzipLevel = &c.GzipLevel.Value
   133  	}
   134  	if c.Format.WasSet {
   135  		input.Format = &c.Format.Value
   136  	}
   137  	if c.FormatVersion.WasSet {
   138  		input.FormatVersion = &c.FormatVersion.Value
   139  	}
   140  	if c.ResponseCondition.WasSet {
   141  		input.ResponseCondition = &c.ResponseCondition.Value
   142  	}
   143  	if c.MessageType.WasSet {
   144  		input.MessageType = &c.MessageType.Value
   145  	}
   146  	if c.TimestampFormat.WasSet {
   147  		input.TimestampFormat = &c.TimestampFormat.Value
   148  	}
   149  	if c.Placement.WasSet {
   150  		input.Placement = &c.Placement.Value
   151  	}
   152  	if c.PublicKey.WasSet {
   153  		input.PublicKey = &c.PublicKey.Value
   154  	}
   155  	if c.FileMaxBytes.WasSet {
   156  		input.FileMaxBytes = &c.FileMaxBytes.Value
   157  	}
   158  	if c.CompressionCodec.WasSet {
   159  		input.CompressionCodec = &c.CompressionCodec.Value
   160  	}
   161  
   162  	return &input, nil
   163  }
   164  
   165  // Exec invokes the application logic for the command.
   166  func (c *CreateCommand) Exec(_ io.Reader, out io.Writer) error {
   167  	serviceID, serviceVersion, err := argparser.ServiceDetails(argparser.ServiceDetailsOpts{
   168  		AutoCloneFlag:      c.AutoClone,
   169  		APIClient:          c.Globals.APIClient,
   170  		Manifest:           *c.Globals.Manifest,
   171  		Out:                out,
   172  		ServiceNameFlag:    c.ServiceName,
   173  		ServiceVersionFlag: c.ServiceVersion,
   174  		VerboseMode:        c.Globals.Flags.Verbose,
   175  	})
   176  	if err != nil {
   177  		c.Globals.ErrLog.AddWithContext(err, map[string]any{
   178  			"Service ID":      serviceID,
   179  			"Service Version": errors.ServiceVersion(serviceVersion),
   180  		})
   181  		return err
   182  	}
   183  
   184  	input, err := c.ConstructInput(serviceID, fastly.ToValue(serviceVersion.Number))
   185  	if err != nil {
   186  		c.Globals.ErrLog.AddWithContext(err, map[string]any{
   187  			"Service ID":      serviceID,
   188  			"Service Version": fastly.ToValue(serviceVersion.Number),
   189  		})
   190  		return err
   191  	}
   192  
   193  	d, err := c.Globals.APIClient.CreateBlobStorage(input)
   194  	if err != nil {
   195  		c.Globals.ErrLog.AddWithContext(err, map[string]any{
   196  			"Service ID":      serviceID,
   197  			"Service Version": fastly.ToValue(serviceVersion.Number),
   198  		})
   199  		return err
   200  	}
   201  
   202  	text.Success(out,
   203  		"Created Azure Blob Storage logging endpoint %s (service %s version %d)",
   204  		fastly.ToValue(d.Name),
   205  		fastly.ToValue(d.ServiceID),
   206  		fastly.ToValue(d.ServiceVersion),
   207  	)
   208  	return nil
   209  }