github.com/fastly/cli@v1.7.2-0.20240304164155-9d0f1d77c3bf/pkg/commands/tls/subscription/update.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  // 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", "Change the TLS domains or common name associated with this subscription, or update the TLS configuration for this set of domains")
    17  	c.Globals = g
    18  
    19  	// Required.
    20  	c.CmdClause.Flag("id", "Alphanumeric string identifying a TLS subscription").Required().StringVar(&c.id)
    21  
    22  	// Optional.
    23  	c.CmdClause.Flag("common-name", "The domain name associated with the subscription").StringVar(&c.commonName)
    24  	c.CmdClause.Flag("config", "Alphanumeric string identifying a TLS configuration").StringVar(&c.config)
    25  	c.CmdClause.Flag("domain", "Domain(s) to add to the TLS certificates generated for the subscription (set flag once per domain)").StringsVar(&c.domains)
    26  	c.CmdClause.Flag("force", "A flag that allows you to edit and delete a subscription with active domains").Action(c.force.Set).BoolVar(&c.force.Value)
    27  
    28  	return &c
    29  }
    30  
    31  // UpdateCommand calls the Fastly API to update an appropriate resource.
    32  type UpdateCommand struct {
    33  	argparser.Base
    34  
    35  	commonName string
    36  	config     string
    37  	domains    []string
    38  	force      argparser.OptionalBool
    39  	id         string
    40  }
    41  
    42  // Exec invokes the application logic for the command.
    43  func (c *UpdateCommand) Exec(_ io.Reader, out io.Writer) error {
    44  	input := c.constructInput()
    45  
    46  	r, err := c.Globals.APIClient.UpdateTLSSubscription(input)
    47  	if err != nil {
    48  		c.Globals.ErrLog.AddWithContext(err, map[string]any{
    49  			"TLS Subscription ID": c.id,
    50  			"Force":               c.force.Value,
    51  		})
    52  		return err
    53  	}
    54  
    55  	text.Success(out, "Updated TLS Subscription '%s' (Authority: %s, Common Name: %s)", r.ID, r.CertificateAuthority, r.CommonName.ID)
    56  	return nil
    57  }
    58  
    59  // constructInput transforms values parsed from CLI flags into an object to be used by the API client library.
    60  func (c *UpdateCommand) constructInput() *fastly.UpdateTLSSubscriptionInput {
    61  	var input fastly.UpdateTLSSubscriptionInput
    62  
    63  	input.ID = c.id
    64  
    65  	domains := make([]*fastly.TLSDomain, len(c.domains))
    66  	for i, v := range c.domains {
    67  		domains[i] = &fastly.TLSDomain{ID: v}
    68  	}
    69  	input.Domains = domains
    70  
    71  	if c.commonName != "" {
    72  		input.CommonName = &fastly.TLSDomain{ID: c.commonName}
    73  	}
    74  	if c.config != "" {
    75  		input.Configuration = &fastly.TLSConfiguration{ID: c.config}
    76  	}
    77  	if c.force.WasSet {
    78  		input.Force = c.force.Value
    79  	}
    80  
    81  	return &input
    82  }