2014-07-19 02:20:28 +02:00
|
|
|
package digitalocean
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/helper/config"
|
2014-09-09 22:13:11 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
2014-07-19 02:20:28 +02:00
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
"github.com/pearkes/digitalocean"
|
|
|
|
)
|
|
|
|
|
|
|
|
type ResourceProvider struct {
|
|
|
|
Config Config
|
|
|
|
|
|
|
|
client *digitalocean.Client
|
2014-09-09 22:13:11 +02:00
|
|
|
|
|
|
|
// 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
|
2014-07-19 02:20:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *ResourceProvider) Validate(c *terraform.ResourceConfig) ([]string, []error) {
|
2014-09-09 22:13:11 +02:00
|
|
|
prov := Provider()
|
|
|
|
return prov.Validate(c)
|
2014-07-19 02:20:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *ResourceProvider) ValidateResource(
|
|
|
|
t string, c *terraform.ResourceConfig) ([]string, []error) {
|
2014-09-09 22:13:11 +02:00
|
|
|
prov := Provider()
|
|
|
|
if _, ok := prov.ResourcesMap[t]; ok {
|
|
|
|
return prov.ValidateResource(t, c)
|
|
|
|
}
|
|
|
|
|
2014-07-19 02:20:28 +02:00
|
|
|
return resourceMap.Validate(t, c)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *ResourceProvider) Configure(c *terraform.ResourceConfig) error {
|
|
|
|
if _, err := config.Decode(&p.Config, c.Config); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Println("[INFO] Initializing DigitalOcean client")
|
|
|
|
var err error
|
|
|
|
p.client, err = p.Config.Client()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-09-09 22:13:11 +02:00
|
|
|
// Create the provider, set the meta
|
|
|
|
p.p = Provider()
|
|
|
|
p.p.SetMeta(p)
|
|
|
|
|
2014-07-19 02:20:28 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *ResourceProvider) Apply(
|
|
|
|
s *terraform.ResourceState,
|
|
|
|
d *terraform.ResourceDiff) (*terraform.ResourceState, error) {
|
2014-09-09 22:13:11 +02:00
|
|
|
if _, ok := p.p.ResourcesMap[s.Type]; ok {
|
|
|
|
return p.p.Apply(s, d)
|
|
|
|
}
|
|
|
|
|
2014-07-19 02:20:28 +02:00
|
|
|
return resourceMap.Apply(s, d, p)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *ResourceProvider) Diff(
|
|
|
|
s *terraform.ResourceState,
|
|
|
|
c *terraform.ResourceConfig) (*terraform.ResourceDiff, error) {
|
2014-09-09 22:13:11 +02:00
|
|
|
if _, ok := p.p.ResourcesMap[s.Type]; ok {
|
|
|
|
return p.p.Diff(s, c)
|
|
|
|
}
|
|
|
|
|
2014-07-19 02:20:28 +02:00
|
|
|
return resourceMap.Diff(s, c, p)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *ResourceProvider) Refresh(
|
|
|
|
s *terraform.ResourceState) (*terraform.ResourceState, error) {
|
2014-09-09 22:13:11 +02:00
|
|
|
if _, ok := p.p.ResourcesMap[s.Type]; ok {
|
|
|
|
return p.p.Refresh(s)
|
|
|
|
}
|
|
|
|
|
2014-07-19 02:20:28 +02:00
|
|
|
return resourceMap.Refresh(s, p)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *ResourceProvider) Resources() []terraform.ResourceType {
|
2014-09-09 22:13:11 +02:00
|
|
|
result := resourceMap.Resources()
|
|
|
|
result = append(result, Provider().Resources()...)
|
|
|
|
return result
|
2014-07-19 02:20:28 +02:00
|
|
|
}
|