Skip to content

Commit 2471e04

Browse files
committed
feat: enhance integration resource handling and update import state logic
1 parent 8db9380 commit 2471e04

File tree

3 files changed

+42
-6
lines changed

3 files changed

+42
-6
lines changed

internal/models/intergation.go

+5
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,11 @@ func ExpandIntergationResource(in *client.IntegrationParam, integration Integrat
185185

186186
func FlattenIntergrationResource(integration *client.IntegrationVO, resource *IntegrationResourceModel) {
187187
resource.ID = types.StringValue(integration.Code)
188+
if integration.EndPoint != nil && *integration.EndPoint != "" {
189+
resource.EndPoint = types.StringValue(*integration.EndPoint)
190+
} else {
191+
resource.EndPoint = types.StringNull()
192+
}
188193
resource.Name = types.StringValue(integration.Name)
189194
resource.Type = types.StringValue(integration.Type)
190195
resource.DeployProfile = types.StringValue(integration.Profile)

internal/provider/resource_integration_test.go

+18-5
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"testing"
77

88
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
9+
"github.com/hashicorp/terraform-plugin-testing/terraform"
910
)
1011

1112
func TestAccIntegrationResource(t *testing.T) {
@@ -17,15 +18,15 @@ func TestAccIntegrationResource(t *testing.T) {
1718
if deployProfile == "" {
1819
t.Skip("AUTOMQ_TEST_DEPLOY_PROFILE must be set for this test")
1920
}
20-
endpoint := os.Getenv("AUTOMQ_TEST_BYOC_ENDPOINT")
21+
endpoint := os.Getenv("AUTOMQ_BYOC_ENDPOINT")
2122
if endpoint == "" {
22-
t.Skip("AUTOMQ_TEST_BYOC_ENDPOINT must be set for this test")
23+
t.Skip("AUTOMQ_BYOC_ENDPOINT must be set for this test")
2324
}
24-
accessKeyId := os.Getenv("AUTOMQ_TEST_BYOC_ACCESS_KEY_ID")
25+
accessKeyId := os.Getenv("AUTOMQ_BYOC_ACCESS_KEY_ID")
2526
if accessKeyId == "" {
2627
t.Skip("AUTOMQ_TEST_BYOC_ACCESS_KEY_ID must be set for this test")
2728
}
28-
secretKey := os.Getenv("AUTOMQ_TEST_BYOC_SECRET_KEY")
29+
secretKey := os.Getenv("AUTOMQ_BYOC_SECRET_KEY")
2930
if secretKey == "" {
3031
t.Skip("AUTOMQ_TEST_BYOC_SECRET_KEY must be set for this test")
3132
}
@@ -56,6 +57,18 @@ func TestAccIntegrationResource(t *testing.T) {
5657
ResourceName: "automq_integration.test_prometheus",
5758
ImportState: true,
5859
ImportStateVerify: true,
60+
ImportStateIdFunc: func(s *terraform.State) (string, error) {
61+
rs, ok := s.RootModule().Resources["automq_integration.test_prometheus"]
62+
if !ok {
63+
return "", fmt.Errorf("Not found: %s", "automq_integration.test_prometheus")
64+
}
65+
id := fmt.Sprintf("%s@%s", rs.Primary.Attributes["environment_id"], rs.Primary.Attributes["id"])
66+
return id, nil
67+
},
68+
ImportStateVerifyIgnore: []string{
69+
"created_at",
70+
"last_updated",
71+
},
5972
},
6073
// Update testing
6174
{
@@ -147,7 +160,7 @@ resource "automq_integration" "test_cloudwatch" {
147160
deploy_profile = %[2]q
148161
name = "test-cloudwatch"
149162
type = "cloudWatch"
150-
cloudwatch_config {
163+
cloudwatch_config = {
151164
namespace = "AutoMQ/Test"
152165
}
153166
}

internal/provider/resource_intergation.go

+19-1
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@ package provider
55
import (
66
"context"
77
"fmt"
8+
"strings"
89
"terraform-provider-automq/client"
910
"terraform-provider-automq/internal/framework"
1011
"terraform-provider-automq/internal/models"
1112

1213
"github.com/hashicorp/terraform-plugin-framework-timetypes/timetypes"
1314
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
15+
"github.com/hashicorp/terraform-plugin-framework/diag"
1416
"github.com/hashicorp/terraform-plugin-framework/path"
1517
"github.com/hashicorp/terraform-plugin-framework/resource"
1618
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
@@ -248,5 +250,21 @@ func (r *IntegrationResource) Delete(ctx context.Context, req resource.DeleteReq
248250
}
249251

250252
func (r *IntegrationResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
251-
resource.ImportStatePassthroughID(ctx, path.Root("id"), req, resp)
253+
idParts := strings.Split(req.ID, "@")
254+
255+
if len(idParts) != 2 || idParts[0] == "" || idParts[1] == "" {
256+
resp.Diagnostics.Append(
257+
diag.NewErrorDiagnostic(
258+
"Invalid Import ID",
259+
fmt.Sprintf("The import ID must be in the format <environment_id>@<integration_id>. Got: %s", req.ID),
260+
),
261+
)
262+
return
263+
}
264+
265+
environmentID := idParts[0]
266+
integrationId := idParts[1]
267+
268+
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("environment_id"), environmentID)...)
269+
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("id"), integrationId)...)
252270
}

0 commit comments

Comments
 (0)