-
Notifications
You must be signed in to change notification settings - Fork 141
/
terraformdiff.go
119 lines (104 loc) · 3.73 KB
/
terraformdiff.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 2023 The PipeCD Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package planpreview
import (
"bytes"
"context"
"fmt"
"io"
"github.com/pipe-cd/pipecd/pkg/app/piped/deploysource"
terraformprovider "github.com/pipe-cd/pipecd/pkg/app/piped/platformprovider/terraform"
"github.com/pipe-cd/pipecd/pkg/app/piped/toolregistry"
"github.com/pipe-cd/pipecd/pkg/model"
)
func (b *builder) terraformDiff(
ctx context.Context,
app *model.Application,
targetDSP deploysource.Provider,
buf *bytes.Buffer,
) (*diffResult, error) {
cp, ok := b.pipedCfg.FindPlatformProvider(app.PlatformProvider, model.ApplicationKind_TERRAFORM)
if !ok {
err := fmt.Errorf("platform provider %s was not found in Piped config", app.PlatformProvider)
fmt.Fprintln(buf, err.Error())
return nil, err
}
cpCfg := cp.TerraformConfig
ds, err := targetDSP.Get(ctx, io.Discard)
if err != nil {
fmt.Fprintf(buf, "failed to prepare deploy source data at the head commit (%v)\n", err)
return nil, err
}
appCfg := ds.ApplicationConfig.TerraformApplicationSpec
if appCfg == nil {
err := fmt.Errorf("missing Terraform spec field in application configuration")
fmt.Fprintln(buf, err.Error())
return nil, err
}
version := appCfg.Input.TerraformVersion
terraformPath, installed, err := toolregistry.DefaultRegistry().Terraform(ctx, version)
if err != nil {
fmt.Fprintf(buf, "unable to find the specified terraform version %q (%v)\n", version, err)
return nil, err
}
if installed {
b.logger.Info(fmt.Sprintf("terraform %q has just been installed to %q because of no pre-installed binary for that version", version, terraformPath))
}
vars := make([]string, 0, len(cpCfg.Vars)+len(appCfg.Input.Vars))
vars = append(vars, cpCfg.Vars...)
vars = append(vars, appCfg.Input.Vars...)
flags := appCfg.Input.CommandFlags
envs := appCfg.Input.CommandEnvs
executor := terraformprovider.NewTerraform(
terraformPath,
ds.AppDir,
terraformprovider.WithoutColor(),
terraformprovider.WithVars(vars),
terraformprovider.WithVarFiles(appCfg.Input.VarFiles),
terraformprovider.WithAdditionalFlags(flags.Shared, flags.Init, flags.Plan, flags.Apply),
terraformprovider.WithAdditionalEnvs(envs.Shared, envs.Init, envs.Plan, envs.Apply),
)
if err := executor.Init(ctx, buf); err != nil {
fmt.Fprintf(buf, "failed while executing terraform init (%v)\n", err)
return nil, err
}
if ws := appCfg.Input.Workspace; ws != "" {
if err := executor.SelectWorkspace(ctx, ws); err != nil {
fmt.Fprintf(buf, "failed to select workspace %q (%v). You might need to create the workspace before using by command %q\n",
ws,
err,
"terraform workspace new "+ws,
)
return nil, err
}
fmt.Fprintf(buf, "selected workspace %q\n", ws)
}
result, err := executor.Plan(ctx, buf)
if err != nil {
fmt.Fprintf(buf, "failed while executing terraform plan (%v)\n", err)
return nil, err
}
if result.NoChanges() {
fmt.Fprintln(buf, "No changes were detected")
return &diffResult{
summary: "No changes were detected",
noChange: true,
}, nil
}
summary := fmt.Sprintf("%d to import, %d to add, %d to change, %d to destroy", result.Imports, result.Adds, result.Changes, result.Destroys)
fmt.Fprintln(buf, summary)
return &diffResult{
summary: summary,
}, nil
}