github.com/fastly/cli@v1.7.2-0.20240304164155-9d0f1d77c3bf/pkg/commands/tls/custom/activation/update.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  // NewUpdateCommand returns a usable command registered under the parent.
    14  func NewUpdateCommand(parent argparser.Registerer, g *global.Data) *UpdateCommand {
    15  	var c UpdateCommand
    16  	c.CmdClause = parent.Command("update", "Update the certificate used to terminate TLS traffic for the domain associated with this TLS activation")
    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  	return &c
    23  }
    24  
    25  // UpdateCommand calls the Fastly API to update an appropriate resource.
    26  type UpdateCommand struct {
    27  	argparser.Base
    28  
    29  	certID string
    30  	id     string
    31  }
    32  
    33  // Exec invokes the application logic for the command.
    34  func (c *UpdateCommand) Exec(_ io.Reader, out io.Writer) error {
    35  	input := c.constructInput()
    36  
    37  	r, err := c.Globals.APIClient.UpdateTLSActivation(input)
    38  	if err != nil {
    39  		c.Globals.ErrLog.AddWithContext(err, map[string]any{
    40  			"TLS Activation ID":             c.id,
    41  			"TLS Activation Certificate ID": c.certID,
    42  		})
    43  		return err
    44  	}
    45  
    46  	text.Success(out, "Updated TLS Activation Certificate '%s' (previously: '%s')", r.Certificate.ID, input.Certificate.ID)
    47  	return nil
    48  }
    49  
    50  // constructInput transforms values parsed from CLI flags into an object to be used by the API client library.
    51  func (c *UpdateCommand) constructInput() *fastly.UpdateTLSActivationInput {
    52  	var input fastly.UpdateTLSActivationInput
    53  
    54  	input.ID = c.id
    55  	input.Certificate = &fastly.CustomTLSCertificate{ID: c.certID}
    56  
    57  	return &input
    58  }