providers/digitalocean: schema framework, validation

This commit is contained in:
Mitchell Hashimoto 2014-09-09 13:13:11 -07:00
parent 46e6e3a7bd
commit 98e1b0aae9
3 changed files with 68 additions and 8 deletions

26
provider.go Normal file
View File

@ -0,0 +1,26 @@
package digitalocean
import (
"github.com/hashicorp/terraform/helper/schema"
)
// Provider returns a schema.Provider for DigitalOcean.
//
// NOTE: schema.Provider became available long after the DO provider
// was started, so resources may not be converted to this new structure
// yet. This is a WIP. To assist with the migration, make sure any resources
// you migrate are acceptance tested, then perform the migration.
func Provider() *schema.Provider {
// TODO: Move the configuration to this, requires validation
return &schema.Provider{
Schema: map[string]*schema.Schema{
"token": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
},
ResourcesMap: map[string]*schema.Resource{},
}
}

11
provider_test.go Normal file
View File

@ -0,0 +1,11 @@
package digitalocean
import (
"testing"
)
func TestProvider(t *testing.T) {
if err := Provider().InternalValidate(); err != nil {
t.Fatalf("err: %s", err)
}
}

View File

@ -4,6 +4,7 @@ import (
"log"
"github.com/hashicorp/terraform/helper/config"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/terraform"
"github.com/pearkes/digitalocean"
)
@ -12,20 +13,24 @@ type ResourceProvider struct {
Config Config
client *digitalocean.Client
// This is the schema.Provider. Eventually this will replace much
// of this structure. For now it is an element of it for compatiblity.
p *schema.Provider
}
func (p *ResourceProvider) Validate(c *terraform.ResourceConfig) ([]string, []error) {
v := &config.Validator{
Required: []string{
"token",
},
}
return v.Validate(c)
prov := Provider()
return prov.Validate(c)
}
func (p *ResourceProvider) ValidateResource(
t string, c *terraform.ResourceConfig) ([]string, []error) {
prov := Provider()
if _, ok := prov.ResourcesMap[t]; ok {
return prov.ValidateResource(t, c)
}
return resourceMap.Validate(t, c)
}
@ -42,26 +47,44 @@ func (p *ResourceProvider) Configure(c *terraform.ResourceConfig) error {
return err
}
// Create the provider, set the meta
p.p = Provider()
p.p.SetMeta(p)
return nil
}
func (p *ResourceProvider) Apply(
s *terraform.ResourceState,
d *terraform.ResourceDiff) (*terraform.ResourceState, error) {
if _, ok := p.p.ResourcesMap[s.Type]; ok {
return p.p.Apply(s, d)
}
return resourceMap.Apply(s, d, p)
}
func (p *ResourceProvider) Diff(
s *terraform.ResourceState,
c *terraform.ResourceConfig) (*terraform.ResourceDiff, error) {
if _, ok := p.p.ResourcesMap[s.Type]; ok {
return p.p.Diff(s, c)
}
return resourceMap.Diff(s, c, p)
}
func (p *ResourceProvider) Refresh(
s *terraform.ResourceState) (*terraform.ResourceState, error) {
if _, ok := p.p.ResourcesMap[s.Type]; ok {
return p.p.Refresh(s)
}
return resourceMap.Refresh(s, p)
}
func (p *ResourceProvider) Resources() []terraform.ResourceType {
return resourceMap.Resources()
result := resourceMap.Resources()
result = append(result, Provider().Resources()...)
return result
}