diff: lazy resource map
This commit is contained in:
parent
dd16ff65c6
commit
847cd7ac51
|
@ -23,6 +23,20 @@ func (p *ResourceProvider) Configure(*terraform.ResourceConfig) error {
|
||||||
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) {
|
||||||
|
diffMap.CreateAttrs([]string{
|
||||||
|
"ami",
|
||||||
|
"availability_zone",
|
||||||
|
"instance_type",
|
||||||
|
"region",
|
||||||
|
})
|
||||||
|
diffMap.CreateComputedAttrs([]string{
|
||||||
|
"id",
|
||||||
|
"public_dns",
|
||||||
|
"public_ip",
|
||||||
|
"private_dns",
|
||||||
|
"private_ip",
|
||||||
|
})
|
||||||
|
|
||||||
return &terraform.ResourceDiff{
|
return &terraform.ResourceDiff{
|
||||||
Attributes: map[string]*terraform.ResourceAttrDiff{
|
Attributes: map[string]*terraform.ResourceAttrDiff{
|
||||||
"id": &terraform.ResourceAttrDiff{
|
"id": &terraform.ResourceAttrDiff{
|
||||||
|
|
|
@ -0,0 +1,51 @@
|
||||||
|
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]
|
||||||
|
}
|
|
@ -0,0 +1,45 @@
|
||||||
|
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
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,18 +1,9 @@
|
||||||
// The diff package provides high-level constructs for easily building
|
|
||||||
// and working with Terraform diff structures.
|
|
||||||
package diff
|
package diff
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/hashicorp/terraform/terraform"
|
"github.com/hashicorp/terraform/terraform"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Builder is a helper that is able to build terraform.ResourceDiff
|
|
||||||
// structures, and should be used by providers instead of hand-building
|
|
||||||
// them which can be tedious and error-prone.
|
|
||||||
type Builder struct {
|
|
||||||
Resources map[string]*ResourceBuilder
|
|
||||||
}
|
|
||||||
|
|
||||||
// ResourceBuilder is a helper that can knows about how a single resource
|
// ResourceBuilder is a helper that can knows about how a single resource
|
||||||
// changes and how those changes affect the diff.
|
// changes and how those changes affect the diff.
|
||||||
type ResourceBuilder struct {
|
type ResourceBuilder struct {
|
||||||
|
@ -20,11 +11,6 @@ type ResourceBuilder struct {
|
||||||
RequiresNewAttrs []string
|
RequiresNewAttrs []string
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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
|
|
||||||
|
|
||||||
// Diff returns the ResourceDiff for a resource given its state and
|
// Diff returns the ResourceDiff for a resource given its state and
|
||||||
// configuration.
|
// configuration.
|
||||||
func (b *ResourceBuilder) Diff(
|
func (b *ResourceBuilder) Diff(
|
Loading…
Reference in New Issue