forked from mercari/hcledit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_test.go
88 lines (74 loc) · 1.86 KB
/
example_test.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
package hcledit_test
import (
"fmt"
"strings"
"go.mercari.io/hcledit"
)
func Example() {
src := `
resource "google_container_node_pool" "nodes1" {
name = "nodes1"
node_config {
preemptible = false
machine_type = "e2-medium"
}
timeouts {
create = "30m"
}
}
resource "google_container_node_pool" "nodes2" {
name = "nodes2"
node_config {
preemptible = false
machine_type = "e2-medium"
}
timeouts {
create = "30m"
}
}
`
// Read HCL contents.
editor, _ := hcledit.Read(strings.NewReader(src), "")
// Create new attribute on the existing block.
editor.Create("resource.google_container_node_pool.*.node_config.disk_size_gb", "200")
// Create new block and add some attributes.
editor.Create("resource.google_container_node_pool.*.master_auth", hcledit.BlockVal())
editor.Create("resource.google_container_node_pool.*.master_auth.username", "")
editor.Create("resource.google_container_node_pool.*.master_auth.password", "")
// Update existing attributes.
editor.Update("resource.google_container_node_pool.*.node_config.machine_type", "COS")
editor.Update("resource.google_container_node_pool.*.node_config.preemptible", true)
// Delete existing attribute and blocks
editor.Delete("resource.google_container_node_pool.*.timeouts")
fmt.Printf("%s", editor.Bytes())
// Output:
// resource "google_container_node_pool" "nodes1" {
// name = "nodes1"
//
// node_config {
// preemptible = true
// machine_type = "COS"
// disk_size_gb = "200"
// }
//
// master_auth {
// username = ""
// password = ""
// }
// }
//
// resource "google_container_node_pool" "nodes2" {
// name = "nodes2"
//
// node_config {
// preemptible = true
// machine_type = "COS"
// disk_size_gb = "200"
// }
//
// master_auth {
// username = ""
// password = ""
// }
// }
}