providers/digitalocean: convert domain to helper/schema [GH-187]

This commit is contained in:
Mitchell Hashimoto 2014-09-09 13:42:06 -07:00
parent 228fd035ba
commit 78672ead21
3 changed files with 50 additions and 75 deletions

View File

@ -22,6 +22,7 @@ func Provider() *schema.Provider {
}, },
ResourcesMap: map[string]*schema.Resource{ ResourcesMap: map[string]*schema.Resource{
"digitalocean_domain": resourceDomain(),
"digitalocean_record": resourceRecord(), "digitalocean_record": resourceRecord(),
}, },
} }

View File

@ -5,103 +5,85 @@ 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/pearkes/digitalocean" "github.com/pearkes/digitalocean"
) )
func resource_digitalocean_domain_create( func resourceDomain() *schema.Resource {
s *terraform.ResourceState, return &schema.Resource{
d *terraform.ResourceDiff, Create: resourceDomainCreate,
meta interface{}) (*terraform.ResourceState, error) { Read: resourceDomainRead,
Delete: resourceDomainDelete,
Schema: map[string]*schema.Schema{
"name": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"ip_address": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
},
}
}
func resourceDomainCreate(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)
// Build up our creation options // Build up our creation options
opts := digitalocean.CreateDomain{ opts := digitalocean.CreateDomain{
Name: rs.Attributes["name"], Name: d.Get("name").(string),
IPAddress: rs.Attributes["ip_address"], IPAddress: d.Get("ip_address").(string),
} }
log.Printf("[DEBUG] Domain create configuration: %#v", opts) log.Printf("[DEBUG] Domain create configuration: %#v", opts)
name, err := client.CreateDomain(&opts) name, err := client.CreateDomain(&opts)
if err != nil { if err != nil {
return nil, fmt.Errorf("Error creating Domain: %s", err) return fmt.Errorf("Error creating Domain: %s", err)
} }
rs.ID = name d.SetId(name)
log.Printf("[INFO] Domain Name: %s", name) log.Printf("[INFO] Domain Name: %s", name)
return rs, nil
}
func resource_digitalocean_domain_destroy(
s *terraform.ResourceState,
meta interface{}) error {
p := meta.(*ResourceProvider)
client := p.client
log.Printf("[INFO] Deleting Domain: %s", s.ID)
err := client.DestroyDomain(s.ID)
if err != nil {
return fmt.Errorf("Error deleting Domain: %s", err)
}
return nil return nil
} }
func resource_digitalocean_domain_refresh( func resourceDomainDelete(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
domain, err := client.RetrieveDomain(s.ID) log.Printf("[INFO] Deleting Domain: %s", d.Id())
err := client.DestroyDomain(d.Id())
if err != nil {
return fmt.Errorf("Error deleting Domain: %s", err)
}
d.SetId("")
return nil
}
func resourceDomainRead(d *schema.ResourceData, meta interface{}) error {
p := meta.(*ResourceProvider)
client := p.client
domain, err := client.RetrieveDomain(d.Id())
if err != nil { if err != nil {
// If the domain is somehow already destroyed, mark as // If the domain 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") {
return nil, nil d.SetId("")
return nil
} }
return s, fmt.Errorf("Error retrieving domain: %s", err) return fmt.Errorf("Error retrieving domain: %s", err)
} }
s.Attributes["name"] = domain.Name d.Set("name", domain.Name)
return s, nil return nil
}
func resource_digitalocean_domain_diff(
s *terraform.ResourceState,
c *terraform.ResourceConfig,
meta interface{}) (*terraform.ResourceDiff, error) {
b := &diff.ResourceBuilder{
Attrs: map[string]diff.AttrType{
"name": diff.AttrTypeCreate,
"ip_address": diff.AttrTypeCreate,
},
ComputedAttrs: []string{},
}
return b.Diff(s, c)
}
func resource_digitalocean_domain_validation() *config.Validator {
return &config.Validator{
Required: []string{
"name",
"ip_address",
},
Optional: []string{},
}
} }

View File

@ -11,14 +11,6 @@ var resourceMap *resource.Map
func init() { func init() {
resourceMap = &resource.Map{ resourceMap = &resource.Map{
Mapping: map[string]resource.Resource{ Mapping: map[string]resource.Resource{
"digitalocean_domain": resource.Resource{
ConfigValidator: resource_digitalocean_domain_validation(),
Create: resource_digitalocean_domain_create,
Destroy: resource_digitalocean_domain_destroy,
Diff: resource_digitalocean_domain_diff,
Refresh: resource_digitalocean_domain_refresh,
},
"digitalocean_droplet": resource.Resource{ "digitalocean_droplet": resource.Resource{
ConfigValidator: resource_digitalocean_droplet_validation(), ConfigValidator: resource_digitalocean_droplet_validation(),
Create: resource_digitalocean_droplet_create, Create: resource_digitalocean_droplet_create,