config: add Config method

This commit is contained in:
Mitchell Hashimoto 2014-10-02 11:34:08 -07:00
parent f772c11103
commit 101ac636a2
9 changed files with 65 additions and 12 deletions

View File

@ -4,6 +4,7 @@ package config
import ( import (
"fmt" "fmt"
"strconv"
"strings" "strings"
"github.com/hashicorp/terraform/flatmap" "github.com/hashicorp/terraform/flatmap"
@ -56,7 +57,7 @@ type ProviderConfig struct {
type Resource struct { type Resource struct {
Name string Name string
Type string Type string
Count *RawConfig RawCount *RawConfig
RawConfig *RawConfig RawConfig *RawConfig
Provisioners []*Provisioner Provisioners []*Provisioner
DependsOn []string DependsOn []string
@ -120,6 +121,16 @@ func (r *Module) Id() string {
return fmt.Sprintf("%s", r.Name) return fmt.Sprintf("%s", r.Name)
} }
// Count returns the count of this resource.
func (r *Resource) Count() (int, error) {
v, err := strconv.ParseInt(r.RawCount.Value().(string), 0, 0)
if err != nil {
return 0, err
}
return int(v), nil
}
// A unique identifier for this resource. // A unique identifier for this resource.
func (r *Resource) Id() string { func (r *Resource) Id() string {
return fmt.Sprintf("%s.%s", r.Type, r.Name) return fmt.Sprintf("%s.%s", r.Type, r.Name)
@ -308,7 +319,7 @@ func (c *Config) InterpolatedVariables() map[string][]InterpolatedVariable {
for _, rc := range c.Resources { for _, rc := range c.Resources {
source := fmt.Sprintf("resource '%s'", rc.Id()) source := fmt.Sprintf("resource '%s'", rc.Id())
for _, v := range rc.Count.Variables { for _, v := range rc.RawCount.Variables {
result[source] = append(result[source], v) result[source] = append(result[source], v)
} }
for _, v := range rc.RawConfig.Variables { for _, v := range rc.RawConfig.Variables {
@ -384,8 +395,8 @@ func (r *Resource) mergerMerge(m merger) merger {
result.Type = r2.Type result.Type = r2.Type
result.RawConfig = result.RawConfig.merge(r2.RawConfig) result.RawConfig = result.RawConfig.merge(r2.RawConfig)
if r2.Count.Value() != "1" { if r2.RawCount.Value() != "1" {
result.Count = r2.Count result.RawCount = r2.RawCount
} }
if len(r2.Provisioners) > 0 { if len(r2.Provisioners) > 0 {

View File

@ -193,7 +193,7 @@ func resourcesStr(rs []*Resource) string {
"%s[%s] (x%s)\n", "%s[%s] (x%s)\n",
r.Type, r.Type,
r.Name, r.Name,
r.Count.Value()) r.RawCount.Value())
ks := make([]string, 0, len(r.RawConfig.Raw)) ks := make([]string, 0, len(r.RawConfig.Raw))
for k, _ := range r.RawConfig.Raw { for k, _ := range r.RawConfig.Raw {

View File

@ -9,6 +9,36 @@ import (
// This is the directory where our test fixtures are. // This is the directory where our test fixtures are.
const fixtureDir = "./test-fixtures" const fixtureDir = "./test-fixtures"
func TestConfigCount(t *testing.T) {
c := testConfig(t, "count-int")
actual, err := c.Resources[0].Count()
if err != nil {
t.Fatalf("err: %s", err)
}
if actual != 5 {
t.Fatalf("bad: %#v", actual)
}
}
func TestConfigCount_string(t *testing.T) {
c := testConfig(t, "count-string")
actual, err := c.Resources[0].Count()
if err != nil {
t.Fatalf("err: %s", err)
}
if actual != 5 {
t.Fatalf("bad: %#v", actual)
}
}
func TestConfigCount_var(t *testing.T) {
c := testConfig(t, "count-var")
_, err := c.Resources[0].Count()
if err == nil {
t.Fatalf("should error")
}
}
func TestConfigValidate(t *testing.T) { func TestConfigValidate(t *testing.T) {
c := testConfig(t, "validate-good") c := testConfig(t, "validate-good")
if err := c.Validate(); err != nil { if err := c.Validate(); err != nil {

View File

@ -482,7 +482,7 @@ func loadResourcesHcl(os *hclobj.Object) ([]*Resource, error) {
result = append(result, &Resource{ result = append(result, &Resource{
Name: k, Name: k,
Type: t.Key, Type: t.Key,
Count: countConfig, RawCount: countConfig,
RawConfig: rawConfig, RawConfig: rawConfig,
Provisioners: provisioners, Provisioners: provisioners,
DependsOn: dependsOn, DependsOn: dependsOn,

View File

@ -0,0 +1,3 @@
resource "foo" "bar" {
count = 5
}

View File

@ -0,0 +1,3 @@
resource "foo" "bar" {
count = "5"
}

View File

@ -0,0 +1,3 @@
resource "foo" "bar" {
count = "${var.foo}"
}

View File

@ -198,9 +198,10 @@ func (m *ModuleState) Orphans(c *config.Config) []string {
for _, r := range c.Resources { for _, r := range c.Resources {
delete(keys, r.Id()) delete(keys, r.Id())
// Mark all the counts as not orphans. for k, _ := range keys {
for i := 0; i < r.Count; i++ { if strings.HasPrefix(k, r.Id()+".") {
delete(keys, fmt.Sprintf("%s.%d", r.Id(), i)) delete(keys, k)
}
} }
} }

View File

@ -7,6 +7,7 @@ import (
"fmt" "fmt"
"io" "io"
"sort" "sort"
"strings"
"sync" "sync"
"github.com/hashicorp/terraform/config" "github.com/hashicorp/terraform/config"
@ -80,9 +81,10 @@ func (s *StateV1) Orphans(c *config.Config) []string {
for _, r := range c.Resources { for _, r := range c.Resources {
delete(keys, r.Id()) delete(keys, r.Id())
// Mark all the counts as not orphans. for k, _ := range keys {
for i := 0; i < r.Count; i++ { if strings.HasPrefix(k, r.Id()+".") {
delete(keys, fmt.Sprintf("%s.%d", r.Id(), i)) delete(keys, k)
}
} }
} }