-
Notifications
You must be signed in to change notification settings - Fork 7
/
state_v011.go
222 lines (181 loc) · 4.75 KB
/
state_v011.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
package main
import (
"fmt"
"sort"
"strings"
)
// The following structs are for Terraform State
// from version v0.11 and prior.
type StateV011 struct {
Modules []ModuleV011 `json:"modules"`
}
// GetGroups will return all ansible_group resources.
func (r StateV011) GetGroups() ([]string, error) {
var groups []string
for _, m := range r.Modules {
for _, resource := range m.Resources {
if resource.Type == "ansible_group" {
groups = append(groups, resource.Primary.ID)
}
}
}
sort.Strings(groups)
return groups, nil
}
// GetHosts will return all ansible_group resources.
func (r StateV011) GetHosts() ([]string, error) {
var hosts []string
for _, m := range r.Modules {
for _, resource := range m.Resources {
if resource.Type == "ansible_host" {
hosts = append(hosts, resource.Primary.ID)
}
}
}
sort.Strings(hosts)
return hosts, nil
}
// GetGroup will find and return a specific ansible_group resource.
func (r StateV011) GetGroup(group string) (interface{}, error) {
for _, m := range r.Modules {
for _, resource := range m.Resources {
if resource.Type == "ansible_group" {
if resource.Primary.ID == group {
return resource, nil
}
}
}
}
return nil, fmt.Errorf("Unable to find group %s", group)
}
// GetChildrenForGroup will return the "children" members of an
// ansible_group resource.
func (r StateV011) GetChildrenForGroup(group string) ([]string, error) {
var children []string
var resource ResourceV011
v, err := r.GetGroup(group)
if err != nil {
return nil, err
}
resource = v.(ResourceV011)
for attrName, attr := range resource.Primary.Attributes {
if strings.HasPrefix(attrName, "children.") {
if attrName == "children.#" {
continue
}
children = append(children, attr)
}
}
sort.Strings(children)
return children, nil
}
// GetVarsForGroup will return the variables defined in an ansible_group
// resource.
func (r StateV011) GetVarsForGroup(group string) (map[string]interface{}, error) {
var resource ResourceV011
vars := make(map[string]interface{})
v, err := r.GetGroup(group)
if err != nil {
return nil, err
}
resource = v.(ResourceV011)
for attrName, attr := range resource.Primary.Attributes {
if strings.HasPrefix(attrName, "vars.") {
if attrName == "vars.%" {
continue
}
pieces := strings.SplitN(attrName, ".", 2)
if len(pieces) == 2 {
vars[pieces[1]] = attr
}
}
}
return vars, nil
}
// GetHostsForGroup will return the hosts that belong to a defined group.
func (r StateV011) GetHostsForGroup(group string) ([]string, error) {
var hosts []string
for _, m := range r.Modules {
for _, resource := range m.Resources {
if resource.Type == "ansible_host" {
for attrName, attr := range resource.Primary.Attributes {
if strings.HasPrefix(attrName, "groups.") {
if group == attr {
hosts = append(hosts, resource.Primary.ID)
}
}
}
}
}
}
sort.Strings(hosts)
return hosts, nil
}
// GetHost will return a specific ansible_host.
func (r StateV011) GetHost(host string) (interface{}, error) {
for _, m := range r.Modules {
for _, resource := range m.Resources {
if resource.Type == "ansible_host" {
if resource.Primary.ID == host {
return resource, nil
}
}
}
}
return nil, fmt.Errorf("Unable to find host %s", host)
}
// GetGroupsForHost will return the groups defined in an ansible_host resource.
func (r StateV011) GetGroupsForHost(host string) ([]string, error) {
var resource ResourceV011
groups := []string{}
v, err := r.GetHost(host)
if err != nil {
return nil, err
}
resource = v.(ResourceV011)
for attrName, attr := range resource.Primary.Attributes {
if strings.HasPrefix(attrName, "groups.") {
if attrName == "groups.#" {
continue
}
pieces := strings.SplitN(attrName, ".", 2)
if len(pieces) == 2 {
groups = append(groups, attr)
}
}
}
return groups, nil
}
// GetVarsForHost will return the variables defined in an ansible_host resource.
func (r StateV011) GetVarsForHost(host string) (map[string]interface{}, error) {
var resource ResourceV011
vars := make(map[string]interface{})
v, err := r.GetHost(host)
if err != nil {
return nil, err
}
resource = v.(ResourceV011)
for attrName, attr := range resource.Primary.Attributes {
if strings.HasPrefix(attrName, "vars.") {
if attrName == "vars.%" {
continue
}
pieces := strings.SplitN(attrName, ".", 2)
if len(pieces) == 2 {
vars[pieces[1]] = attr
}
}
}
return vars, nil
}
type ModuleV011 struct {
Resources map[string]ResourceV011 `json:"resources"`
}
type ResourceV011 struct {
Type string `json:"type"`
Primary PrimaryV011 `json:"primary"`
}
type PrimaryV011 struct {
ID string `json:"id"`
Attributes map[string]string `json:"attributes"`
}