Rename disableIntermediateSnapshots > enableIntermediateSnapshots

sebasslash/snapshot-interval-header-check
Brandon Croft 3 years ago
parent d03fd37ee6
commit 86eed095b3
No known key found for this signature in database
GPG Key ID: B01E32423322EB9D

@ -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.

@ -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

@ -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)
}
}

Loading…
Cancel
Save