-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
remotestate.go
119 lines (106 loc) · 2.62 KB
/
remotestate.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
package infra
import (
"fmt"
"io/ioutil"
"os"
"strings"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/pkg/errors"
"github.com/spf13/viper"
)
// RemoteState is an action that verifies remote state is available
type RemoteState struct {
ID string
Name string
}
// Validate checks the remote state buckets and table exist
func (r *RemoteState) Validate() error {
if err := r.validateConfig(); err != nil {
fmt.Fprintf(os.Stdout, "[%s] The remote state backend is invalid!\n", r.Name)
return err
}
fmt.Fprintf(os.Stdout, "[%s] The remote state backend is valid\n", r.Name)
return nil
}
// Plan does nothing
func (r *RemoteState) Plan() error {
return nil
}
// Apply does nothing
func (r *RemoteState) Apply() error {
return nil
}
// Finalise does nothing
func (r *RemoteState) Finalise() error {
return nil
}
// Destroy does nothing
func (r *RemoteState) Destroy() error {
return nil
}
func (r *RemoteState) validateConfig() error {
stateStore := viper.GetString("state-store")
switch stateStore {
case "aws":
return r.validateAws()
case "azure":
return r.validateAzure()
default:
return errors.Errorf("%s is not a supported state store", stateStore)
}
}
func (r *RemoteState) validateAzure() error {
//TODO: meaningful validation
return nil
}
func (r *RemoteState) validateAws() error {
client := s3.New(
session.New(
&aws.Config{
Region: func() *string {
if r := os.Getenv("AWS_REGION"); len(r) != 0 {
return &r
}
return aws.String("eu-west-2")
}(),
},
),
)
if _, err := client.PutObject(
&s3.PutObjectInput{
Key: aws.String(r.ID),
Bucket: aws.String(viper.GetString("aws-s3-bucket")),
Body: strings.NewReader(r.ID),
},
); err != nil {
return errors.Wrap(err, "Could not put an object in to the remote state bucket")
}
read, err := client.GetObject(
&s3.GetObjectInput{
Key: aws.String(r.ID),
Bucket: aws.String(viper.GetString("aws-s3-bucket")),
},
)
if err != nil {
return errors.Wrap(err, "Could not read back an object from to the remote state bucket")
}
body, err := ioutil.ReadAll(read.Body)
read.Body.Close()
if err != nil {
return errors.Wrap(err, "Error reading body from S3")
}
if string(body) != r.ID {
return fmt.Errorf("Read back an invalid value from remote state. Expected %s, got %s", r.ID, string(body))
}
if _, err := client.DeleteObject(
&s3.DeleteObjectInput{
Key: aws.String(r.ID),
Bucket: aws.String(viper.GetString("aws-s3-bucket")),
},
); err != nil {
return errors.Wrap(err, "Error deleting object from S3")
}
return nil
}