-
Notifications
You must be signed in to change notification settings - Fork 189
/
Copy pathresource_third_party_integration_test.go
544 lines (508 loc) · 18.2 KB
/
resource_third_party_integration_test.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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
package thirdpartyintegration_test
import (
"context"
"fmt"
"testing"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/terraform"
"github.com/mongodb/terraform-provider-mongodbatlas/internal/testutil/acc"
)
// dummy keys used for credential values in third party notifications
const dummy32CharKey = "11111111111111111111111111111111"
const dummy32CharKeyUpdated = "11111111111111111111111111111112"
const dummy36CharKey = "11111111-1111-1111-1111-111111111111"
const dummy36CharKeyUpdated = "11111111-1111-1111-1111-111111111112"
const resourceName = "mongodbatlas_third_party_integration.test"
const dataSourceName = "data." + resourceName
const dataSourcePluralName = "data.mongodbatlas_third_party_integrations.test"
func TestAccThirdPartyIntegration_basicPagerDuty(t *testing.T) {
// basic test also include testing of plural data source which is why it cannot run in parallel
resource.Test(t, *basicPagerDutyTest(t))
}
func TestAccThirdPartyIntegration_opsGenie(t *testing.T) {
resource.ParallelTest(t, *opsGenieTest(t))
}
func TestAccThirdPartyIntegration_victorOps(t *testing.T) {
resource.ParallelTest(t, *victorOpsTest(t))
}
func TestAccThirdPartyIntegration_datadog(t *testing.T) {
resource.ParallelTest(t, *datadogTest(t))
}
func TestAccThirdPartyIntegration_prometheus(t *testing.T) {
resource.ParallelTest(t, *prometheusTest(t))
}
func TestAccThirdPartyIntegration_microsoftTeams(t *testing.T) {
resource.ParallelTest(t, *microsoftTeamsTest(t))
}
func TestAccThirdPartyIntegration_webhook(t *testing.T) {
resource.ParallelTest(t, *webhookTest(t))
}
func basicPagerDutyTest(tb testing.TB) *resource.TestCase {
tb.Helper()
var (
projectID = acc.ProjectIDExecution(tb)
serviceKey = dummy32CharKey
updatedServiceKey = dummy32CharKeyUpdated
intType = "PAGER_DUTY"
)
return &resource.TestCase{
PreCheck: func() { acc.PreCheckBasic(tb) },
ProtoV6ProviderFactories: acc.TestAccProviderV6Factories,
CheckDestroy: checkDestroy,
Steps: []resource.TestStep{
{
Config: configBasicPagerDuty(projectID, serviceKey),
Check: resource.ComposeAggregateTestCheckFunc(
checkExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "type", intType),
resource.TestCheckResourceAttr(resourceName, "service_key", serviceKey),
resource.TestCheckResourceAttr(dataSourceName, "type", intType),
resource.TestCheckResourceAttr(dataSourcePluralName, "project_id", projectID),
resource.TestCheckResourceAttrSet(dataSourcePluralName, "project_id"),
resource.TestCheckResourceAttrSet(dataSourcePluralName, "results.#"),
resource.TestCheckResourceAttr(dataSourcePluralName, "results.0.type", intType),
),
},
{
Config: configBasicPagerDuty(projectID, updatedServiceKey),
Check: resource.ComposeAggregateTestCheckFunc(
checkExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "type", intType),
resource.TestCheckResourceAttr(resourceName, "service_key", updatedServiceKey),
),
},
importStep(resourceName),
},
}
}
func opsGenieTest(tb testing.TB) *resource.TestCase {
tb.Helper()
var (
projectID = acc.ProjectIDExecution(tb)
apiKey = dummy36CharKey
updatedAPIKey = dummy36CharKeyUpdated
intType = "OPS_GENIE"
)
return &resource.TestCase{
PreCheck: func() { acc.PreCheckBasic(tb) },
ProtoV6ProviderFactories: acc.TestAccProviderV6Factories,
CheckDestroy: checkDestroy,
Steps: []resource.TestStep{
{
Config: configOpsGenie(projectID, apiKey),
Check: resource.ComposeAggregateTestCheckFunc(
checkExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "type", intType),
resource.TestCheckResourceAttr(resourceName, "api_key", apiKey),
resource.TestCheckResourceAttr(resourceName, "region", "US"),
resource.TestCheckResourceAttr(dataSourceName, "type", intType),
resource.TestCheckResourceAttr(dataSourceName, "region", "US"),
),
},
{
Config: configOpsGenie(projectID, updatedAPIKey),
Check: resource.ComposeAggregateTestCheckFunc(
checkExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "type", intType),
resource.TestCheckResourceAttr(resourceName, "api_key", updatedAPIKey),
resource.TestCheckResourceAttr(resourceName, "region", "US"),
),
},
importStep(resourceName),
},
}
}
func victorOpsTest(tb testing.TB) *resource.TestCase {
tb.Helper()
var (
projectID = acc.ProjectIDExecution(tb)
apiKey = dummy36CharKey
updatedAPIKey = dummy36CharKeyUpdated
intType = "VICTOR_OPS"
)
return &resource.TestCase{
PreCheck: func() { acc.PreCheckBasic(tb) },
ProtoV6ProviderFactories: acc.TestAccProviderV6Factories,
CheckDestroy: checkDestroy,
Steps: []resource.TestStep{
{
Config: configVictorOps(projectID, apiKey),
Check: resource.ComposeAggregateTestCheckFunc(
checkExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "type", intType),
resource.TestCheckResourceAttr(resourceName, "api_key", apiKey),
resource.TestCheckResourceAttrSet(resourceName, "routing_key"),
resource.TestCheckResourceAttr(dataSourceName, "type", intType),
resource.TestCheckResourceAttrSet(dataSourceName, "routing_key"),
),
},
{
Config: configVictorOps(projectID, updatedAPIKey),
Check: resource.ComposeAggregateTestCheckFunc(
checkExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "type", intType),
resource.TestCheckResourceAttr(resourceName, "api_key", updatedAPIKey),
),
},
importStep(resourceName),
},
}
}
func datadogTest(tb testing.TB) *resource.TestCase {
tb.Helper()
var (
projectID = acc.ProjectIDExecution(tb)
apiKey = dummy32CharKey
updatedAPIKey = dummy32CharKeyUpdated
region = "US"
intType = "DATADOG"
)
return &resource.TestCase{
PreCheck: func() { acc.PreCheckBasic(tb) },
ProtoV6ProviderFactories: acc.TestAccProviderV6Factories,
CheckDestroy: checkDestroy,
Steps: []resource.TestStep{
{
Config: configDatadog(projectID, apiKey, "US", false, false, false),
Check: resource.ComposeAggregateTestCheckFunc(
checkExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "type", intType),
resource.TestCheckResourceAttr(resourceName, "api_key", apiKey),
resource.TestCheckResourceAttr(resourceName, "region", region),
resource.TestCheckResourceAttr(resourceName, "send_collection_latency_metrics", "false"),
resource.TestCheckResourceAttr(resourceName, "send_database_metrics", "false"),
resource.TestCheckResourceAttr(dataSourceName, "type", intType),
resource.TestCheckResourceAttr(dataSourceName, "region", region),
resource.TestCheckResourceAttr(dataSourceName, "send_collection_latency_metrics", "false"),
resource.TestCheckResourceAttr(dataSourceName, "send_database_metrics", "false"),
),
},
{
Config: configDatadog(projectID, apiKey, "US", true, true, false),
Check: resource.ComposeAggregateTestCheckFunc(
checkExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "type", intType),
resource.TestCheckResourceAttr(resourceName, "api_key", apiKey),
resource.TestCheckResourceAttr(resourceName, "region", region),
resource.TestCheckResourceAttr(resourceName, "send_collection_latency_metrics", "true"),
resource.TestCheckResourceAttr(resourceName, "send_database_metrics", "false"),
resource.TestCheckResourceAttr(dataSourceName, "type", intType),
resource.TestCheckResourceAttr(dataSourceName, "region", region),
resource.TestCheckResourceAttr(dataSourceName, "send_collection_latency_metrics", "true"),
resource.TestCheckResourceAttr(dataSourceName, "send_database_metrics", "false"),
),
},
{
Config: configDatadog(projectID, updatedAPIKey, "US", true, false, true),
Check: resource.ComposeAggregateTestCheckFunc(
checkExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "type", intType),
resource.TestCheckResourceAttr(resourceName, "api_key", updatedAPIKey),
resource.TestCheckResourceAttr(resourceName, "region", region),
resource.TestCheckResourceAttr(resourceName, "send_collection_latency_metrics", "false"),
resource.TestCheckResourceAttr(resourceName, "send_database_metrics", "true"),
),
},
{
Config: configDatadog(projectID, updatedAPIKey, "US", true, true, true),
Check: resource.ComposeAggregateTestCheckFunc(
checkExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "type", intType),
resource.TestCheckResourceAttr(resourceName, "api_key", updatedAPIKey),
resource.TestCheckResourceAttr(resourceName, "region", region),
resource.TestCheckResourceAttr(resourceName, "send_collection_latency_metrics", "true"),
resource.TestCheckResourceAttr(resourceName, "send_database_metrics", "true"),
resource.TestCheckResourceAttr(dataSourceName, "send_collection_latency_metrics", "true"),
resource.TestCheckResourceAttr(dataSourceName, "send_database_metrics", "true"),
),
},
importStep(resourceName),
},
}
}
func prometheusTest(tb testing.TB) *resource.TestCase {
tb.Helper()
var (
projectID = acc.ProjectIDExecution(tb)
username = "someuser"
updatedUsername = "otheruser"
password = "somepassword"
serviceDiscovery = "http"
intType = "PROMETHEUS"
)
return &resource.TestCase{
PreCheck: func() { acc.PreCheckBasic(tb) },
ProtoV6ProviderFactories: acc.TestAccProviderV6Factories,
CheckDestroy: checkDestroy,
Steps: []resource.TestStep{
{
Config: configPrometheus(projectID, username, password, serviceDiscovery),
Check: resource.ComposeAggregateTestCheckFunc(
checkExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "type", intType),
resource.TestCheckResourceAttr(resourceName, "user_name", username),
resource.TestCheckResourceAttr(resourceName, "password", password),
resource.TestCheckResourceAttr(resourceName, "service_discovery", serviceDiscovery),
resource.TestCheckResourceAttr(dataSourceName, "type", intType),
resource.TestCheckResourceAttr(dataSourceName, "user_name", username),
resource.TestCheckResourceAttr(dataSourceName, "service_discovery", serviceDiscovery),
),
},
{
Config: configPrometheus(projectID, updatedUsername, password, serviceDiscovery),
Check: resource.ComposeAggregateTestCheckFunc(
checkExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "type", intType),
resource.TestCheckResourceAttr(resourceName, "user_name", updatedUsername),
),
},
importStep(resourceName),
},
}
}
func microsoftTeamsTest(tb testing.TB) *resource.TestCase {
tb.Helper()
var (
projectID = acc.ProjectIDExecution(tb)
intType = "MICROSOFT_TEAMS"
url = "https://apps.webhook.office.com/webhookb2/" +
"c9c5fafc-d9fe-4ffb-9773-77d804ea4372@c9656" +
"3a8-841b-4ef9-af16-33548fffc958/IncomingWebhook" +
"/484cccf0a678fffff86388b63203110a/42a0070b-5f35-ffff-be83-ac7e7f55d7d3"
updatedURL = "https://apps.webhook.office.com/webhookb2/" +
"c9c5fafc-d9fe-4ffb-9773-77d804ea4372@c9656" +
"3a8-841b-4ef9-af16-33548fffc958/IncomingWebhook" +
"/484cccf0a678fffff86388b63203110a/42a0070b-5f35-ffff-be83-ac7e7f55d7d4"
)
return &resource.TestCase{
PreCheck: func() { acc.PreCheckBasic(tb) },
ProtoV6ProviderFactories: acc.TestAccProviderV6Factories,
CheckDestroy: checkDestroy,
Steps: []resource.TestStep{
{
Config: configMicrosoftTeams(projectID, url),
Check: resource.ComposeAggregateTestCheckFunc(
checkExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "type", intType),
resource.TestCheckResourceAttr(resourceName, "microsoft_teams_webhook_url", url),
resource.TestCheckResourceAttr(dataSourceName, "type", intType),
),
},
{
Config: configMicrosoftTeams(projectID, updatedURL),
Check: resource.ComposeAggregateTestCheckFunc(
checkExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "type", intType),
resource.TestCheckResourceAttr(resourceName, "microsoft_teams_webhook_url", updatedURL),
),
},
importStep(resourceName),
},
}
}
func webhookTest(tb testing.TB) *resource.TestCase {
tb.Helper()
var (
projectID = acc.ProjectIDExecution(tb)
intType = "WEBHOOK"
url = "https://www.mongodb.com/webhook"
updatedURL = "https://www.mongodb.com/webhook/updated"
)
return &resource.TestCase{
PreCheck: func() { acc.PreCheckBasic(tb) },
ProtoV6ProviderFactories: acc.TestAccProviderV6Factories,
CheckDestroy: checkDestroy,
Steps: []resource.TestStep{
{
Config: configWebhook(projectID, url),
Check: resource.ComposeAggregateTestCheckFunc(
checkExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "type", intType),
resource.TestCheckResourceAttr(resourceName, "url", url),
resource.TestCheckResourceAttr(dataSourceName, "type", intType),
),
},
{
Config: configWebhook(projectID, updatedURL),
Check: resource.ComposeAggregateTestCheckFunc(
checkExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "type", intType),
resource.TestCheckResourceAttr(resourceName, "url", updatedURL),
),
},
importStep(resourceName),
},
}
}
func importStep(resourceName string) resource.TestStep {
return resource.TestStep{
ResourceName: resourceName,
ImportStateIdFunc: importStateIDFunc(resourceName),
ImportState: true,
ImportStateVerify: false, // API Obfuscation will always make import mismatch
}
}
func checkDestroy(s *terraform.State) error {
for _, rs := range s.RootModule().Resources {
if rs.Type != "mongodbatlas_third_party_integration" {
continue
}
attrs := rs.Primary.Attributes
if attrs["project_id"] == "" {
return fmt.Errorf("no project id is set")
}
if attrs["type"] == "" {
return fmt.Errorf("no type is set")
}
_, _, err := acc.ConnV2().ThirdPartyIntegrationsApi.GetThirdPartyIntegration(context.Background(), attrs["project_id"], attrs["type"]).Execute()
if err == nil {
return fmt.Errorf("third party integration service (%s) still exists", attrs["type"])
}
}
return nil
}
func importStateIDFunc(resourceName string) resource.ImportStateIdFunc {
return func(s *terraform.State) (string, error) {
rs, ok := s.RootModule().Resources[resourceName]
if !ok {
return "", fmt.Errorf("not found: %s", resourceName)
}
attrs := rs.Primary.Attributes
if attrs["project_id"] == "" {
return "", fmt.Errorf("no project id is set")
}
if attrs["type"] == "" {
return "", fmt.Errorf("no type is set")
}
return fmt.Sprintf("%s-%s", attrs["project_id"], attrs["type"]), nil
}
}
var singularDataStr = `
data "mongodbatlas_third_party_integration" "test" {
project_id = mongodbatlas_third_party_integration.test.project_id
type = mongodbatlas_third_party_integration.test.type
}
`
var pluralDataStr = `
data "mongodbatlas_third_party_integrations" "test" {
project_id = mongodbatlas_third_party_integration.test.project_id
}`
func configOpsGenie(projectID, apiKey string) string {
return fmt.Sprintf(`
resource "mongodbatlas_third_party_integration" "test" {
project_id = "%[1]s"
type = "%[2]s"
api_key = "%[3]s"
region = "%[4]s"
}`,
projectID,
"OPS_GENIE",
apiKey,
"US",
) + singularDataStr
}
func configBasicPagerDuty(projectID, serviceKey string) string {
return fmt.Sprintf(`
resource "mongodbatlas_third_party_integration" "test" {
project_id = "%[1]s"
type = "%[2]s"
service_key = "%[3]s"
}
`,
projectID,
"PAGER_DUTY",
serviceKey,
) + singularDataStr + pluralDataStr
}
func configVictorOps(projectID, apiKey string) string {
return fmt.Sprintf(`
resource "mongodbatlas_third_party_integration" "test" {
project_id = "%[1]s"
type = "%[2]s"
api_key = "%[3]s"
routing_key = "testing"
}
`,
projectID,
"VICTOR_OPS",
apiKey,
) + singularDataStr
}
func configDatadog(projectID, apiKey, region string, useOptionalAttr, sendCollectionLatencyMetrics, sendDatabaseMetrics bool) string {
optionalConfigAttrs := ""
if useOptionalAttr {
optionalConfigAttrs = fmt.Sprintf(
`send_collection_latency_metrics = %[1]t
send_database_metrics = %[2]t`, sendCollectionLatencyMetrics, sendDatabaseMetrics)
}
return fmt.Sprintf(`
resource "mongodbatlas_third_party_integration" "test" {
project_id = "%[1]s"
type = "%[2]s"
api_key = "%[3]s"
region ="%[4]s"
%[5]s
}
`,
projectID,
"DATADOG",
apiKey,
region,
optionalConfigAttrs,
) + singularDataStr
}
func configPrometheus(projectID, username, password, serviceDiscovery string) string {
return fmt.Sprintf(`
resource "mongodbatlas_third_party_integration" "test" {
project_id = "%[1]s"
type = "%[2]s"
user_name = "%[3]s"
password = "%[4]s"
service_discovery = "%[5]s"
enabled = true
}
`,
projectID,
"PROMETHEUS",
username,
password,
serviceDiscovery,
) + singularDataStr
}
func configMicrosoftTeams(projectID, url string) string {
return fmt.Sprintf(`
resource "mongodbatlas_third_party_integration" "test" {
project_id = "%[1]s"
type = "MICROSOFT_TEAMS"
microsoft_teams_webhook_url = "%[2]s"
}
`, projectID, url) + singularDataStr
}
func configWebhook(projectID, url string) string {
return fmt.Sprintf(`
resource "mongodbatlas_third_party_integration" "test" {
project_id = "%[1]s"
type = "WEBHOOK"
url = "%[2]s"
}
`, projectID, url) + singularDataStr
}
func checkExists(resourceName string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[resourceName]
if !ok {
return fmt.Errorf("not found: %s", resourceName)
}
attrs := rs.Primary.Attributes
if attrs["project_id"] == "" {
return fmt.Errorf("no project id is set")
}
if attrs["type"] == "" {
return fmt.Errorf("no type is set")
}
if _, _, err := acc.ConnV2().ThirdPartyIntegrationsApi.GetThirdPartyIntegration(context.Background(), attrs["project_id"], attrs["type"]).Execute(); err == nil {
return nil
}
return fmt.Errorf("third party integration (%s) does not exist", attrs["project_id"])
}
}