/
backend.go
1169 lines (1004 loc) · 36.9 KB
/
backend.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package cloud
import (
"context"
"fmt"
"log"
"net/http"
"net/url"
"os"
"sort"
"strings"
"sync"
"time"
tfe "github.com/hashicorp/go-tfe"
version "github.com/hashicorp/go-version"
svchost "github.com/hashicorp/terraform-svchost"
"github.com/hashicorp/terraform-svchost/disco"
"github.com/mitchellh/cli"
"github.com/mitchellh/colorstring"
"github.com/zclconf/go-cty/cty"
"github.com/zclconf/go-cty/cty/gocty"
"github.com/hashicorp/terraform/internal/backend"
"github.com/hashicorp/terraform/internal/configs/configschema"
"github.com/hashicorp/terraform/internal/plans"
"github.com/hashicorp/terraform/internal/states/statemgr"
"github.com/hashicorp/terraform/internal/terraform"
"github.com/hashicorp/terraform/internal/tfdiags"
tfversion "github.com/hashicorp/terraform/version"
backendLocal "github.com/hashicorp/terraform/internal/backend/local"
)
const (
defaultHostname = "app.terraform.io"
defaultParallelism = 10
tfeServiceID = "tfe.v2"
headerSourceKey = "X-Terraform-Integration"
headerSourceValue = "cloud"
)
// Cloud is an implementation of EnhancedBackend in service of the Terraform Cloud/Enterprise
// integration for Terraform CLI. This backend is not intended to be surfaced at the user level and
// is instead an implementation detail of cloud.Cloud.
type Cloud struct {
// CLI and Colorize control the CLI output. If CLI is nil then no CLI
// output will be done. If CLIColor is nil then no coloring will be done.
CLI cli.Ui
CLIColor *colorstring.Colorize
// ContextOpts are the base context options to set when initializing a
// new Terraform context. Many of these will be overridden or merged by
// Operation. See Operation for more details.
ContextOpts *terraform.ContextOpts
// client is the Terraform Cloud/Enterprise API client.
client *tfe.Client
// lastRetry is set to the last time a request was retried.
lastRetry time.Time
// hostname of Terraform Cloud or Terraform Enterprise
hostname string
// organization is the organization that contains the target workspaces.
organization string
// WorkspaceMapping contains strategies for mapping CLI workspaces in the working directory
// to remote Terraform Cloud workspaces.
WorkspaceMapping WorkspaceMapping
// services is used for service discovery
services *disco.Disco
// local allows local operations, where Terraform Cloud serves as a state storage backend.
local backend.Enhanced
// forceLocal, if true, will force the use of the local backend.
forceLocal bool
// opLock locks operations
opLock sync.Mutex
// ignoreVersionConflict, if true, will disable the requirement that the
// local Terraform version matches the remote workspace's configured
// version. This will also cause VerifyWorkspaceTerraformVersion to return
// a warning diagnostic instead of an error.
ignoreVersionConflict bool
runningInAutomation bool
// input stores the value of the -input flag, since it will be used
// to determine whether or not to ask the user for approval of a run.
input bool
}
var _ backend.Backend = (*Cloud)(nil)
var _ backend.Enhanced = (*Cloud)(nil)
var _ backend.Local = (*Cloud)(nil)
// New creates a new initialized cloud backend.
func New(services *disco.Disco) *Cloud {
return &Cloud{
services: services,
}
}
// ConfigSchema implements backend.Enhanced.
func (b *Cloud) ConfigSchema() *configschema.Block {
return &configschema.Block{
Attributes: map[string]*configschema.Attribute{
"hostname": {
Type: cty.String,
Optional: true,
Description: schemaDescriptionHostname,
},
"organization": {
Type: cty.String,
Optional: true,
Description: schemaDescriptionOrganization,
},
"token": {
Type: cty.String,
Optional: true,
Description: schemaDescriptionToken,
},
},
BlockTypes: map[string]*configschema.NestedBlock{
"workspaces": {
Block: configschema.Block{
Attributes: map[string]*configschema.Attribute{
"name": {
Type: cty.String,
Optional: true,
Description: schemaDescriptionName,
},
"tags": {
Type: cty.Set(cty.String),
Optional: true,
Description: schemaDescriptionTags,
},
},
},
Nesting: configschema.NestingSingle,
},
},
}
}
// PrepareConfig implements backend.Backend.
func (b *Cloud) PrepareConfig(obj cty.Value) (cty.Value, tfdiags.Diagnostics) {
var diags tfdiags.Diagnostics
if obj.IsNull() {
return obj, diags
}
// check if organization is specified in the config.
if val := obj.GetAttr("organization"); val.IsNull() || val.AsString() == "" {
// organization is specified in the config but is invalid, so
// we'll fallback on TF_CLOUD_ORGANIZATION
if val := os.Getenv("TF_CLOUD_ORGANIZATION"); val == "" {
diags = diags.Append(missingConfigAttributeAndEnvVar("organization", "TF_CLOUD_ORGANIZATION"))
}
}
WorkspaceMapping := WorkspaceMapping{}
if workspaces := obj.GetAttr("workspaces"); !workspaces.IsNull() {
if val := workspaces.GetAttr("name"); !val.IsNull() {
WorkspaceMapping.Name = val.AsString()
}
if val := workspaces.GetAttr("tags"); !val.IsNull() {
err := gocty.FromCtyValue(val, &WorkspaceMapping.Tags)
if err != nil {
log.Panicf("An unxpected error occurred: %s", err)
}
}
} else {
WorkspaceMapping.Name = os.Getenv("TF_WORKSPACE")
}
switch WorkspaceMapping.Strategy() {
// Make sure have a workspace mapping strategy present
case WorkspaceNoneStrategy:
diags = diags.Append(invalidWorkspaceConfigMissingValues)
// Make sure that a workspace name is configured.
case WorkspaceInvalidStrategy:
diags = diags.Append(invalidWorkspaceConfigMisconfiguration)
}
return obj, diags
}
// Configure implements backend.Enhanced.
func (b *Cloud) Configure(obj cty.Value) tfdiags.Diagnostics {
var diags tfdiags.Diagnostics
if obj.IsNull() {
return diags
}
diagErr := b.setConfigurationFields(obj)
if diagErr.HasErrors() {
return diagErr
}
// Discover the service URL to confirm that it provides the Terraform Cloud/Enterprise API
service, err := b.discover()
// Check for errors before we continue.
if err != nil {
diags = diags.Append(tfdiags.AttributeValue(
tfdiags.Error,
strings.ToUpper(err.Error()[:1])+err.Error()[1:],
"", // no description is needed here, the error is clear
cty.Path{cty.GetAttrStep{Name: "hostname"}},
))
return diags
}
// First we'll retrieve the token from the configuration
var token string
if val := obj.GetAttr("token"); !val.IsNull() {
token = val.AsString()
}
// Get the token from the CLI Config File in the credentials section
// if no token was not set in the configuration
if token == "" {
token, err = b.token()
if err != nil {
diags = diags.Append(tfdiags.AttributeValue(
tfdiags.Error,
strings.ToUpper(err.Error()[:1])+err.Error()[1:],
"", // no description is needed here, the error is clear
cty.Path{cty.GetAttrStep{Name: "hostname"}},
))
return diags
}
}
// Return an error if we still don't have a token at this point.
if token == "" {
loginCommand := "terraform login"
if b.hostname != defaultHostname {
loginCommand = loginCommand + " " + b.hostname
}
diags = diags.Append(tfdiags.Sourceless(
tfdiags.Error,
"Required token could not be found",
fmt.Sprintf(
"Run the following command to generate a token for %s:\n %s",
b.hostname,
loginCommand,
),
))
return diags
}
if b.client == nil {
cfg := &tfe.Config{
Address: service.String(),
BasePath: service.Path,
Token: token,
Headers: make(http.Header),
RetryLogHook: b.retryLogHook,
}
// Set the version header to the current version.
cfg.Headers.Set(tfversion.Header, tfversion.Version)
cfg.Headers.Set(headerSourceKey, headerSourceValue)
// Create the TFC/E API client.
b.client, err = tfe.NewClient(cfg)
if err != nil {
diags = diags.Append(tfdiags.Sourceless(
tfdiags.Error,
"Failed to create the Terraform Cloud/Enterprise client",
fmt.Sprintf(
`Encountered an unexpected error while creating the `+
`Terraform Cloud/Enterprise client: %s.`, err,
),
))
return diags
}
}
// Check if the organization exists by reading its entitlements.
entitlements, err := b.client.Organizations.ReadEntitlements(context.Background(), b.organization)
if err != nil {
if err == tfe.ErrResourceNotFound {
err = fmt.Errorf("organization %q at host %s not found.\n\n"+
"Please ensure that the organization and hostname are correct "+
"and that your API token for %s is valid.",
b.organization, b.hostname, b.hostname)
}
diags = diags.Append(tfdiags.AttributeValue(
tfdiags.Error,
fmt.Sprintf("Failed to read organization %q at host %s", b.organization, b.hostname),
fmt.Sprintf("Encountered an unexpected error while reading the "+
"organization settings: %s", err),
cty.Path{cty.GetAttrStep{Name: "organization"}},
))
return diags
}
if ws, ok := os.LookupEnv("TF_WORKSPACE"); ok {
if ws == b.WorkspaceMapping.Name || b.WorkspaceMapping.Strategy() == WorkspaceTagsStrategy {
diag := b.validWorkspaceEnvVar(context.Background(), b.organization, ws)
if diag != nil {
diags = diags.Append(diag)
return diags
}
}
}
// Check for the minimum version of Terraform Enterprise required.
//
// For API versions prior to 2.3, RemoteAPIVersion will return an empty string,
// so if there's an error when parsing the RemoteAPIVersion, it's handled as
// equivalent to an API version < 2.3.
currentAPIVersion, parseErr := version.NewVersion(b.client.RemoteAPIVersion())
desiredAPIVersion, _ := version.NewVersion("2.5")
if parseErr != nil || currentAPIVersion.LessThan(desiredAPIVersion) {
log.Printf("[TRACE] API version check failed; want: >= %s, got: %s", desiredAPIVersion.Original(), currentAPIVersion)
if b.runningInAutomation {
// It should never be possible for this Terraform process to be mistakenly
// used internally within an unsupported Terraform Enterprise install - but
// just in case it happens, give an actionable error.
diags = diags.Append(
tfdiags.Sourceless(
tfdiags.Error,
"Unsupported Terraform Enterprise version",
cloudIntegrationUsedInUnsupportedTFE,
),
)
} else {
diags = diags.Append(tfdiags.Sourceless(
tfdiags.Error,
"Unsupported Terraform Enterprise version",
`The 'cloud' option is not supported with this version of Terraform Enterprise.`,
),
)
}
}
// Configure a local backend for when we need to run operations locally.
b.local = backendLocal.NewWithBackend(b)
b.forceLocal = b.forceLocal || !entitlements.Operations
// Enable retries for server errors as the backend is now fully configured.
b.client.RetryServerErrors(true)
return diags
}
func (b *Cloud) setConfigurationFields(obj cty.Value) tfdiags.Diagnostics {
var diags tfdiags.Diagnostics
// Get the hostname.
b.hostname = os.Getenv("TF_CLOUD_HOSTNAME")
if val := obj.GetAttr("hostname"); !val.IsNull() && val.AsString() != "" {
b.hostname = val.AsString()
} else if b.hostname == "" {
b.hostname = defaultHostname
}
// We can have two options, setting the organization via the config
// or using TF_CLOUD_ORGANIZATION. Since PrepareConfig() validates that one of these
// values must exist, we'll initially set it to the env var and override it if
// specified in the configuration.
b.organization = os.Getenv("TF_CLOUD_ORGANIZATION")
// Check if the organization is present and valid in the config.
if val := obj.GetAttr("organization"); !val.IsNull() && val.AsString() != "" {
b.organization = val.AsString()
}
// Get the workspaces configuration block and retrieve the
// default workspace name.
if workspaces := obj.GetAttr("workspaces"); !workspaces.IsNull() {
// PrepareConfig checks that you cannot set both of these.
if val := workspaces.GetAttr("name"); !val.IsNull() {
b.WorkspaceMapping.Name = val.AsString()
}
if val := workspaces.GetAttr("tags"); !val.IsNull() {
var tags []string
err := gocty.FromCtyValue(val, &tags)
if err != nil {
log.Panicf("An unexpected error occurred: %s", err)
}
b.WorkspaceMapping.Tags = tags
}
} else {
b.WorkspaceMapping.Name = os.Getenv("TF_WORKSPACE")
}
// Determine if we are forced to use the local backend.
b.forceLocal = os.Getenv("TF_FORCE_LOCAL_BACKEND") != ""
return diags
}
// discover the TFC/E API service URL and version constraints.
func (b *Cloud) discover() (*url.URL, error) {
hostname, err := svchost.ForComparison(b.hostname)
if err != nil {
return nil, err
}
host, err := b.services.Discover(hostname)
if err != nil {
return nil, err
}
service, err := host.ServiceURL(tfeServiceID)
// Return the error, unless its a disco.ErrVersionNotSupported error.
if _, ok := err.(*disco.ErrVersionNotSupported); !ok && err != nil {
return nil, err
}
return service, err
}
// token returns the token for this host as configured in the credentials
// section of the CLI Config File. If no token was configured, an empty
// string will be returned instead.
func (b *Cloud) token() (string, error) {
hostname, err := svchost.ForComparison(b.hostname)
if err != nil {
return "", err
}
creds, err := b.services.CredentialsForHost(hostname)
if err != nil {
log.Printf("[WARN] Failed to get credentials for %s: %s (ignoring)", b.hostname, err)
return "", nil
}
if creds != nil {
return creds.Token(), nil
}
return "", nil
}
// retryLogHook is invoked each time a request is retried allowing the
// backend to log any connection issues to prevent data loss.
func (b *Cloud) retryLogHook(attemptNum int, resp *http.Response) {
if b.CLI != nil {
// Ignore the first retry to make sure any delayed output will
// be written to the console before we start logging retries.
//
// The retry logic in the TFE client will retry both rate limited
// requests and server errors, but in the cloud backend we only
// care about server errors so we ignore rate limit (429) errors.
if attemptNum == 0 || (resp != nil && resp.StatusCode == 429) {
// Reset the last retry time.
b.lastRetry = time.Now()
return
}
if attemptNum == 1 {
b.CLI.Output(b.Colorize().Color(strings.TrimSpace(initialRetryError)))
} else {
b.CLI.Output(b.Colorize().Color(strings.TrimSpace(
fmt.Sprintf(repeatedRetryError, time.Since(b.lastRetry).Round(time.Second)))))
}
}
}
// Workspaces implements backend.Enhanced, returning a filtered list of workspace names according to
// the workspace mapping strategy configured.
func (b *Cloud) Workspaces() ([]string, error) {
// Create a slice to contain all the names.
var names []string
// If configured for a single workspace, return that exact name only. The StateMgr for this
// backend will automatically create the remote workspace if it does not yet exist.
if b.WorkspaceMapping.Strategy() == WorkspaceNameStrategy {
names = append(names, b.WorkspaceMapping.Name)
return names, nil
}
// Otherwise, multiple workspaces are being mapped. Query Terraform Cloud for all the remote
// workspaces by the provided mapping strategy.
options := &tfe.WorkspaceListOptions{}
if b.WorkspaceMapping.Strategy() == WorkspaceTagsStrategy {
taglist := strings.Join(b.WorkspaceMapping.Tags, ",")
options.Tags = taglist
}
for {
wl, err := b.client.Workspaces.List(context.Background(), b.organization, options)
if err != nil {
return nil, err
}
for _, w := range wl.Items {
names = append(names, w.Name)
}
// Exit the loop when we've seen all pages.
if wl.CurrentPage >= wl.TotalPages {
break
}
// Update the page number to get the next page.
options.PageNumber = wl.NextPage
}
// Sort the result so we have consistent output.
sort.StringSlice(names).Sort()
return names, nil
}
// DeleteWorkspace implements backend.Enhanced.
func (b *Cloud) DeleteWorkspace(name string) error {
if name == backend.DefaultStateName {
return backend.ErrDefaultWorkspaceNotSupported
}
if b.WorkspaceMapping.Strategy() == WorkspaceNameStrategy {
return backend.ErrWorkspacesNotSupported
}
// Configure the remote workspace name.
State := &State{tfeClient: b.client, organization: b.organization, workspace: &tfe.Workspace{
Name: name,
}}
return State.Delete()
}
// StateMgr implements backend.Enhanced.
func (b *Cloud) StateMgr(name string) (statemgr.Full, error) {
var remoteTFVersion string
if name == backend.DefaultStateName {
return nil, backend.ErrDefaultWorkspaceNotSupported
}
if b.WorkspaceMapping.Strategy() == WorkspaceNameStrategy && name != b.WorkspaceMapping.Name {
return nil, backend.ErrWorkspacesNotSupported
}
workspace, err := b.client.Workspaces.Read(context.Background(), b.organization, name)
if err != nil && err != tfe.ErrResourceNotFound {
return nil, fmt.Errorf("Failed to retrieve workspace %s: %v", name, err)
}
if workspace != nil {
remoteTFVersion = workspace.TerraformVersion
}
if err == tfe.ErrResourceNotFound {
// Create a workspace
options := tfe.WorkspaceCreateOptions{
Name: tfe.String(name),
Tags: b.WorkspaceMapping.tfeTags(),
}
log.Printf("[TRACE] cloud: Creating Terraform Cloud workspace %s/%s", b.organization, name)
workspace, err = b.client.Workspaces.Create(context.Background(), b.organization, options)
if err != nil {
return nil, fmt.Errorf("Error creating workspace %s: %v", name, err)
}
remoteTFVersion = workspace.TerraformVersion
// Attempt to set the new workspace to use this version of Terraform. This
// can fail if there's no enabled tool_version whose name matches our
// version string, but that's expected sometimes -- just warn and continue.
versionOptions := tfe.WorkspaceUpdateOptions{
TerraformVersion: tfe.String(tfversion.String()),
}
_, err := b.client.Workspaces.UpdateByID(context.Background(), workspace.ID, versionOptions)
if err == nil {
remoteTFVersion = tfversion.String()
} else {
// TODO: Ideally we could rely on the client to tell us what the actual
// problem was, but we currently can't get enough context from the error
// object to do a nicely formatted message, so we're just assuming the
// issue was that the version wasn't available since that's probably what
// happened.
log.Printf("[TRACE] cloud: Attempted to select version %s for TFC workspace; unavailable, so %s will be used instead.", tfversion.String(), workspace.TerraformVersion)
if b.CLI != nil {
versionUnavailable := fmt.Sprintf(unavailableTerraformVersion, tfversion.String(), workspace.TerraformVersion)
b.CLI.Output(b.Colorize().Color(versionUnavailable))
}
}
}
if b.workspaceTagsRequireUpdate(workspace, b.WorkspaceMapping) {
options := tfe.WorkspaceAddTagsOptions{
Tags: b.WorkspaceMapping.tfeTags(),
}
log.Printf("[TRACE] cloud: Adding tags for Terraform Cloud workspace %s/%s", b.organization, name)
err = b.client.Workspaces.AddTags(context.Background(), workspace.ID, options)
if err != nil {
return nil, fmt.Errorf("Error updating workspace %s: %v", name, err)
}
}
// This is a fallback error check. Most code paths should use other
// mechanisms to check the version, then set the ignoreVersionConflict
// field to true. This check is only in place to ensure that we don't
// accidentally upgrade state with a new code path, and the version check
// logic is coarser and simpler.
if !b.ignoreVersionConflict {
// Explicitly ignore the pseudo-version "latest" here, as it will cause
// plan and apply to always fail.
if remoteTFVersion != tfversion.String() && remoteTFVersion != "latest" {
return nil, fmt.Errorf("Remote workspace Terraform version %q does not match local Terraform version %q", remoteTFVersion, tfversion.String())
}
}
return &State{tfeClient: b.client, organization: b.organization, workspace: workspace}, nil
}
// Operation implements backend.Enhanced.
func (b *Cloud) Operation(ctx context.Context, op *backend.Operation) (*backend.RunningOperation, error) {
// Retrieve the workspace for this operation.
w, err := b.fetchWorkspace(ctx, b.organization, op.Workspace)
if err != nil {
return nil, err
}
// Terraform remote version conflicts are not a concern for operations. We
// are in one of three states:
//
// - Running remotely, in which case the local version is irrelevant;
// - Workspace configured for local operations, in which case the remote
// version is meaningless;
// - Forcing local operations, which should only happen in the Terraform Cloud worker, in
// which case the Terraform versions by definition match.
b.IgnoreVersionConflict()
// Check if we need to use the local backend to run the operation.
if b.forceLocal || isLocalExecutionMode(w.ExecutionMode) {
// Record that we're forced to run operations locally to allow the
// command package UI to operate correctly
b.forceLocal = true
return b.local.Operation(ctx, op)
}
// Set the remote workspace name.
op.Workspace = w.Name
// Determine the function to call for our operation
var f func(context.Context, context.Context, *backend.Operation, *tfe.Workspace) (*tfe.Run, error)
switch op.Type {
case backend.OperationTypePlan:
f = b.opPlan
case backend.OperationTypeApply:
f = b.opApply
case backend.OperationTypeRefresh:
// The `terraform refresh` command has been deprecated in favor of `terraform apply -refresh-state`.
// Rather than respond with an error telling the user to run the other command we can just run
// that command instead. We will tell the user what we are doing, and then do it.
if b.CLI != nil {
b.CLI.Output(b.Colorize().Color(strings.TrimSpace(refreshToApplyRefresh) + "\n"))
}
op.PlanMode = plans.RefreshOnlyMode
op.PlanRefresh = true
op.AutoApprove = true
f = b.opApply
default:
return nil, fmt.Errorf(
"\n\nTerraform Cloud does not support the %q operation.", op.Type)
}
// Lock
b.opLock.Lock()
// Build our running operation
// the runninCtx is only used to block until the operation returns.
runningCtx, done := context.WithCancel(context.Background())
runningOp := &backend.RunningOperation{
Context: runningCtx,
PlanEmpty: true,
}
// stopCtx wraps the context passed in, and is used to signal a graceful Stop.
stopCtx, stop := context.WithCancel(ctx)
runningOp.Stop = stop
// cancelCtx is used to cancel the operation immediately, usually
// indicating that the process is exiting.
cancelCtx, cancel := context.WithCancel(context.Background())
runningOp.Cancel = cancel
// Do it.
go func() {
defer done()
defer stop()
defer cancel()
defer b.opLock.Unlock()
r, opErr := f(stopCtx, cancelCtx, op, w)
if opErr != nil && opErr != context.Canceled {
var diags tfdiags.Diagnostics
diags = diags.Append(opErr)
op.ReportResult(runningOp, diags)
return
}
if r == nil && opErr == context.Canceled {
runningOp.Result = backend.OperationFailure
return
}
if r != nil {
// Retrieve the run to get its current status.
r, err := b.client.Runs.Read(cancelCtx, r.ID)
if err != nil {
var diags tfdiags.Diagnostics
diags = diags.Append(generalError("Failed to retrieve run", err))
op.ReportResult(runningOp, diags)
return
}
// Record if there are any changes.
runningOp.PlanEmpty = !r.HasChanges
if opErr == context.Canceled {
if err := b.cancel(cancelCtx, op, r); err != nil {
var diags tfdiags.Diagnostics
diags = diags.Append(generalError("Failed to retrieve run", err))
op.ReportResult(runningOp, diags)
return
}
}
if r.Status == tfe.RunCanceled || r.Status == tfe.RunErrored {
runningOp.Result = backend.OperationFailure
}
}
}()
// Return the running operation.
return runningOp, nil
}
func (b *Cloud) cancel(cancelCtx context.Context, op *backend.Operation, r *tfe.Run) error {
if r.Actions.IsCancelable {
// Only ask if the remote operation should be canceled
// if the auto approve flag is not set.
if !op.AutoApprove {
v, err := op.UIIn.Input(cancelCtx, &terraform.InputOpts{
Id: "cancel",
Query: "\nDo you want to cancel the remote operation?",
Description: "Only 'yes' will be accepted to cancel.",
})
if err != nil {
return generalError("Failed asking to cancel", err)
}
if v != "yes" {
if b.CLI != nil {
b.CLI.Output(b.Colorize().Color(strings.TrimSpace(operationNotCanceled)))
}
return nil
}
} else {
if b.CLI != nil {
// Insert a blank line to separate the ouputs.
b.CLI.Output("")
}
}
// Try to cancel the remote operation.
err := b.client.Runs.Cancel(cancelCtx, r.ID, tfe.RunCancelOptions{})
if err != nil {
return generalError("Failed to cancel run", err)
}
if b.CLI != nil {
b.CLI.Output(b.Colorize().Color(strings.TrimSpace(operationCanceled)))
}
}
return nil
}
// IgnoreVersionConflict allows commands to disable the fall-back check that
// the local Terraform version matches the remote workspace's configured
// Terraform version. This should be called by commands where this check is
// unnecessary, such as those performing remote operations, or read-only
// operations. It will also be called if the user uses a command-line flag to
// override this check.
func (b *Cloud) IgnoreVersionConflict() {
b.ignoreVersionConflict = true
}
// VerifyWorkspaceTerraformVersion compares the local Terraform version against
// the workspace's configured Terraform version. If they are compatible, this
// means that there are no state compatibility concerns, so it returns no
// diagnostics.
//
// If the versions aren't compatible, it returns an error (or, if
// b.ignoreVersionConflict is set, a warning).
func (b *Cloud) VerifyWorkspaceTerraformVersion(workspaceName string) tfdiags.Diagnostics {
var diags tfdiags.Diagnostics
workspace, err := b.getRemoteWorkspace(context.Background(), workspaceName)
if err != nil {
// If the workspace doesn't exist, there can be no compatibility
// problem, so we can return. This is most likely to happen when
// migrating state from a local backend to a new workspace.
if err == tfe.ErrResourceNotFound {
return nil
}
diags = diags.Append(tfdiags.Sourceless(
tfdiags.Error,
"Error looking up workspace",
fmt.Sprintf("Workspace read failed: %s", err),
))
return diags
}
// If the workspace has the pseudo-version "latest", all bets are off. We
// cannot reasonably determine what the intended Terraform version is, so
// we'll skip version verification.
if workspace.TerraformVersion == "latest" {
return nil
}
// If the workspace has execution-mode set to local, the remote Terraform
// version is effectively meaningless, so we'll skip version verification.
if isLocalExecutionMode(workspace.ExecutionMode) {
return nil
}
remoteConstraint, err := version.NewConstraint(workspace.TerraformVersion)
if err != nil {
message := fmt.Sprintf(
"The remote workspace specified an invalid Terraform version or constraint (%s), "+
"and it isn't possible to determine whether the local Terraform version (%s) is compatible.",
workspace.TerraformVersion,
tfversion.String(),
)
diags = diags.Append(incompatibleWorkspaceTerraformVersion(message, b.ignoreVersionConflict))
return diags
}
remoteVersion, _ := version.NewSemver(workspace.TerraformVersion)
// We can use a looser version constraint if the workspace specifies a
// literal Terraform version, and it is not a prerelease. The latter
// restriction is because we cannot compare prerelease versions with any
// operator other than simple equality.
if remoteVersion != nil && remoteVersion.Prerelease() == "" {
v014 := version.Must(version.NewSemver("0.14.0"))
v130 := version.Must(version.NewSemver("1.3.0"))
// Versions from 0.14 through the early 1.x series should be compatible
// (though we don't know about 1.3 yet).
if remoteVersion.GreaterThanOrEqual(v014) && remoteVersion.LessThan(v130) {
early1xCompatible, err := version.NewConstraint(fmt.Sprintf(">= 0.14.0, < %s", v130.String()))
if err != nil {
panic(err)
}
remoteConstraint = early1xCompatible
}
// Any future new state format will require at least a minor version
// increment, so x.y.* will always be compatible with each other.
if remoteVersion.GreaterThanOrEqual(v130) {
rwvs := remoteVersion.Segments64()
if len(rwvs) >= 3 {
// ~> x.y.0
minorVersionCompatible, err := version.NewConstraint(fmt.Sprintf("~> %d.%d.0", rwvs[0], rwvs[1]))
if err != nil {
panic(err)
}
remoteConstraint = minorVersionCompatible
}
}
}
// Re-parsing tfversion.String because tfversion.SemVer omits the prerelease
// prefix, and we want to allow constraints like `~> 1.2.0-beta1`.
fullTfversion := version.Must(version.NewSemver(tfversion.String()))
if remoteConstraint.Check(fullTfversion) {
return diags
}
message := fmt.Sprintf(
"The local Terraform version (%s) does not meet the version requirements for remote workspace %s/%s (%s).",
tfversion.String(),
b.organization,
workspace.Name,
remoteConstraint,
)
diags = diags.Append(incompatibleWorkspaceTerraformVersion(message, b.ignoreVersionConflict))
return diags
}
func (b *Cloud) IsLocalOperations() bool {
return b.forceLocal
}
// Colorize returns the Colorize structure that can be used for colorizing
// output. This is guaranteed to always return a non-nil value and so useful
// as a helper to wrap any potentially colored strings.
//
// TODO SvH: Rename this back to Colorize as soon as we can pass -no-color.
//
//lint:ignore U1000 see above todo
func (b *Cloud) cliColorize() *colorstring.Colorize {
if b.CLIColor != nil {
return b.CLIColor
}
return &colorstring.Colorize{
Colors: colorstring.DefaultColors,
Disable: true,
}
}
func (b *Cloud) workspaceTagsRequireUpdate(workspace *tfe.Workspace, workspaceMapping WorkspaceMapping) bool {
if workspaceMapping.Strategy() != WorkspaceTagsStrategy {
return false
}
existingTags := map[string]struct{}{}
for _, t := range workspace.TagNames {
existingTags[t] = struct{}{}
}
for _, tag := range workspaceMapping.Tags {
if _, ok := existingTags[tag]; !ok {
return true
}
}
return false
}
type WorkspaceMapping struct {
Name string
Tags []string
}
type workspaceStrategy string
const (
WorkspaceTagsStrategy workspaceStrategy = "tags"
WorkspaceNameStrategy workspaceStrategy = "name"
WorkspaceNoneStrategy workspaceStrategy = "none"
WorkspaceInvalidStrategy workspaceStrategy = "invalid"
)
func (wm WorkspaceMapping) Strategy() workspaceStrategy {
switch {
case len(wm.Tags) > 0 && wm.Name == "":
return WorkspaceTagsStrategy
case len(wm.Tags) == 0 && wm.Name != "":
return WorkspaceNameStrategy
case len(wm.Tags) == 0 && wm.Name == "":
return WorkspaceNoneStrategy
default:
// Any other combination is invalid as each strategy is mutually exclusive
return WorkspaceInvalidStrategy
}
}
func isLocalExecutionMode(execMode string) bool {
return execMode == "local"
}
func (b *Cloud) fetchWorkspace(ctx context.Context, organization string, workspace string) (*tfe.Workspace, error) {
// Retrieve the workspace for this operation.
w, err := b.client.Workspaces.Read(ctx, organization, workspace)
if err != nil {
switch err {
case context.Canceled:
return nil, err
case tfe.ErrResourceNotFound:
return nil, fmt.Errorf(
"workspace %s not found\n\n"+
"For security, Terraform Cloud returns '404 Not Found' responses for resources\n"+
"for resources that a user doesn't have access to, in addition to resources that\n"+
"do not exist. If the resource does exist, please check the permissions of the provided token.",
workspace,
)
default:
err := fmt.Errorf(
"Terraform Cloud returned an unexpected error:\n\n%s",
err,
)
return nil, err
}
}
return w, nil
}
// validWorkspaceEnvVar ensures we have selected a valid workspace using TF_WORKSPACE: