forked from hashicorp/terraform
/
resource_rancher_host.go
200 lines (167 loc) · 4.64 KB
/
resource_rancher_host.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package rancher
import (
"fmt"
"log"
"time"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
rancher "github.com/rancher/go-rancher/client"
)
// ro_labels are used internally by Rancher
// They are not documented and should not be set in Terraform
var ro_labels = []string{
"io.rancher.host.agent_image",
"io.rancher.host.docker_version",
"io.rancher.host.kvm",
"io.rancher.host.linux_kernel_version",
}
func resourceRancherHost() *schema.Resource {
return &schema.Resource{
Create: resourceRancherHostCreate,
Read: resourceRancherHostRead,
Update: resourceRancherHostUpdate,
Delete: resourceRancherHostDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"id": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"name": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"description": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
"environment_id": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"hostname": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"labels": {
Type: schema.TypeMap,
Optional: true,
},
},
}
}
func resourceRancherHostCreate(d *schema.ResourceData, meta interface{}) error {
log.Printf("[INFO][rancher] Creating Host: %s", d.Id())
client, err := meta.(*Config).EnvironmentClient(d.Get("environment_id").(string))
if err != nil {
return err
}
hosts, _ := client.Host.List(NewListOpts())
hostname := d.Get("hostname").(string)
var host rancher.Host
for _, h := range hosts.Data {
if h.Hostname == hostname {
host = h
break
}
}
if host.Hostname == "" {
return fmt.Errorf("Failed to find host %s", hostname)
}
d.SetId(host.Id)
return resourceRancherHostUpdate(d, meta)
}
func resourceRancherHostRead(d *schema.ResourceData, meta interface{}) error {
log.Printf("[INFO] Refreshing Host: %s", d.Id())
client, err := meta.(*Config).EnvironmentClient(d.Get("environment_id").(string))
if err != nil {
return err
}
host, err := client.Host.ById(d.Id())
if err != nil {
return err
}
log.Printf("[INFO] Host Name: %s", host.Name)
d.Set("description", host.Description)
d.Set("name", host.Name)
d.Set("hostname", host.Hostname)
labels := host.Labels
// Remove read-only labels
for _, lbl := range ro_labels {
delete(labels, lbl)
}
d.Set("labels", host.Labels)
return nil
}
func resourceRancherHostUpdate(d *schema.ResourceData, meta interface{}) error {
log.Printf("[INFO] Updating Host: %s", d.Id())
client, err := meta.(*Config).EnvironmentClient(d.Get("environment_id").(string))
if err != nil {
return err
}
name := d.Get("name").(string)
description := d.Get("description").(string)
// Process labels: merge ro_labels into new labels
labels := d.Get("labels").(map[string]interface{})
host, err := client.Host.ById(d.Id())
if err != nil {
return err
}
for _, lbl := range ro_labels {
labels[lbl] = host.Labels[lbl]
}
data := map[string]interface{}{
"name": &name,
"description": &description,
"labels": &labels,
}
var newHost rancher.Host
if err := client.Update("host", &host.Resource, data, &newHost); err != nil {
return err
}
return resourceRancherHostRead(d, meta)
}
func resourceRancherHostDelete(d *schema.ResourceData, meta interface{}) error {
log.Printf("[INFO] Deleting Host: %s", d.Id())
id := d.Id()
client, err := meta.(*Config).EnvironmentClient(d.Get("environment_id").(string))
if err != nil {
return err
}
host, err := client.Host.ById(id)
if err != nil {
return err
}
if err := client.Host.Delete(host); err != nil {
return fmt.Errorf("Error deleting Host: %s", err)
}
log.Printf("[DEBUG] Waiting for host (%s) to be removed", id)
stateConf := &resource.StateChangeConf{
Pending: []string{"active", "removed", "removing"},
Target: []string{"removed"},
Refresh: HostStateRefreshFunc(client, id),
Timeout: 10 * time.Minute,
Delay: 1 * time.Second,
MinTimeout: 3 * time.Second,
}
_, waitErr := stateConf.WaitForState()
if waitErr != nil {
return fmt.Errorf(
"Error waiting for host (%s) to be removed: %s", id, waitErr)
}
d.SetId("")
return nil
}
// HostStateRefreshFunc returns a resource.StateRefreshFunc that is used to watch
// a Rancher Host.
func HostStateRefreshFunc(client *rancher.RancherClient, hostID string) resource.StateRefreshFunc {
return func() (interface{}, string, error) {
host, err := client.Host.ById(hostID)
if err != nil {
return nil, "", err
}
return host, host.State, nil
}
}