config: add Config method
This commit is contained in:
parent
f772c11103
commit
101ac636a2
|
@ -4,6 +4,7 @@ package config
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/hashicorp/terraform/flatmap"
|
||||
|
@ -56,7 +57,7 @@ type ProviderConfig struct {
|
|||
type Resource struct {
|
||||
Name string
|
||||
Type string
|
||||
Count *RawConfig
|
||||
RawCount *RawConfig
|
||||
RawConfig *RawConfig
|
||||
Provisioners []*Provisioner
|
||||
DependsOn []string
|
||||
|
@ -120,6 +121,16 @@ func (r *Module) Id() string {
|
|||
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.
|
||||
func (r *Resource) Id() string {
|
||||
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 {
|
||||
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)
|
||||
}
|
||||
for _, v := range rc.RawConfig.Variables {
|
||||
|
@ -384,8 +395,8 @@ func (r *Resource) mergerMerge(m merger) merger {
|
|||
result.Type = r2.Type
|
||||
result.RawConfig = result.RawConfig.merge(r2.RawConfig)
|
||||
|
||||
if r2.Count.Value() != "1" {
|
||||
result.Count = r2.Count
|
||||
if r2.RawCount.Value() != "1" {
|
||||
result.RawCount = r2.RawCount
|
||||
}
|
||||
|
||||
if len(r2.Provisioners) > 0 {
|
||||
|
|
|
@ -193,7 +193,7 @@ func resourcesStr(rs []*Resource) string {
|
|||
"%s[%s] (x%s)\n",
|
||||
r.Type,
|
||||
r.Name,
|
||||
r.Count.Value())
|
||||
r.RawCount.Value())
|
||||
|
||||
ks := make([]string, 0, len(r.RawConfig.Raw))
|
||||
for k, _ := range r.RawConfig.Raw {
|
||||
|
|
|
@ -9,6 +9,36 @@ import (
|
|||
// This is the directory where our test fixtures are.
|
||||
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) {
|
||||
c := testConfig(t, "validate-good")
|
||||
if err := c.Validate(); err != nil {
|
||||
|
|
|
@ -482,7 +482,7 @@ func loadResourcesHcl(os *hclobj.Object) ([]*Resource, error) {
|
|||
result = append(result, &Resource{
|
||||
Name: k,
|
||||
Type: t.Key,
|
||||
Count: countConfig,
|
||||
RawCount: countConfig,
|
||||
RawConfig: rawConfig,
|
||||
Provisioners: provisioners,
|
||||
DependsOn: dependsOn,
|
||||
|
|
|
@ -0,0 +1,3 @@
|
|||
resource "foo" "bar" {
|
||||
count = 5
|
||||
}
|
|
@ -0,0 +1,3 @@
|
|||
resource "foo" "bar" {
|
||||
count = "5"
|
||||
}
|
|
@ -0,0 +1,3 @@
|
|||
resource "foo" "bar" {
|
||||
count = "${var.foo}"
|
||||
}
|
|
@ -198,9 +198,10 @@ func (m *ModuleState) Orphans(c *config.Config) []string {
|
|||
for _, r := range c.Resources {
|
||||
delete(keys, r.Id())
|
||||
|
||||
// Mark all the counts as not orphans.
|
||||
for i := 0; i < r.Count; i++ {
|
||||
delete(keys, fmt.Sprintf("%s.%d", r.Id(), i))
|
||||
for k, _ := range keys {
|
||||
if strings.HasPrefix(k, r.Id()+".") {
|
||||
delete(keys, k)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -7,6 +7,7 @@ import (
|
|||
"fmt"
|
||||
"io"
|
||||
"sort"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"github.com/hashicorp/terraform/config"
|
||||
|
@ -80,9 +81,10 @@ func (s *StateV1) Orphans(c *config.Config) []string {
|
|||
for _, r := range c.Resources {
|
||||
delete(keys, r.Id())
|
||||
|
||||
// Mark all the counts as not orphans.
|
||||
for i := 0; i < r.Count; i++ {
|
||||
delete(keys, fmt.Sprintf("%s.%d", r.Id(), i))
|
||||
for k, _ := range keys {
|
||||
if strings.HasPrefix(k, r.Id()+".") {
|
||||
delete(keys, k)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue