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

     1  package activation
     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  	var c CreateCommand
    16  	c.CmdClause = parent.Command("enable", "Enable TLS for a particular TLS domain and certificate combination").Alias("add")
    17  	c.Globals = g
    18  
    19  	// Required.
    20  	c.CmdClause.Flag("cert-id", "Alphanumeric string identifying a TLS certificate").Required().StringVar(&c.certID)
    21  	c.CmdClause.Flag("id", "Alphanumeric string identifying a TLS activation").Required().StringVar(&c.id)
    22  
    23  	return &c
    24  }
    25  
    26  // CreateCommand calls the Fastly API to create an appropriate resource.
    27  type CreateCommand struct {
    28  	argparser.Base
    29  
    30  	certID string
    31  	id     string
    32  }
    33  
    34  // Exec invokes the application logic for the command.
    35  func (c *CreateCommand) Exec(_ io.Reader, out io.Writer) error {
    36  	input := c.constructInput()
    37  
    38  	r, err := c.Globals.APIClient.CreateTLSActivation(input)
    39  	if err != nil {
    40  		c.Globals.ErrLog.AddWithContext(err, map[string]any{
    41  			"TLS Activation ID":             c.id,
    42  			"TLS Activation Certificate ID": c.certID,
    43  		})
    44  		return err
    45  	}
    46  
    47  	text.Success(out, "Enabled TLS Activation '%s' (Certificate '%s')", r.ID, r.Certificate.ID)
    48  	return nil
    49  }
    50  
    51  // constructInput transforms values parsed from CLI flags into an object to be used by the API client library.
    52  func (c *CreateCommand) constructInput() *fastly.CreateTLSActivationInput {
    53  	var input fastly.CreateTLSActivationInput
    54  
    55  	input.ID = c.id
    56  	input.Certificate = &fastly.CustomTLSCertificate{ID: c.certID}
    57  
    58  	return &input
    59  }