forked from hashicorp/terraform
-
Notifications
You must be signed in to change notification settings - Fork 2
/
resource_consul_node.go
156 lines (125 loc) · 3.49 KB
/
resource_consul_node.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package consul
import (
"fmt"
consulapi "github.com/hashicorp/consul/api"
"github.com/hashicorp/terraform/helper/schema"
)
func resourceConsulNode() *schema.Resource {
return &schema.Resource{
Create: resourceConsulNodeCreate,
Update: resourceConsulNodeCreate,
Read: resourceConsulNodeRead,
Delete: resourceConsulNodeDelete,
Schema: map[string]*schema.Schema{
"address": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"datacenter": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"name": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"token": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
},
}
}
func resourceConsulNodeCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*consulapi.Client)
catalog := client.Catalog()
var dc string
if v, ok := d.GetOk("datacenter"); ok {
dc = v.(string)
} else {
var err error
if dc, err = getDC(d, client); err != nil {
return err
}
}
var token string
if v, ok := d.GetOk("token"); ok {
token = v.(string)
}
// Setup the operations using the datacenter
wOpts := consulapi.WriteOptions{Datacenter: dc, Token: token}
address := d.Get("address").(string)
name := d.Get("name").(string)
registration := &consulapi.CatalogRegistration{
Address: address,
Datacenter: dc,
Node: name,
}
if _, err := catalog.Register(registration, &wOpts); err != nil {
return fmt.Errorf("Failed to register Consul catalog node with name '%s' at address '%s' in %s: %v",
name, address, dc, err)
}
// Update the resource
qOpts := consulapi.QueryOptions{Datacenter: dc}
if _, _, err := catalog.Node(name, &qOpts); err != nil {
return fmt.Errorf("Failed to read Consul catalog node with name '%s' at address '%s' in %s: %v",
name, address, dc, err)
} else {
d.Set("datacenter", dc)
}
d.SetId(fmt.Sprintf("%s-%s", name, address))
return nil
}
func resourceConsulNodeRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*consulapi.Client)
catalog := client.Catalog()
// Get the DC, error if not available.
var dc string
if v, ok := d.GetOk("datacenter"); ok {
dc = v.(string)
}
name := d.Get("name").(string)
// Setup the operations using the datacenter
qOpts := consulapi.QueryOptions{Datacenter: dc}
if _, _, err := catalog.Node(name, &qOpts); err != nil {
return fmt.Errorf("Failed to get name '%s' from Consul catalog: %v", name, err)
}
return nil
}
func resourceConsulNodeDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*consulapi.Client)
catalog := client.Catalog()
var dc string
if v, ok := d.GetOk("datacenter"); ok {
dc = v.(string)
} else {
var err error
if dc, err = getDC(d, client); err != nil {
return err
}
}
var token string
if v, ok := d.GetOk("token"); ok {
token = v.(string)
}
// Setup the operations using the datacenter
wOpts := consulapi.WriteOptions{Datacenter: dc, Token: token}
address := d.Get("address").(string)
name := d.Get("name").(string)
deregistration := consulapi.CatalogDeregistration{
Address: address,
Datacenter: dc,
Node: name,
}
if _, err := catalog.Deregister(&deregistration, &wOpts); err != nil {
return fmt.Errorf("Failed to deregister Consul catalog node with name '%s' at address '%s' in %s: %v",
name, address, dc, err)
}
// Clear the ID
d.SetId("")
return nil
}