/
state.go
119 lines (102 loc) · 2.83 KB
/
state.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
/*
* Copyright (C) 2022 Appvia Ltd <info@appvia.io>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package fixtures
import (
"bytes"
"compress/gzip"
v1 "k8s.io/api/core/v1"
terraformv1alpha1 "github.com/appvia/terranetes-controller/pkg/apis/terraform/v1alpha1"
)
var state = `
{
"terraform_version": "1.1.9",
"resources": [
{
"mode": "managed",
"type" : "aws_instance",
"instances": [
{
"id": "i-0f9c9f9c9f9c9f9c9",
"name": "foo"
}
]
}
],
"outputs": {
"test_output": {
"value": "test",
"type": "string"
}
}
}
`
var fakeCostsReport = `
{
"totalHourlyCost": "0.01",
"totalMonthlyCost": "100.00"
}
`
var fakeBackendTemplate = `
terraform {
backend "s3" {
bucket = "terranetes-controller-state"
key = "cluster_one/{{ .namespace }}/{{ .name }}"
region = "eu-west-2"
access_key = "AWS_ACCESS_KEY_ID"
secret_key = "AWS_SECRET_ACCESS_KEY"
}
}
`
// NewBackendTemplateSecret returns a fake backend template secret
func NewBackendTemplateSecret(namespace, name string) *v1.Secret {
secret := &v1.Secret{}
secret.Name = name
secret.Namespace = namespace
secret.Data = map[string][]byte{
"backend.tf": []byte(fakeBackendTemplate),
}
return secret
}
// NewCostsSecret returns a fake costs secret
func NewCostsSecret(namespace, name string) *v1.Secret {
secret := &v1.Secret{}
secret.Name = name
secret.Namespace = namespace
secret.Data = map[string][]byte{"INFRACOST_API_KEY": []byte("api-key")}
return secret
}
// NewCostsReport returns a secret used to mock a cost report for a configuration
func NewCostsReport(configuration *terraformv1alpha1.Configuration) *v1.Secret {
secret := &v1.Secret{}
secret.Name = configuration.GetTerraformCostSecretName()
secret.Data = map[string][]byte{"costs.json": []byte(fakeCostsReport)}
return secret
}
// NewTerraformState returns a fake state
func NewTerraformState(configuration *terraformv1alpha1.Configuration) *v1.Secret {
encoded := &bytes.Buffer{}
w := gzip.NewWriter(encoded)
//nolint:errcheck
w.Write([]byte(state))
w.Close()
secret := &v1.Secret{}
secret.Name = configuration.GetTerraformStateSecretName()
secret.Data = map[string][]byte{
terraformv1alpha1.TerraformStateSecretKey: encoded.Bytes(),
}
return secret
}