forked from hashicorp/terraform
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_digitalocean_domain.go
89 lines (71 loc) · 1.84 KB
/
resource_digitalocean_domain.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package digitalocean
import (
"fmt"
"log"
"strings"
"github.com/hashicorp/terraform/helper/schema"
"github.com/pearkes/digitalocean"
)
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
// Build up our creation options
opts := digitalocean.CreateDomain{
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 fmt.Errorf("Error creating Domain: %s", err)
}
d.SetId(name)
log.Printf("[INFO] Domain Name: %s", name)
return nil
}
func resourceDomainDelete(d *schema.ResourceData, meta interface{}) error {
p := meta.(*ResourceProvider)
client := p.client
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") {
d.SetId("")
return nil
}
return fmt.Errorf("Error retrieving domain: %s", err)
}
d.Set("name", domain.Name)
return nil
}