/
resource_cloud_project_database_log_subscription.go
218 lines (192 loc) · 6.69 KB
/
resource_cloud_project_database_log_subscription.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
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/schema"
"github.com/ovh/terraform-provider-ovh/ovh/helpers"
)
func resourceCloudProjectDatabaseLogSubscription() *schema.Resource {
return &schema.Resource{
CreateContext: resourceCloudProjectDatabaseLogSubscriptionCreate,
ReadContext: resourceCloudProjectDatabaseLogSubscriptionRead,
DeleteContext: resourceCloudProjectDatabaseLogSubscriptionDelete,
Importer: &schema.ResourceImporter{
State: resourceCloudProjectDatabaseLogSubscriptionImportState,
},
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,
},
"stream_id": {
Type: schema.TypeString,
Description: "Id of the target Log data platform stream",
ForceNew: true,
Required: true,
},
//Computed
"created_at": {
Type: schema.TypeString,
Description: "Creation date of the subscription",
Computed: true,
},
"kind": {
Type: schema.TypeString,
Description: "Log kind name of this subscription",
Computed: true,
},
"ldp_service_name": {
Type: schema.TypeString,
Description: "Name of the destination log service",
Sensitive: true,
Computed: true,
},
"operation_id": {
Type: schema.TypeString,
Description: "Identifier of the operation",
Computed: true,
},
"resource_name": {
Type: schema.TypeString,
Description: "Name of subscribed resource, where the logs come from",
Computed: true,
},
"resource_type": {
Type: schema.TypeString,
Description: "Type of subscribed resource, where the logs come from",
Computed: true,
},
"updated_at": {
Type: schema.TypeString,
Description: "Last update date of the subscription",
Computed: true,
},
},
}
}
func resourceCloudProjectDatabaseLogSubscriptionImportState(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/id formatted")
}
serviceName := splitID[0]
engine := splitID[1]
clusterID := splitID[2]
id := splitID[3]
d.SetId(id)
d.Set("cluster_id", clusterID)
d.Set("engine", engine)
d.Set("service_name", serviceName)
results := make([]*schema.ResourceData, 1)
results[0] = d
return results, nil
}
func resourceCloudProjectDatabaseLogSubscriptionCreate(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/log/subscription",
url.PathEscape(serviceName),
url.PathEscape(engine),
url.PathEscape(clusterID),
)
params := (&CloudProjectDatabaseLogSubscriptionCreateOpts{}).fromResource(d)
res := &CloudProjectDatabaseLogSubscriptionResponse{}
log.Printf("[DEBUG] Will create Log subscrition : %+v for cluster %s from project %s", params, clusterID, serviceName)
err := config.OVHClient.PostWithContext(ctx, endpoint, params, res)
if err != nil {
diag.Errorf("calling Post %s with params %+v:\n\t %q", endpoint, params, err)
}
log.Printf("[DEBUG] Waiting for Log subscription operation %s to be READY", res.OperationID)
op, err := waitForDbaasLogsOperation(ctx, config.OVHClient, res.LDPServiceName, res.OperationID)
if err != nil {
return diag.Errorf("timeout while waiting log subscrition operation %s to be READY: %q", res.OperationID, err)
}
log.Printf("[DEBUG] Log subscrition operation %s is READY", res.OperationID)
d.SetId(*op.SubscriptionID)
d.Set("operation_id", res.OperationID)
return resourceCloudProjectDatabaseLogSubscriptionRead(ctx, d, meta)
}
func resourceCloudProjectDatabaseLogSubscriptionRead(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)
id := d.Id()
endpoint := fmt.Sprintf("/cloud/project/%s/database/%s/%s/log/subscription/%s",
url.PathEscape(serviceName),
url.PathEscape(engine),
url.PathEscape(clusterID),
url.PathEscape(id),
)
res := &CloudProjectDatabaseLogSubscriptionResponse{}
log.Printf("[DEBUG] Will read log subscrition %s from cluster %s from project %s", id, clusterID, serviceName)
if err := config.OVHClient.GetWithContext(ctx, endpoint, res); err != nil {
return diag.FromErr(helpers.CheckDeleted(d, err, endpoint))
}
for k, v := range res.toMap() {
if k == "operation_id" {
continue
} else if k != "id" {
d.Set(k, v)
} else {
d.SetId(fmt.Sprint(v))
}
}
log.Printf("[DEBUG] Read log subscrition %+v", res)
return nil
}
func resourceCloudProjectDatabaseLogSubscriptionDelete(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)
id := d.Id()
endpoint := fmt.Sprintf("/cloud/project/%s/database/%s/%s/log/subscription/%s",
url.PathEscape(serviceName),
url.PathEscape(engine),
url.PathEscape(clusterID),
url.PathEscape(id),
)
res := &CloudProjectDatabaseLogSubscriptionResponse{}
log.Printf("[DEBUG] Will delete Log subscrition %s from cluster %s from project %s", id, clusterID, serviceName)
err := config.OVHClient.DeleteWithContext(ctx, endpoint, res)
if err != nil {
diag.Errorf("calling DELETE %s:\n\t %q", endpoint, err)
}
log.Printf("[DEBUG] Waiting for user %s to be DELETED", id)
_, err = waitForDbaasLogsOperation(ctx, config.OVHClient, res.LDPServiceName, res.OperationID)
if err != nil {
return diag.Errorf("timeout while waiting log subscription %s to be DELETED: %q", id, err)
}
log.Printf("[DEBUG] Log subsription %s is DELETED", id)
d.SetId("")
return nil
}