github.com/fastly/cli@v1.7.2-0.20240304164155-9d0f1d77c3bf/pkg/commands/logging/gcs/create.go (about) 1 package gcs 2 3 import ( 4 "fmt" 5 "io" 6 7 "github.com/fastly/go-fastly/v9/fastly" 8 9 "github.com/fastly/cli/pkg/argparser" 10 "github.com/fastly/cli/pkg/commands/logging/common" 11 "github.com/fastly/cli/pkg/errors" 12 "github.com/fastly/cli/pkg/global" 13 "github.com/fastly/cli/pkg/manifest" 14 "github.com/fastly/cli/pkg/text" 15 ) 16 17 // CreateCommand calls the Fastly API to create a GCS logging endpoint. 18 type CreateCommand struct { 19 argparser.Base 20 Manifest manifest.Data 21 22 // Required. 23 ServiceName argparser.OptionalServiceNameID 24 ServiceVersion argparser.OptionalServiceVersion 25 26 // Optional. 27 AccountName argparser.OptionalString 28 AutoClone argparser.OptionalAutoClone 29 Bucket argparser.OptionalString 30 CompressionCodec argparser.OptionalString 31 EndpointName argparser.OptionalString // Can't shadow argparser.Base method Name(). 32 Format argparser.OptionalString 33 FormatVersion argparser.OptionalInt 34 GzipLevel argparser.OptionalInt 35 MessageType argparser.OptionalString 36 Path argparser.OptionalString 37 Period argparser.OptionalInt 38 Placement argparser.OptionalString 39 ResponseCondition argparser.OptionalString 40 SecretKey argparser.OptionalString 41 TimestampFormat argparser.OptionalString 42 User argparser.OptionalString 43 } 44 45 // NewCreateCommand returns a usable command registered under the parent. 46 func NewCreateCommand(parent argparser.Registerer, g *global.Data) *CreateCommand { 47 c := CreateCommand{ 48 Base: argparser.Base{ 49 Globals: g, 50 }, 51 } 52 c.CmdClause = parent.Command("create", "Create a GCS logging endpoint on a Fastly service version").Alias("add") 53 54 // Required. 55 c.CmdClause.Flag("name", "The name of the GCS logging object. Used as a primary key for API access").Short('n').Action(c.EndpointName.Set).StringVar(&c.EndpointName.Value) 56 c.RegisterFlag(argparser.StringFlagOpts{ 57 Name: argparser.FlagVersionName, 58 Description: argparser.FlagVersionDesc, 59 Dst: &c.ServiceVersion.Value, 60 Required: true, 61 }) 62 63 // Optional. 64 common.AccountName(c.CmdClause, &c.AccountName) 65 c.RegisterAutoCloneFlag(argparser.AutoCloneFlagOpts{ 66 Action: c.AutoClone.Set, 67 Dst: &c.AutoClone.Value, 68 }) 69 c.CmdClause.Flag("bucket", "The bucket of the GCS bucket").Action(c.Bucket.Set).StringVar(&c.Bucket.Value) 70 common.CompressionCodec(c.CmdClause, &c.CompressionCodec) 71 common.Format(c.CmdClause, &c.Format) 72 common.FormatVersion(c.CmdClause, &c.FormatVersion) 73 common.GzipLevel(c.CmdClause, &c.GzipLevel) 74 common.MessageType(c.CmdClause, &c.MessageType) 75 common.Path(c.CmdClause, &c.Path) 76 common.Period(c.CmdClause, &c.Period) 77 common.Placement(c.CmdClause, &c.Placement) 78 common.ResponseCondition(c.CmdClause, &c.ResponseCondition) 79 c.CmdClause.Flag("secret-key", "Your GCS account secret key. The private_key field in your service account authentication JSON").Action(c.SecretKey.Set).StringVar(&c.SecretKey.Value) 80 c.RegisterFlag(argparser.StringFlagOpts{ 81 Name: argparser.FlagServiceIDName, 82 Description: argparser.FlagServiceIDDesc, 83 Dst: &g.Manifest.Flag.ServiceID, 84 Short: 's', 85 }) 86 c.RegisterFlag(argparser.StringFlagOpts{ 87 Action: c.ServiceName.Set, 88 Name: argparser.FlagServiceName, 89 Description: argparser.FlagServiceDesc, 90 Dst: &c.ServiceName.Value, 91 }) 92 common.TimestampFormat(c.CmdClause, &c.TimestampFormat) 93 c.CmdClause.Flag("user", "Your GCS service account email address. The client_email field in your service account authentication JSON").Action(c.User.Set).StringVar(&c.User.Value) 94 return &c 95 } 96 97 // ConstructInput transforms values parsed from CLI flags into an object to be used by the API client library. 98 func (c *CreateCommand) ConstructInput(serviceID string, serviceVersion int) (*fastly.CreateGCSInput, error) { 99 input := fastly.CreateGCSInput{ 100 ServiceID: serviceID, 101 ServiceVersion: serviceVersion, 102 } 103 104 // The following blocks enforces the mutual exclusivity of the 105 // CompressionCodec and GzipLevel flags. 106 if c.CompressionCodec.WasSet && c.GzipLevel.WasSet { 107 return nil, fmt.Errorf("error parsing arguments: the --compression-codec flag is mutually exclusive with the --gzip-level flag") 108 } 109 110 if c.AccountName.WasSet { 111 input.AccountName = &c.AccountName.Value 112 } 113 if c.Bucket.WasSet { 114 input.Bucket = &c.Bucket.Value 115 } 116 if c.CompressionCodec.WasSet { 117 input.CompressionCodec = &c.CompressionCodec.Value 118 } 119 if c.EndpointName.WasSet { 120 input.Name = &c.EndpointName.Value 121 } 122 if c.Format.WasSet { 123 input.Format = &c.Format.Value 124 } 125 if c.FormatVersion.WasSet { 126 input.FormatVersion = &c.FormatVersion.Value 127 } 128 if c.GzipLevel.WasSet { 129 input.GzipLevel = &c.GzipLevel.Value 130 } 131 if c.MessageType.WasSet { 132 input.MessageType = &c.MessageType.Value 133 } 134 if c.Path.WasSet { 135 input.Path = &c.Path.Value 136 } 137 if c.Period.WasSet { 138 input.Period = &c.Period.Value 139 } 140 if c.Placement.WasSet { 141 input.Placement = &c.Placement.Value 142 } 143 if c.ResponseCondition.WasSet { 144 input.ResponseCondition = &c.ResponseCondition.Value 145 } 146 if c.SecretKey.WasSet { 147 input.SecretKey = &c.SecretKey.Value 148 } 149 if c.TimestampFormat.WasSet { 150 input.TimestampFormat = &c.TimestampFormat.Value 151 } 152 if c.User.WasSet { 153 input.User = &c.User.Value 154 } 155 156 return &input, nil 157 } 158 159 // Exec invokes the application logic for the command. 160 func (c *CreateCommand) Exec(_ io.Reader, out io.Writer) error { 161 serviceID, serviceVersion, err := argparser.ServiceDetails(argparser.ServiceDetailsOpts{ 162 AutoCloneFlag: c.AutoClone, 163 APIClient: c.Globals.APIClient, 164 Manifest: *c.Globals.Manifest, 165 Out: out, 166 ServiceNameFlag: c.ServiceName, 167 ServiceVersionFlag: c.ServiceVersion, 168 VerboseMode: c.Globals.Flags.Verbose, 169 }) 170 if err != nil { 171 c.Globals.ErrLog.AddWithContext(err, map[string]any{ 172 "Service ID": serviceID, 173 "Service Version": errors.ServiceVersion(serviceVersion), 174 }) 175 return err 176 } 177 178 input, err := c.ConstructInput(serviceID, fastly.ToValue(serviceVersion.Number)) 179 if err != nil { 180 c.Globals.ErrLog.Add(err) 181 return err 182 } 183 184 d, err := c.Globals.APIClient.CreateGCS(input) 185 if err != nil { 186 c.Globals.ErrLog.Add(err) 187 return err 188 } 189 190 text.Success(out, 191 "Created GCS logging endpoint %s (service %s version %d)", 192 fastly.ToValue(d.Name), 193 fastly.ToValue(d.ServiceID), 194 fastly.ToValue(d.ServiceVersion), 195 ) 196 return nil 197 }