forked from hashicorp/terraform
-
Notifications
You must be signed in to change notification settings - Fork 2
/
resource_heroku_domain.go
86 lines (66 loc) · 1.87 KB
/
resource_heroku_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
package heroku
import (
"fmt"
"log"
"github.com/cyberdelia/heroku-go/v3"
"github.com/hashicorp/terraform/helper/schema"
)
func resourceHerokuDomain() *schema.Resource {
return &schema.Resource{
Create: resourceHerokuDomainCreate,
Read: resourceHerokuDomainRead,
Delete: resourceHerokuDomainDelete,
Schema: map[string]*schema.Schema{
"hostname": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"app": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"cname": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
},
}
}
func resourceHerokuDomainCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*heroku.Service)
app := d.Get("app").(string)
hostname := d.Get("hostname").(string)
log.Printf("[DEBUG] Domain create configuration: %#v, %#v", app, hostname)
do, err := client.DomainCreate(app, heroku.DomainCreateOpts{Hostname: hostname})
if err != nil {
return err
}
d.SetId(do.ID)
d.Set("hostname", do.Hostname)
d.Set("cname", fmt.Sprintf("%s.herokuapp.com", app))
log.Printf("[INFO] Domain ID: %s", d.Id())
return nil
}
func resourceHerokuDomainDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*heroku.Service)
log.Printf("[INFO] Deleting Domain: %s", d.Id())
// Destroy the domain
err := client.DomainDelete(d.Get("app").(string), d.Id())
if err != nil {
return fmt.Errorf("Error deleting domain: %s", err)
}
return nil
}
func resourceHerokuDomainRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*heroku.Service)
app := d.Get("app").(string)
do, err := client.DomainInfo(app, d.Id())
if err != nil {
return fmt.Errorf("Error retrieving domain: %s", err)
}
d.Set("hostname", do.Hostname)
d.Set("cname", fmt.Sprintf("%s.herokuapp.com", app))
return nil
}