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

     1  package openstack
     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 OpenStack 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  	AccessKey         argparser.OptionalString
    28  	AutoClone         argparser.OptionalAutoClone
    29  	BucketName        argparser.OptionalString
    30  	CompressionCodec  argparser.OptionalString
    31  	EndpointName      argparser.OptionalString // Can't shadow argparser.Base method Name().
    32  	Format            argparser.OptionalString
    33  	FormatVersion     argparser.OptionalInt
    34  	GzipLevel         argparser.OptionalInt
    35  	MessageType       argparser.OptionalString
    36  	Path              argparser.OptionalString
    37  	Period            argparser.OptionalInt
    38  	Placement         argparser.OptionalString
    39  	PublicKey         argparser.OptionalString
    40  	ResponseCondition argparser.OptionalString
    41  	TimestampFormat   argparser.OptionalString
    42  	URL               argparser.OptionalString
    43  	User              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 OpenStack logging endpoint on a Fastly service version").Alias("add")
    54  
    55  	// Required.
    56  	c.CmdClause.Flag("name", "The name of the OpenStack logging object. Used as a primary key for API access").Short('n').Action(c.EndpointName.Set).StringVar(&c.EndpointName.Value)
    57  	c.RegisterFlag(argparser.StringFlagOpts{
    58  		Name:        argparser.FlagVersionName,
    59  		Description: argparser.FlagVersionDesc,
    60  		Dst:         &c.ServiceVersion.Value,
    61  		Required:    true,
    62  	})
    63  
    64  	// Optional.
    65  	c.CmdClause.Flag("access-key", "Your OpenStack account access key").Action(c.AccessKey.Set).StringVar(&c.AccessKey.Value)
    66  	c.RegisterAutoCloneFlag(argparser.AutoCloneFlagOpts{
    67  		Action: c.AutoClone.Set,
    68  		Dst:    &c.AutoClone.Value,
    69  	})
    70  	c.CmdClause.Flag("bucket", "The name of your OpenStack container").Action(c.BucketName.Set).StringVar(&c.BucketName.Value)
    71  	common.CompressionCodec(c.CmdClause, &c.CompressionCodec)
    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  	common.Path(c.CmdClause, &c.Path)
    77  	common.Period(c.CmdClause, &c.Period)
    78  	common.Placement(c.CmdClause, &c.Placement)
    79  	common.PublicKey(c.CmdClause, &c.PublicKey)
    80  	common.ResponseCondition(c.CmdClause, &c.ResponseCondition)
    81  	common.TimestampFormat(c.CmdClause, &c.TimestampFormat)
    82  	c.RegisterFlag(argparser.StringFlagOpts{
    83  		Name:        argparser.FlagServiceIDName,
    84  		Description: argparser.FlagServiceIDDesc,
    85  		Dst:         &g.Manifest.Flag.ServiceID,
    86  		Short:       's',
    87  	})
    88  	c.RegisterFlag(argparser.StringFlagOpts{
    89  		Action:      c.ServiceName.Set,
    90  		Name:        argparser.FlagServiceName,
    91  		Description: argparser.FlagServiceDesc,
    92  		Dst:         &c.ServiceName.Value,
    93  	})
    94  	c.CmdClause.Flag("url", "Your OpenStack auth url").Action(c.URL.Set).StringVar(&c.URL.Value)
    95  	c.CmdClause.Flag("user", "The username for your OpenStack account").Action(c.User.Set).StringVar(&c.User.Value)
    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.CreateOpenstackInput, error) {
   101  	var input fastly.CreateOpenstackInput
   102  
   103  	input.ServiceID = serviceID
   104  	input.ServiceVersion = serviceVersion
   105  	if c.EndpointName.WasSet {
   106  		input.Name = &c.EndpointName.Value
   107  	}
   108  	if c.BucketName.WasSet {
   109  		input.BucketName = &c.BucketName.Value
   110  	}
   111  	if c.AccessKey.WasSet {
   112  		input.AccessKey = &c.AccessKey.Value
   113  	}
   114  	if c.User.WasSet {
   115  		input.User = &c.User.Value
   116  	}
   117  	if c.URL.WasSet {
   118  		input.URL = &c.URL.Value
   119  	}
   120  
   121  	// The following blocks enforces the mutual exclusivity of the
   122  	// CompressionCodec and GzipLevel flags.
   123  	if c.CompressionCodec.WasSet && c.GzipLevel.WasSet {
   124  		return nil, fmt.Errorf("error parsing arguments: the --compression-codec flag is mutually exclusive with the --gzip-level flag")
   125  	}
   126  
   127  	if c.PublicKey.WasSet {
   128  		input.PublicKey = &c.PublicKey.Value
   129  	}
   130  
   131  	if c.Path.WasSet {
   132  		input.Path = &c.Path.Value
   133  	}
   134  
   135  	if c.Period.WasSet {
   136  		input.Period = &c.Period.Value
   137  	}
   138  
   139  	if c.GzipLevel.WasSet {
   140  		input.GzipLevel = &c.GzipLevel.Value
   141  	}
   142  
   143  	if c.Format.WasSet {
   144  		input.Format = &c.Format.Value
   145  	}
   146  
   147  	if c.FormatVersion.WasSet {
   148  		input.FormatVersion = &c.FormatVersion.Value
   149  	}
   150  
   151  	if c.ResponseCondition.WasSet {
   152  		input.ResponseCondition = &c.ResponseCondition.Value
   153  	}
   154  
   155  	if c.MessageType.WasSet {
   156  		input.MessageType = &c.MessageType.Value
   157  	}
   158  
   159  	if c.TimestampFormat.WasSet {
   160  		input.TimestampFormat = &c.TimestampFormat.Value
   161  	}
   162  
   163  	if c.Placement.WasSet {
   164  		input.Placement = &c.Placement.Value
   165  	}
   166  
   167  	if c.CompressionCodec.WasSet {
   168  		input.CompressionCodec = &c.CompressionCodec.Value
   169  	}
   170  
   171  	return &input, nil
   172  }
   173  
   174  // Exec invokes the application logic for the command.
   175  func (c *CreateCommand) Exec(_ io.Reader, out io.Writer) error {
   176  	serviceID, serviceVersion, err := argparser.ServiceDetails(argparser.ServiceDetailsOpts{
   177  		AutoCloneFlag:      c.AutoClone,
   178  		APIClient:          c.Globals.APIClient,
   179  		Manifest:           *c.Globals.Manifest,
   180  		Out:                out,
   181  		ServiceNameFlag:    c.ServiceName,
   182  		ServiceVersionFlag: c.ServiceVersion,
   183  		VerboseMode:        c.Globals.Flags.Verbose,
   184  	})
   185  	if err != nil {
   186  		c.Globals.ErrLog.AddWithContext(err, map[string]any{
   187  			"Service ID":      serviceID,
   188  			"Service Version": errors.ServiceVersion(serviceVersion),
   189  		})
   190  		return err
   191  	}
   192  
   193  	input, err := c.ConstructInput(serviceID, fastly.ToValue(serviceVersion.Number))
   194  	if err != nil {
   195  		c.Globals.ErrLog.Add(err)
   196  		return err
   197  	}
   198  
   199  	d, err := c.Globals.APIClient.CreateOpenstack(input)
   200  	if err != nil {
   201  		c.Globals.ErrLog.Add(err)
   202  		return err
   203  	}
   204  
   205  	text.Success(out,
   206  		"Created OpenStack logging endpoint %s (service %s version %d)",
   207  		fastly.ToValue(d.Name),
   208  		fastly.ToValue(d.ServiceID),
   209  		fastly.ToValue(d.ServiceVersion),
   210  	)
   211  	return nil
   212  }