2015-02-06 22:31:59 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2015-05-27 20:10:43 +02:00
|
|
|
"encoding/json"
|
2015-12-10 04:52:47 +01:00
|
|
|
"fmt"
|
2015-02-06 22:31:59 +01:00
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
2015-06-05 04:43:56 +02:00
|
|
|
"regexp"
|
2015-12-10 04:52:47 +01:00
|
|
|
"strconv"
|
2015-02-06 22:31:59 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type state struct {
|
|
|
|
Modules []moduleState `json:"modules"`
|
|
|
|
}
|
|
|
|
|
2015-06-05 06:03:59 +02:00
|
|
|
// keyNames contains the names of the keys to check for in each resource in the
|
|
|
|
// state file. This allows us to support multiple types of resource without too
|
|
|
|
// much fuss.
|
|
|
|
var keyNames []string
|
2015-12-10 04:52:47 +01:00
|
|
|
var nameParser *regexp.Regexp
|
2015-06-05 06:03:59 +02:00
|
|
|
|
|
|
|
func init() {
|
|
|
|
keyNames = []string{
|
|
|
|
"ipv4_address", // DO
|
2015-06-05 06:22:08 +02:00
|
|
|
"public_ip", // AWS
|
|
|
|
"private_ip", // AWS
|
2015-06-23 19:39:56 +02:00
|
|
|
"ipaddress", // CS
|
2015-08-10 13:05:47 +02:00
|
|
|
"ip_address", // VMware
|
2015-07-31 21:15:15 +02:00
|
|
|
"access_ip_v4", // OPENSTACK
|
2015-06-05 06:03:59 +02:00
|
|
|
}
|
2015-12-10 04:52:47 +01:00
|
|
|
|
|
|
|
// type.name.0
|
|
|
|
nameParser = regexp.MustCompile(`^(\w+)\.([\w\-]+)(?:\.(\d+))?$`)
|
2015-06-05 06:03:59 +02:00
|
|
|
}
|
|
|
|
|
2015-02-06 22:31:59 +01:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2015-06-05 04:43:56 +02:00
|
|
|
// resources returns a map of name to resourceState, for any supported resources
|
|
|
|
// found in the statefile.
|
|
|
|
func (s *state) resources() map[string]resourceState {
|
|
|
|
inst := make(map[string]resourceState)
|
2015-02-06 22:31:59 +01:00
|
|
|
|
|
|
|
for _, m := range s.Modules {
|
|
|
|
for k, r := range m.Resources {
|
2015-06-05 04:43:56 +02:00
|
|
|
if r.isSupported() {
|
2015-12-10 04:52:47 +01:00
|
|
|
|
|
|
|
_, name, counter := parseName(k)
|
|
|
|
//fmt.Println(resType, name, counter)
|
|
|
|
r.Name = name
|
|
|
|
r.Counter = counter
|
|
|
|
inst[k] = r
|
2015-02-06 22:31:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return inst
|
|
|
|
}
|
|
|
|
|
2015-12-10 04:52:47 +01:00
|
|
|
func parseName(name string) (string, string, int) {
|
|
|
|
m := nameParser.FindStringSubmatch(name)
|
|
|
|
|
|
|
|
// This should not happen unless our regex changes.
|
|
|
|
// TODO: Warn instead of silently ignore error?
|
|
|
|
if len(m) != 4 {
|
|
|
|
//fmt.Printf("len=%d\n", len(m))
|
|
|
|
return "", "", 0
|
|
|
|
}
|
|
|
|
|
|
|
|
var c int
|
|
|
|
var err error
|
|
|
|
if m[3] != "" {
|
|
|
|
c, err = strconv.Atoi(m[3])
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("err: %s\n", err)
|
|
|
|
// ???
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return m[1], m[2], c
|
|
|
|
}
|
|
|
|
|
2015-02-06 22:31:59 +01:00
|
|
|
type moduleState struct {
|
|
|
|
Resources map[string]resourceState `json:"resources"`
|
|
|
|
}
|
|
|
|
|
|
|
|
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-12-10 04:52:47 +01:00
|
|
|
|
|
|
|
// Extracted from key name, and injected by resources method
|
|
|
|
Name string
|
|
|
|
Counter int
|
2015-02-06 22:31:59 +01:00
|
|
|
}
|
|
|
|
|
2015-06-05 04:43:56 +02:00
|
|
|
// isSupported returns true if terraform-inventory supports this resource.
|
2015-06-05 06:03:59 +02:00
|
|
|
func (s resourceState) isSupported() bool {
|
2015-06-05 04:43:56 +02:00
|
|
|
return s.Address() != ""
|
|
|
|
}
|
|
|
|
|
2015-12-10 04:52:47 +01:00
|
|
|
// NameWithCounter returns the resource name with its counter. For resources
|
|
|
|
// created without a 'count=' attribute, this will always be zero.
|
|
|
|
func (s resourceState) NameWithCounter() string {
|
|
|
|
return fmt.Sprintf("%s.%d", s.Name, s.Counter)
|
|
|
|
}
|
|
|
|
|
2015-06-05 04:43:56 +02:00
|
|
|
// Address returns the IP address of this resource.
|
2015-06-05 06:03:59 +02:00
|
|
|
func (s resourceState) Address() string {
|
2015-06-05 06:22:08 +02:00
|
|
|
for _, key := range keyNames {
|
2015-06-05 06:03:59 +02:00
|
|
|
if ip := s.Primary.Attributes[key]; ip != "" {
|
|
|
|
return ip
|
|
|
|
}
|
2015-06-05 04:43:56 +02:00
|
|
|
}
|
2015-06-05 06:03:59 +02:00
|
|
|
|
|
|
|
return ""
|
2015-06-05 04:43:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Attributes returns a map containing everything we know about this resource.
|
2015-06-05 06:03:59 +02:00
|
|
|
func (s resourceState) Attributes() map[string]string {
|
2015-06-05 04:43:56 +02:00
|
|
|
return s.Primary.Attributes
|
|
|
|
}
|
|
|
|
|
2015-02-06 22:31:59 +01:00
|
|
|
type instanceState struct {
|
|
|
|
ID string `json:"id"`
|
|
|
|
Attributes map[string]string `json:"attributes,omitempty"`
|
|
|
|
}
|