-
Notifications
You must be signed in to change notification settings - Fork 133
/
resource_cloud_project_database_ip_restriction.go
256 lines (225 loc) · 8.56 KB
/
resource_cloud_project_database_ip_restriction.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
245
246
247
248
249
250
251
252
253
254
255
256
package ovh
import (
"context"
"fmt"
"log"
"net/url"
"strings"
"time"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/ovh/go-ovh/ovh"
"github.com/ovh/terraform-provider-ovh/ovh/helpers"
"github.com/ovh/terraform-provider-ovh/ovh/helpers/hashcode"
)
func resourceCloudProjectDatabaseIpRestriction() *schema.Resource {
return &schema.Resource{
CreateContext: resourceCloudProjectDatabaseIpRestrictionCreate,
ReadContext: resourceCloudProjectDatabaseIpRestrictionRead,
DeleteContext: resourceCloudProjectDatabaseIpRestrictionDelete,
UpdateContext: resourceCloudProjectDatabaseIpRestrictionUpdate,
Importer: &schema.ResourceImporter{
State: resourceCloudProjectDatabaseIpRestrictionImportState,
},
Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(20 * time.Minute),
Update: schema.DefaultTimeout(20 * time.Minute),
Delete: schema.DefaultTimeout(20 * time.Minute),
},
Schema: map[string]*schema.Schema{
"service_name": {
Type: schema.TypeString,
ForceNew: true,
Required: true,
DefaultFunc: schema.EnvDefaultFunc("OVH_CLOUD_PROJECT_SERVICE", nil),
},
"engine": {
Type: schema.TypeString,
Description: "Name of the engine of the service",
ForceNew: true,
Required: true,
},
"cluster_id": {
Type: schema.TypeString,
Description: "Id of the database cluster",
ForceNew: true,
Required: true,
},
"ip": {
Type: schema.TypeString,
Description: "Authorized IP",
ForceNew: true,
Required: true,
},
"description": {
Type: schema.TypeString,
Description: "Description of the IP restriction",
Optional: true,
},
//Computed
"status": {
Type: schema.TypeString,
Description: "Current status of the IP restriction",
Computed: true,
},
},
}
}
func resourceCloudProjectDatabaseIpRestrictionImportState(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
givenId := d.Id()
n := 4
splitId := strings.SplitN(givenId, "/", n)
if len(splitId) != n {
return nil, fmt.Errorf("Import Id is not service_name/engine/cluster_id/ip formatted")
}
serviceName := splitId[0]
engine := splitId[1]
clusterId := splitId[2]
ip := splitId[3]
d.SetId(hashcode.Strings([]string{ip}))
d.Set("engine", engine)
d.Set("service_name", serviceName)
d.Set("cluster_id", clusterId)
d.Set("ip", ip)
results := make([]*schema.ResourceData, 1)
results[0] = d
return results, nil
}
func resourceCloudProjectDatabaseIpRestrictionCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
config := meta.(*Config)
serviceName := d.Get("service_name").(string)
engine := d.Get("engine").(string)
clusterId := d.Get("cluster_id").(string)
endpoint := fmt.Sprintf("/cloud/project/%s/database/%s/%s/ipRestriction",
url.PathEscape(serviceName),
url.PathEscape(engine),
url.PathEscape(clusterId),
)
params := (&CloudProjectDatabaseIpRestrictionCreateOpts{}).FromResource(d)
res := &CloudProjectDatabaseIpRestrictionResponse{}
return diag.FromErr(
resource.RetryContext(ctx, d.Timeout(schema.TimeoutCreate),
func() *resource.RetryError {
log.Printf("[DEBUG] Will create IP restriction: %+v for cluster %s from project %s", params, clusterId, serviceName)
rErr := config.OVHClient.Post(endpoint, params, res)
if rErr != nil {
if errOvh, ok := rErr.(*ovh.APIError); ok && (errOvh.Code == 409) {
return resource.RetryableError(rErr)
}
return resource.NonRetryableError(fmt.Errorf("calling Post %s with params %+v:\n\t %q", endpoint, params, rErr))
}
log.Printf("[DEBUG] Waiting for IP restriction %s to be READY", res.Ip)
rErr = waitForCloudProjectDatabaseIpRestrictionReady(ctx, config.OVHClient, serviceName, engine, clusterId, res.Ip, d.Timeout(schema.TimeoutCreate))
if rErr != nil {
return resource.NonRetryableError(fmt.Errorf("timeout while waiting IP restriction %s to be READY: %w", res.Ip, rErr))
}
log.Printf("[DEBUG] IP restriction %s is READY", res.Ip)
d.SetId(hashcode.Strings([]string{res.Ip}))
readDiags := resourceCloudProjectDatabaseIpRestrictionRead(ctx, d, meta)
rErr = diagnosticsToError(readDiags)
if rErr != nil {
return resource.NonRetryableError(rErr)
}
return nil
},
),
)
}
func resourceCloudProjectDatabaseIpRestrictionRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
config := meta.(*Config)
serviceName := d.Get("service_name").(string)
engine := d.Get("engine").(string)
clusterId := d.Get("cluster_id").(string)
ip := d.Get("ip").(string)
endpoint := fmt.Sprintf("/cloud/project/%s/database/%s/%s/ipRestriction/%s",
url.PathEscape(serviceName),
url.PathEscape(engine),
url.PathEscape(clusterId),
url.PathEscape(ip),
)
res := &CloudProjectDatabaseIpRestrictionResponse{}
log.Printf("[DEBUG] Will read IP restriction %s from cluster %s from project %s", ip, clusterId, serviceName)
if err := config.OVHClient.Get(endpoint, res); err != nil {
return diag.FromErr(helpers.CheckDeleted(d, err, endpoint))
}
d.SetId(hashcode.Strings([]string{res.Ip}))
for k, v := range res.ToMap() {
d.Set(k, v)
}
log.Printf("[DEBUG] Read IP restriction %+v", res)
return nil
}
func resourceCloudProjectDatabaseIpRestrictionUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
config := meta.(*Config)
serviceName := d.Get("service_name").(string)
engine := d.Get("engine").(string)
clusterId := d.Get("cluster_id").(string)
ip := d.Get("ip").(string)
endpoint := fmt.Sprintf("/cloud/project/%s/database/%s/%s/ipRestriction/%s",
url.PathEscape(serviceName),
url.PathEscape(engine),
url.PathEscape(clusterId),
url.PathEscape(ip),
)
params := (&CloudProjectDatabaseIpRestrictionUpdateOpts{}).FromResource(d)
err := resource.Retry(d.Timeout(schema.TimeoutUpdate), func() *resource.RetryError {
log.Printf("[DEBUG] Will update IP restriction: %+v from cluster %s from project %s", params, clusterId, serviceName)
rErr := config.OVHClient.Put(endpoint, params, nil)
if rErr != nil {
if errOvh, ok := rErr.(*ovh.APIError); ok && (errOvh.Code == 409) {
return resource.RetryableError(rErr)
}
return resource.NonRetryableError(fmt.Errorf("calling Put %s with params %+v:\n\t %q", endpoint, params, rErr))
}
log.Printf("[DEBUG] Waiting for IP restriction %s to be READY", ip)
rErr = waitForCloudProjectDatabaseIpRestrictionReady(ctx, config.OVHClient, serviceName, engine, clusterId, ip, d.Timeout(schema.TimeoutUpdate))
if rErr != nil {
return resource.NonRetryableError(fmt.Errorf("timeout while waiting IP restriction %s to be READY: %w", ip, rErr))
}
log.Printf("[DEBUG] IP restriction %s is READY", ip)
readDiags := resourceCloudProjectDatabaseIpRestrictionRead(ctx, d, meta)
rErr = diagnosticsToError(readDiags)
if rErr != nil {
return resource.NonRetryableError(rErr)
}
return nil
})
return diag.FromErr(err)
}
func resourceCloudProjectDatabaseIpRestrictionDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
config := meta.(*Config)
serviceName := d.Get("service_name").(string)
engine := d.Get("engine").(string)
clusterId := d.Get("cluster_id").(string)
ip := d.Get("ip").(string)
endpoint := fmt.Sprintf("/cloud/project/%s/database/%s/%s/ipRestriction/%s",
url.PathEscape(serviceName),
url.PathEscape(engine),
url.PathEscape(clusterId),
url.PathEscape(ip),
)
err := resource.Retry(d.Timeout(schema.TimeoutDelete), func() *resource.RetryError {
log.Printf("[DEBUG] Will delete IP restriction %s from cluster %s from project %s", ip, clusterId, serviceName)
rErr := config.OVHClient.Delete(endpoint, nil)
if rErr != nil {
if errOvh, ok := rErr.(*ovh.APIError); ok && (errOvh.Code == 409) {
return resource.RetryableError(rErr)
}
rErr = helpers.CheckDeleted(d, rErr, endpoint)
if rErr != nil {
return resource.NonRetryableError(rErr)
}
return nil
}
log.Printf("[DEBUG] Waiting for IP restriction %s to be DELETED", clusterId)
rErr = waitForCloudProjectDatabaseIpRestrictionDeleted(ctx, config.OVHClient, serviceName, engine, clusterId, ip, d.Timeout(schema.TimeoutDelete))
if rErr != nil {
return resource.NonRetryableError(fmt.Errorf("timeout while waiting IP restriction %s to be DELETED: %w", clusterId, rErr))
}
log.Printf("[DEBUG] IP restriction %s is DELETED", clusterId)
d.SetId("")
return nil
})
return diag.FromErr(err)
}