Merge pull request #281 from hashicorp/f-do-schema

Convert DO to partial-helper.Schema
This commit is contained in:
Mitchell Hashimoto 2014-09-09 13:33:38 -07:00
commit a9c8ccb29b
5 changed files with 163 additions and 147 deletions

View File

@ -0,0 +1,28 @@
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
return &schema.Provider{
Schema: map[string]*schema.Schema{
"token": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
},
ResourcesMap: map[string]*schema.Resource{
"digitalocean_record": resourceRecord(),
},
}
}

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

@ -5,94 +5,118 @@ import (
"log" "log"
"strings" "strings"
"github.com/hashicorp/terraform/helper/config" "github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/diff"
"github.com/hashicorp/terraform/terraform" "github.com/hashicorp/terraform/terraform"
"github.com/pearkes/digitalocean" "github.com/pearkes/digitalocean"
) )
func resource_digitalocean_record_create( func resourceRecord() *schema.Resource {
s *terraform.ResourceState, return &schema.Resource{
d *terraform.ResourceDiff, Create: resourceRecordCreate,
meta interface{}) (*terraform.ResourceState, error) { Read: resourceRecordRead,
Update: resourceRecordUpdate,
Delete: resourceRecordDelete,
Schema: map[string]*schema.Schema{
"type": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"domain": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"name": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
"port": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"priority": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"weight": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"value": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
},
}
}
func resourceRecordCreate(d *schema.ResourceData, meta interface{}) error {
p := meta.(*ResourceProvider) p := meta.(*ResourceProvider)
client := p.client client := p.client
// Merge the diff into the state so that we have all the attributes
// properly.
rs := s.MergeDiff(d)
var err error
newRecord := digitalocean.CreateRecord{ newRecord := digitalocean.CreateRecord{
Type: rs.Attributes["type"], Type: d.Get("type").(string),
Name: rs.Attributes["name"], Name: d.Get("name").(string),
Data: rs.Attributes["value"], Data: d.Get("value").(string),
Priority: rs.Attributes["priority"], Priority: d.Get("priority").(string),
Port: rs.Attributes["port"], Port: d.Get("port").(string),
Weight: rs.Attributes["weight"], Weight: d.Get("weight").(string),
} }
log.Printf("[DEBUG] record create configuration: %#v", newRecord) log.Printf("[DEBUG] record create configuration: %#v", newRecord)
recId, err := client.CreateRecord(d.Get("domain").(string), &newRecord)
recId, err := client.CreateRecord(rs.Attributes["domain"], &newRecord)
if err != nil { if err != nil {
return nil, fmt.Errorf("Failed to create record: %s", err) return fmt.Errorf("Failed to create record: %s", err)
} }
rs.ID = recId d.SetId(recId)
log.Printf("[INFO] Record ID: %s", rs.ID) log.Printf("[INFO] Record ID: %s", d.Id())
record, err := resource_digitalocean_record_retrieve(rs.Attributes["domain"], rs.ID, client) return resourceRecordRead(d, meta)
if err != nil {
return nil, fmt.Errorf("Couldn't find record: %s", err)
}
return resource_digitalocean_record_update_state(rs, record)
} }
func resource_digitalocean_record_update( func resourceRecordUpdate(d *schema.ResourceData, meta interface{}) error {
s *terraform.ResourceState,
d *terraform.ResourceDiff,
meta interface{}) (*terraform.ResourceState, error) {
p := meta.(*ResourceProvider) p := meta.(*ResourceProvider)
client := p.client client := p.client
rs := s.MergeDiff(d)
updateRecord := digitalocean.UpdateRecord{} var updateRecord digitalocean.UpdateRecord
if v, ok := d.GetOk("name"); ok {
if attr, ok := d.Attributes["name"]; ok { updateRecord.Name = v.(string)
updateRecord.Name = attr.New
} }
log.Printf("[DEBUG] record update configuration: %#v", updateRecord) log.Printf("[DEBUG] record update configuration: %#v", updateRecord)
err := client.UpdateRecord(d.Get("domain").(string), d.Id(), &updateRecord)
err := client.UpdateRecord(rs.Attributes["domain"], rs.ID, &updateRecord)
if err != nil { if err != nil {
return rs, fmt.Errorf("Failed to update record: %s", err) return fmt.Errorf("Failed to update record: %s", err)
} }
record, err := resource_digitalocean_record_retrieve(rs.Attributes["domain"], rs.ID, client) return resourceRecordRead(d, meta)
if err != nil {
return rs, fmt.Errorf("Couldn't find record: %s", err)
}
return resource_digitalocean_record_update_state(rs, record)
} }
func resource_digitalocean_record_destroy( func resourceRecordDelete(d *schema.ResourceData, meta interface{}) error {
s *terraform.ResourceState,
meta interface{}) error {
p := meta.(*ResourceProvider) p := meta.(*ResourceProvider)
client := p.client client := p.client
log.Printf("[INFO] Deleting record: %s, %s", s.Attributes["domain"], s.ID) log.Printf(
"[INFO] Deleting record: %s, %s", d.Get("domain").(string), d.Id())
err := client.DestroyRecord(s.Attributes["domain"], s.ID) err := client.DestroyRecord(d.Get("domain").(string), d.Id())
if err != nil { if err != nil {
// If the record is somehow already destroyed, mark as // If the record is somehow already destroyed, mark as
// succesfully gone // succesfully gone
if strings.Contains(err.Error(), "404 Not Found") { if strings.Contains(err.Error(), "404 Not Found") {
@ -105,87 +129,26 @@ func resource_digitalocean_record_destroy(
return nil return nil
} }
func resource_digitalocean_record_refresh( func resourceRecordRead(d *schema.ResourceData, meta interface{}) error {
s *terraform.ResourceState,
meta interface{}) (*terraform.ResourceState, error) {
p := meta.(*ResourceProvider) p := meta.(*ResourceProvider)
client := p.client client := p.client
rec, err := resource_digitalocean_record_retrieve(s.Attributes["domain"], s.ID, client) rec, err := client.RetrieveRecord(d.Get("domain").(string), d.Id())
if err != nil { if err != nil {
return nil, err return err
} }
return resource_digitalocean_record_update_state(s, rec) d.Set("name", rec.Name)
} d.Set("type", rec.Type)
d.Set("value", rec.Data)
func resource_digitalocean_record_diff( d.Set("weight", rec.StringWeight())
s *terraform.ResourceState, d.Set("priority", rec.StringPriority())
c *terraform.ResourceConfig, d.Set("port", rec.StringPort())
meta interface{}) (*terraform.ResourceDiff, error) {
b := &diff.ResourceBuilder{
Attrs: map[string]diff.AttrType{
"domain": diff.AttrTypeCreate,
"name": diff.AttrTypeUpdate,
"type": diff.AttrTypeCreate,
"value": diff.AttrTypeCreate,
"priority": diff.AttrTypeCreate,
"port": diff.AttrTypeCreate,
"weight": diff.AttrTypeCreate,
},
ComputedAttrs: []string{
"value",
"priority",
"weight",
"port",
},
}
return b.Diff(s, c)
}
func resource_digitalocean_record_update_state(
s *terraform.ResourceState,
rec *digitalocean.Record) (*terraform.ResourceState, error) {
s.Attributes["name"] = rec.Name
s.Attributes["type"] = rec.Type
s.Attributes["value"] = rec.Data
s.Attributes["weight"] = rec.StringWeight()
s.Attributes["priority"] = rec.StringPriority()
s.Attributes["port"] = rec.StringPort()
// We belong to a Domain // We belong to a Domain
s.Dependencies = []terraform.ResourceDependency{ d.SetDependencies([]terraform.ResourceDependency{
terraform.ResourceDependency{ID: s.Attributes["domain"]}, terraform.ResourceDependency{ID: d.Get("domain").(string)},
} })
return s, nil return nil
}
func resource_digitalocean_record_retrieve(domain string, id string, client *digitalocean.Client) (*digitalocean.Record, error) {
record, err := client.RetrieveRecord(domain, id)
if err != nil {
return nil, err
}
return &record, nil
}
func resource_digitalocean_record_validation() *config.Validator {
return &config.Validator{
Required: []string{
"type",
"domain",
},
Optional: []string{
"value",
"name",
"weight",
"port",
"priority",
},
}
} }

View File

@ -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
} }

View File

@ -27,15 +27,6 @@ func init() {
Refresh: resource_digitalocean_droplet_refresh, Refresh: resource_digitalocean_droplet_refresh,
Update: resource_digitalocean_droplet_update, Update: resource_digitalocean_droplet_update,
}, },
"digitalocean_record": resource.Resource{
ConfigValidator: resource_digitalocean_record_validation(),
Create: resource_digitalocean_record_create,
Destroy: resource_digitalocean_record_destroy,
Update: resource_digitalocean_record_update,
Diff: resource_digitalocean_record_diff,
Refresh: resource_digitalocean_record_refresh,
},
}, },
} }
} }