github.com/myhau/pulumi/pkg/v3@v3.70.2-0.20221116134521-f2775972e587/backend/filestate/stack.go (about) 1 // Copyright 2016-2018, Pulumi Corporation. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package filestate 16 17 import ( 18 "context" 19 "time" 20 21 "github.com/pulumi/pulumi/sdk/v3/go/common/tokens" 22 23 "github.com/pulumi/pulumi/pkg/v3/backend" 24 "github.com/pulumi/pulumi/pkg/v3/operations" 25 "github.com/pulumi/pulumi/pkg/v3/resource/deploy" 26 "github.com/pulumi/pulumi/pkg/v3/secrets" 27 "github.com/pulumi/pulumi/sdk/v3/go/common/apitype" 28 "github.com/pulumi/pulumi/sdk/v3/go/common/display" 29 "github.com/pulumi/pulumi/sdk/v3/go/common/util/result" 30 ) 31 32 // Stack is a local stack. This simply adds some local-specific properties atop the standard backend stack interface. 33 type Stack interface { 34 backend.Stack 35 Path() string // a path to the stack's checkpoint file on disk. 36 } 37 38 // localStack is a local stack descriptor. 39 type localStack struct { 40 ref backend.StackReference // the stack's reference (qualified name). 41 path string // a path to the stack's checkpoint file on disk. 42 snapshot *deploy.Snapshot // a snapshot representing the latest deployment state. 43 b *localBackend // a pointer to the backend this stack belongs to. 44 } 45 46 func newStack(ref backend.StackReference, path string, snapshot *deploy.Snapshot, b *localBackend) Stack { 47 return &localStack{ 48 ref: ref, 49 path: path, 50 snapshot: snapshot, 51 b: b, 52 } 53 } 54 55 func (s *localStack) Ref() backend.StackReference { return s.ref } 56 func (s *localStack) Snapshot(ctx context.Context) (*deploy.Snapshot, error) { return s.snapshot, nil } 57 func (s *localStack) Backend() backend.Backend { return s.b } 58 func (s *localStack) Path() string { return s.path } 59 func (s *localStack) Tags() map[apitype.StackTagName]string { return nil } 60 61 func (s *localStack) Remove(ctx context.Context, force bool) (bool, error) { 62 return backend.RemoveStack(ctx, s, force) 63 } 64 65 func (s *localStack) Rename(ctx context.Context, newName tokens.QName) (backend.StackReference, error) { 66 return backend.RenameStack(ctx, s, newName) 67 } 68 69 func (s *localStack) Preview( 70 ctx context.Context, 71 op backend.UpdateOperation) (*deploy.Plan, display.ResourceChanges, result.Result) { 72 73 return backend.PreviewStack(ctx, s, op) 74 } 75 76 func (s *localStack) Update(ctx context.Context, op backend.UpdateOperation) (display.ResourceChanges, result.Result) { 77 return backend.UpdateStack(ctx, s, op) 78 } 79 80 func (s *localStack) Import(ctx context.Context, op backend.UpdateOperation, 81 imports []deploy.Import) (display.ResourceChanges, result.Result) { 82 return backend.ImportStack(ctx, s, op, imports) 83 } 84 85 func (s *localStack) Refresh(ctx context.Context, op backend.UpdateOperation) (display.ResourceChanges, result.Result) { 86 return backend.RefreshStack(ctx, s, op) 87 } 88 89 func (s *localStack) Destroy(ctx context.Context, op backend.UpdateOperation) (display.ResourceChanges, result.Result) { 90 return backend.DestroyStack(ctx, s, op) 91 } 92 93 func (s *localStack) Watch(ctx context.Context, op backend.UpdateOperation, paths []string) result.Result { 94 return backend.WatchStack(ctx, s, op, paths) 95 } 96 97 func (s *localStack) GetLogs(ctx context.Context, cfg backend.StackConfiguration, 98 query operations.LogQuery) ([]operations.LogEntry, error) { 99 return backend.GetStackLogs(ctx, s, cfg, query) 100 } 101 102 func (s *localStack) ExportDeployment(ctx context.Context) (*apitype.UntypedDeployment, error) { 103 return backend.ExportStackDeployment(ctx, s) 104 } 105 106 func (s *localStack) ImportDeployment(ctx context.Context, deployment *apitype.UntypedDeployment) error { 107 return backend.ImportStackDeployment(ctx, s, deployment) 108 } 109 110 func (s *localStack) DefaultSecretManager(configFile string) (secrets.Manager, error) { 111 return NewPassphraseSecretsManager(s.Ref().Name(), configFile, false /* rotatePassphraseSecretsProvider */) 112 } 113 114 type localStackSummary struct { 115 name backend.StackReference 116 chk *apitype.CheckpointV3 117 } 118 119 func newLocalStackSummary(name backend.StackReference, chk *apitype.CheckpointV3) localStackSummary { 120 return localStackSummary{name: name, chk: chk} 121 } 122 123 func (lss localStackSummary) Name() backend.StackReference { 124 return lss.name 125 } 126 127 func (lss localStackSummary) LastUpdate() *time.Time { 128 if lss.chk != nil && lss.chk.Latest != nil { 129 if t := lss.chk.Latest.Manifest.Time; !t.IsZero() { 130 return &t 131 } 132 } 133 return nil 134 } 135 136 func (lss localStackSummary) ResourceCount() *int { 137 if lss.chk != nil && lss.chk.Latest != nil { 138 count := len(lss.chk.Latest.Resources) 139 return &count 140 } 141 return nil 142 }