helper/schema: Provider.Resources

This commit is contained in:
Mitchell Hashimoto 2014-08-17 15:07:01 -07:00
parent eaac13dd9b
commit afd3a7b811
2 changed files with 49 additions and 4 deletions

View File

@ -12,8 +12,8 @@ import (
// This is a friendlier API than the core Terraform ResourceProvider API,
// and is recommended to be used over that.
type Provider struct {
Schema map[string]*Schema
Resources map[string]*Resource
Schema map[string]*Schema
ResourcesMap map[string]*Resource
ConfigureFunc ConfigureFunc
}
@ -30,7 +30,7 @@ func (p *Provider) Validate(c *terraform.ResourceConfig) ([]string, []error) {
// proper schema.
func (p *Provider) ValidateResource(
t string, c *terraform.ResourceConfig) ([]string, []error) {
r, ok := p.Resources[t]
r, ok := p.ResourcesMap[t]
if !ok {
return nil, []error{fmt.Errorf(
"Provider doesn't support resource: %s", t)}
@ -62,3 +62,15 @@ func (p *Provider) Configure(c *terraform.ResourceConfig) error {
return p.ConfigureFunc(data)
}
// Resources implementation of terraform.ResourceProvider interface.
func (p *Provider) Resources() []terraform.ResourceType {
result := make([]terraform.ResourceType, 0, len(p.ResourcesMap))
for k, _ := range p.ResourcesMap {
result = append(result, terraform.ResourceType{
Name: k,
})
}
return result
}

View File

@ -2,6 +2,7 @@ package schema
import (
"fmt"
"reflect"
"testing"
"github.com/hashicorp/terraform/config"
@ -80,6 +81,38 @@ func TestProviderConfigure(t *testing.T) {
}
}
func TestProviderResources(t *testing.T) {
cases := []struct {
P *Provider
Result []terraform.ResourceType
}{
{
P: &Provider{},
Result: []terraform.ResourceType{},
},
{
P: &Provider{
ResourcesMap: map[string]*Resource{
"foo": nil,
"bar": nil,
},
},
Result: []terraform.ResourceType{
terraform.ResourceType{Name: "foo"},
terraform.ResourceType{Name: "bar"},
},
},
}
for i, tc := range cases {
actual := tc.P.Resources()
if !reflect.DeepEqual(actual, tc.Result) {
t.Fatalf("%d: %#v", i, actual)
}
}
}
func TestProviderValidateResource(t *testing.T) {
cases := []struct {
P *Provider
@ -96,7 +129,7 @@ func TestProviderValidateResource(t *testing.T) {
{
P: &Provider{
Resources: map[string]*Resource{
ResourcesMap: map[string]*Resource{
"foo": &Resource{},
},
},