This repository has been archived by the owner on Jul 20, 2020. It is now read-only.
forked from digitalocean/terraform-provider-digitalocean
-
Notifications
You must be signed in to change notification settings - Fork 5
/
resource_digitalocean_database_replica.go
237 lines (196 loc) · 5.77 KB
/
resource_digitalocean_database_replica.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
package digitalocean
import (
"context"
"fmt"
"log"
"strings"
"time"
"github.com/digitalocean/godo"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
)
func resourceDigitalOceanDatabaseReplica() *schema.Resource {
return &schema.Resource{
Create: resourceDigitalOceanDatabaseReplicaCreate,
Read: resourceDigitalOceanDatabaseReplicaRead,
Delete: resourceDigitalOceanDatabaseReplicaDelete,
Importer: &schema.ResourceImporter{
State: resourceDigitalOceanDatabaseReplicaImport,
},
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.NoZeroValues,
},
"cluster_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.NoZeroValues,
},
"region": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"size": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"private_network_uuid": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Computed: true,
ValidateFunc: validation.NoZeroValues,
},
"host": {
Type: schema.TypeString,
Computed: true,
},
"private_host": {
Type: schema.TypeString,
Computed: true,
},
"port": {
Type: schema.TypeInt,
Computed: true,
},
"uri": {
Type: schema.TypeString,
Computed: true,
Sensitive: true,
},
"private_uri": {
Type: schema.TypeString,
Computed: true,
Sensitive: true,
},
"database": {
Type: schema.TypeString,
Computed: true,
},
"user": {
Type: schema.TypeString,
Computed: true,
},
"password": {
Type: schema.TypeString,
Computed: true,
Sensitive: true,
},
"tags": {
Type: schema.TypeSet,
Optional: true,
ForceNew: true,
Elem: &schema.Schema{
Type: schema.TypeString,
ValidateFunc: validateTag,
},
Set: HashStringIgnoreCase,
},
},
}
}
func resourceDigitalOceanDatabaseReplicaCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*CombinedConfig).godoClient()
clusterId := d.Get("cluster_id").(string)
opts := &godo.DatabaseCreateReplicaRequest{
Name: d.Get("name").(string),
Region: d.Get("region").(string),
Size: d.Get("size").(string),
Tags: expandTags(d.Get("tags").(*schema.Set).List()),
}
if v, ok := d.GetOk("private_network_uuid"); ok {
opts.PrivateNetworkUUID = v.(string)
}
log.Printf("[DEBUG] DatabaseReplica create configuration: %#v", opts)
replica, _, err := client.Databases.CreateReplica(context.Background(), clusterId, opts)
if err != nil {
return fmt.Errorf("Error creating DatabaseReplica: %s", err)
}
replica, err = waitForDatabaseReplica(client, clusterId, "online", replica.Name)
if err != nil {
return fmt.Errorf("Error creating DatabaseReplica: %s", err)
}
d.SetId(makeReplicaId(clusterId, replica.Name))
log.Printf("[INFO] DatabaseReplica Name: %s", replica.Name)
return resourceDigitalOceanDatabaseReplicaRead(d, meta)
}
func resourceDigitalOceanDatabaseReplicaRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*CombinedConfig).godoClient()
clusterId := d.Get("cluster_id").(string)
name := d.Get("name").(string)
replica, resp, err := client.Databases.GetReplica(context.Background(), clusterId, name)
if err != nil {
// If the database is somehow already destroyed, mark as
// successfully gone
if resp != nil && resp.StatusCode == 404 {
d.SetId("")
return nil
}
return fmt.Errorf("Error retrieving DatabaseReplica: %s", err)
}
d.Set("region", replica.Region)
d.Set("tags", replica.Tags)
// Computed values
d.Set("host", replica.Connection.Host)
d.Set("private_host", replica.PrivateConnection.Host)
d.Set("port", replica.Connection.Port)
d.Set("uri", replica.Connection.URI)
d.Set("private_uri", replica.PrivateConnection.URI)
d.Set("database", replica.Connection.Database)
d.Set("user", replica.Connection.User)
d.Set("password", replica.Connection.Password)
d.Set("private_network_uuid", replica.PrivateNetworkUUID)
return nil
}
func resourceDigitalOceanDatabaseReplicaImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
if strings.Contains(d.Id(), ",") {
s := strings.Split(d.Id(), ",")
d.SetId(makeReplicaId(s[0], s[1]))
d.Set("cluster_id", s[0])
d.Set("name", s[1])
}
return []*schema.ResourceData{d}, nil
}
func resourceDigitalOceanDatabaseReplicaDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*CombinedConfig).godoClient()
clusterId := d.Get("cluster_id").(string)
name := d.Get("name").(string)
log.Printf("[INFO] Deleting DatabaseReplica: %s", d.Id())
_, err := client.Databases.DeleteReplica(context.Background(), clusterId, name)
if err != nil {
return fmt.Errorf("Error deleting DatabaseReplica: %s", err)
}
d.SetId("")
return nil
}
func makeReplicaId(clusterId string, replicaName string) string {
return fmt.Sprintf("%s/replicas/%s", clusterId, replicaName)
}
func waitForDatabaseReplica(client *godo.Client, cluster_id, status, name string) (*godo.DatabaseReplica, error) {
ticker := time.NewTicker(15 * time.Second)
timeout := 120
n := 0
for range ticker.C {
replica, _, err := client.Databases.GetReplica(context.Background(), cluster_id, name)
if err != nil {
ticker.Stop()
return nil, fmt.Errorf("Error trying to read DatabaseReplica state: %s", err)
}
if replica.Status == status {
ticker.Stop()
return replica, nil
}
if n > timeout {
ticker.Stop()
break
}
n++
}
return nil, fmt.Errorf("Timeout waiting to DatabaseReplica to become %s", status)
}