github.com/fastly/cli@v1.7.2-0.20240304164155-9d0f1d77c3bf/pkg/commands/logging/digitalocean/create.go (about) 1 package digitalocean 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 DigitalOcean Spaces 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 Domain argparser.OptionalString 32 EndpointName argparser.OptionalString // Can't shadow argparser.Base method Name(). 33 Format argparser.OptionalString 34 FormatVersion argparser.OptionalInt 35 GzipLevel argparser.OptionalInt 36 MessageType argparser.OptionalString 37 Path argparser.OptionalString 38 Period argparser.OptionalInt 39 Placement argparser.OptionalString 40 PublicKey argparser.OptionalString 41 ResponseCondition argparser.OptionalString 42 SecretKey argparser.OptionalString 43 TimestampFormat argparser.OptionalString 44 } 45 46 // NewCreateCommand returns a usable command registered under the parent. 47 func NewCreateCommand(parent argparser.Registerer, g *global.Data) *CreateCommand { 48 c := CreateCommand{ 49 Base: argparser.Base{ 50 Globals: g, 51 }, 52 } 53 c.CmdClause = parent.Command("create", "Create a DigitalOcean Spaces logging endpoint on a Fastly service version").Alias("add") 54 55 // Required. 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 c.RegisterAutoCloneFlag(argparser.AutoCloneFlagOpts{ 65 Action: c.AutoClone.Set, 66 Dst: &c.AutoClone.Value, 67 }) 68 c.CmdClause.Flag("access-key", "Your DigitalOcean Spaces account access key").Action(c.AccessKey.Set).StringVar(&c.AccessKey.Value) 69 c.CmdClause.Flag("bucket", "The name of the DigitalOcean Space").Action(c.BucketName.Set).StringVar(&c.BucketName.Value) 70 common.CompressionCodec(c.CmdClause, &c.CompressionCodec) 71 c.CmdClause.Flag("domain", "The domain of the DigitalOcean Spaces endpoint (default 'nyc3.digitaloceanspaces.com')").Action(c.Domain.Set).StringVar(&c.Domain.Value) 72 common.Format(c.CmdClause, &c.Format) 73 common.FormatVersion(c.CmdClause, &c.FormatVersion) 74 common.GzipLevel(c.CmdClause, &c.GzipLevel) 75 c.CmdClause.Flag("name", "The name of the DigitalOcean Spaces logging object. Used as a primary key for API access").Short('n').Action(c.EndpointName.Set).StringVar(&c.EndpointName.Value) 76 common.MessageType(c.CmdClause, &c.MessageType) 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 common.ResponseCondition(c.CmdClause, &c.ResponseCondition) 82 c.CmdClause.Flag("secret-key", "Your DigitalOcean Spaces account secret key").Action(c.SecretKey.Set).StringVar(&c.SecretKey.Value) 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 return &c 97 } 98 99 // ConstructInput transforms values parsed from CLI flags into an object to be used by the API client library. 100 func (c *CreateCommand) ConstructInput(serviceID string, serviceVersion int) (*fastly.CreateDigitalOceanInput, error) { 101 var input fastly.CreateDigitalOceanInput 102 103 input.ServiceID = serviceID 104 input.ServiceVersion = serviceVersion 105 if c.EndpointName.WasSet { 106 input.Name = &c.EndpointName.Value 107 } 108 if c.BucketName.WasSet { 109 input.BucketName = &c.BucketName.Value 110 } 111 if c.AccessKey.WasSet { 112 input.AccessKey = &c.AccessKey.Value 113 } 114 if c.SecretKey.WasSet { 115 input.SecretKey = &c.SecretKey.Value 116 } 117 118 // The following blocks enforces the mutual exclusivity of the 119 // CompressionCodec and GzipLevel flags. 120 if c.CompressionCodec.WasSet && c.GzipLevel.WasSet { 121 return nil, fmt.Errorf("error parsing arguments: the --compression-codec flag is mutually exclusive with the --gzip-level flag") 122 } 123 124 if c.Domain.WasSet { 125 input.Domain = &c.Domain.Value 126 } 127 128 if c.Path.WasSet { 129 input.Path = &c.Path.Value 130 } 131 132 if c.Period.WasSet { 133 input.Period = &c.Period.Value 134 } 135 136 if c.GzipLevel.WasSet { 137 input.GzipLevel = &c.GzipLevel.Value 138 } 139 140 if c.Format.WasSet { 141 input.Format = &c.Format.Value 142 } 143 144 if c.FormatVersion.WasSet { 145 input.FormatVersion = &c.FormatVersion.Value 146 } 147 148 if c.ResponseCondition.WasSet { 149 input.ResponseCondition = &c.ResponseCondition.Value 150 } 151 152 if c.MessageType.WasSet { 153 input.MessageType = &c.MessageType.Value 154 } 155 156 if c.TimestampFormat.WasSet { 157 input.TimestampFormat = &c.TimestampFormat.Value 158 } 159 160 if c.Placement.WasSet { 161 input.Placement = &c.Placement.Value 162 } 163 164 if c.PublicKey.WasSet { 165 input.PublicKey = &c.PublicKey.Value 166 } 167 168 if c.CompressionCodec.WasSet { 169 input.CompressionCodec = &c.CompressionCodec.Value 170 } 171 172 return &input, nil 173 } 174 175 // Exec invokes the application logic for the command. 176 func (c *CreateCommand) Exec(_ io.Reader, out io.Writer) error { 177 serviceID, serviceVersion, err := argparser.ServiceDetails(argparser.ServiceDetailsOpts{ 178 AutoCloneFlag: c.AutoClone, 179 APIClient: c.Globals.APIClient, 180 Manifest: *c.Globals.Manifest, 181 Out: out, 182 ServiceNameFlag: c.ServiceName, 183 ServiceVersionFlag: c.ServiceVersion, 184 VerboseMode: c.Globals.Flags.Verbose, 185 }) 186 if err != nil { 187 c.Globals.ErrLog.AddWithContext(err, map[string]any{ 188 "Service ID": serviceID, 189 "Service Version": errors.ServiceVersion(serviceVersion), 190 }) 191 return err 192 } 193 194 input, err := c.ConstructInput(serviceID, fastly.ToValue(serviceVersion.Number)) 195 if err != nil { 196 c.Globals.ErrLog.Add(err) 197 return err 198 } 199 200 d, err := c.Globals.APIClient.CreateDigitalOcean(input) 201 if err != nil { 202 c.Globals.ErrLog.Add(err) 203 return err 204 } 205 206 text.Success(out, 207 "Created DigitalOcean Spaces logging endpoint %s (service %s version %d)", 208 fastly.ToValue(d.Name), 209 fastly.ToValue(d.ServiceID), 210 fastly.ToValue(d.ServiceVersion), 211 ) 212 return nil 213 }