/
gcs_backend.go
98 lines (78 loc) · 3.1 KB
/
gcs_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
package terraformworkspace
import (
"context"
"fmt"
"io"
"os"
"cloud.google.com/go/storage"
"github.com/sirupsen/logrus"
"google.golang.org/api/option"
"github.com/dragondrop-cloud/cloud-concierge/main/internal/interfaces"
)
// GCSBackend is an implementation of the interfaces.TerraformWorkspace interface that uses GCS as a backend.
type GCSBackend struct {
// config is the configuration for the Azure Blob Storage backend.
config TfStackConfig
// workspaceToBackendDetails is a map of Terraform workspace names to their respective backend details.
workspaceToBackendDetails map[string]interface{}
}
// FindTerraformWorkspaces returns a map of Terraform workspace names to their respective directories.
func (b *GCSBackend) FindTerraformWorkspaces(ctx context.Context) (map[string]string, error) {
logrus.Debugf("[GCS Terraform workspace] Finding Terraform workspaces in %v", b.config.WorkspaceDirectories)
workspaces, workspaceToBackendDetails, err := findTerraformWorkspaces(ctx, b.config.WorkspaceDirectories, "gcs")
if err != nil {
return nil, err
}
b.workspaceToBackendDetails = workspaceToBackendDetails
return workspaces, err
}
// NewGCSBackend creates a new GCSBackend instance.
func NewGCSBackend(ctx context.Context, config TfStackConfig) interfaces.TerraformWorkspace {
return &GCSBackend{config: config}
}
// DownloadWorkspaceState downloads from the remote Azure Blob Storage backend the latest state file
func (b *GCSBackend) DownloadWorkspaceState(ctx context.Context, workspaceToDirectory map[string]string) error {
logrus.Debugf("[GCS Terraform workspace] Downloading workspace state files for %v", workspaceToDirectory)
for workspaceName := range workspaceToDirectory {
err := b.getWorkspaceStateByTestingAllGoogleCredentials(ctx, workspaceName)
if err == nil {
break
}
}
return nil
}
// getWorkspaceStateByTestingAllGoogleCredentials attempts to download the state file for the given workspace using all
func (b *GCSBackend) getWorkspaceStateByTestingAllGoogleCredentials(ctx context.Context, workspaceName string) error {
stateFileName := fmt.Sprintf("%v.json", workspaceName)
fileOutPath := fmt.Sprintf("state_files/%v", stateFileName)
outFile, err := os.Create(fileOutPath)
if err != nil {
return fmt.Errorf("[os.Create] %v", err)
}
client, err := storage.NewClient(ctx, option.WithCredentialsJSON([]byte(b.config.CloudCredential)))
if err != nil {
return fmt.Errorf("[storage.NewClient] %v", err)
}
gcsBackendDetails := b.workspaceToBackendDetails[workspaceName].(GCSBackendBlock)
bucket := client.Bucket(gcsBackendDetails.Bucket)
rc, err := bucket.Object(stateFileName).NewReader(ctx)
if err != nil {
err = outFileCloser(outFile)
if err != nil {
return fmt.Errorf("[bucket.Object().NewReader][outFileCloser]%v", err)
}
return fmt.Errorf("[bucket.Object().NewReader] %v", err)
}
defer rc.Close()
if _, err = io.Copy(outFile, rc); err != nil {
err = outFileCloser(outFile)
if err != nil {
return fmt.Errorf("[io.Copy][outFileCloser]%v", err)
}
}
err = outFileCloser(outFile)
if err != nil {
return fmt.Errorf("[outFileCloser] %v", err)
}
return nil
}