github.com/fastly/cli@v1.7.2-0.20240304164155-9d0f1d77c3bf/pkg/commands/logging/cloudfiles/create.go (about) 1 package cloudfiles 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 fsterr "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 Cloudfiles 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 AccessKey argparser.OptionalString 28 AutoClone argparser.OptionalAutoClone 29 BucketName 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 PublicKey argparser.OptionalString 40 Region argparser.OptionalString 41 ResponseCondition argparser.OptionalString 42 TimestampFormat argparser.OptionalString 43 Token argparser.OptionalString 44 User argparser.OptionalString 45 } 46 47 // NewCreateCommand returns a usable command registered under the parent. 48 func NewCreateCommand(parent argparser.Registerer, g *global.Data) *CreateCommand { 49 c := CreateCommand{ 50 Base: argparser.Base{ 51 Globals: g, 52 }, 53 } 54 c.CmdClause = parent.Command("create", "Create a Cloudfiles logging endpoint on a Fastly service version").Alias("add") 55 56 // Required. 57 c.RegisterFlag(argparser.StringFlagOpts{ 58 Name: argparser.FlagVersionName, 59 Description: argparser.FlagVersionDesc, 60 Dst: &c.ServiceVersion.Value, 61 Required: true, 62 }) 63 64 // Optional. 65 c.CmdClause.Flag("access-key", "Your Cloudfile account access key").Action(c.AccessKey.Set).StringVar(&c.AccessKey.Value) 66 c.RegisterAutoCloneFlag(argparser.AutoCloneFlagOpts{ 67 Action: c.AutoClone.Set, 68 Dst: &c.AutoClone.Value, 69 }) 70 c.CmdClause.Flag("bucket", "The name of your Cloudfiles container").Action(c.BucketName.Set).StringVar(&c.BucketName.Value) 71 common.CompressionCodec(c.CmdClause, &c.CompressionCodec) 72 common.Format(c.CmdClause, &c.Format) 73 common.FormatVersion(c.CmdClause, &c.FormatVersion) 74 common.GzipLevel(c.CmdClause, &c.GzipLevel) 75 common.MessageType(c.CmdClause, &c.MessageType) 76 c.CmdClause.Flag("name", "The name of the Cloudfiles logging object. Used as a primary key for API access").Short('n').Action(c.EndpointName.Set).StringVar(&c.EndpointName.Value) 77 common.Path(c.CmdClause, &c.Path) 78 common.Period(c.CmdClause, &c.Period) 79 common.Placement(c.CmdClause, &c.Placement) 80 common.PublicKey(c.CmdClause, &c.PublicKey) 81 c.CmdClause.Flag("region", "The region to stream logs to. One of: DFW-Dallas, ORD-Chicago, IAD-Northern Virginia, LON-London, SYD-Sydney, HKG-Hong Kong").Action(c.Region.Set).StringVar(&c.Region.Value) 82 common.ResponseCondition(c.CmdClause, &c.ResponseCondition) 83 c.RegisterFlag(argparser.StringFlagOpts{ 84 Name: argparser.FlagServiceIDName, 85 Description: argparser.FlagServiceIDDesc, 86 Dst: &g.Manifest.Flag.ServiceID, 87 Short: 's', 88 }) 89 c.RegisterFlag(argparser.StringFlagOpts{ 90 Action: c.ServiceName.Set, 91 Name: argparser.FlagServiceName, 92 Description: argparser.FlagServiceDesc, 93 Dst: &c.ServiceName.Value, 94 }) 95 common.TimestampFormat(c.CmdClause, &c.TimestampFormat) 96 c.CmdClause.Flag("user", "The username for your Cloudfile account").Action(c.User.Set).StringVar(&c.User.Value) 97 return &c 98 } 99 100 // ConstructInput transforms values parsed from CLI flags into an object to be used by the API client library. 101 func (c *CreateCommand) ConstructInput(serviceID string, serviceVersion int) (*fastly.CreateCloudfilesInput, error) { 102 var input fastly.CreateCloudfilesInput 103 104 input.ServiceID = serviceID 105 input.ServiceVersion = serviceVersion 106 if c.EndpointName.WasSet { 107 input.Name = &c.EndpointName.Value 108 } 109 if c.User.WasSet { 110 input.User = &c.User.Value 111 } 112 if c.AccessKey.WasSet { 113 input.AccessKey = &c.AccessKey.Value 114 } 115 if c.BucketName.WasSet { 116 input.BucketName = &c.BucketName.Value 117 } 118 119 // The following blocks enforces the mutual exclusivity of the 120 // CompressionCodec and GzipLevel flags. 121 if c.CompressionCodec.WasSet && c.GzipLevel.WasSet { 122 return nil, fmt.Errorf("error parsing arguments: the --compression-codec flag is mutually exclusive with the --gzip-level flag") 123 } 124 125 if c.Path.WasSet { 126 input.Path = &c.Path.Value 127 } 128 if c.Region.WasSet { 129 input.Region = &c.Region.Value 130 } 131 if c.Placement.WasSet { 132 input.Placement = &c.Placement.Value 133 } 134 if c.Period.WasSet { 135 input.Period = &c.Period.Value 136 } 137 if c.GzipLevel.WasSet { 138 input.GzipLevel = &c.GzipLevel.Value 139 } 140 if c.Format.WasSet { 141 input.Format = &c.Format.Value 142 } 143 if c.FormatVersion.WasSet { 144 input.FormatVersion = &c.FormatVersion.Value 145 } 146 if c.ResponseCondition.WasSet { 147 input.ResponseCondition = &c.ResponseCondition.Value 148 } 149 if c.MessageType.WasSet { 150 input.MessageType = &c.MessageType.Value 151 } 152 if c.TimestampFormat.WasSet { 153 input.TimestampFormat = &c.TimestampFormat.Value 154 } 155 if c.PublicKey.WasSet { 156 input.PublicKey = &c.PublicKey.Value 157 } 158 if c.CompressionCodec.WasSet { 159 input.CompressionCodec = &c.CompressionCodec.Value 160 } 161 162 return &input, nil 163 } 164 165 // Exec invokes the application logic for the command. 166 func (c *CreateCommand) Exec(_ io.Reader, out io.Writer) error { 167 serviceID, serviceVersion, err := argparser.ServiceDetails(argparser.ServiceDetailsOpts{ 168 AutoCloneFlag: c.AutoClone, 169 APIClient: c.Globals.APIClient, 170 Manifest: *c.Globals.Manifest, 171 Out: out, 172 ServiceNameFlag: c.ServiceName, 173 ServiceVersionFlag: c.ServiceVersion, 174 VerboseMode: c.Globals.Flags.Verbose, 175 }) 176 if err != nil { 177 c.Globals.ErrLog.AddWithContext(err, map[string]any{ 178 "Service ID": serviceID, 179 "Service Version": fsterr.ServiceVersion(serviceVersion), 180 }) 181 return err 182 } 183 184 input, err := c.ConstructInput(serviceID, fastly.ToValue(serviceVersion.Number)) 185 if err != nil { 186 c.Globals.ErrLog.AddWithContext(err, map[string]any{ 187 "Service ID": serviceID, 188 "Service Version": fastly.ToValue(serviceVersion.Number), 189 }) 190 return err 191 } 192 193 d, err := c.Globals.APIClient.CreateCloudfiles(input) 194 if err != nil { 195 c.Globals.ErrLog.AddWithContext(err, map[string]any{ 196 "Service ID": serviceID, 197 "Service Version": fastly.ToValue(serviceVersion.Number), 198 }) 199 return err 200 } 201 202 text.Success(out, 203 "Created Cloudfiles logging endpoint %s (service %s version %d)", 204 fastly.ToValue(d.Name), 205 fastly.ToValue(d.ServiceID), 206 fastly.ToValue(d.ServiceVersion), 207 ) 208 return nil 209 }