-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathresource_transip_domain.go
103 lines (81 loc) · 2.33 KB
/
resource_transip_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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package main
import (
"fmt"
"time"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/transip/gotransip/v6"
"github.com/transip/gotransip/v6/domain"
"github.com/transip/gotransip/v6/repository"
)
func resourceDomain() *schema.Resource {
return &schema.Resource{
Create: resourceDomainCreate,
Read: resourceDomainRead,
// Update: resourceDomainUpdate,
Delete: resourceDomainDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"name": &schema.Schema{
Type: schema.TypeString,
Description: "The name, including the tld of this domain",
Required: true,
// TODO: implement update
ForceNew: true,
},
},
}
}
func resourceDomainCreate(d *schema.ResourceData, m interface{}) error {
name := d.Get("name").(string)
client := m.(repository.Client)
repository := domain.Repository{Client: client}
register := domain.Register{
DomainName: name,
}
err := repository.Register(register)
if err != nil {
return fmt.Errorf("failed to register domain %q: %s", name, err)
}
err = resource.Retry(30*time.Second, func() *resource.RetryError {
var err error
_, err = repository.GetByDomainName(name)
if err != nil {
return resource.RetryableError(err)
}
return nil
})
if err != nil {
return fmt.Errorf("Error waiting for domain to be created: %s", err)
}
d.SetId(name)
return resourceDomainRead(d, m)
}
func resourceDomainRead(d *schema.ResourceData, m interface{}) error {
name := d.Id()
client := m.(repository.Client)
repository := domain.Repository{Client: client}
i, err := repository.GetByDomainName(name)
if err != nil {
return fmt.Errorf("failed to lookup domain %q: %s", name, err)
}
d.SetId(i.Name)
d.Set("name", name)
return nil
}
// func resourceDomainUpdate(d *schema.ResourceData, m interface{}) error {
// return resourceDomainRead(d, m)
// }
func resourceDomainDelete(d *schema.ResourceData, m interface{}) error {
name := d.Get("name").(string)
client := m.(repository.Client)
repository := domain.Repository{Client: client}
err := repository.Cancel(name, gotransip.CancellationTimeImmediately)
if err != nil {
return fmt.Errorf("failed to cancel domain %q: %s", name, err)
}
d.SetId("")
return nil
}