github.com/graywolf-at-work-2/terraform-vendor@v1.4.5/internal/cloud/migration.go (about) 1 package cloud 2 3 import ( 4 "github.com/hashicorp/terraform/internal/configs" 5 legacy "github.com/hashicorp/terraform/internal/legacy/terraform" 6 ) 7 8 // Most of the logic for migrating into and out of "cloud mode" actually lives 9 // in the "command" package as part of the general backend init mechanisms, 10 // but we have some cloud-specific helper functionality here. 11 12 // ConfigChangeMode is a rough way to think about different situations that 13 // our backend change and state migration codepaths need to distinguish in 14 // the context of Cloud integration mode. 15 type ConfigChangeMode rune 16 17 //go:generate go run golang.org/x/tools/cmd/stringer -type ConfigChangeMode 18 19 const ( 20 // ConfigMigrationIn represents when the configuration calls for using 21 // Cloud mode but the working directory state disagrees. 22 ConfigMigrationIn ConfigChangeMode = '↘' 23 24 // ConfigMigrationOut represents when the working directory state calls 25 // for using Cloud mode but the working directory state disagrees. 26 ConfigMigrationOut ConfigChangeMode = '↖' 27 28 // ConfigChangeInPlace represents when both the working directory state 29 // and the config call for using Cloud mode, and so there might be 30 // (but won't necessarily be) cloud settings changing, but we don't 31 // need to do any actual migration. 32 ConfigChangeInPlace ConfigChangeMode = '↻' 33 34 // ConfigChangeIrrelevant represents when the config and working directory 35 // state disagree but neither calls for using Cloud mode, and so the 36 // Cloud integration is not involved in dealing with this. 37 ConfigChangeIrrelevant ConfigChangeMode = '🤷' 38 ) 39 40 // DetectConfigChangeType encapsulates the fiddly logic for deciding what kind 41 // of Cloud configuration change we seem to be making, based on the existing 42 // working directory state (if any) and the current configuration. 43 // 44 // This is a pretty specialized sort of thing focused on finicky details of 45 // the way we currently model working directory settings and config, so its 46 // signature probably won't survive any non-trivial refactoring of how 47 // the CLI layer thinks about backends/state storage. 48 func DetectConfigChangeType(wdState *legacy.BackendState, config *configs.Backend, haveLocalStates bool) ConfigChangeMode { 49 // Although externally the cloud integration isn't really a "backend", 50 // internally we treat it a bit like one just to preserve all of our 51 // existing interfaces that assume backends. "cloud" is the placeholder 52 // name we use for it, even though that isn't a backend that's actually 53 // available for selection in the usual way. 54 wdIsCloud := wdState != nil && wdState.Type == "cloud" 55 configIsCloud := config != nil && config.Type == "cloud" 56 57 // "uninit" here means that the working directory is totally uninitialized, 58 // even taking into account the possibility of implied local state that 59 // therefore doesn't typically require explicit "terraform init". 60 wdIsUninit := wdState == nil && !haveLocalStates 61 62 switch { 63 case configIsCloud: 64 switch { 65 case wdIsCloud || wdIsUninit: 66 // If config has cloud and the working directory is completely 67 // uninitialized then we assume we're doing the initial activation 68 // of this working directory for an already-migrated-to-cloud 69 // remote state. 70 return ConfigChangeInPlace 71 default: 72 // Otherwise, we seem to be migrating into cloud mode from a backend. 73 return ConfigMigrationIn 74 } 75 default: 76 switch { 77 case wdIsCloud: 78 // If working directory is already cloud but config isn't, we're 79 // migrating away from cloud to a backend. 80 return ConfigMigrationOut 81 default: 82 // Otherwise, this situation seems to be something unrelated to 83 // cloud mode and so outside of our scope here. 84 return ConfigChangeIrrelevant 85 } 86 } 87 88 } 89 90 func (m ConfigChangeMode) InvolvesCloud() bool { 91 switch m { 92 case ConfigMigrationIn, ConfigMigrationOut, ConfigChangeInPlace: 93 return true 94 default: 95 return false 96 } 97 } 98 99 func (m ConfigChangeMode) IsCloudMigration() bool { 100 switch m { 101 case ConfigMigrationIn, ConfigMigrationOut: 102 return true 103 default: 104 return false 105 } 106 }