Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Showing Terraform outputs as "global" variables #30

Merged
merged 7 commits into from
Apr 9, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 37 additions & 30 deletions cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"io"
)

func gatherResources(s *state) map[string][]string {
groups := make(map[string][]string, 0)
func gatherResources(s *state) map[string]interface{} {
groups := make(map[string]interface{}, 0)
for _, res := range s.resources() {
for _, grp := range res.Groups() {

Expand All @@ -16,43 +16,50 @@ func gatherResources(s *state) map[string][]string {
groups[grp] = []string{}
}

groups[grp] = append(groups[grp], res.Address())
groups[grp] = append(groups[grp].([]string), res.Address())
}
}
return groups

if len(s.outputs()) > 0 {
groups["all"] = make(map[string]string, 0)
for _, out := range s.outputs() {
groups["all"].(map[string]string)[out.keyName] = out.value
}
}
return groups
}

func cmdList(stdout io.Writer, stderr io.Writer, s *state) int {
return output(stdout, stderr, gatherResources(s))
}

func cmdInventory(stdout io.Writer, stderr io.Writer, s *state) int {
groups := gatherResources(s)
for group, res := range groups {

_, err := io.WriteString(stdout, "["+group+"]\n")
if err != nil {
fmt.Fprintf(stderr, "Error writing Invetory: %s\n", err)
return 1;
}

for _, ress := range res {

_, err := io.WriteString(stdout, ress + "\n")
if err != nil {
fmt.Fprintf(stderr, "Error writing Invetory: %s\n", err)
return 1;
}
}

_, err = io.WriteString(stdout, "\n")
if err != nil {
fmt.Fprintf(stderr, "Error writing Invetory: %s\n", err)
return 1;
}
}

return 0;
groups := gatherResources(s)
for group, res := range groups {

_, err := io.WriteString(stdout, "["+group+"]\n")
if err != nil {
fmt.Fprintf(stderr, "Error writing Invetory: %s\n", err)
return 1
}

for _, ress := range res.([]string) {

_, err := io.WriteString(stdout, ress+"\n")
if err != nil {
fmt.Fprintf(stderr, "Error writing Invetory: %s\n", err)
return 1
}
}

_, err = io.WriteString(stdout, "\n")
if err != nil {
fmt.Fprintf(stderr, "Error writing Invetory: %s\n", err)
return 1
}
}

return 0
}

func cmdHost(stdout io.Writer, stderr io.Writer, s *state, hostname string) int {
Expand Down
4 changes: 3 additions & 1 deletion fixtures/example.tfstate
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
"path": [
"root"
],
"outputs": {},
"outputs": {
"datacenter": "mydc"
},
"resources": {
"aws_instance.alpha.0": {
"type": "aws_instance",
Expand Down
4 changes: 2 additions & 2 deletions fixtures/playbook.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
- hosts:
- web-aws
- web-do
- alpha
- beta
tasks:
- command: "echo Hello, world!"
25 changes: 25 additions & 0 deletions output.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package main

import (
"fmt"
)

type Output struct {

// The keyName and value of the output
keyName string
value string
}

func NewOutput(keyName string, value string) (*Output, error) {

// TODO: Warn instead of silently ignore error?
if len(keyName) == 0 {
return nil, fmt.Errorf("couldn't parse keyName: %s", keyName)
}

return &Output{
keyName: keyName,
value: value,
}, nil
}
16 changes: 15 additions & 1 deletion parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,20 @@ func (s *state) read(stateFile io.Reader) error {
return nil
}

// 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 {
o, _ := NewOutput(k, v)
inst = append(inst, o)
}
}

return inst
}

// resources returns a slice of the Resources found in the statefile.
func (s *state) resources() []*Resource {
inst := make([]*Resource, 0)
Expand All @@ -43,7 +57,6 @@ func (s *state) resources() []*Resource {
if err != nil {
continue
}

if r.IsSupported() {
inst = append(inst, r)
}
Expand All @@ -55,6 +68,7 @@ func (s *state) resources() []*Resource {

type moduleState struct {
ResourceStates map[string]resourceState `json:"resources"`
Outputs map[string]string `json:"outputs"`
}

// resourceKeys returns a sorted slice of the key names of the resources in this
Expand Down
5 changes: 4 additions & 1 deletion parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ const exampleStateFile = `
"path": [
"root"
],
"outputs": {},
"outputs": {
"datacenter": "mydc"
},
"resources": {
"aws_instance.one.0": {
"type": "aws_instance",
Expand Down Expand Up @@ -122,6 +124,7 @@ const exampleStateFile = `

const expectedListOutput = `
{
"all": {"datacenter": "mydc"},
"one": ["10.0.0.1", "10.0.1.1"],
"two": ["50.0.0.1"],
"three": ["192.168.0.3"],
Expand Down