@@ -16,6 +16,7 @@ import (
16
16
"path/filepath"
17
17
"strings"
18
18
19
+ "github.com/ljfranklin/terraform-resource/helper"
19
20
"github.com/ljfranklin/terraform-resource/models"
20
21
"github.com/ljfranklin/terraform-resource/runner"
21
22
)
@@ -115,7 +116,16 @@ func (c *client) InitWithBackend() error {
115
116
}
116
117
117
118
func (c * client ) writeBackendConfig (outputDir string ) (string , error ) {
118
- configContents , err := json .Marshal (c .model .BackendConfig )
119
+ bc := make (map [string ]interface {})
120
+ for key , value := range c .model .BackendConfig {
121
+ bc [key ] = value
122
+ }
123
+
124
+ if _ , ok := c .model .BackendConfig ["plan_address" ]; ok && c .model .BackendType == "http" {
125
+ delete (bc , "plan_address" )
126
+ }
127
+
128
+ configContents , err := json .Marshal (bc )
119
129
if err != nil {
120
130
return "" , err
121
131
}
@@ -389,6 +399,9 @@ func (c *client) Output(envName string) (map[string]map[string]interface{}, erro
389
399
"output" ,
390
400
"-json" ,
391
401
}
402
+ if c .model .BackendType == "http" {
403
+ envName = defaultWorkspace
404
+ }
392
405
outputCmd , err := c .terraformCmd (outputArgs , []string {
393
406
fmt .Sprintf ("TF_WORKSPACE=%s" , envName ),
394
407
})
@@ -537,6 +550,9 @@ func (c *client) ImportWithLegacyStorage() error {
537
550
}
538
551
539
552
func (c * client ) WorkspaceList () ([]string , error ) {
553
+ if c .model .BackendType == "http" {
554
+ return []string {defaultWorkspace }, nil
555
+ }
540
556
cmd , err := c .terraformCmd ([]string {
541
557
"workspace" ,
542
558
"list" ,
@@ -563,6 +579,9 @@ func (c *client) WorkspaceList() ([]string, error) {
563
579
}
564
580
565
581
func (c * client ) WorkspaceSelect (envName string ) error {
582
+ if c .model .BackendType == "http" {
583
+ return nil
584
+ }
566
585
cmd , err := c .terraformCmd ([]string {
567
586
"workspace" ,
568
587
"select" ,
@@ -580,6 +599,10 @@ func (c *client) WorkspaceSelect(envName string) error {
580
599
}
581
600
582
601
func (c * client ) WorkspaceNewIfNotExists (envName string ) error {
602
+ if c .model .BackendType == "http" {
603
+ return nil
604
+ }
605
+
583
606
workspaces , err := c .WorkspaceList ()
584
607
585
608
if err != nil {
@@ -614,6 +637,9 @@ func (c *client) WorkspaceNewIfNotExists(envName string) error {
614
637
}
615
638
616
639
func (c * client ) WorkspaceNewFromExistingStateFile (envName string , localStateFilePath string ) error {
640
+ if c .model .BackendType == "http" {
641
+ return nil
642
+ }
617
643
cmd , err := c .terraformCmd ([]string {
618
644
"workspace" ,
619
645
"new" ,
@@ -644,7 +670,7 @@ func (c *client) WorkspaceNewFromExistingStateFile(envName string, localStateFil
644
670
}
645
671
646
672
func (c * client ) WorkspaceDelete (envName string ) error {
647
- if envName == defaultWorkspace {
673
+ if envName == defaultWorkspace || c . model . BackendType == "http" {
648
674
return nil
649
675
}
650
676
@@ -667,7 +693,7 @@ func (c *client) WorkspaceDelete(envName string) error {
667
693
}
668
694
669
695
func (c * client ) WorkspaceDeleteWithForce (envName string ) error {
670
- if envName == defaultWorkspace {
696
+ if envName == defaultWorkspace || c . model . BackendType == "http" {
671
697
return nil
672
698
}
673
699
@@ -691,6 +717,9 @@ func (c *client) WorkspaceDeleteWithForce(envName string) error {
691
717
}
692
718
693
719
func (c * client ) StatePull (envName string ) ([]byte , error ) {
720
+ if c .model .BackendType == "http" {
721
+ envName = defaultWorkspace
722
+ }
694
723
cmd , err := c .terraformCmd ([]string {
695
724
"state" ,
696
725
"pull" ,
@@ -762,6 +791,7 @@ func (c *client) SavePlanToBackend(planEnvName string) error {
762
791
}
763
792
origSource := c .model .Source
764
793
origLogger := c .logWriter
794
+ origBackendConfig := c .model .BackendConfig
765
795
766
796
err = os .Chdir (tmpDir )
767
797
if err != nil {
@@ -781,6 +811,7 @@ func (c *client) SavePlanToBackend(planEnvName string) error {
781
811
os .Chdir (origDir )
782
812
c .model .Source = origSource
783
813
c .logWriter = origLogger
814
+ c .model .BackendConfig = origBackendConfig
784
815
}()
785
816
786
817
// The /tmp/tf-plan.log file can contain credentials, so we tell the user to
@@ -791,6 +822,15 @@ func (c *client) SavePlanToBackend(planEnvName string) error {
791
822
return fmt .Errorf (errPrefix , logPath , err )
792
823
}
793
824
825
+ // Override the backendConfig for HTTP type in order to target a different "-plan"
826
+ if c .model .BackendType == "http" {
827
+ planAddress , err := helper .PlanAddressForHTTPBackend (c .model .BackendConfig )
828
+ if err != nil {
829
+ return err
830
+ }
831
+ c .model .BackendConfig ["address" ] = planAddress
832
+ }
833
+
794
834
err = c .InitWithBackend ()
795
835
if err != nil {
796
836
return fmt .Errorf (errPrefix , logPath , err )
@@ -810,6 +850,46 @@ func (c *client) SavePlanToBackend(planEnvName string) error {
810
850
}
811
851
812
852
func (c * client ) GetPlanFromBackend (planEnvName string ) error {
853
+ tmpDir , err := ioutil .TempDir ("" , "tf-resource-plan" )
854
+ if err != nil {
855
+ return err
856
+ }
857
+ defer os .RemoveAll (tmpDir )
858
+
859
+ // TODO: this stateful set and reset isn't great
860
+ origDir , err := os .Getwd ()
861
+ if err != nil {
862
+ return err
863
+ }
864
+ origSource := c .model .Source
865
+ origBackendConfig := c .model .BackendConfig
866
+
867
+ err = os .Chdir (tmpDir )
868
+ if err != nil {
869
+ return err
870
+ }
871
+ c .model .Source = tmpDir
872
+ defer func () {
873
+ os .Chdir (origDir )
874
+ c .model .Source = origSource
875
+ c .model .BackendConfig = origBackendConfig
876
+ }()
877
+ // Override the backendConfig for HTTP type in order to target a different "-plan"
878
+ if c .model .BackendType == "http" {
879
+ planAddress , err := helper .PlanAddressForHTTPBackend (c .model .BackendConfig )
880
+ if err != nil {
881
+ return err
882
+ }
883
+ c .model .BackendConfig ["address" ] = planAddress
884
+ }
885
+
886
+ // Run again the init as we change directory.
887
+ // This should init to get the tfplan, then let the regular workflow get back on the previously init dir due to the defer func()
888
+ err = c .InitWithBackend ()
889
+ if err != nil {
890
+ return fmt .Errorf ("init failed %s" , err )
891
+ }
892
+
813
893
if err := c .WorkspaceSelect (planEnvName ); err != nil {
814
894
return err
815
895
}
@@ -843,6 +923,9 @@ func (c *client) SetModel(model models.Terraform) {
843
923
}
844
924
845
925
func (c * client ) resourceExists (tfID string , envName string ) (bool , error ) {
926
+ if c .model .BackendType == "http" {
927
+ envName = defaultWorkspace
928
+ }
846
929
cmd , err := c .terraformCmd ([]string {
847
930
"state" ,
848
931
"list" ,
0 commit comments