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

     1  package loggly
     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  // CreateCommand calls the Fastly API to create a Loggly 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  	AutoClone         argparser.OptionalAutoClone
    27  	EndpointName      argparser.OptionalString // Can't shadow argparser.Base method Name().
    28  	Format            argparser.OptionalString
    29  	FormatVersion     argparser.OptionalInt
    30  	Placement         argparser.OptionalString
    31  	ResponseCondition argparser.OptionalString
    32  	Token             argparser.OptionalString
    33  }
    34  
    35  // NewCreateCommand returns a usable command registered under the parent.
    36  func NewCreateCommand(parent argparser.Registerer, g *global.Data) *CreateCommand {
    37  	c := CreateCommand{
    38  		Base: argparser.Base{
    39  			Globals: g,
    40  		},
    41  	}
    42  	c.CmdClause = parent.Command("create", "Create a Loggly logging endpoint on a Fastly service version").Alias("add")
    43  
    44  	// Required.
    45  	c.CmdClause.Flag("name", "The name of the Loggly logging object. Used as a primary key for API access").Short('n').Action(c.EndpointName.Set).StringVar(&c.EndpointName.Value)
    46  	c.RegisterFlag(argparser.StringFlagOpts{
    47  		Name:        argparser.FlagVersionName,
    48  		Description: argparser.FlagVersionDesc,
    49  		Dst:         &c.ServiceVersion.Value,
    50  		Required:    true,
    51  	})
    52  
    53  	// Optional.
    54  	c.CmdClause.Flag("auth-token", "The token to use for authentication (https://www.loggly.com/docs/customer-token-authentication-token/)").Action(c.Token.Set).StringVar(&c.Token.Value)
    55  	c.RegisterAutoCloneFlag(argparser.AutoCloneFlagOpts{
    56  		Action: c.AutoClone.Set,
    57  		Dst:    &c.AutoClone.Value,
    58  	})
    59  	common.Format(c.CmdClause, &c.Format)
    60  	common.FormatVersion(c.CmdClause, &c.FormatVersion)
    61  	common.ResponseCondition(c.CmdClause, &c.ResponseCondition)
    62  	common.Placement(c.CmdClause, &c.Placement)
    63  	c.RegisterFlag(argparser.StringFlagOpts{
    64  		Name:        argparser.FlagServiceIDName,
    65  		Description: argparser.FlagServiceIDDesc,
    66  		Dst:         &g.Manifest.Flag.ServiceID,
    67  		Short:       's',
    68  	})
    69  	c.RegisterFlag(argparser.StringFlagOpts{
    70  		Action:      c.ServiceName.Set,
    71  		Name:        argparser.FlagServiceName,
    72  		Description: argparser.FlagServiceDesc,
    73  		Dst:         &c.ServiceName.Value,
    74  	})
    75  	return &c
    76  }
    77  
    78  // ConstructInput transforms values parsed from CLI flags into an object to be used by the API client library.
    79  func (c *CreateCommand) ConstructInput(serviceID string, serviceVersion int) (*fastly.CreateLogglyInput, error) {
    80  	var input fastly.CreateLogglyInput
    81  
    82  	input.ServiceID = serviceID
    83  	input.ServiceVersion = serviceVersion
    84  	if c.EndpointName.WasSet {
    85  		input.Name = &c.EndpointName.Value
    86  	}
    87  	if c.Token.WasSet {
    88  		input.Token = &c.Token.Value
    89  	}
    90  
    91  	if c.Format.WasSet {
    92  		input.Format = &c.Format.Value
    93  	}
    94  
    95  	if c.FormatVersion.WasSet {
    96  		input.FormatVersion = &c.FormatVersion.Value
    97  	}
    98  
    99  	if c.ResponseCondition.WasSet {
   100  		input.ResponseCondition = &c.ResponseCondition.Value
   101  	}
   102  
   103  	if c.Placement.WasSet {
   104  		input.Placement = &c.Placement.Value
   105  	}
   106  
   107  	return &input, nil
   108  }
   109  
   110  // Exec invokes the application logic for the command.
   111  func (c *CreateCommand) Exec(_ io.Reader, out io.Writer) error {
   112  	serviceID, serviceVersion, err := argparser.ServiceDetails(argparser.ServiceDetailsOpts{
   113  		AutoCloneFlag:      c.AutoClone,
   114  		APIClient:          c.Globals.APIClient,
   115  		Manifest:           *c.Globals.Manifest,
   116  		Out:                out,
   117  		ServiceNameFlag:    c.ServiceName,
   118  		ServiceVersionFlag: c.ServiceVersion,
   119  		VerboseMode:        c.Globals.Flags.Verbose,
   120  	})
   121  	if err != nil {
   122  		c.Globals.ErrLog.AddWithContext(err, map[string]any{
   123  			"Service ID":      serviceID,
   124  			"Service Version": errors.ServiceVersion(serviceVersion),
   125  		})
   126  		return err
   127  	}
   128  
   129  	input, err := c.ConstructInput(serviceID, fastly.ToValue(serviceVersion.Number))
   130  	if err != nil {
   131  		c.Globals.ErrLog.Add(err)
   132  		return err
   133  	}
   134  
   135  	d, err := c.Globals.APIClient.CreateLoggly(input)
   136  	if err != nil {
   137  		c.Globals.ErrLog.Add(err)
   138  		return err
   139  	}
   140  
   141  	text.Success(out,
   142  		"Created Loggly logging endpoint %s (service %s version %d)",
   143  		fastly.ToValue(d.Name),
   144  		fastly.ToValue(d.ServiceID),
   145  		fastly.ToValue(d.ServiceVersion),
   146  	)
   147  	return nil
   148  }