2015-02-06 22:31:59 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2015-05-27 20:10:43 +02:00
|
|
|
"encoding/json"
|
2015-02-06 22:31:59 +01:00
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
2015-12-15 05:30:23 +01:00
|
|
|
"sort"
|
2018-09-20 16:37:44 +02:00
|
|
|
"strings"
|
2015-02-06 22:31:59 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type state struct {
|
|
|
|
Modules []moduleState `json:"modules"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// read populates the state object from a statefile.
|
|
|
|
func (s *state) read(stateFile io.Reader) error {
|
|
|
|
|
|
|
|
// read statefile contents
|
|
|
|
b, err := ioutil.ReadAll(stateFile)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// parse into struct
|
|
|
|
err = json.Unmarshal(b, s)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-02-16 12:12:49 +01:00
|
|
|
// outputs returns a slice of the Outputs found in the statefile.
|
|
|
|
func (s *state) outputs() []*Output {
|
|
|
|
inst := make([]*Output, 0)
|
|
|
|
|
|
|
|
for _, m := range s.Modules {
|
|
|
|
for k, v := range m.Outputs {
|
2016-09-07 05:05:30 +02:00
|
|
|
var o *Output
|
2016-08-19 16:43:14 +02:00
|
|
|
switch v := v.(type) {
|
|
|
|
case map[string]interface{}:
|
2016-09-07 05:05:30 +02:00
|
|
|
o, _ = NewOutput(k, v["value"])
|
2016-08-19 16:43:14 +02:00
|
|
|
case string:
|
|
|
|
o, _ = NewOutput(k, v)
|
|
|
|
default:
|
|
|
|
o, _ = NewOutput(k, "<error>")
|
|
|
|
}
|
|
|
|
|
2016-02-16 12:12:49 +01:00
|
|
|
inst = append(inst, o)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return inst
|
|
|
|
}
|
|
|
|
|
2018-09-20 16:37:44 +02:00
|
|
|
// map of resource ID -> resource Name
|
|
|
|
func (s *state) mapResourceIDNames() map[string]string {
|
|
|
|
t := map[string]string{}
|
|
|
|
|
|
|
|
for _, m := range s.Modules {
|
|
|
|
for _, k := range m.resourceKeys() {
|
|
|
|
if m.ResourceStates[k].Primary.ID != "" && m.ResourceStates[k].Primary.Attributes["name"] != "" {
|
|
|
|
kk := strings.ToLower(m.ResourceStates[k].Primary.ID)
|
|
|
|
t[kk] = m.ResourceStates[k].Primary.Attributes["name"]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
2015-12-15 04:24:41 +01:00
|
|
|
// resources returns a slice of the Resources found in the statefile.
|
|
|
|
func (s *state) resources() []*Resource {
|
|
|
|
inst := make([]*Resource, 0)
|
2015-02-06 22:31:59 +01:00
|
|
|
|
|
|
|
for _, m := range s.Modules {
|
2015-12-15 05:30:23 +01:00
|
|
|
for _, k := range m.resourceKeys() {
|
2015-12-15 04:24:41 +01:00
|
|
|
// Terraform stores resources in a name->map map, but we need the name to
|
|
|
|
// decide which groups to include the resource in. So wrap it in a higher-
|
|
|
|
// level object with both properties.
|
2015-12-15 05:30:23 +01:00
|
|
|
r, err := NewResource(k, m.ResourceStates[k])
|
2015-12-15 04:24:41 +01:00
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if r.IsSupported() {
|
|
|
|
inst = append(inst, r)
|
2015-02-06 22:31:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return inst
|
|
|
|
}
|
|
|
|
|
|
|
|
type moduleState struct {
|
2015-12-15 04:24:41 +01:00
|
|
|
ResourceStates map[string]resourceState `json:"resources"`
|
2016-08-19 16:43:14 +02:00
|
|
|
Outputs map[string]interface{} `json:"outputs"`
|
2015-02-06 22:31:59 +01:00
|
|
|
}
|
|
|
|
|
2015-12-15 05:30:23 +01:00
|
|
|
// resourceKeys returns a sorted slice of the key names of the resources in this
|
|
|
|
// module. Do this instead of range over ResourceStates, to ensure that the
|
|
|
|
// output is consistent.
|
|
|
|
func (ms *moduleState) resourceKeys() []string {
|
|
|
|
lk := len(ms.ResourceStates)
|
|
|
|
keys := make([]string, lk, lk)
|
|
|
|
i := 0
|
|
|
|
|
|
|
|
for k := range ms.ResourceStates {
|
|
|
|
keys[i] = k
|
|
|
|
i += 1
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Strings(keys)
|
|
|
|
return keys
|
|
|
|
}
|
|
|
|
|
2015-02-06 22:31:59 +01:00
|
|
|
type resourceState struct {
|
2015-12-10 04:52:47 +01:00
|
|
|
|
|
|
|
// Populated from statefile
|
2015-02-06 22:31:59 +01:00
|
|
|
Type string `json:"type"`
|
|
|
|
Primary instanceState `json:"primary"`
|
2015-06-05 04:43:56 +02:00
|
|
|
}
|
|
|
|
|
2015-02-06 22:31:59 +01:00
|
|
|
type instanceState struct {
|
|
|
|
ID string `json:"id"`
|
|
|
|
Attributes map[string]string `json:"attributes,omitempty"`
|
|
|
|
}
|