github.com/fastly/cli@v1.7.2-0.20240304164155-9d0f1d77c3bf/pkg/commands/tls/platform/create.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 // 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("upload", "Upload a new certificate") 17 c.Globals = g 18 19 // Required. 20 c.CmdClause.Flag("cert-blob", "The PEM-formatted certificate blob").Required().StringVar(&c.certBlob) 21 c.CmdClause.Flag("intermediates-blob", "The PEM-formatted chain of intermediate blobs").Required().StringVar(&c.intermediatesBlob) 22 23 // Optional. 24 c.CmdClause.Flag("allow-untrusted", "Allow certificates that chain to untrusted roots").Action(c.allowUntrusted.Set).BoolVar(&c.allowUntrusted.Value) 25 c.CmdClause.Flag("config", "Alphanumeric string identifying a TLS configuration (set flag once per Configuration ID)").StringsVar(&c.config) 26 27 return &c 28 } 29 30 // CreateCommand calls the Fastly API to update an appropriate resource. 31 type CreateCommand struct { 32 argparser.Base 33 34 allowUntrusted argparser.OptionalBool 35 certBlob string 36 config []string 37 intermediatesBlob string 38 } 39 40 // Exec invokes the application logic for the command. 41 func (c *CreateCommand) Exec(_ io.Reader, out io.Writer) error { 42 input := c.constructInput() 43 44 r, err := c.Globals.APIClient.CreateBulkCertificate(input) 45 if err != nil { 46 c.Globals.ErrLog.AddWithContext(err, map[string]any{ 47 "Allow Untrusted": c.allowUntrusted.Value, 48 "Configs": c.config, 49 }) 50 return err 51 } 52 53 text.Success(out, "Uploaded TLS Bulk Certificate '%s'", r.ID) 54 return nil 55 } 56 57 // constructInput transforms values parsed from CLI flags into an object to be used by the API client library. 58 func (c *CreateCommand) constructInput() *fastly.CreateBulkCertificateInput { 59 var input fastly.CreateBulkCertificateInput 60 61 input.CertBlob = c.certBlob 62 input.IntermediatesBlob = c.intermediatesBlob 63 64 if c.allowUntrusted.WasSet { 65 input.AllowUntrusted = c.allowUntrusted.Value 66 } 67 68 var configs []*fastly.TLSConfiguration 69 for _, v := range c.config { 70 configs = append(configs, &fastly.TLSConfiguration{ID: v}) 71 } 72 input.Configurations = configs 73 74 return &input 75 }