From be82b94f8199efa8138732774a447b06223873ec Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Wed, 16 May 2018 08:55:33 -0700 Subject: [PATCH] command/intention/create: -replace does an atomic change --- command/intention/create/create.go | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/command/intention/create/create.go b/command/intention/create/create.go index dd5f615656..de53c743d6 100644 --- a/command/intention/create/create.go +++ b/command/intention/create/create.go @@ -45,8 +45,7 @@ func (c *cmd) init() { c.flags.BoolVar(&c.flagFile, "file", false, "Read intention data from one or more files.") c.flags.BoolVar(&c.flagReplace, "replace", false, - "Replace matching intentions. This is not an atomic operation. "+ - "If the insert fails, then the previous intention will still be deleted.") + "Replace matching intentions.") c.flags.Var((*flags.FlagMapValue)(&c.flagMeta), "meta", "Metadata to set on the intention, formatted as key=value. This flag "+ "may be specified multiple times to set multiple meta fields.") @@ -95,7 +94,7 @@ func (c *cmd) Run(args []string) int { for _, ixn := range ixns { // If replace is set to true, then find this intention and delete it. if c.flagReplace { - ixn, err := find.Find(ixn.SourceString(), ixn.DestinationString()) + oldIxn, err := find.Find(ixn.SourceString(), ixn.DestinationString()) if err != nil { c.UI.Error(fmt.Sprintf( "Error looking up intention for replacement with source %q "+ @@ -105,16 +104,22 @@ func (c *cmd) Run(args []string) int { err)) return 1 } - if ixn != nil { - if _, err := client.Connect().IntentionDelete(ixn.ID, nil); err != nil { + if oldIxn != nil { + // We set the ID of our intention so we overwrite it + ixn.ID = oldIxn.ID + + if _, err := client.Connect().IntentionUpdate(ixn, nil); err != nil { c.UI.Error(fmt.Sprintf( - "Error deleting intention for replacement with source %q "+ + "Error replacing intention with source %q "+ "and destination %q: %s", ixn.SourceString(), ixn.DestinationString(), err)) return 1 } + + // Continue since we don't want to try to insert a new intention + continue } }