forked from hashicorp/terraform
-
Notifications
You must be signed in to change notification settings - Fork 2
/
resource_rancher_registry.go
244 lines (206 loc) · 6.25 KB
/
resource_rancher_registry.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package rancher
import (
"fmt"
"log"
"time"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
rancherClient "github.com/rancher/go-rancher/client"
)
func resourceRancherRegistry() *schema.Resource {
return &schema.Resource{
Create: resourceRancherRegistryCreate,
Read: resourceRancherRegistryRead,
Update: resourceRancherRegistryUpdate,
Delete: resourceRancherRegistryDelete,
Importer: &schema.ResourceImporter{
State: resourceRancherRegistryImport,
},
Schema: map[string]*schema.Schema{
"id": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"server_address": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: 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,
ForceNew: true,
},
},
}
}
func resourceRancherRegistryCreate(d *schema.ResourceData, meta interface{}) error {
log.Printf("[INFO] Creating Registry: %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)
serverAddress := d.Get("server_address").(string)
registry := rancherClient.Registry{
Name: name,
Description: description,
ServerAddress: serverAddress,
}
newRegistry, err := client.Registry.Create(®istry)
if err != nil {
return err
}
stateConf := &resource.StateChangeConf{
Pending: []string{"active", "removed", "removing"},
Target: []string{"active"},
Refresh: RegistryStateRefreshFunc(client, newRegistry.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 registry (%s) to be created: %s", newRegistry.Id, waitErr)
}
d.SetId(newRegistry.Id)
log.Printf("[INFO] Registry ID: %s", d.Id())
return resourceRancherRegistryRead(d, meta)
}
func resourceRancherRegistryRead(d *schema.ResourceData, meta interface{}) error {
log.Printf("[INFO] Refreshing Registry: %s", d.Id())
client, err := meta.(*Config).EnvironmentClient(d.Get("environment_id").(string))
if err != nil {
return err
}
registry, err := client.Registry.ById(d.Id())
if err != nil {
return err
}
if registry == nil {
log.Printf("[INFO] Registry %s not found", d.Id())
d.SetId("")
return nil
}
if removed(registry.State) {
log.Printf("[INFO] Registry %s was removed on %v", d.Id(), registry.Removed)
d.SetId("")
return nil
}
log.Printf("[INFO] Registry Name: %s", registry.Name)
d.Set("description", registry.Description)
d.Set("name", registry.Name)
d.Set("server_address", registry.ServerAddress)
d.Set("environment_id", registry.AccountId)
return nil
}
func resourceRancherRegistryUpdate(d *schema.ResourceData, meta interface{}) error {
client, err := meta.(*Config).EnvironmentClient(d.Get("environment_id").(string))
if err != nil {
return err
}
registry, err := client.Registry.ById(d.Id())
if err != nil {
return err
}
name := d.Get("name").(string)
description := d.Get("description").(string)
registry.Name = name
registry.Description = description
client.Registry.Update(registry, ®istry)
return resourceRancherRegistryRead(d, meta)
}
func resourceRancherRegistryDelete(d *schema.ResourceData, meta interface{}) error {
log.Printf("[INFO] Deleting Registry: %s", d.Id())
id := d.Id()
client, err := meta.(*Config).EnvironmentClient(d.Get("environment_id").(string))
if err != nil {
return err
}
reg, err := client.Registry.ById(id)
if err != nil {
return err
}
// Step 1: Deactivate
if _, e := client.Registry.ActionDeactivate(reg); e != nil {
return fmt.Errorf("Error deactivating Registry: %s", err)
}
log.Printf("[DEBUG] Waiting for registry (%s) to be deactivated", id)
stateConf := &resource.StateChangeConf{
Pending: []string{"active", "inactive", "deactivating"},
Target: []string{"inactive"},
Refresh: RegistryStateRefreshFunc(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 registry (%s) to be deactivated: %s", id, waitErr)
}
// Update resource to reflect its state
reg, err = client.Registry.ById(id)
if err != nil {
return fmt.Errorf("Failed to refresh state of deactivated registry (%s): %s", id, err)
}
// Step 2: Remove
if _, err := client.Registry.ActionRemove(reg); err != nil {
return fmt.Errorf("Error removing Registry: %s", err)
}
log.Printf("[DEBUG] Waiting for registry (%s) to be removed", id)
stateConf = &resource.StateChangeConf{
Pending: []string{"inactive", "removed", "removing"},
Target: []string{"removed"},
Refresh: RegistryStateRefreshFunc(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 registry (%s) to be removed: %s", id, waitErr)
}
d.SetId("")
return nil
}
func resourceRancherRegistryImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
envID, resourceID := splitID(d.Id())
d.SetId(resourceID)
if envID != "" {
d.Set("environment_id", envID)
} else {
client, err := meta.(*Config).GlobalClient()
if err != nil {
return []*schema.ResourceData{}, err
}
registry, err := client.Registry.ById(d.Id())
if err != nil {
return []*schema.ResourceData{}, err
}
d.Set("environment_id", registry.AccountId)
}
return []*schema.ResourceData{d}, nil
}
// RegistryStateRefreshFunc returns a resource.StateRefreshFunc that is used to watch
// a Rancher Environment.
func RegistryStateRefreshFunc(client *rancherClient.RancherClient, registryID string) resource.StateRefreshFunc {
return func() (interface{}, string, error) {
env, err := client.Registry.ById(registryID)
if err != nil {
return nil, "", err
}
return env, env.State, nil
}
}