/
set.go
67 lines (58 loc) · 1.54 KB
/
set.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
package openstack
import (
"encoding/json"
"io/ioutil"
)
type Set []Resource
func NewSet(tf_file_name string) (set Set) {
set.Init(tf_file_name)
return set
}
func (set *Set) Init(tf_file_name string) {
var tfstate map[string]interface{}
content, _ := ioutil.ReadFile(tf_file_name)
_ = json.Unmarshal(content, &tfstate)
*set = append(*set, parse_tfstate(tfstate)...)
}
func parse_tfstate(tfstate map[string]interface{}) (set Set) {
modules := tfstate["modules"].([]interface{})
for _, module := range modules {
osr_list := parse_module(module.(map[string]interface{}))
set = append(set, osr_list...)
}
return set
}
func parse_module(module map[string]interface{}) (osr_list []Resource) {
resources := module["resources"].(map[string]interface{})
for _, res := range resources {
osr := parse_resource(res.(map[string]interface{}))
osr_list = append(osr_list, osr)
}
return osr_list
}
func parse_resource(resource map[string]interface{}) (osr Resource) {
osr.Init()
osr.os_type = resource["type"].(string)
res_primary := resource["primary"].(map[string]interface{})
res_attr := res_primary["attributes"].(map[string]interface{})
for k, v := range res_attr {
osr.attributes[k] = v.(string)
}
return osr
}
func (set Set) GetComputes() (computes Set) {
for _, osr := range set {
if osr.IsCompute() {
computes = append(computes, osr)
}
}
return computes
}
func (set Set) GetFloatingAssoc() (floatings Set) {
for _, osr := range set {
if osr.IsFloatingAssoc() {
floatings = append(floatings, osr)
}
}
return floatings
}