Skip to content

Commit c614c50

Browse files
feat: Defines id value returned from API for third_party_integration resource and data sources (#2217)
* feat: Defines id value returned from API for third party integration * add changelog entry * adjust alert config test so it can run in CI * fix pre check * force test to create new project ensureing no third party integration is already created
1 parent f16d276 commit c614c50

11 files changed

+93
-75
lines changed

.changelog/2217.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
```release-note:enhancement
2+
resource/mongodbatlas_third_party_integration: New `id` value which can be used for referencing a third party integration
3+
```
4+
5+
```release-note:enhancement
6+
data-source/mongodbatlas_third_party_integration: New `id` value which can be used for referencing a third party integration
7+
```
8+
9+
```release-note:enhancement
10+
data-source/mongodbatlas_third_party_integrations: New `id` value which can be used for referencing a third party integration
11+
```

internal/service/alertconfiguration/resource_alert_configuration_test.go

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -423,25 +423,24 @@ func TestAccConfigRSAlertConfiguration_withPagerDuty(t *testing.T) {
423423
}
424424

425425
func TestAccConfigAlertConfiguration_PagerDutyUsingIntegrationID(t *testing.T) {
426-
acc.SkipTestForCI(t) // currently skipped as third party integration id cannot be created and obtained during test
427-
proxyPort := replay.SetupReplayProxy(t)
428-
426+
// create a new project as it need to ensure no third party integration has already been created
429427
var (
430-
pagerDutyIntegrationID = os.Getenv("MONGODB_ATLAS_PAGER_DUTY_THIRD_PARTY_INTEGRATION_ID")
431-
projectID = replay.ManageProjectID(t, acc.ProjectIDExecution)
428+
orgID = os.Getenv("MONGODB_ATLAS_ORG_ID")
429+
projectName = acc.RandomProjectName()
430+
serviceKey = dummy32CharKey
432431
)
433432

434433
resource.ParallelTest(t, resource.TestCase{
435-
PreCheck: func() { acc.PreCheckPagerDutyIntegrationID(t); acc.PreCheckBasic(t) },
436-
ProtoV6ProviderFactories: acc.TestAccProviderV6FactoriesWithProxy(proxyPort),
437-
CheckDestroy: checkDestroyUsingProxy(proxyPort),
434+
PreCheck: func() { acc.PreCheckBasic(t) },
435+
ProtoV6ProviderFactories: acc.TestAccProviderV6FactoriesWithProxy(nil),
436+
CheckDestroy: checkDestroyUsingProxy(nil),
438437
Steps: []resource.TestStep{
439438
{
440-
Config: configWithPagerDutyIntegrationID(projectID, pagerDutyIntegrationID),
439+
Config: configWithPagerDutyIntegrationID(orgID, projectName, serviceKey),
441440
Check: resource.ComposeTestCheckFunc(
442-
checkExistsUsingProxy(proxyPort, resourceName),
443-
resource.TestCheckResourceAttr(resourceName, "notification.0.integration_id", pagerDutyIntegrationID),
444-
resource.TestCheckResourceAttr(dataSourceName, "notification.0.integration_id", pagerDutyIntegrationID),
441+
checkExistsUsingProxy(nil, resourceName),
442+
resource.TestCheckResourceAttrSet(resourceName, "notification.0.integration_id"),
443+
resource.TestCheckResourceAttrSet(dataSourceName, "notification.0.integration_id"),
445444
),
446445
},
447446
},
@@ -799,24 +798,35 @@ func configWithPagerDuty(projectID, serviceKey string, enabled bool) string {
799798
`, projectID, serviceKey, enabled)
800799
}
801800

802-
func configWithPagerDutyIntegrationID(projectID, pagerDutyIntegrationID string) string {
801+
func configWithPagerDutyIntegrationID(orgID, projectName, serviceKey string) string {
803802
return fmt.Sprintf(`
803+
resource "mongodbatlas_project" "test" {
804+
org_id = %[1]q
805+
name = %[2]q
806+
}
807+
808+
resource "mongodbatlas_third_party_integration" "test" {
809+
project_id = mongodbatlas_project.test.id
810+
type = "PAGER_DUTY"
811+
service_key = %[3]q
812+
}
813+
804814
resource "mongodbatlas_alert_configuration" "test" {
805-
project_id = %[1]q
815+
project_id = mongodbatlas_project.test.id
806816
enabled = true
807817
event_type = "USERS_WITHOUT_MULTI_FACTOR_AUTH"
808818
809819
notification {
810820
type_name = "PAGER_DUTY"
811-
integration_id = %[2]q
821+
integration_id = mongodbatlas_third_party_integration.test.id
812822
}
813823
}
814824
815825
data "mongodbatlas_alert_configuration" "test" {
816826
project_id = mongodbatlas_alert_configuration.test.project_id
817827
alert_configuration_id = mongodbatlas_alert_configuration.test.id
818828
}
819-
`, projectID, pagerDutyIntegrationID)
829+
`, orgID, projectName, serviceKey)
820830
}
821831

822832
func configWithPagerDutyNotifierID(projectID, notifierID string, delayMin int, serviceKey *string) string {

internal/service/thirdpartyintegration/data_source_third_party_integration.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
88
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
99
"github.com/mongodb/terraform-provider-mongodbatlas/internal/common/constant"
10-
"github.com/mongodb/terraform-provider-mongodbatlas/internal/common/conversion"
1110
"github.com/mongodb/terraform-provider-mongodbatlas/internal/config"
1211
)
1312

@@ -32,6 +31,10 @@ func DataSource() *schema.Resource {
3231
func thirdPartyIntegrationSchema() *schema.Resource {
3332
return &schema.Resource{
3433
Schema: map[string]*schema.Schema{
34+
"id": {
35+
Type: schema.TypeString,
36+
Computed: true,
37+
},
3538
"project_id": {
3639
Type: schema.TypeString,
3740
Computed: true,
@@ -128,10 +131,6 @@ func dataSourceMongoDBAtlasThirdPartyIntegrationRead(ctx context.Context, d *sch
128131
}
129132
}
130133

131-
d.SetId(conversion.EncodeStateID(map[string]string{
132-
"project_id": projectID,
133-
"type": queryType,
134-
}))
135-
134+
d.SetId(integration.GetId())
136135
return nil
137136
}

internal/service/thirdpartyintegration/data_source_third_party_integrations.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ func integrationToSchema(d *schema.ResourceData, integration *admin.ThirdPartyIn
9191
}
9292

9393
out := map[string]any{
94+
"id": integration.Id,
9495
"type": integration.Type,
9596
"api_key": integrationSchema.ApiKey,
9697
"region": integration.Region,

internal/service/thirdpartyintegration/resource_third_party_integration.go

Lines changed: 14 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
1111
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1212
"github.com/mongodb/terraform-provider-mongodbatlas/internal/common/constant"
13-
"github.com/mongodb/terraform-provider-mongodbatlas/internal/common/conversion"
1413
"github.com/mongodb/terraform-provider-mongodbatlas/internal/config"
1514
)
1615

@@ -44,6 +43,10 @@ func Resource() *schema.Resource {
4443
StateContext: resourceMongoDBAtlasThirdPartyIntegrationImportState,
4544
},
4645
Schema: map[string]*schema.Schema{
46+
"id": {
47+
Type: schema.TypeString,
48+
Computed: true,
49+
},
4750
"project_id": {
4851
Type: schema.TypeString,
4952
Required: true,
@@ -161,21 +164,14 @@ func resourceMongoDBAtlasThirdPartyIntegrationCreate(ctx context.Context, d *sch
161164
return diag.FromErr(fmt.Errorf("error creating third party integration %s", err))
162165
}
163166

164-
// ID is equal to project_id+type need to ask
165-
d.SetId(conversion.EncodeStateID(map[string]string{
166-
"project_id": projectID,
167-
"type": integrationType,
168-
}))
169-
170167
return resourceMongoDBAtlasThirdPartyIntegrationRead(ctx, d, meta)
171168
}
172169

173170
func resourceMongoDBAtlasThirdPartyIntegrationRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
174171
connV2 := meta.(*config.MongoDBClient).AtlasV2
175-
ids := conversion.DecodeStateID(d.Id())
176172

177-
projectID := ids["project_id"]
178-
integrationType := ids["type"]
173+
projectID := d.Get("project_id").(string)
174+
integrationType := d.Get("type").(string)
179175

180176
integration, resp, err := connV2.ThirdPartyIntegrationsApi.GetThirdPartyIntegration(ctx, projectID, integrationType).Execute()
181177
if err != nil {
@@ -195,20 +191,15 @@ func resourceMongoDBAtlasThirdPartyIntegrationRead(ctx context.Context, d *schem
195191
}
196192
}
197193

198-
d.SetId(conversion.EncodeStateID(map[string]string{
199-
"project_id": projectID,
200-
"type": integrationType,
201-
}))
202-
194+
d.SetId(integration.GetId())
203195
return nil
204196
}
205197

206198
func resourceMongoDBAtlasThirdPartyIntegrationUpdate(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
207199
connV2 := meta.(*config.MongoDBClient).AtlasV2
208-
ids := conversion.DecodeStateID(d.Id())
209200

210-
projectID := ids["project_id"]
211-
integrationType := ids["type"]
201+
projectID := d.Get("project_id").(string)
202+
integrationType := d.Get("type").(string)
212203

213204
integration, _, err := connV2.ThirdPartyIntegrationsApi.GetThirdPartyIntegration(ctx, projectID, integrationType).Execute()
214205

@@ -231,10 +222,9 @@ func resourceMongoDBAtlasThirdPartyIntegrationUpdate(ctx context.Context, d *sch
231222

232223
func resourceMongoDBAtlasThirdPartyIntegrationDelete(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
233224
conn := meta.(*config.MongoDBClient).Atlas
234-
ids := conversion.DecodeStateID(d.Id())
235225

236-
projectID := ids["project_id"]
237-
integrationType := ids["type"]
226+
projectID := d.Get("project_id").(string)
227+
integrationType := d.Get("type").(string)
238228

239229
_, err := conn.Integrations.Delete(ctx, projectID, integrationType)
240230

@@ -246,15 +236,15 @@ func resourceMongoDBAtlasThirdPartyIntegrationDelete(ctx context.Context, d *sch
246236
}
247237

248238
func resourceMongoDBAtlasThirdPartyIntegrationImportState(ctx context.Context, d *schema.ResourceData, meta any) ([]*schema.ResourceData, error) {
249-
conn := meta.(*config.MongoDBClient).Atlas
239+
connV2 := meta.(*config.MongoDBClient).AtlasV2
250240

251241
projectID, integrationType, err := splitIntegrationTypeID(d.Id())
252242

253243
if err != nil {
254244
return nil, err
255245
}
256246

257-
integration, _, err := conn.Integrations.Get(ctx, projectID, integrationType)
247+
_, _, err = connV2.ThirdPartyIntegrationsApi.GetThirdPartyIntegration(ctx, projectID, integrationType).Execute()
258248

259249
if err != nil {
260250
return nil, fmt.Errorf("couldn't import third party integration (%s) in project(%s), error: %w", integrationType, projectID, err)
@@ -264,15 +254,10 @@ func resourceMongoDBAtlasThirdPartyIntegrationImportState(ctx context.Context, d
264254
return nil, fmt.Errorf("error setting `project_id` for third party integration (%s): %w", d.Id(), err)
265255
}
266256

267-
if err := d.Set("type", integration.Type); err != nil {
257+
if err := d.Set("type", integrationType); err != nil {
268258
return nil, fmt.Errorf("error setting `type` for third party integration (%s): %w", d.Id(), err)
269259
}
270260

271-
d.SetId(conversion.EncodeStateID(map[string]string{
272-
"project_id": projectID,
273-
"type": integrationType,
274-
}))
275-
276261
return []*schema.ResourceData{d}, nil
277262
}
278263

internal/service/thirdpartyintegration/resource_third_party_integration_test.go

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77

88
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
99
"github.com/hashicorp/terraform-plugin-testing/terraform"
10-
"github.com/mongodb/terraform-provider-mongodbatlas/internal/common/conversion"
1110
"github.com/mongodb/terraform-provider-mongodbatlas/internal/testutil/acc"
1211
)
1312

@@ -336,10 +335,16 @@ func checkDestroy(s *terraform.State) error {
336335
if rs.Type != "mongodbatlas_third_party_integration" {
337336
continue
338337
}
339-
ids := conversion.DecodeStateID(rs.Primary.ID)
340-
_, _, err := acc.Conn().Integrations.Get(context.Background(), ids["project_id"], ids["type"])
338+
attrs := rs.Primary.Attributes
339+
if attrs["project_id"] == "" {
340+
return fmt.Errorf("no project id is set")
341+
}
342+
if attrs["type"] == "" {
343+
return fmt.Errorf("no type is set")
344+
}
345+
_, _, err := acc.Conn().Integrations.Get(context.Background(), attrs["project_id"], attrs["type"])
341346
if err == nil {
342-
return fmt.Errorf("third party integration service (%s) still exists", ids["type"])
347+
return fmt.Errorf("third party integration service (%s) still exists", attrs["type"])
343348
}
344349
}
345350
return nil
@@ -352,9 +357,15 @@ func importStateIDFunc(resourceName string) resource.ImportStateIdFunc {
352357
return "", fmt.Errorf("not found: %s", resourceName)
353358
}
354359

355-
ids := conversion.DecodeStateID(rs.Primary.ID)
360+
attrs := rs.Primary.Attributes
361+
if attrs["project_id"] == "" {
362+
return "", fmt.Errorf("no project id is set")
363+
}
364+
if attrs["type"] == "" {
365+
return "", fmt.Errorf("no type is set")
366+
}
356367

357-
return fmt.Sprintf("%s-%s", ids["project_id"], ids["type"]), nil
368+
return fmt.Sprintf("%s-%s", attrs["project_id"], attrs["type"]), nil
358369
}
359370
}
360371

@@ -478,13 +489,16 @@ func checkExists(resourceName string) resource.TestCheckFunc {
478489
if !ok {
479490
return fmt.Errorf("not found: %s", resourceName)
480491
}
481-
if rs.Primary.Attributes["project_id"] == "" {
482-
return fmt.Errorf("no ID is set")
492+
attrs := rs.Primary.Attributes
493+
if attrs["project_id"] == "" {
494+
return fmt.Errorf("no project id is set")
495+
}
496+
if attrs["type"] == "" {
497+
return fmt.Errorf("no type is set")
483498
}
484-
ids := conversion.DecodeStateID(rs.Primary.ID)
485-
if _, _, err := acc.Conn().Integrations.Get(context.Background(), ids["project_id"], ids["type"]); err == nil {
499+
if _, _, err := acc.Conn().Integrations.Get(context.Background(), attrs["project_id"], attrs["type"]); err == nil {
486500
return nil
487501
}
488-
return fmt.Errorf("third party integration (%s) does not exist", ids["project_id"])
502+
return fmt.Errorf("third party integration (%s) does not exist", attrs["project_id"])
489503
}
490504
}

internal/testutil/acc/pre_check.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -277,10 +277,3 @@ func PreCheckS3Bucket(tb testing.TB) {
277277
tb.Fatal("`AWS_S3_BUCKET` must be set ")
278278
}
279279
}
280-
281-
func PreCheckPagerDutyIntegrationID(tb testing.TB) {
282-
tb.Helper()
283-
if os.Getenv("MONGODB_ATLAS_PAGER_DUTY_THIRD_PARTY_INTEGRATION_ID") == "" {
284-
tb.Fatal("`MONGODB_ATLAS_PAGER_DUTY_THIRD_PARTY_INTEGRATION_ID` must be set ")
285-
}
286-
}

website/docs/d/third_party_integration.markdown

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,7 @@ data "mongodbatlas_third_party_integration" "test" {
4545

4646
In addition to all arguments above, the following attributes are exported:
4747

48-
* `id` - Unique identifier used for terraform for internal manages and can be used to import.
49-
* `type` - Property equal to its own integration type
48+
* `id` - Unique identifier of the integration.
5049

5150
Additional values based on Type
5251

website/docs/d/third_party_integrations.markdown

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,9 @@ In addition to all arguments above, the following attributes are exported:
4848

4949
### Third-Party Service Integration
5050

51-
* `project_id` - (Required) ID of the Atlas project the Third-Party Service Integration belongs to.
52-
* `type` - (Required) Thirt-Party service integration type.
51+
* `project_id` - ID of the Atlas project the Third-Party Service Integration belongs to.
52+
* `type` - Thirt-Party service integration type.
53+
* `id` - Unique identifier of the integration.
5354

5455
* PAGER_DUTY
5556
* DATADOG

website/docs/r/alert_configuration.html.markdown

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,14 +123,19 @@ resource "mongodbatlas_alert_configuration" "test" {
123123

124124

125125
```terraform
126+
data "mongodbatlas_third_party_integration" "test" {
127+
project_id = "PROJECT ID"
128+
type = "PAGER_DUTY"
129+
}
130+
126131
resource "mongodbatlas_alert_configuration" "test" {
127132
project_id = "PROJECT ID"
128133
enabled = true
129134
event_type = "USERS_WITHOUT_MULTI_FACTOR_AUTH"
130135
131136
notification {
132137
type_name = "PAGER_DUTY"
133-
integration_id = "THIRD PARTY INTEGRATION ID"
138+
integration_id = data.mongodbatlas_third_party_integration.test.id
134139
}
135140
}
136141
```

0 commit comments

Comments
 (0)