From 75458a182d4f17e00d7e16dfb759dd75dda296d0 Mon Sep 17 00:00:00 2001 From: James Bardin Date: Fri, 31 Mar 2017 17:04:56 -0400 Subject: [PATCH] remove extra state.Locker assertions All states are lockers, so get rid of extra asertions. --- backend/remote-state/consul/backend_state.go | 7 ++----- command/state/state.go | 14 ++------------ command/unlock.go | 9 +-------- state/backup.go | 11 ++--------- 4 files changed, 7 insertions(+), 34 deletions(-) diff --git a/backend/remote-state/consul/backend_state.go b/backend/remote-state/consul/backend_state.go index 74f30c842..9a8fd080f 100644 --- a/backend/remote-state/consul/backend_state.go +++ b/backend/remote-state/consul/backend_state.go @@ -102,22 +102,19 @@ func (b *Backend) State(name string) (state.State, error) { stateMgr = &state.LockDisabled{Inner: stateMgr} } - // Get the locker, which we know always exists - stateMgrLocker := stateMgr.(state.Locker) - // Grab a lock, we use this to write an empty state if one doesn't // exist already. We have to write an empty state as a sentinel value // so States() knows it exists. lockInfo := state.NewLockInfo() lockInfo.Operation = "init" - lockId, err := stateMgrLocker.Lock(lockInfo) + lockId, err := stateMgr.Lock(lockInfo) if err != nil { return nil, fmt.Errorf("failed to lock state in Consul: %s", err) } // Local helper function so we can call it multiple places lockUnlock := func(parent error) error { - if err := stateMgrLocker.Unlock(lockId); err != nil { + if err := stateMgr.Unlock(lockId); err != nil { return fmt.Errorf(strings.TrimSpace(errStateUnlock), lockId, err) } diff --git a/command/state/state.go b/command/state/state.go index 3915e8ca2..52adf62a6 100644 --- a/command/state/state.go +++ b/command/state/state.go @@ -50,15 +50,10 @@ that no one else is holding a lock. // Lock locks the given state and outputs to the user if locking // is taking longer than the threshold. func Lock(s state.State, info *state.LockInfo, ui cli.Ui, color *colorstring.Colorize) (string, error) { - sl, ok := s.(state.Locker) - if !ok { - return "", nil - } - var lockID string err := slowmessage.Do(LockThreshold, func() error { - id, err := sl.Lock(info) + id, err := s.Lock(info) lockID = id return err }, func() { @@ -77,13 +72,8 @@ func Lock(s state.State, info *state.LockInfo, ui cli.Ui, color *colorstring.Col // Unlock unlocks the given state and outputs to the user if the // unlock fails what can be done. func Unlock(s state.State, id string, ui cli.Ui, color *colorstring.Colorize) error { - sl, ok := s.(state.Locker) - if !ok { - return nil - } - err := slowmessage.Do(LockThreshold, func() error { - return sl.Unlock(id) + return s.Unlock(id) }, func() { if ui != nil { ui.Output(color.Color(UnlockMessage)) diff --git a/command/unlock.go b/command/unlock.go index 010fd9332..666e4f346 100644 --- a/command/unlock.go +++ b/command/unlock.go @@ -59,13 +59,6 @@ func (c *UnlockCommand) Run(args []string) int { return 1 } - s, ok := st.(state.Locker) - if !ok { - c.Ui.Error("The remote state backend in use does not support locking, and therefor\n" + - "cannot be unlocked.") - return 1 - } - isLocal := false switch s := st.(type) { case *state.BackupState: @@ -103,7 +96,7 @@ func (c *UnlockCommand) Run(args []string) int { } } - if err := s.Unlock(lockID); err != nil { + if err := st.Unlock(lockID); err != nil { c.Ui.Error(fmt.Sprintf("Failed to unlock state: %s", err)) return 1 } diff --git a/state/backup.go b/state/backup.go index 15d8f6f3e..c357bba49 100644 --- a/state/backup.go +++ b/state/backup.go @@ -41,19 +41,12 @@ func (s *BackupState) PersistState() error { return s.Real.PersistState() } -// all states get wrapped by BackupState, so it has to be a Locker func (s *BackupState) Lock(info *LockInfo) (string, error) { - if s, ok := s.Real.(Locker); ok { - return s.Lock(info) - } - return "", nil + return s.Real.Lock(info) } func (s *BackupState) Unlock(id string) error { - if s, ok := s.Real.(Locker); ok { - return s.Unlock(id) - } - return nil + return s.Real.Unlock(id) } func (s *BackupState) backup() error {