mirror of
https://github.com/opentofu/opentofu.git
synced 2025-02-25 18:45:20 -06:00
providers/digitalocean: schema framework, validation
This commit is contained in:
parent
b5daa2f41e
commit
6a1a8b9487
26
builtin/providers/digitalocean/provider.go
Normal file
26
builtin/providers/digitalocean/provider.go
Normal 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
builtin/providers/digitalocean/provider_test.go
Normal file
11
builtin/providers/digitalocean/provider_test.go
Normal 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)
|
||||||
|
}
|
||||||
|
}
|
@ -4,6 +4,7 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
|
|
||||||
"github.com/hashicorp/terraform/helper/config"
|
"github.com/hashicorp/terraform/helper/config"
|
||||||
|
"github.com/hashicorp/terraform/helper/schema"
|
||||||
"github.com/hashicorp/terraform/terraform"
|
"github.com/hashicorp/terraform/terraform"
|
||||||
"github.com/pearkes/digitalocean"
|
"github.com/pearkes/digitalocean"
|
||||||
)
|
)
|
||||||
@ -12,20 +13,24 @@ type ResourceProvider struct {
|
|||||||
Config Config
|
Config Config
|
||||||
|
|
||||||
client *digitalocean.Client
|
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) {
|
func (p *ResourceProvider) Validate(c *terraform.ResourceConfig) ([]string, []error) {
|
||||||
v := &config.Validator{
|
prov := Provider()
|
||||||
Required: []string{
|
return prov.Validate(c)
|
||||||
"token",
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
return v.Validate(c)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *ResourceProvider) ValidateResource(
|
func (p *ResourceProvider) ValidateResource(
|
||||||
t string, c *terraform.ResourceConfig) ([]string, []error) {
|
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)
|
return resourceMap.Validate(t, c)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -42,26 +47,44 @@ func (p *ResourceProvider) Configure(c *terraform.ResourceConfig) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Create the provider, set the meta
|
||||||
|
p.p = Provider()
|
||||||
|
p.p.SetMeta(p)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *ResourceProvider) Apply(
|
func (p *ResourceProvider) Apply(
|
||||||
s *terraform.ResourceState,
|
s *terraform.ResourceState,
|
||||||
d *terraform.ResourceDiff) (*terraform.ResourceState, error) {
|
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)
|
return resourceMap.Apply(s, d, p)
|
||||||
}
|
}
|
||||||
|
|
||||||
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) {
|
||||||
|
if _, ok := p.p.ResourcesMap[s.Type]; ok {
|
||||||
|
return p.p.Diff(s, c)
|
||||||
|
}
|
||||||
|
|
||||||
return resourceMap.Diff(s, c, p)
|
return resourceMap.Diff(s, c, p)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *ResourceProvider) Refresh(
|
func (p *ResourceProvider) Refresh(
|
||||||
s *terraform.ResourceState) (*terraform.ResourceState, error) {
|
s *terraform.ResourceState) (*terraform.ResourceState, error) {
|
||||||
|
if _, ok := p.p.ResourcesMap[s.Type]; ok {
|
||||||
|
return p.p.Refresh(s)
|
||||||
|
}
|
||||||
|
|
||||||
return resourceMap.Refresh(s, p)
|
return resourceMap.Refresh(s, p)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *ResourceProvider) Resources() []terraform.ResourceType {
|
func (p *ResourceProvider) Resources() []terraform.ResourceType {
|
||||||
return resourceMap.Resources()
|
result := resourceMap.Resources()
|
||||||
|
result = append(result, Provider().Resources()...)
|
||||||
|
return result
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user