github.com/fastly/cli@v1.7.2-0.20240304164155-9d0f1d77c3bf/pkg/commands/logging/googlepubsub/create.go (about) 1 package googlepubsub 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/commands/logging/common" 10 "github.com/fastly/cli/pkg/errors" 11 "github.com/fastly/cli/pkg/global" 12 "github.com/fastly/cli/pkg/manifest" 13 "github.com/fastly/cli/pkg/text" 14 ) 15 16 // CreateCommand calls the Fastly API to create a Google Cloud Pub/Sub logging endpoint. 17 type CreateCommand struct { 18 argparser.Base 19 Manifest manifest.Data 20 21 // Required. 22 ServiceName argparser.OptionalServiceNameID 23 ServiceVersion argparser.OptionalServiceVersion 24 25 // Optional. 26 AccountName argparser.OptionalString 27 AutoClone argparser.OptionalAutoClone 28 EndpointName argparser.OptionalString // Can't shadow argparser.Base method Name(). 29 Format argparser.OptionalString 30 FormatVersion argparser.OptionalInt 31 Placement argparser.OptionalString 32 ProjectID argparser.OptionalString 33 ResponseCondition argparser.OptionalString 34 SecretKey argparser.OptionalString 35 Topic argparser.OptionalString 36 User argparser.OptionalString 37 } 38 39 // NewCreateCommand returns a usable command registered under the parent. 40 func NewCreateCommand(parent argparser.Registerer, g *global.Data) *CreateCommand { 41 c := CreateCommand{ 42 Base: argparser.Base{ 43 Globals: g, 44 }, 45 } 46 c.CmdClause = parent.Command("create", "Create a Google Cloud Pub/Sub logging endpoint on a Fastly service version").Alias("add") 47 48 // Required. 49 c.CmdClause.Flag("name", "The name of the Google Cloud Pub/Sub logging object. Used as a primary key for API access").Short('n').Action(c.EndpointName.Set).StringVar(&c.EndpointName.Value) 50 c.RegisterFlag(argparser.StringFlagOpts{ 51 Name: argparser.FlagVersionName, 52 Description: argparser.FlagVersionDesc, 53 Dst: &c.ServiceVersion.Value, 54 Required: true, 55 }) 56 57 // Optional. 58 common.AccountName(c.CmdClause, &c.AccountName) 59 c.RegisterAutoCloneFlag(argparser.AutoCloneFlagOpts{ 60 Action: c.AutoClone.Set, 61 Dst: &c.AutoClone.Value, 62 }) 63 common.Format(c.CmdClause, &c.Format) 64 common.FormatVersion(c.CmdClause, &c.FormatVersion) 65 common.Placement(c.CmdClause, &c.Placement) 66 c.CmdClause.Flag("project-id", "The ID of your Google Cloud Platform project").Action(c.ProjectID.Set).StringVar(&c.ProjectID.Value) 67 common.ResponseCondition(c.CmdClause, &c.ResponseCondition) 68 c.CmdClause.Flag("secret-key", "Your Google Cloud Platform account secret key. The private_key field in your service account authentication JSON").Action(c.SecretKey.Set).StringVar(&c.SecretKey.Value) 69 c.RegisterFlag(argparser.StringFlagOpts{ 70 Name: argparser.FlagServiceIDName, 71 Description: argparser.FlagServiceIDDesc, 72 Dst: &g.Manifest.Flag.ServiceID, 73 Short: 's', 74 }) 75 c.RegisterFlag(argparser.StringFlagOpts{ 76 Action: c.ServiceName.Set, 77 Name: argparser.FlagServiceName, 78 Description: argparser.FlagServiceDesc, 79 Dst: &c.ServiceName.Value, 80 }) 81 c.CmdClause.Flag("topic", "The Google Cloud Pub/Sub topic to which logs will be published").Action(c.Topic.Set).StringVar(&c.Topic.Value) 82 c.CmdClause.Flag("user", "Your Google Cloud Platform service account email address. The client_email field in your service account authentication JSON").Action(c.User.Set).StringVar(&c.User.Value) 83 return &c 84 } 85 86 // ConstructInput transforms values parsed from CLI flags into an object to be used by the API client library. 87 func (c *CreateCommand) ConstructInput(serviceID string, serviceVersion int) (*fastly.CreatePubsubInput, error) { 88 input := fastly.CreatePubsubInput{ 89 ServiceID: serviceID, 90 ServiceVersion: serviceVersion, 91 } 92 93 if c.AccountName.WasSet { 94 input.AccountName = &c.AccountName.Value 95 } 96 if c.EndpointName.WasSet { 97 input.Name = &c.EndpointName.Value 98 } 99 if c.Format.WasSet { 100 input.Format = &c.Format.Value 101 } 102 if c.FormatVersion.WasSet { 103 input.FormatVersion = &c.FormatVersion.Value 104 } 105 if c.Placement.WasSet { 106 input.Placement = &c.Placement.Value 107 } 108 if c.ProjectID.WasSet { 109 input.ProjectID = &c.ProjectID.Value 110 } 111 if c.ResponseCondition.WasSet { 112 input.ResponseCondition = &c.ResponseCondition.Value 113 } 114 if c.SecretKey.WasSet { 115 input.SecretKey = &c.SecretKey.Value 116 } 117 if c.Topic.WasSet { 118 input.Topic = &c.Topic.Value 119 } 120 if c.User.WasSet { 121 input.User = &c.User.Value 122 } 123 124 return &input, nil 125 } 126 127 // Exec invokes the application logic for the command. 128 func (c *CreateCommand) Exec(_ io.Reader, out io.Writer) error { 129 serviceID, serviceVersion, err := argparser.ServiceDetails(argparser.ServiceDetailsOpts{ 130 AutoCloneFlag: c.AutoClone, 131 APIClient: c.Globals.APIClient, 132 Manifest: *c.Globals.Manifest, 133 Out: out, 134 ServiceNameFlag: c.ServiceName, 135 ServiceVersionFlag: c.ServiceVersion, 136 VerboseMode: c.Globals.Flags.Verbose, 137 }) 138 if err != nil { 139 c.Globals.ErrLog.AddWithContext(err, map[string]any{ 140 "Service ID": serviceID, 141 "Service Version": errors.ServiceVersion(serviceVersion), 142 }) 143 return err 144 } 145 146 input, err := c.ConstructInput(serviceID, fastly.ToValue(serviceVersion.Number)) 147 if err != nil { 148 c.Globals.ErrLog.Add(err) 149 return err 150 } 151 152 d, err := c.Globals.APIClient.CreatePubsub(input) 153 if err != nil { 154 c.Globals.ErrLog.Add(err) 155 return err 156 } 157 158 text.Success(out, 159 "Created Google Cloud Pub/Sub logging endpoint %s (service %s version %d)", 160 fastly.ToValue(d.Name), 161 fastly.ToValue(d.ServiceID), 162 fastly.ToValue(d.ServiceVersion), 163 ) 164 return nil 165 }