forked from GoogleCloudPlatform/terraformer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplan.go
129 lines (110 loc) · 3.19 KB
/
plan.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
// Copyright 2018 The Terraformer 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 cmd
import (
"encoding/json"
"fmt"
"log"
"os"
"path/filepath"
"github.com/GoogleCloudPlatform/terraformer/terraformutils"
"github.com/spf13/cobra"
)
type ImportPlan struct {
Version string
Provider string
Options ImportOptions
Args []string
ImportedResource map[string][]terraformutils.Resource
}
func newPlanCmd() *cobra.Command {
options := ImportOptions{
Plan: true,
}
cmd := &cobra.Command{
Use: "plan",
Short: "Plan to import current state to Terraform configuration",
Long: "Plan to import current state to Terraform configuration",
SilenceUsage: true,
SilenceErrors: false,
//Version: version.String(),
}
for _, subcommand := range providerImporterSubcommands() {
cmd.AddCommand(subcommand(options))
}
return cmd
}
func newCmdPlanImporter(options ImportOptions) *cobra.Command {
cmd := &cobra.Command{
Use: "plan",
Short: "Import planned state to Terraform configuration",
Long: "Import planned state to Terraform configuration",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
plan, err := LoadPlanfile(args[0])
if err != nil {
return err
}
var provider terraformutils.ProviderGenerator
if providerGen, ok := providerGenerators()[plan.Provider]; ok {
provider = providerGen()
} else {
return fmt.Errorf("unsupported provider: %s", plan.Provider)
}
if err = provider.Init(plan.Args); err != nil {
return err
}
for _, service := range plan.Options.Resources {
if err = provider.InitService(service, options.Verbose); err != nil {
return err
}
}
return ImportFromPlan(provider, plan)
},
}
return cmd
}
func LoadPlanfile(path string) (*ImportPlan, error) {
f, err := os.Open(path)
if err != nil {
return nil, err
}
defer f.Close()
plan := &ImportPlan{}
dec := json.NewDecoder(f)
dec.DisallowUnknownFields()
if err := dec.Decode(plan); err != nil {
return nil, err
}
if plan.Version != version {
return nil, fmt.Errorf("planfile version did not match. expected: %s, actual: %s", version, plan.Version)
}
return plan, nil
}
func ExportPlanFile(plan *ImportPlan, path, filename string) error {
plan.Version = version
planfilePath := filepath.Join(path, filename)
log.Println("Saving planfile to", planfilePath)
if err := os.MkdirAll(path, os.ModePerm); err != nil {
return err
}
f, err := os.OpenFile(planfilePath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, os.ModePerm)
if err != nil {
return err
}
defer f.Close()
enc := json.NewEncoder(f)
enc.SetIndent("", "\t")
return enc.Encode(plan)
}