-
Notifications
You must be signed in to change notification settings - Fork 73
/
command_terraform.go
105 lines (87 loc) · 2.96 KB
/
command_terraform.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
package utils
import (
"fmt"
"io/ioutil"
"os"
"regexp"
"github.com/newrelic/newrelic-cli/internal/utils/terraform"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
var (
label string
file string
outFile string
shiftWidth int
snakeCaseRE = regexp.MustCompile("^[a-z][a-z0-9]+(_[a-z0-9]+)*$")
)
var cmdTerraform = &cobra.Command{
Use: "terraform",
Short: "Tools for working with Terraform",
Long: `Tools for working with Terraform
The terraform commands can be used for generating Terraform HCL for simple observability
as code use cases.
`,
Example: `cat terraform.json | newrelic utils terraform dashboard --label my_dashboard_resource`,
}
var cmdTerraformDashboard = &cobra.Command{
Use: "dashboard",
Short: "Generate HCL for the newrelic_one_dashboard resource",
Long: `Generate HCL for the newrelic_one_dashboard resource
This command generates HCL configuration for newrelic_one_dashboard resources from
exported JSON documents. For more detail on exporting dashboards to JSON, see
https://docs.newrelic.com/docs/query-your-data/explore-query-data/dashboards/manage-your-dashboard/#dash-json
Input can be sourced from STDIN per the provided example, or from a file using the --file option.
Output will be sent to STDOUT by default but can be redirected to a file with the --out option.
`,
Example: `cat terraform.json | newrelic utils terraform dashboard --label my_dashboard_resource`,
Args: func(cmd *cobra.Command, args []string) error {
if ok := snakeCaseRE.MatchString(label); !ok {
return fmt.Errorf("resource label must be formatted with snake case: %s", label)
}
if file != "" {
if _, err := os.Stat(file); os.IsNotExist(err) {
return fmt.Errorf("file not found: %s", file)
}
}
return nil
},
Run: func(cmd *cobra.Command, args []string) {
var input []byte
var err error
if file != "" {
input, err = ioutil.ReadFile(file)
if err != nil {
log.Fatal(err)
}
} else {
input, err = ioutil.ReadAll(os.Stdin)
if err != nil {
log.Fatal(err)
}
}
hcl, err := terraform.GenerateDashboardHCL(label, shiftWidth, input)
if err != nil {
log.Fatal(err)
}
if outFile != "" {
if err := ioutil.WriteFile(outFile, []byte(hcl), 0644); err != nil {
log.Fatal(err)
}
log.Info("success")
} else {
fmt.Print(hcl)
}
},
}
func init() {
Command.AddCommand(cmdTerraform)
cmdTerraform.AddCommand(cmdTerraformDashboard)
cmdTerraformDashboard.Flags().StringVarP(&label, "label", "l", "", "the resource label to use when generating resource HCL")
if err := cmdTerraformDashboard.MarkFlagRequired("label"); err != nil {
log.Error(err)
}
cmdTerraformDashboard.Flags().StringVarP(&file, "file", "f", "", "a file that contains exported dashboard JSON")
cmdTerraformDashboard.Flags().StringVarP(&outFile, "out", "o", "", "the file to send the generated HCL to")
cmdTerraformDashboard.Flags().IntVarP(&shiftWidth, "shiftWidth", "w", 2, "the indentation shift with of the output")
}