This repository has been archived by the owner on Apr 3, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
/
client.go
141 lines (119 loc) · 3.33 KB
/
client.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
package config
import (
"encoding/json"
"fmt"
"github.com/EngineerBetter/concourse-up/aws"
)
const terraformStateFileName = "terraform.tfstate"
const configFilePath = "config.json"
// IClient is an interface for the config file client
type IClient interface {
Load() (*Config, error)
DeleteAll(config *Config) error
LoadOrCreate(deployArgs *DeployArgs) (*Config, bool, error)
Update(*Config) error
StoreAsset(filename string, contents []byte) error
HasAsset(filename string) (bool, error)
LoadAsset(filename string) ([]byte, error)
DeleteAsset(filename string) error
}
// Client is a client for loading the config file from S3
type Client struct {
Project string
S3Region string
}
// StoreAsset stores an associated configuration file
func (client *Client) StoreAsset(filename string, contents []byte) error {
return aws.WriteFile(client.configBucket(),
filename,
client.S3Region,
contents,
)
}
// LoadAsset loads an associated configuration file
func (client *Client) LoadAsset(filename string) ([]byte, error) {
return aws.LoadFile(
client.configBucket(),
filename,
client.S3Region,
)
}
// DeleteAsset deletes an associated configuration file
func (client *Client) DeleteAsset(filename string) error {
return aws.DeleteFile(
client.configBucket(),
filename,
client.S3Region,
)
}
// HasAsset returns true if an associated configuration file exists
func (client *Client) HasAsset(filename string) (bool, error) {
return aws.HasFile(
client.configBucket(),
filename,
client.S3Region,
)
}
// Update stores the conconcourse up config file to S3
func (client *Client) Update(config *Config) error {
bytes, err := json.Marshal(config)
if err != nil {
return err
}
return aws.WriteFile(client.configBucket(), configFilePath, client.S3Region, bytes)
}
// DeleteAll deletes the entire configuration bucket
func (client *Client) DeleteAll(config *Config) error {
return aws.DeleteVersionedBucket(config.ConfigBucket, client.S3Region)
}
// Load loads an existing config file from S3
func (client *Client) Load() (*Config, error) {
configBytes, err := aws.LoadFile(
client.configBucket(),
configFilePath,
client.S3Region,
)
if err != nil {
return nil, err
}
conf := Config{}
if err := json.Unmarshal(configBytes, &conf); err != nil {
return nil, err
}
return &conf, nil
}
// LoadOrCreate loads an existing config file from S3, or creates a default if one doesn't already exist
func (client *Client) LoadOrCreate(deployArgs *DeployArgs) (*Config, bool, error) {
defaultConfigBytes, err := generateDefaultConfig(
client.Project,
client.deployment(),
client.configBucket(),
deployArgs.AWSRegion,
)
if err != nil {
return nil, false, err
}
if err = aws.EnsureBucketExists(client.configBucket(), client.S3Region); err != nil {
return nil, false, err
}
configBytes, createdNewFile, err := aws.EnsureFileExists(
client.configBucket(),
configFilePath,
client.S3Region,
defaultConfigBytes,
)
if err != nil {
return nil, false, err
}
conf := Config{}
if err := json.Unmarshal(configBytes, &conf); err != nil {
return nil, false, err
}
return &conf, createdNewFile, nil
}
func (client *Client) deployment() string {
return fmt.Sprintf("concourse-up-%s", client.Project)
}
func (client *Client) configBucket() string {
return fmt.Sprintf("%s-%s-config", client.deployment(), client.S3Region)
}