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

     1  package serviceauth
     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  // Permissions is a list of supported permission values.
    14  // https://developer.fastly.com/reference/api/account/service-authorization/#data-model
    15  var Permissions = []string{"full", "read_only", "purge_select", "purge_all"}
    16  
    17  // CreateCommand calls the Fastly API to create a service authorization.
    18  type CreateCommand struct {
    19  	argparser.Base
    20  	input       fastly.CreateServiceAuthorizationInput
    21  	serviceName argparser.OptionalServiceNameID
    22  	userID      string
    23  }
    24  
    25  // NewCreateCommand returns a usable command registered under the parent.
    26  func NewCreateCommand(parent argparser.Registerer, g *global.Data) *CreateCommand {
    27  	c := CreateCommand{
    28  		Base: argparser.Base{
    29  			Globals: g,
    30  		},
    31  	}
    32  	c.CmdClause = parent.Command("create", "Create service authorization").Alias("add")
    33  
    34  	// Required.
    35  	c.CmdClause.Flag("user-id", "Alphanumeric string identifying the user").Required().Short('u').StringVar(&c.userID)
    36  
    37  	// Optional.
    38  	// NOTE: We default to 'read_only' for security reasons.
    39  	// The API otherwise defaults to 'full' permissions!
    40  	c.CmdClause.Flag("permission", "The permission the user has in relation to the service (default: read_only)").HintOptions(Permissions...).Default("read_only").Short('p').EnumVar(&c.input.Permission, Permissions...)
    41  	c.RegisterFlag(argparser.StringFlagOpts{
    42  		Name:        argparser.FlagServiceIDName,
    43  		Description: argparser.FlagServiceIDDesc,
    44  		Dst:         &g.Manifest.Flag.ServiceID,
    45  		Short:       's',
    46  	})
    47  	c.RegisterFlag(argparser.StringFlagOpts{
    48  		Action:      c.serviceName.Set,
    49  		Name:        argparser.FlagServiceName,
    50  		Description: argparser.FlagServiceDesc,
    51  		Dst:         &c.serviceName.Value,
    52  	})
    53  	return &c
    54  }
    55  
    56  // Exec invokes the application logic for the command.
    57  func (c *CreateCommand) Exec(_ io.Reader, out io.Writer) error {
    58  	serviceID, source, flag, err := argparser.ServiceID(c.serviceName, *c.Globals.Manifest, c.Globals.APIClient, c.Globals.ErrLog)
    59  	if err != nil {
    60  		c.Globals.ErrLog.AddWithContext(err, map[string]any{
    61  			"Service ID":   c.Globals.Manifest.Flag.ServiceID,
    62  			"Service Name": c.serviceName.Value,
    63  		})
    64  		return err
    65  	}
    66  	if c.Globals.Flags.Verbose {
    67  		argparser.DisplayServiceID(serviceID, flag, source, out)
    68  	}
    69  
    70  	c.input.Service = &fastly.SAService{
    71  		ID: serviceID,
    72  	}
    73  	c.input.User = &fastly.SAUser{
    74  		ID: c.userID,
    75  	}
    76  
    77  	s, err := c.Globals.APIClient.CreateServiceAuthorization(&c.input)
    78  	if err != nil {
    79  		c.Globals.ErrLog.AddWithContext(err, map[string]any{
    80  			"Service ID": serviceID,
    81  			"Flag":       flag,
    82  		})
    83  		return err
    84  	}
    85  
    86  	text.Success(out, "Created service authorization %s", s.ID)
    87  	return nil
    88  }