github.com/fastly/cli@v1.7.2-0.20240304164155-9d0f1d77c3bf/pkg/commands/logging/sftp/update.go (about) 1 package sftp 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 // UpdateCommand calls the Fastly API to update an SFTP logging endpoint. 17 type UpdateCommand struct { 18 argparser.Base 19 Manifest manifest.Data 20 21 // Required. 22 EndpointName string 23 ServiceName argparser.OptionalServiceNameID 24 ServiceVersion argparser.OptionalServiceVersion 25 26 // Optional. 27 AutoClone argparser.OptionalAutoClone 28 NewName argparser.OptionalString 29 Address argparser.OptionalString 30 Port argparser.OptionalInt 31 PublicKey argparser.OptionalString 32 SecretKey argparser.OptionalString 33 SSHKnownHosts argparser.OptionalString 34 User argparser.OptionalString 35 Password argparser.OptionalString 36 Path argparser.OptionalString 37 Period argparser.OptionalInt 38 FormatVersion argparser.OptionalInt 39 GzipLevel argparser.OptionalInt 40 Format argparser.OptionalString 41 MessageType argparser.OptionalString 42 ResponseCondition argparser.OptionalString 43 TimestampFormat argparser.OptionalString 44 Placement argparser.OptionalString 45 CompressionCodec argparser.OptionalString 46 } 47 48 // NewUpdateCommand returns a usable command registered under the parent. 49 func NewUpdateCommand(parent argparser.Registerer, g *global.Data) *UpdateCommand { 50 c := UpdateCommand{ 51 Base: argparser.Base{ 52 Globals: g, 53 }, 54 } 55 c.CmdClause = parent.Command("update", "Update an SFTP logging endpoint on a Fastly service version") 56 57 // Required. 58 c.CmdClause.Flag("name", "The name of the SFTP logging object").Short('n').Required().StringVar(&c.EndpointName) 59 c.RegisterFlag(argparser.StringFlagOpts{ 60 Name: argparser.FlagVersionName, 61 Description: argparser.FlagVersionDesc, 62 Dst: &c.ServiceVersion.Value, 63 Required: true, 64 }) 65 66 // Optional. 67 c.RegisterAutoCloneFlag(argparser.AutoCloneFlagOpts{ 68 Action: c.AutoClone.Set, 69 Dst: &c.AutoClone.Value, 70 }) 71 c.CmdClause.Flag("address", "The hostname or IPv4 address").Action(c.Address.Set).StringVar(&c.Address.Value) 72 common.CompressionCodec(c.CmdClause, &c.CompressionCodec) 73 c.CmdClause.Flag("new-name", "New name of the SFTP logging object").Action(c.NewName.Set).StringVar(&c.NewName.Value) 74 common.Format(c.CmdClause, &c.Format) 75 common.FormatVersion(c.CmdClause, &c.FormatVersion) 76 common.GzipLevel(c.CmdClause, &c.GzipLevel) 77 common.MessageType(c.CmdClause, &c.MessageType) 78 c.CmdClause.Flag("password", "The password for the server. If both password and secret_key are passed, secret_key will be used in preference").Action(c.Password.Set).StringVar(&c.Password.Value) 79 c.CmdClause.Flag("path", "The path to upload logs to. The directory must exist on the SFTP server before logs can be saved to it").Action(c.Path.Set).StringVar(&c.Path.Value) 80 common.Period(c.CmdClause, &c.Period) 81 common.Placement(c.CmdClause, &c.Placement) 82 c.CmdClause.Flag("port", "The port number").Action(c.Port.Set).IntVar(&c.Port.Value) 83 common.PublicKey(c.CmdClause, &c.PublicKey) 84 common.ResponseCondition(c.CmdClause, &c.ResponseCondition) 85 c.CmdClause.Flag("secret-key", "The SSH private key for the server. If both password and secret_key are passed, secret_key will be used in preference").Action(c.SecretKey.Set).StringVar(&c.SecretKey.Value) 86 c.RegisterFlag(argparser.StringFlagOpts{ 87 Name: argparser.FlagServiceIDName, 88 Description: argparser.FlagServiceIDDesc, 89 Dst: &g.Manifest.Flag.ServiceID, 90 Short: 's', 91 }) 92 c.RegisterFlag(argparser.StringFlagOpts{ 93 Action: c.ServiceName.Set, 94 Name: argparser.FlagServiceName, 95 Description: argparser.FlagServiceDesc, 96 Dst: &c.ServiceName.Value, 97 }) 98 c.CmdClause.Flag("ssh-known-hosts", "A list of host keys for all hosts we can connect to over SFTP").Action(c.SSHKnownHosts.Set).StringVar(&c.SSHKnownHosts.Value) 99 c.CmdClause.Flag("user", "The username for the server").Action(c.User.Set).StringVar(&c.User.Value) 100 common.TimestampFormat(c.CmdClause, &c.TimestampFormat) 101 return &c 102 } 103 104 // ConstructInput transforms values parsed from CLI flags into an object to be used by the API client library. 105 func (c *UpdateCommand) ConstructInput(serviceID string, serviceVersion int) (*fastly.UpdateSFTPInput, error) { 106 input := fastly.UpdateSFTPInput{ 107 ServiceID: serviceID, 108 ServiceVersion: serviceVersion, 109 Name: c.EndpointName, 110 } 111 112 if c.NewName.WasSet { 113 input.NewName = &c.NewName.Value 114 } 115 116 if c.Address.WasSet { 117 input.Address = &c.Address.Value 118 } 119 120 if c.Port.WasSet { 121 input.Port = &c.Port.Value 122 } 123 124 if c.Password.WasSet { 125 input.Password = &c.Password.Value 126 } 127 128 if c.PublicKey.WasSet { 129 input.PublicKey = &c.PublicKey.Value 130 } 131 132 if c.SecretKey.WasSet { 133 input.SecretKey = &c.SecretKey.Value 134 } 135 136 if c.SSHKnownHosts.WasSet { 137 input.SSHKnownHosts = &c.SSHKnownHosts.Value 138 } 139 140 if c.User.WasSet { 141 input.User = &c.User.Value 142 } 143 144 if c.Path.WasSet { 145 input.Path = &c.Path.Value 146 } 147 148 if c.Period.WasSet { 149 input.Period = &c.Period.Value 150 } 151 152 if c.Format.WasSet { 153 input.Format = &c.Format.Value 154 } 155 156 if c.FormatVersion.WasSet { 157 input.FormatVersion = &c.FormatVersion.Value 158 } 159 160 if c.GzipLevel.WasSet { 161 input.GzipLevel = &c.GzipLevel.Value 162 } 163 164 if c.MessageType.WasSet { 165 input.MessageType = &c.MessageType.Value 166 } 167 168 if c.ResponseCondition.WasSet { 169 input.ResponseCondition = &c.ResponseCondition.Value 170 } 171 172 if c.TimestampFormat.WasSet { 173 input.TimestampFormat = &c.TimestampFormat.Value 174 } 175 176 if c.Placement.WasSet { 177 input.Placement = &c.Placement.Value 178 } 179 180 if c.CompressionCodec.WasSet { 181 input.CompressionCodec = &c.CompressionCodec.Value 182 } 183 184 return &input, nil 185 } 186 187 // Exec invokes the application logic for the command. 188 func (c *UpdateCommand) Exec(_ io.Reader, out io.Writer) error { 189 serviceID, serviceVersion, err := argparser.ServiceDetails(argparser.ServiceDetailsOpts{ 190 AutoCloneFlag: c.AutoClone, 191 APIClient: c.Globals.APIClient, 192 Manifest: *c.Globals.Manifest, 193 Out: out, 194 ServiceNameFlag: c.ServiceName, 195 ServiceVersionFlag: c.ServiceVersion, 196 VerboseMode: c.Globals.Flags.Verbose, 197 }) 198 if err != nil { 199 c.Globals.ErrLog.AddWithContext(err, map[string]any{ 200 "Service ID": serviceID, 201 "Service Version": errors.ServiceVersion(serviceVersion), 202 }) 203 return err 204 } 205 206 input, err := c.ConstructInput(serviceID, fastly.ToValue(serviceVersion.Number)) 207 if err != nil { 208 c.Globals.ErrLog.Add(err) 209 return err 210 } 211 212 sftp, err := c.Globals.APIClient.UpdateSFTP(input) 213 if err != nil { 214 c.Globals.ErrLog.Add(err) 215 return err 216 } 217 218 text.Success(out, 219 "Updated SFTP logging endpoint %s (service %s version %d)", 220 fastly.ToValue(sftp.Name), 221 fastly.ToValue(sftp.ServiceID), 222 fastly.ToValue(sftp.ServiceVersion), 223 ) 224 return nil 225 }