Move diff to helper/diff, helper/resource knows about it
This commit is contained in:
parent
938b4da8a5
commit
bd0f23ce25
|
@ -1,32 +0,0 @@
|
||||||
package aws
|
|
||||||
|
|
||||||
import (
|
|
||||||
"github.com/hashicorp/terraform/diff"
|
|
||||||
)
|
|
||||||
|
|
||||||
var diffMap *diff.LazyResourceMap
|
|
||||||
|
|
||||||
func init() {
|
|
||||||
diffMap = &diff.LazyResourceMap{
|
|
||||||
Resources: map[string]diff.ResourceBuilderFactory{
|
|
||||||
"aws_instance": diffBuilder_aws_instance,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func diffBuilder_aws_instance() *diff.ResourceBuilder {
|
|
||||||
return &diff.ResourceBuilder{
|
|
||||||
CreateComputedAttrs: []string{
|
|
||||||
"public_dns",
|
|
||||||
"public_ip",
|
|
||||||
"private_dns",
|
|
||||||
"private_ip",
|
|
||||||
},
|
|
||||||
|
|
||||||
RequiresNewAttrs: []string{
|
|
||||||
"ami",
|
|
||||||
"availability_zone",
|
|
||||||
"instance_type",
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,7 +1,6 @@
|
||||||
package aws
|
package aws
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"log"
|
"log"
|
||||||
|
|
||||||
"github.com/hashicorp/terraform/helper/config"
|
"github.com/hashicorp/terraform/helper/config"
|
||||||
|
@ -60,12 +59,7 @@ func (p *ResourceProvider) Apply(
|
||||||
func (p *ResourceProvider) Diff(
|
func (p *ResourceProvider) Diff(
|
||||||
s *terraform.ResourceState,
|
s *terraform.ResourceState,
|
||||||
c *terraform.ResourceConfig) (*terraform.ResourceDiff, error) {
|
c *terraform.ResourceConfig) (*terraform.ResourceDiff, error) {
|
||||||
b := diffMap.Get(s.Type)
|
return resourceMap.Diff(s, c, p)
|
||||||
if b == nil {
|
|
||||||
return nil, fmt.Errorf("Unknown type: %s", s.Type)
|
|
||||||
}
|
|
||||||
|
|
||||||
return b.Diff(s, c)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *ResourceProvider) Refresh(
|
func (p *ResourceProvider) Refresh(
|
||||||
|
|
|
@ -4,6 +4,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
|
|
||||||
|
"github.com/hashicorp/terraform/helper/diff"
|
||||||
"github.com/hashicorp/terraform/helper/resource"
|
"github.com/hashicorp/terraform/helper/resource"
|
||||||
"github.com/hashicorp/terraform/terraform"
|
"github.com/hashicorp/terraform/terraform"
|
||||||
"github.com/mitchellh/goamz/ec2"
|
"github.com/mitchellh/goamz/ec2"
|
||||||
|
@ -19,6 +20,7 @@ func init() {
|
||||||
"aws_instance": resource.Resource{
|
"aws_instance": resource.Resource{
|
||||||
Create: resource_aws_instance_create,
|
Create: resource_aws_instance_create,
|
||||||
Destroy: resource_aws_instance_destroy,
|
Destroy: resource_aws_instance_destroy,
|
||||||
|
Diff: resource_aws_instance_diff,
|
||||||
Refresh: resource_aws_instance_refresh,
|
Refresh: resource_aws_instance_refresh,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
@ -101,6 +103,28 @@ func resource_aws_instance_destroy(
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func resource_aws_instance_diff(
|
||||||
|
s *terraform.ResourceState,
|
||||||
|
c *terraform.ResourceConfig,
|
||||||
|
meta interface{}) (*terraform.ResourceDiff, error) {
|
||||||
|
b := &diff.ResourceBuilder{
|
||||||
|
CreateComputedAttrs: []string{
|
||||||
|
"public_dns",
|
||||||
|
"public_ip",
|
||||||
|
"private_dns",
|
||||||
|
"private_ip",
|
||||||
|
},
|
||||||
|
|
||||||
|
RequiresNewAttrs: []string{
|
||||||
|
"ami",
|
||||||
|
"availability_zone",
|
||||||
|
"instance_type",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
return b.Diff(s, c)
|
||||||
|
}
|
||||||
|
|
||||||
func resource_aws_instance_refresh(
|
func resource_aws_instance_refresh(
|
||||||
s *terraform.ResourceState,
|
s *terraform.ResourceState,
|
||||||
meta interface{}) (*terraform.ResourceState, error) {
|
meta interface{}) (*terraform.ResourceState, error) {
|
||||||
|
|
|
@ -1,51 +0,0 @@
|
||||||
package diff
|
|
||||||
|
|
||||||
import (
|
|
||||||
"sync"
|
|
||||||
)
|
|
||||||
|
|
||||||
// LazyResourceMap is a way to lazy-load resource builders.
|
|
||||||
//
|
|
||||||
// By lazy loading resource builders, a considerable amount of compute
|
|
||||||
// effort for building the builders can be avoided. This is especially
|
|
||||||
// helpful in Terraform providers that support many resource types.
|
|
||||||
type LazyResourceMap struct {
|
|
||||||
Resources map[string]ResourceBuilderFactory
|
|
||||||
|
|
||||||
l sync.Mutex
|
|
||||||
memoized map[string]*ResourceBuilder
|
|
||||||
}
|
|
||||||
|
|
||||||
// ResourceBuilderFactory is a factory function for creating a resource
|
|
||||||
// builder that is used for lazy loading resource builders in the Builder
|
|
||||||
// struct.
|
|
||||||
type ResourceBuilderFactory func() *ResourceBuilder
|
|
||||||
|
|
||||||
// Get gets the ResourceBuilder for the given resource type, and returns
|
|
||||||
// nil if the resource builder cannot be found.
|
|
||||||
//
|
|
||||||
// This will memoize the result, returning the same result for the same
|
|
||||||
// type if called again.
|
|
||||||
func (m *LazyResourceMap) Get(r string) *ResourceBuilder {
|
|
||||||
m.l.Lock()
|
|
||||||
defer m.l.Unlock()
|
|
||||||
|
|
||||||
// If we have it saved, return that
|
|
||||||
if rb, ok := m.memoized[r]; ok {
|
|
||||||
return rb
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get the factory function
|
|
||||||
f, ok := m.Resources[r]
|
|
||||||
if !ok {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Save it so that we don't rebuild
|
|
||||||
if m.memoized == nil {
|
|
||||||
m.memoized = make(map[string]*ResourceBuilder)
|
|
||||||
}
|
|
||||||
m.memoized[r] = f()
|
|
||||||
|
|
||||||
return m.memoized[r]
|
|
||||||
}
|
|
|
@ -1,45 +0,0 @@
|
||||||
package diff
|
|
||||||
|
|
||||||
import (
|
|
||||||
"testing"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestLazyResourceMap(t *testing.T) {
|
|
||||||
rb1 := new(ResourceBuilder)
|
|
||||||
rb2 := new(ResourceBuilder)
|
|
||||||
|
|
||||||
rm := &LazyResourceMap{
|
|
||||||
Resources: map[string]ResourceBuilderFactory{
|
|
||||||
"foo": testRBFactory(rb1),
|
|
||||||
"bar": testRBFactory(rb2),
|
|
||||||
"diff": func() *ResourceBuilder {
|
|
||||||
return new(ResourceBuilder)
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
actual := rm.Get("foo")
|
|
||||||
if actual == nil {
|
|
||||||
t.Fatal("should not be nil")
|
|
||||||
}
|
|
||||||
if actual != rb1 {
|
|
||||||
t.Fatalf("bad: %p %p", rb1, actual)
|
|
||||||
}
|
|
||||||
if actual == rm.Get("bar") {
|
|
||||||
t.Fatalf("bad: %p %p", actual, rm.Get("bar"))
|
|
||||||
}
|
|
||||||
|
|
||||||
actual = rm.Get("diff")
|
|
||||||
if actual == nil {
|
|
||||||
t.Fatal("should not be nil")
|
|
||||||
}
|
|
||||||
if actual != rm.Get("diff") {
|
|
||||||
t.Fatal("should memoize")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func testRBFactory(rb *ResourceBuilder) ResourceBuilderFactory {
|
|
||||||
return func() *ResourceBuilder {
|
|
||||||
return rb
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -31,6 +31,19 @@ func (m *Map) Apply(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Diff peforms a diff on the proper resource type.
|
||||||
|
func (m *Map) Diff(
|
||||||
|
s *terraform.ResourceState,
|
||||||
|
c *terraform.ResourceConfig,
|
||||||
|
meta interface{}) (*terraform.ResourceDiff, error) {
|
||||||
|
r, ok := m.Mapping[s.Type]
|
||||||
|
if !ok {
|
||||||
|
return nil, fmt.Errorf("Unknown resource type: %s", s.Type)
|
||||||
|
}
|
||||||
|
|
||||||
|
return r.Diff(s, c, meta)
|
||||||
|
}
|
||||||
|
|
||||||
// Refresh performs a Refresh on the proper resource type.
|
// Refresh performs a Refresh on the proper resource type.
|
||||||
//
|
//
|
||||||
// Refresh on the Resource won't be called if the state represents a
|
// Refresh on the Resource won't be called if the state represents a
|
||||||
|
|
Loading…
Reference in New Issue