From 86eed095b34d0ee5e8d9afea03fb44cc5c304950 Mon Sep 17 00:00:00 2001 From: Brandon Croft Date: Tue, 30 May 2023 12:35:23 -0600 Subject: [PATCH] Rename disableIntermediateSnapshots > enableIntermediateSnapshots --- internal/cloud/backend.go | 4 ++-- internal/cloud/state.go | 36 +++++++++++++++++++----------------- internal/cloud/state_test.go | 4 ++-- 3 files changed, 23 insertions(+), 21 deletions(-) diff --git a/internal/cloud/backend.go b/internal/cloud/backend.go index fd49e56788..b851fafb03 100644 --- a/internal/cloud/backend.go +++ b/internal/cloud/backend.go @@ -575,7 +575,7 @@ func (b *Cloud) DeleteWorkspace(name string, force bool) error { } // Configure the remote workspace name. - State := &State{tfeClient: b.client, organization: b.organization, workspace: workspace, disableIntermediateSnapshots: true} + State := &State{tfeClient: b.client, organization: b.organization, workspace: workspace, enableIntermediateSnapshots: false} return State.Delete(force) } @@ -661,7 +661,7 @@ func (b *Cloud) StateMgr(name string) (statemgr.Full, error) { } } - return &State{tfeClient: b.client, organization: b.organization, workspace: workspace, disableIntermediateSnapshots: true}, nil + return &State{tfeClient: b.client, organization: b.organization, workspace: workspace, enableIntermediateSnapshots: false}, nil } // Operation implements backend.Enhanced. diff --git a/internal/cloud/state.go b/internal/cloud/state.go index 6555a4e01e..69bafd01b3 100644 --- a/internal/cloud/state.go +++ b/internal/cloud/state.go @@ -69,9 +69,9 @@ type State struct { // not effect final snapshots after an operation, which will always // be written to the remote API. stateSnapshotInterval time.Duration - // If the header, X-Terraform-Snapshot-Interval is not present then - // we will disable snapshots - disableIntermediateSnapshots bool + // If the header X-Terraform-Snapshot-Interval is present then + // we will enable snapshots + enableIntermediateSnapshots bool } var ErrStateVersionUnauthorizedUpgradeState = errors.New(strings.TrimSpace(` @@ -247,7 +247,7 @@ func (s *State) ShouldPersistIntermediateState(info *local.IntermediateStatePers return true } - if s.disableIntermediateSnapshots && info.RequestedPersistInterval == time.Duration(0) { + if !s.enableIntermediateSnapshots && info.RequestedPersistInterval == time.Duration(0) { return false } @@ -535,31 +535,33 @@ func (s *State) GetRootOutputValues() (map[string]*states.OutputValue, error) { return result, nil } +func clamp(val, min, max int64) int64 { + if val < min { + return min + } else if val > max { + return max + } + return val +} + func (s *State) readSnapshotIntervalHeader(status int, header http.Header) { intervalStr := header.Get("x-terraform-snapshot-interval") if intervalSecs, err := strconv.ParseInt(intervalStr, 10, 64); err == nil { - if intervalSecs > 3600 { - // More than an hour is an unreasonable delay, so we'll just - // saturate at one hour. - intervalSecs = 3600 - } else if intervalSecs < 0 { - intervalSecs = 0 - } + // More than an hour is an unreasonable delay, so we'll just + // limit to one hour max. + intervalSecs = clamp(intervalSecs, 0, 3600) s.stateSnapshotInterval = time.Duration(intervalSecs) * time.Second - - // We will only enable snapshots for intervals greater than zero - if intervalSecs > 0 { - s.disableIntermediateSnapshots = false - } } else { // If the header field is either absent or invalid then we'll // just choose zero, which effectively means that we'll just use // the caller's requested interval instead. If the caller has no // requested interval or it is zero, then we will disable snapshots. s.stateSnapshotInterval = time.Duration(0) - s.disableIntermediateSnapshots = true } + + // We will only enable snapshots for intervals greater than zero + s.enableIntermediateSnapshots = s.stateSnapshotInterval > 0 } // tfeOutputToCtyValue decodes a combination of TFE output value and detailed-type to create a diff --git a/internal/cloud/state_test.go b/internal/cloud/state_test.go index c3d31c9259..14930a274d 100644 --- a/internal/cloud/state_test.go +++ b/internal/cloud/state_test.go @@ -290,7 +290,7 @@ func TestState_PersistState(t *testing.T) { t.Error("state manager already has a nonzero snapshot interval") } - if !cloudState.disableIntermediateSnapshots { + if cloudState.enableIntermediateSnapshots { t.Error("expected state manager to have disabled snapshots") } @@ -352,7 +352,7 @@ func TestState_PersistState(t *testing.T) { t.Errorf("wrong state snapshot interval after PersistState\ngot: %s\nwant: %s", got, testCase.expectedInterval) } - if got, want := cloudState.disableIntermediateSnapshots, !testCase.snapshotsEnabled; got != want { + if got, want := cloudState.enableIntermediateSnapshots, testCase.snapshotsEnabled; got != want { t.Errorf("expected disable intermediate snapshots to be\ngot: %t\nwant: %t", got, want) } }