Skip to content

Commit 3f462f7

Browse files
committed
Delete + Tests
1 parent 1c78ac0 commit 3f462f7

3 files changed

Lines changed: 318 additions & 19 deletions

File tree

pkg/commands/kvstoreentry/delete.go

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ type DeleteCommand struct {
3333
// NOTE: Public fields can be set via `kv-store delete`.
3434
DeleteAll bool
3535
Force bool
36-
IfGenerationMatch uint64
36+
IfGenerationMatch string
3737
MaxErrors int
3838
PoolSize int
3939
StoreID string
@@ -55,7 +55,7 @@ func NewDeleteCommand(parent argparser.Registerer, g *global.Data) *DeleteComman
5555
c.CmdClause.Flag("all", "Delete all entries within the store").Short('a').BoolVar(&c.DeleteAll)
5656
c.CmdClause.Flag("concurrency", "The thread pool size (ignored when set without the --all flag)").Default(strconv.Itoa(DeleteKeysPoolSize)).Short('r').IntVar(&c.PoolSize)
5757
c.CmdClause.Flag("force", "Return a successful result from a 'delete' operation even if the specified key was not found").BoolVar(&c.Force)
58-
c.CmdClause.Flag("if-generation-match", "Value which must match the current generation marker in an item for an update operation to proceed").Uint64Var(&c.IfGenerationMatch)
58+
c.CmdClause.Flag("if-generation-match", "Value which must match the current generation marker in an item for an update operation to proceed").StringVar(&c.IfGenerationMatch)
5959
c.RegisterFlagBool(c.JSONFlag()) // --json
6060
c.CmdClause.Flag("key", "Key name").Short('k').Action(c.key.Set).StringVar(&c.key.Value)
6161
c.CmdClause.Flag("max-errors", "The number of errors to accept before stopping (ignored when set without the --all flag)").Default(strconv.Itoa(DeleteKeysMaxErrors)).Short('m').IntVar(&c.MaxErrors)
@@ -95,10 +95,37 @@ func (c *DeleteCommand) Exec(in io.Reader, out io.Writer) error {
9595
}
9696

9797
input := fastly.DeleteKVStoreKeyInput{
98-
StoreID: c.StoreID,
99-
Force: c.Force,
100-
Key: c.key.Value,
101-
IfGenerationMatch: c.IfGenerationMatch,
98+
StoreID: c.StoreID,
99+
Force: c.Force,
100+
Key: c.key.Value,
101+
}
102+
103+
// Check if the generation marker provided matches the API.
104+
if c.IfGenerationMatch != "" {
105+
getInput := fastly.GetKVStoreItemInput{
106+
StoreID: c.StoreID,
107+
Key: c.key.Value,
108+
}
109+
110+
result, err := c.Globals.APIClient.GetKVStoreItem(context.TODO(), &getInput)
111+
if err != nil {
112+
c.Globals.ErrLog.Add(err)
113+
return err
114+
}
115+
116+
// Ensure we close the value reader.
117+
if result.Value != nil {
118+
defer result.Value.Close()
119+
}
120+
121+
inputGeneration, err := strconv.ParseUint(c.IfGenerationMatch, 10, 64)
122+
if err != nil {
123+
return fmt.Errorf("invalid generation value: %s", c.IfGenerationMatch)
124+
}
125+
126+
if inputGeneration != result.Generation {
127+
return fmt.Errorf("generation value does not match: expected %d, got %d", result.Generation, inputGeneration)
128+
}
102129
}
103130

104131
err := c.Globals.APIClient.DeleteKVStoreKey(context.TODO(), &input)

pkg/commands/kvstoreentry/get.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func NewGetCommand(parent argparser.Registerer, g *global.Data) *GetCommand {
3838
c.CmdClause.Flag("store-id", "Store ID").Short('s').Required().StringVar(&c.Input.StoreID)
3939

4040
// Optional.
41-
c.CmdClause.Flag("generation", "Compares if the provided generastion market matches that of the object").StringVar(&c.Generation)
41+
c.CmdClause.Flag("if-generation-match", "Compares if the provided generation marker matches that of the object").StringVar(&c.Generation)
4242
c.RegisterFlagBool(c.JSONFlag()) // --json
4343

4444
return &c

0 commit comments

Comments
 (0)