From ec82e327c18886dfd284416459cb1c43691b9d57 Mon Sep 17 00:00:00 2001 From: Agustin Bettati Date: Fri, 26 Apr 2024 17:12:16 +0200 Subject: [PATCH] feat: Defines id value returned from API for third party integration --- .../data_source_third_party_integration.go | 11 +++-- .../data_source_third_party_integrations.go | 1 + .../resource_third_party_integration.go | 43 ++++++------------- .../resource_third_party_integration_test.go | 36 +++++++++++----- .../docs/d/third_party_integration.markdown | 3 +- .../docs/d/third_party_integrations.markdown | 5 ++- .../docs/r/alert_configuration.html.markdown | 7 ++- .../docs/r/third_party_integration.markdown | 2 +- 8 files changed, 56 insertions(+), 52 deletions(-) diff --git a/internal/service/thirdpartyintegration/data_source_third_party_integration.go b/internal/service/thirdpartyintegration/data_source_third_party_integration.go index 5789757ab8..f579223c04 100644 --- a/internal/service/thirdpartyintegration/data_source_third_party_integration.go +++ b/internal/service/thirdpartyintegration/data_source_third_party_integration.go @@ -7,7 +7,6 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/v2/diag" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" "github.com/mongodb/terraform-provider-mongodbatlas/internal/common/constant" - "github.com/mongodb/terraform-provider-mongodbatlas/internal/common/conversion" "github.com/mongodb/terraform-provider-mongodbatlas/internal/config" ) @@ -32,6 +31,10 @@ func DataSource() *schema.Resource { func thirdPartyIntegrationSchema() *schema.Resource { return &schema.Resource{ Schema: map[string]*schema.Schema{ + "id": { + Type: schema.TypeString, + Computed: true, + }, "project_id": { Type: schema.TypeString, Computed: true, @@ -128,10 +131,6 @@ func dataSourceMongoDBAtlasThirdPartyIntegrationRead(ctx context.Context, d *sch } } - d.SetId(conversion.EncodeStateID(map[string]string{ - "project_id": projectID, - "type": queryType, - })) - + d.SetId(integration.GetId()) return nil } diff --git a/internal/service/thirdpartyintegration/data_source_third_party_integrations.go b/internal/service/thirdpartyintegration/data_source_third_party_integrations.go index a6c9daed2f..25e5ccb976 100644 --- a/internal/service/thirdpartyintegration/data_source_third_party_integrations.go +++ b/internal/service/thirdpartyintegration/data_source_third_party_integrations.go @@ -91,6 +91,7 @@ func integrationToSchema(d *schema.ResourceData, integration *admin.ThirdPartyIn } out := map[string]any{ + "id": integration.Id, "type": integration.Type, "api_key": integrationSchema.ApiKey, "region": integration.Region, diff --git a/internal/service/thirdpartyintegration/resource_third_party_integration.go b/internal/service/thirdpartyintegration/resource_third_party_integration.go index 5f00566e8d..b34293a077 100644 --- a/internal/service/thirdpartyintegration/resource_third_party_integration.go +++ b/internal/service/thirdpartyintegration/resource_third_party_integration.go @@ -10,7 +10,6 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/v2/diag" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" "github.com/mongodb/terraform-provider-mongodbatlas/internal/common/constant" - "github.com/mongodb/terraform-provider-mongodbatlas/internal/common/conversion" "github.com/mongodb/terraform-provider-mongodbatlas/internal/config" ) @@ -44,6 +43,10 @@ func Resource() *schema.Resource { StateContext: resourceMongoDBAtlasThirdPartyIntegrationImportState, }, Schema: map[string]*schema.Schema{ + "id": { + Type: schema.TypeString, + Computed: true, + }, "project_id": { Type: schema.TypeString, Required: true, @@ -161,21 +164,14 @@ func resourceMongoDBAtlasThirdPartyIntegrationCreate(ctx context.Context, d *sch return diag.FromErr(fmt.Errorf("error creating third party integration %s", err)) } - // ID is equal to project_id+type need to ask - d.SetId(conversion.EncodeStateID(map[string]string{ - "project_id": projectID, - "type": integrationType, - })) - return resourceMongoDBAtlasThirdPartyIntegrationRead(ctx, d, meta) } func resourceMongoDBAtlasThirdPartyIntegrationRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics { connV2 := meta.(*config.MongoDBClient).AtlasV2 - ids := conversion.DecodeStateID(d.Id()) - projectID := ids["project_id"] - integrationType := ids["type"] + projectID := d.Get("project_id").(string) + integrationType := d.Get("type").(string) integration, resp, err := connV2.ThirdPartyIntegrationsApi.GetThirdPartyIntegration(ctx, projectID, integrationType).Execute() if err != nil { @@ -195,20 +191,15 @@ func resourceMongoDBAtlasThirdPartyIntegrationRead(ctx context.Context, d *schem } } - d.SetId(conversion.EncodeStateID(map[string]string{ - "project_id": projectID, - "type": integrationType, - })) - + d.SetId(integration.GetId()) return nil } func resourceMongoDBAtlasThirdPartyIntegrationUpdate(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics { connV2 := meta.(*config.MongoDBClient).AtlasV2 - ids := conversion.DecodeStateID(d.Id()) - projectID := ids["project_id"] - integrationType := ids["type"] + projectID := d.Get("project_id").(string) + integrationType := d.Get("type").(string) integration, _, err := connV2.ThirdPartyIntegrationsApi.GetThirdPartyIntegration(ctx, projectID, integrationType).Execute() @@ -231,10 +222,9 @@ func resourceMongoDBAtlasThirdPartyIntegrationUpdate(ctx context.Context, d *sch func resourceMongoDBAtlasThirdPartyIntegrationDelete(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics { conn := meta.(*config.MongoDBClient).Atlas - ids := conversion.DecodeStateID(d.Id()) - projectID := ids["project_id"] - integrationType := ids["type"] + projectID := d.Get("project_id").(string) + integrationType := d.Get("type").(string) _, err := conn.Integrations.Delete(ctx, projectID, integrationType) @@ -246,7 +236,7 @@ func resourceMongoDBAtlasThirdPartyIntegrationDelete(ctx context.Context, d *sch } func resourceMongoDBAtlasThirdPartyIntegrationImportState(ctx context.Context, d *schema.ResourceData, meta any) ([]*schema.ResourceData, error) { - conn := meta.(*config.MongoDBClient).Atlas + connV2 := meta.(*config.MongoDBClient).AtlasV2 projectID, integrationType, err := splitIntegrationTypeID(d.Id()) @@ -254,7 +244,7 @@ func resourceMongoDBAtlasThirdPartyIntegrationImportState(ctx context.Context, d return nil, err } - integration, _, err := conn.Integrations.Get(ctx, projectID, integrationType) + _, _, err = connV2.ThirdPartyIntegrationsApi.GetThirdPartyIntegration(ctx, projectID, integrationType).Execute() if err != nil { 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 return nil, fmt.Errorf("error setting `project_id` for third party integration (%s): %w", d.Id(), err) } - if err := d.Set("type", integration.Type); err != nil { + if err := d.Set("type", integrationType); err != nil { return nil, fmt.Errorf("error setting `type` for third party integration (%s): %w", d.Id(), err) } - d.SetId(conversion.EncodeStateID(map[string]string{ - "project_id": projectID, - "type": integrationType, - })) - return []*schema.ResourceData{d}, nil } diff --git a/internal/service/thirdpartyintegration/resource_third_party_integration_test.go b/internal/service/thirdpartyintegration/resource_third_party_integration_test.go index 3d008a5f1c..729c00cadb 100644 --- a/internal/service/thirdpartyintegration/resource_third_party_integration_test.go +++ b/internal/service/thirdpartyintegration/resource_third_party_integration_test.go @@ -7,7 +7,6 @@ import ( "github.com/hashicorp/terraform-plugin-testing/helper/resource" "github.com/hashicorp/terraform-plugin-testing/terraform" - "github.com/mongodb/terraform-provider-mongodbatlas/internal/common/conversion" "github.com/mongodb/terraform-provider-mongodbatlas/internal/testutil/acc" ) @@ -336,10 +335,16 @@ func checkDestroy(s *terraform.State) error { if rs.Type != "mongodbatlas_third_party_integration" { continue } - ids := conversion.DecodeStateID(rs.Primary.ID) - _, _, err := acc.Conn().Integrations.Get(context.Background(), ids["project_id"], ids["type"]) + 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.Conn().Integrations.Get(context.Background(), attrs["project_id"], attrs["type"]) if err == nil { - return fmt.Errorf("third party integration service (%s) still exists", ids["type"]) + return fmt.Errorf("third party integration service (%s) still exists", attrs["type"]) } } return nil @@ -352,9 +357,15 @@ func importStateIDFunc(resourceName string) resource.ImportStateIdFunc { return "", fmt.Errorf("not found: %s", resourceName) } - ids := conversion.DecodeStateID(rs.Primary.ID) + 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", ids["project_id"], ids["type"]), nil + return fmt.Sprintf("%s-%s", attrs["project_id"], attrs["type"]), nil } } @@ -478,13 +489,16 @@ func checkExists(resourceName string) resource.TestCheckFunc { if !ok { return fmt.Errorf("not found: %s", resourceName) } - if rs.Primary.Attributes["project_id"] == "" { - return fmt.Errorf("no ID is set") + 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") } - ids := conversion.DecodeStateID(rs.Primary.ID) - if _, _, err := acc.Conn().Integrations.Get(context.Background(), ids["project_id"], ids["type"]); err == nil { + if _, _, err := acc.Conn().Integrations.Get(context.Background(), attrs["project_id"], attrs["type"]); err == nil { return nil } - return fmt.Errorf("third party integration (%s) does not exist", ids["project_id"]) + return fmt.Errorf("third party integration (%s) does not exist", attrs["project_id"]) } } diff --git a/website/docs/d/third_party_integration.markdown b/website/docs/d/third_party_integration.markdown index 85f2567224..0d4894f046 100644 --- a/website/docs/d/third_party_integration.markdown +++ b/website/docs/d/third_party_integration.markdown @@ -45,8 +45,7 @@ data "mongodbatlas_third_party_integration" "test" { In addition to all arguments above, the following attributes are exported: -* `id` - Unique identifier used for terraform for internal manages and can be used to import. -* `type` - Property equal to its own integration type +* `id` - Unique identifier of the integration. Additional values based on Type diff --git a/website/docs/d/third_party_integrations.markdown b/website/docs/d/third_party_integrations.markdown index 716fbb0a24..caccd881bc 100644 --- a/website/docs/d/third_party_integrations.markdown +++ b/website/docs/d/third_party_integrations.markdown @@ -48,8 +48,9 @@ In addition to all arguments above, the following attributes are exported: ### Third-Party Service Integration -* `project_id` - (Required) ID of the Atlas project the Third-Party Service Integration belongs to. -* `type` - (Required) Thirt-Party service integration type. +* `project_id` - ID of the Atlas project the Third-Party Service Integration belongs to. +* `type` - Thirt-Party service integration type. +* `id` - Unique identifier of the integration. * PAGER_DUTY * DATADOG diff --git a/website/docs/r/alert_configuration.html.markdown b/website/docs/r/alert_configuration.html.markdown index e9cb61cc11..a59246878c 100644 --- a/website/docs/r/alert_configuration.html.markdown +++ b/website/docs/r/alert_configuration.html.markdown @@ -123,6 +123,11 @@ resource "mongodbatlas_alert_configuration" "test" { ```terraform +data "mongodbatlas_third_party_integration" "test" { + project_id = "PROJECT ID" + type = "PAGER_DUTY" +} + resource "mongodbatlas_alert_configuration" "test" { project_id = "PROJECT ID" enabled = true @@ -130,7 +135,7 @@ resource "mongodbatlas_alert_configuration" "test" { notification { type_name = "PAGER_DUTY" - integration_id = "THIRD PARTY INTEGRATION ID" + integration_id = data.mongodbatlas_third_party_integration.test.id } } ``` diff --git a/website/docs/r/third_party_integration.markdown b/website/docs/r/third_party_integration.markdown index a428cda664..6b9d003326 100644 --- a/website/docs/r/third_party_integration.markdown +++ b/website/docs/r/third_party_integration.markdown @@ -71,7 +71,7 @@ resource "mongodbatlas_third_party_integration" "test_datadog" { ## Attributes Reference -* `id` - Unique identifier used by terraform for internal management, which can also be used to import. +* `id` - Unique identifier of the integration. ## Import