mirror of
https://github.com/opentofu/opentofu.git
synced 2025-02-25 18:45:20 -06:00
providers/digitalocean: convert domain to helper/schema [GH-187]
This commit is contained in:
parent
228fd035ba
commit
78672ead21
@ -22,6 +22,7 @@ func Provider() *schema.Provider {
|
||||
},
|
||||
|
||||
ResourcesMap: map[string]*schema.Resource{
|
||||
"digitalocean_domain": resourceDomain(),
|
||||
"digitalocean_record": resourceRecord(),
|
||||
},
|
||||
}
|
||||
|
@ -5,103 +5,85 @@ import (
|
||||
"log"
|
||||
"strings"
|
||||
|
||||
"github.com/hashicorp/terraform/helper/config"
|
||||
"github.com/hashicorp/terraform/helper/diff"
|
||||
"github.com/hashicorp/terraform/terraform"
|
||||
"github.com/hashicorp/terraform/helper/schema"
|
||||
"github.com/pearkes/digitalocean"
|
||||
)
|
||||
|
||||
func resource_digitalocean_domain_create(
|
||||
s *terraform.ResourceState,
|
||||
d *terraform.ResourceDiff,
|
||||
meta interface{}) (*terraform.ResourceState, error) {
|
||||
func resourceDomain() *schema.Resource {
|
||||
return &schema.Resource{
|
||||
Create: resourceDomainCreate,
|
||||
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)
|
||||
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
|
||||
opts := digitalocean.CreateDomain{
|
||||
Name: rs.Attributes["name"],
|
||||
IPAddress: rs.Attributes["ip_address"],
|
||||
Name: d.Get("name").(string),
|
||||
IPAddress: d.Get("ip_address").(string),
|
||||
}
|
||||
|
||||
log.Printf("[DEBUG] Domain create configuration: %#v", opts)
|
||||
|
||||
name, err := client.CreateDomain(&opts)
|
||||
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)
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
func resource_digitalocean_domain_refresh(
|
||||
s *terraform.ResourceState,
|
||||
meta interface{}) (*terraform.ResourceState, error) {
|
||||
func resourceDomainDelete(d *schema.ResourceData, meta interface{}) error {
|
||||
p := meta.(*ResourceProvider)
|
||||
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 the domain is somehow already destroyed, mark as
|
||||
// succesfully gone
|
||||
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
|
||||
}
|
||||
|
||||
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{},
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
@ -11,14 +11,6 @@ var resourceMap *resource.Map
|
||||
func init() {
|
||||
resourceMap = &resource.Map{
|
||||
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{
|
||||
ConfigValidator: resource_digitalocean_droplet_validation(),
|
||||
Create: resource_digitalocean_droplet_create,
|
||||
|
Loading…
Reference in New Issue
Block a user