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

     1  package platform
     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(
    17  		"update", "Replace a certificate with a newly reissued certificate",
    18  	)
    19  	c.Globals = g
    20  
    21  	// Required.
    22  
    23  	c.CmdClause.Flag(
    24  		"id", "Alphanumeric string identifying a TLS bulk certificate",
    25  	).Required().StringVar(&c.id)
    26  
    27  	c.CmdClause.Flag(
    28  		"cert-blob", "The PEM-formatted certificate blob",
    29  	).Required().StringVar(&c.certBlob)
    30  
    31  	c.CmdClause.Flag(
    32  		"intermediates-blob", "The PEM-formatted chain of intermediate blobs",
    33  	).Required().StringVar(&c.intermediatesBlob)
    34  
    35  	// Optional.
    36  
    37  	c.CmdClause.Flag(
    38  		"allow-untrusted", "Allow certificates that chain to untrusted roots",
    39  	).Action(c.allowUntrusted.Set).BoolVar(&c.allowUntrusted.Value)
    40  
    41  	return &c
    42  }
    43  
    44  // UpdateCommand calls the Fastly API to update an appropriate resource.
    45  type UpdateCommand struct {
    46  	argparser.Base
    47  
    48  	allowUntrusted    argparser.OptionalBool
    49  	certBlob          string
    50  	id                string
    51  	intermediatesBlob string
    52  }
    53  
    54  // Exec invokes the application logic for the command.
    55  func (c *UpdateCommand) Exec(_ io.Reader, out io.Writer) error {
    56  	input := c.constructInput()
    57  
    58  	r, err := c.Globals.APIClient.UpdateBulkCertificate(input)
    59  	if err != nil {
    60  		c.Globals.ErrLog.AddWithContext(err, map[string]any{
    61  			"TLS Bulk Certificate ID": c.id,
    62  			"Allow Untrusted":         c.allowUntrusted.Value,
    63  		})
    64  		return err
    65  	}
    66  
    67  	text.Success(out, "Updated TLS Bulk Certificate '%s'", r.ID)
    68  	return nil
    69  }
    70  
    71  // constructInput transforms values parsed from CLI flags into an object to be
    72  // used by the API client library.
    73  func (c *UpdateCommand) constructInput() *fastly.UpdateBulkCertificateInput {
    74  	var input fastly.UpdateBulkCertificateInput
    75  
    76  	input.ID = c.id
    77  	input.CertBlob = c.certBlob
    78  	input.IntermediatesBlob = c.intermediatesBlob
    79  
    80  	if c.allowUntrusted.WasSet {
    81  		input.AllowUntrusted = c.allowUntrusted.Value
    82  	}
    83  
    84  	return &input
    85  }