terraform: Resources method for providers

This commit is contained in:
Mitchell Hashimoto 2014-06-03 14:26:31 -07:00
parent 9895f8162f
commit 0c1a341d90
8 changed files with 72 additions and 3 deletions

View File

@ -1,8 +1,8 @@
package main
import (
"github.com/hashicorp/terraform/plugin"
"github.com/hashicorp/terraform/builtin/providers/aws"
"github.com/hashicorp/terraform/plugin"
)
func main() {

View File

@ -1 +1 @@
package main
package main

View File

@ -1,8 +1,16 @@
package aws
import (
"github.com/hashicorp/terraform/terraform"
)
type ResourceProvider struct {
}
func (p *ResourceProvider) Configure(map[string]interface{}) ([]string, error) {
return nil, nil
}
func (p *ResourceProvider) Resources() []terraform.ResourceType {
return nil
}

View File

@ -26,6 +26,18 @@ func (p *ResourceProvider) Configure(c map[string]interface{}) ([]string, error)
return resp.Warnings, err
}
func (p *ResourceProvider) Resources() []terraform.ResourceType {
var result []terraform.ResourceType
err := p.Client.Call(p.Name+".Resources", new(interface{}), &result)
if err != nil {
// TODO: panic, log, what?
return nil
}
return result
}
// ResourceProviderServer is a net/rpc compatible structure for serving
// a ResourceProvider. This should not be used directly.
type ResourceProviderServer struct {
@ -47,3 +59,10 @@ func (s *ResourceProviderServer) Configure(
}
return nil
}
func (s *ResourceProviderServer) Resources(
nothing interface{},
result *[]terraform.ResourceType) error {
*result = s.Provider.Resources()
return nil
}

View File

@ -95,3 +95,29 @@ func TestResourceProvider_configure_warnings(t *testing.T) {
t.Fatalf("bad: %#v", e)
}
}
func TestResourceProvider_resources(t *testing.T) {
p := new(terraform.MockResourceProvider)
client, server := testClientServer(t)
name, err := Register(server, p)
if err != nil {
t.Fatalf("err: %s", err)
}
provider := &ResourceProvider{Client: client, Name: name}
expected := []terraform.ResourceType{
{"foo"},
{"bar"},
}
p.ResourcesReturn = expected
// Resources
result := provider.Resources()
if !p.ResourcesCalled {
t.Fatal("resources should be called")
}
if !reflect.DeepEqual(result, expected) {
t.Fatalf("bad: %#v", result)
}
}

View File

@ -12,7 +12,7 @@ type ResourceProvider interface {
// Resources returns all the available resource types that this provider
// knows how to manage.
//Resources() []ResourceType
Resources() []ResourceType
}
// ResourceType is a type of resource that a resource provider can manage.

View File

@ -7,6 +7,8 @@ type MockResourceProvider struct {
ConfigureConfig map[string]interface{}
ConfigureReturnWarnings []string
ConfigureReturnError error
ResourcesCalled bool
ResourcesReturn []ResourceType
}
func (p *MockResourceProvider) Configure(c map[string]interface{}) ([]string, error) {
@ -14,3 +16,8 @@ func (p *MockResourceProvider) Configure(c map[string]interface{}) ([]string, er
p.ConfigureConfig = c
return p.ConfigureReturnWarnings, p.ConfigureReturnError
}
func (p *MockResourceProvider) Resources() []ResourceType {
p.ResourcesCalled = true
return p.ResourcesReturn
}

View File

@ -0,0 +1,9 @@
package terraform
import (
"testing"
)
func TestMockResourceProvider_impl(t *testing.T) {
var _ ResourceProvider = new(MockResourceProvider)
}