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

     1  package aclentry
     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/global"
    10  	"github.com/fastly/cli/pkg/text"
    11  )
    12  
    13  // NewCreateCommand returns a usable command registered under the parent.
    14  func NewCreateCommand(parent argparser.Registerer, g *global.Data) *CreateCommand {
    15  	c := CreateCommand{
    16  		Base: argparser.Base{
    17  			Globals: g,
    18  		},
    19  	}
    20  	c.CmdClause = parent.Command("create", "Add an ACL entry to an ACL").Alias("add")
    21  
    22  	// Required.
    23  	c.CmdClause.Flag("acl-id", "Alphanumeric string identifying a ACL").Required().StringVar(&c.aclID)
    24  
    25  	// Optional.
    26  	c.CmdClause.Flag("comment", "A freeform descriptive note").Action(c.comment.Set).StringVar(&c.comment.Value)
    27  	c.CmdClause.Flag("ip", "An IP address").Action(c.ip.Set).StringVar(&c.ip.Value)
    28  	c.CmdClause.Flag("negated", "Whether to negate the match").Action(c.negated.Set).BoolVar(&c.negated.Value)
    29  	c.RegisterFlag(argparser.StringFlagOpts{
    30  		Name:        argparser.FlagServiceIDName,
    31  		Description: argparser.FlagServiceIDDesc,
    32  		Dst:         &g.Manifest.Flag.ServiceID,
    33  		Short:       's',
    34  	})
    35  	c.RegisterFlag(argparser.StringFlagOpts{
    36  		Action:      c.serviceName.Set,
    37  		Name:        argparser.FlagServiceName,
    38  		Description: argparser.FlagServiceDesc,
    39  		Dst:         &c.serviceName.Value,
    40  	})
    41  	c.CmdClause.Flag("subnet", "Number of bits for the subnet mask applied to the IP address").Action(c.subnet.Set).IntVar(&c.subnet.Value)
    42  
    43  	return &c
    44  }
    45  
    46  // CreateCommand calls the Fastly API to create an appropriate resource.
    47  type CreateCommand struct {
    48  	argparser.Base
    49  
    50  	aclID       string
    51  	comment     argparser.OptionalString
    52  	ip          argparser.OptionalString
    53  	negated     argparser.OptionalBool
    54  	serviceName argparser.OptionalServiceNameID
    55  	subnet      argparser.OptionalInt
    56  }
    57  
    58  // Exec invokes the application logic for the command.
    59  func (c *CreateCommand) Exec(_ io.Reader, out io.Writer) error {
    60  	serviceID, source, flag, err := argparser.ServiceID(c.serviceName, *c.Globals.Manifest, c.Globals.APIClient, c.Globals.ErrLog)
    61  	if err != nil {
    62  		return err
    63  	}
    64  	if c.Globals.Verbose() {
    65  		argparser.DisplayServiceID(serviceID, flag, source, out)
    66  	}
    67  
    68  	input := c.constructInput(serviceID)
    69  
    70  	a, err := c.Globals.APIClient.CreateACLEntry(input)
    71  	if err != nil {
    72  		c.Globals.ErrLog.AddWithContext(err, map[string]any{
    73  			"Service ID": serviceID,
    74  		})
    75  		return err
    76  	}
    77  
    78  	text.Success(out, "Created ACL entry '%s' (ip: %s, negated: %t, service: %s)", fastly.ToValue(a.EntryID), fastly.ToValue(a.IP), fastly.ToValue(a.Negated), fastly.ToValue(a.ServiceID))
    79  	return nil
    80  }
    81  
    82  // constructInput transforms values parsed from CLI flags into an object to be used by the API client library.
    83  func (c *CreateCommand) constructInput(serviceID string) *fastly.CreateACLEntryInput {
    84  	input := fastly.CreateACLEntryInput{
    85  		ACLID:     c.aclID,
    86  		ServiceID: serviceID,
    87  	}
    88  	if c.ip.WasSet {
    89  		input.IP = &c.ip.Value
    90  	}
    91  	if c.comment.WasSet {
    92  		input.Comment = &c.comment.Value
    93  	}
    94  	if c.negated.WasSet {
    95  		input.Negated = fastly.ToPointer(fastly.Compatibool(c.negated.Value))
    96  	}
    97  	if c.subnet.WasSet {
    98  		input.Subnet = &c.subnet.Value
    99  	}
   100  
   101  	return &input
   102  }