Skip to content

feat: enhance integration resource handling and update import state l… #78

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions internal/models/intergation.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,11 @@ func ExpandIntergationResource(in *client.IntegrationParam, integration Integrat

func FlattenIntergrationResource(integration *client.IntegrationVO, resource *IntegrationResourceModel) {
resource.ID = types.StringValue(integration.Code)
if integration.EndPoint != nil && *integration.EndPoint != "" {
resource.EndPoint = types.StringValue(*integration.EndPoint)
} else {
resource.EndPoint = types.StringNull()
}
resource.Name = types.StringValue(integration.Name)
resource.Type = types.StringValue(integration.Type)
resource.DeployProfile = types.StringValue(integration.Profile)
Expand Down
23 changes: 18 additions & 5 deletions internal/provider/resource_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"testing"

"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/terraform"
)

func TestAccIntegrationResource(t *testing.T) {
Expand All @@ -17,15 +18,15 @@ func TestAccIntegrationResource(t *testing.T) {
if deployProfile == "" {
t.Skip("AUTOMQ_TEST_DEPLOY_PROFILE must be set for this test")
}
endpoint := os.Getenv("AUTOMQ_TEST_BYOC_ENDPOINT")
endpoint := os.Getenv("AUTOMQ_BYOC_ENDPOINT")
if endpoint == "" {
t.Skip("AUTOMQ_TEST_BYOC_ENDPOINT must be set for this test")
t.Skip("AUTOMQ_BYOC_ENDPOINT must be set for this test")
}
accessKeyId := os.Getenv("AUTOMQ_TEST_BYOC_ACCESS_KEY_ID")
accessKeyId := os.Getenv("AUTOMQ_BYOC_ACCESS_KEY_ID")
if accessKeyId == "" {
t.Skip("AUTOMQ_TEST_BYOC_ACCESS_KEY_ID must be set for this test")
}
secretKey := os.Getenv("AUTOMQ_TEST_BYOC_SECRET_KEY")
secretKey := os.Getenv("AUTOMQ_BYOC_SECRET_KEY")
if secretKey == "" {
t.Skip("AUTOMQ_TEST_BYOC_SECRET_KEY must be set for this test")
}
Expand Down Expand Up @@ -56,6 +57,18 @@ func TestAccIntegrationResource(t *testing.T) {
ResourceName: "automq_integration.test_prometheus",
ImportState: true,
ImportStateVerify: true,
ImportStateIdFunc: func(s *terraform.State) (string, error) {
rs, ok := s.RootModule().Resources["automq_integration.test_prometheus"]
if !ok {
return "", fmt.Errorf("Not found: %s", "automq_integration.test_prometheus")
}
id := fmt.Sprintf("%s@%s", rs.Primary.Attributes["environment_id"], rs.Primary.Attributes["id"])
return id, nil
},
ImportStateVerifyIgnore: []string{
"created_at",
"last_updated",
},
},
// Update testing
{
Expand Down Expand Up @@ -147,7 +160,7 @@ resource "automq_integration" "test_cloudwatch" {
deploy_profile = %[2]q
name = "test-cloudwatch"
type = "cloudWatch"
cloudwatch_config {
cloudwatch_config = {
namespace = "AutoMQ/Test"
}
}
Expand Down
20 changes: 19 additions & 1 deletion internal/provider/resource_intergation.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ package provider
import (
"context"
"fmt"
"strings"
"terraform-provider-automq/client"
"terraform-provider-automq/internal/framework"
"terraform-provider-automq/internal/models"

"github.com/hashicorp/terraform-plugin-framework-timetypes/timetypes"
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
Expand Down Expand Up @@ -248,5 +250,21 @@ func (r *IntegrationResource) Delete(ctx context.Context, req resource.DeleteReq
}

func (r *IntegrationResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
resource.ImportStatePassthroughID(ctx, path.Root("id"), req, resp)
idParts := strings.Split(req.ID, "@")

if len(idParts) != 2 || idParts[0] == "" || idParts[1] == "" {
resp.Diagnostics.Append(
diag.NewErrorDiagnostic(
"Invalid Import ID",
fmt.Sprintf("The import ID must be in the format <environment_id>@<integration_id>. Got: %s", req.ID),
),
)
return
}

environmentID := idParts[0]
integrationId := idParts[1]

resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("environment_id"), environmentID)...)
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("id"), integrationId)...)
}
Loading