/
revision.go
302 lines (264 loc) · 8.71 KB
/
revision.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/*
* Copyright (C) 2023 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 convert
import (
"context"
"errors"
"fmt"
"path/filepath"
"strconv"
"strings"
"github.com/AlecAivazis/survey/v2"
"github.com/fatih/color"
"github.com/spf13/cobra"
"github.com/tidwall/sjson"
"k8s.io/apimachinery/pkg/runtime"
terraformv1alpha1 "github.com/appvia/terranetes-controller/pkg/apis/terraform/v1alpha1"
"github.com/appvia/terranetes-controller/pkg/cmd"
"github.com/appvia/terranetes-controller/pkg/utils"
"github.com/appvia/terranetes-controller/pkg/utils/kubernetes"
)
// RevisionCommand are the options for the command
type RevisionCommand struct {
cmd.Factory
// Name is the name of the resource
Name string
// Namespace is the namespace of the resource
Namespace string
// IncludeProvider is whether to include the provider in the output
IncludeProvider bool
// IncludeCheckov is whether to include checkov in the output
IncludeCheckov bool
// IncludeTerraform is whether to include terraform in the output
IncludeTerraform bool
// Directory is the path to write the files to
Directory string
// File is the path to the file to containing the revision
File string
// Revision the revision we are converting
Revision *terraformv1alpha1.Revision
// Contexts is a list of contexts from the cluster
Contexts *terraformv1alpha1.ContextList
// Policies is a list of policies from the cluster
Policies *terraformv1alpha1.PolicyList
// Providers is a collection of providers in the cluster
Providers *terraformv1alpha1.ProviderList
// configuration is the generated configuration
configuration *terraformv1alpha1.Configuration
}
// NewRevisionCommand creates and returns a new command
func NewRevisionCommand(factory cmd.Factory) *cobra.Command {
o := &RevisionCommand{Factory: factory}
ccm := &cobra.Command{
Use: "revision [OPTIONS] [NAME|--file PATH]",
Short: "Used to convert revision back to terraform",
Long: strings.TrimPrefix(longDescription, "\n"),
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) > 0 {
o.Name = args[0]
}
return o.Run(cmd.Context())
},
}
ccm.SetErr(o.GetStreams().ErrOut)
ccm.SetIn(o.GetStreams().In)
ccm.SetOut(o.GetStreams().Out)
flags := ccm.Flags()
flags.BoolVar(&o.IncludeCheckov, "include-checkov", true, "Include checkov in the output")
flags.BoolVar(&o.IncludeProvider, "include-provider", true, "Include provider in the output")
flags.BoolVar(&o.IncludeTerraform, "include-terraform", true, "Include terraform in the output")
flags.StringVarP(&o.Directory, "path", "p", ".", "The path to write the files to")
flags.StringVarP(&o.File, "file", "f", "", "The path to the file containing the revision")
flags.StringVarP(&o.Namespace, "namespace", "n", "", "The namespace of the revision")
cmd.RegisterFlagCompletionFunc(ccm, "namespace", cmd.AutoCompleteNamespaces(factory))
return ccm
}
// Run implements the command
func (o *RevisionCommand) Run(ctx context.Context) error {
switch {
case o.File != "":
break
case o.Name != "" && o.Namespace != "":
break
case o.Revision != nil:
break
default:
return errors.New("either file or name and namespace must be provided")
}
// @step: retrieve the revision from file or kubernetes
if err := o.retrieveRevision(ctx); err != nil {
return err
}
// @step: retrieve the inputs
if err := o.retrieveInputs(ctx); err != nil {
return err
}
// @step: we first convert to a Configuration - and pass on to the configuration
// convert command to handle the rest
if err := o.renderConfiguration(ctx); err != nil {
return err
}
// @step: render a cloud resource file
if err := o.renderCloudResource(ctx); err != nil {
return err
}
return (&ConfigurationCommand{
Factory: o.Factory,
Configuration: o.configuration,
Contexts: o.Contexts,
Directory: o.Directory,
IncludeCheckov: o.IncludeCheckov,
IncludeProvider: o.IncludeProvider,
IncludeTerraform: o.IncludeTerraform,
Policies: o.Policies,
Providers: o.Providers,
}).Run(ctx)
}
// renderCloudResource is responsible for rendering the cloud resource
func (o *RevisionCommand) renderCloudResource(_ context.Context) error {
cr, err := terraformv1alpha1.NewCloudResourceFromRevision(o.Revision)
if err != nil {
return err
}
// @step: render the cloud resource
filename := filepath.Join(o.Directory, "cloudresource.yaml")
if err := utils.WriteYAML(filename, cr); err != nil {
return err
}
return nil
}
// retrieveInputs retrieves the inputs if the revision has user defined inputs
func (o *RevisionCommand) retrieveInputs(_ context.Context) error {
if len(o.Revision.Spec.Inputs) == 0 {
return nil
}
// @step: check if there are any complex types in the inputs as prompting for these
// via the CLI will be horrid; it better to ask them to edit and fill in
for i, input := range o.Revision.Spec.Inputs {
switch {
case input.Required == nil:
continue
case input.Default != nil:
continue
case input.Type == nil:
continue
}
switch *input.Type {
case "string", "number", "bool":
break
case "":
return fmt.Errorf("spec.inputs[%d].type is required (if a complex type and no default set, use an editor to define)", i)
default:
return fmt.Errorf("spec.inputs[%d] is a complex type, please use an editor to specify a default", i)
}
}
for i, input := range o.Revision.Spec.Inputs {
switch {
case input.Required == nil:
continue
case input.Default != nil:
continue
case input.Type == nil:
continue
case !utils.Contains(*input.Type, []string{"string", "number", "bool"}):
continue
}
// @step: else we need to ask the user
var selected string
if err := survey.AskOne(&survey.Input{
Message: fmt.Sprintf("Input %s is a mandatory input, what should it's value be?", color.YellowString(input.Key)),
Help: input.Description,
}, &selected, survey.WithValidator(survey.Required), survey.WithKeepFilter(false)); err != nil {
return err
}
switch *input.Type {
case "bool":
o.Revision.Spec.Inputs[i].Default = &runtime.RawExtension{
Raw: []byte(fmt.Sprintf(`{"value":%T}`, selected)),
}
case "number":
number, err := strconv.ParseInt(selected, 10, 64)
if err != nil {
return err
}
o.Revision.Spec.Inputs[i].Default = &runtime.RawExtension{
Raw: []byte(fmt.Sprintf(`{"value":%d}`, number)),
}
case "string":
o.Revision.Spec.Inputs[i].Default = &runtime.RawExtension{
Raw: []byte(`{"value": "` + selected + `"}`),
}
}
}
return nil
}
// renderConfiguration renders the configuration from the revision
func (o *RevisionCommand) renderConfiguration(_ context.Context) error {
// @step: we first convert to a Configuration - and pass on to the configuration
// convert command to handle the rest
configuration := &terraformv1alpha1.Configuration{}
configuration.Name = o.Revision.Name
configuration.Namespace = "default"
configuration.Annotations = o.Revision.Annotations
configuration.Labels = o.Revision.Labels
configuration.Spec = o.Revision.Spec.Configuration
o.configuration = configuration
// @step: add this inputs if any
if o.Revision.Spec.Inputs == nil {
return nil
}
for _, input := range o.Revision.Spec.Inputs {
if input.Default == nil {
continue
}
value, found, err := o.Revision.Spec.GetInputDefaultValue(input.Key)
if err != nil {
return err
}
if !found {
continue
}
configuration.Spec.Variables.Raw, err = sjson.SetBytes(configuration.Spec.Variables.Raw, input.Key, value)
if err != nil {
return err
}
}
return nil
}
// retrieveRevision retrieves the revision from the file or kubernetes
func (o *RevisionCommand) retrieveRevision(ctx context.Context) error {
switch {
case o.Revision != nil:
return nil
case o.File != "":
o.Revision = &terraformv1alpha1.Revision{}
return utils.LoadYAML(o.File, o.Revision)
}
o.Revision = &terraformv1alpha1.Revision{}
o.Revision.Namespace = o.Namespace
o.Revision.Name = o.Name
cc, err := o.GetClient()
if err != nil {
return err
}
if found, err := kubernetes.GetIfExists(ctx, cc, o.Revision); err != nil {
return err
} else if !found {
return errors.New("revision not found")
}
return nil
}