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

     1  package subscription
     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  const emptyString = ""
    14  
    15  var certAuth = []string{"lets-encrypt", "globalsign"}
    16  
    17  // NewCreateCommand returns a usable command registered under the parent.
    18  func NewCreateCommand(parent argparser.Registerer, g *global.Data) *CreateCommand {
    19  	var c CreateCommand
    20  	c.CmdClause = parent.Command("create", "Create a new TLS subscription").Alias("add")
    21  	c.Globals = g
    22  
    23  	// Required.
    24  	c.CmdClause.Flag("domain", "Domain(s) to add to the TLS certificates generated for the subscription (set flag once per domain)").Required().StringsVar(&c.domains)
    25  
    26  	// Optional.
    27  	c.CmdClause.Flag("cert-auth", "The entity that issues and certifies the TLS certificates for your subscription. Valid values are lets-encrypt or globalsign").HintOptions(certAuth...).EnumVar(&c.certAuth, certAuth...)
    28  	c.CmdClause.Flag("common-name", "The domain name associated with the subscription. Default to the first domain specified by --domain").StringVar(&c.commonName)
    29  	c.CmdClause.Flag("config", "Alphanumeric string identifying a TLS configuration").StringVar(&c.config)
    30  
    31  	return &c
    32  }
    33  
    34  // CreateCommand calls the Fastly API to create an appropriate resource.
    35  type CreateCommand struct {
    36  	argparser.Base
    37  
    38  	certAuth   string
    39  	commonName string
    40  	config     string
    41  	domains    []string
    42  }
    43  
    44  // Exec invokes the application logic for the command.
    45  func (c *CreateCommand) Exec(_ io.Reader, out io.Writer) error {
    46  	input := c.constructInput()
    47  
    48  	r, err := c.Globals.APIClient.CreateTLSSubscription(input)
    49  	if err != nil {
    50  		c.Globals.ErrLog.AddWithContext(err, map[string]any{
    51  			"TLS Domains":               c.domains,
    52  			"TLS Common Name":           c.commonName,
    53  			"TLS Configuration ID":      c.config,
    54  			"TLS Certificate Authority": c.certAuth,
    55  		})
    56  		return err
    57  	}
    58  
    59  	text.Success(out, "Created TLS Subscription '%s' (Authority: %s, Common Name: %s)", r.ID, r.CertificateAuthority, r.CommonName.ID)
    60  	return nil
    61  }
    62  
    63  // constructInput transforms values parsed from CLI flags into an object to be used by the API client library.
    64  func (c *CreateCommand) constructInput() *fastly.CreateTLSSubscriptionInput {
    65  	var input fastly.CreateTLSSubscriptionInput
    66  
    67  	domains := make([]*fastly.TLSDomain, len(c.domains))
    68  	for i, v := range c.domains {
    69  		domains[i] = &fastly.TLSDomain{ID: v}
    70  	}
    71  	input.Domains = domains
    72  
    73  	if c.commonName != emptyString {
    74  		input.CommonName = &fastly.TLSDomain{ID: c.commonName}
    75  	}
    76  	if c.certAuth != emptyString {
    77  		input.CertificateAuthority = c.certAuth
    78  	}
    79  	if c.config != emptyString {
    80  		input.Configuration = &fastly.TLSConfiguration{ID: c.config}
    81  	}
    82  
    83  	return &input
    84  }