From 4c03faeb9c5b9fc85d718685120798c44d0d4bad Mon Sep 17 00:00:00 2001
From: Aleksandr Boldyrev <aleksandr.boldyrev@fivetran.com>
Date: Fri, 15 Nov 2024 23:54:44 +0100
Subject: [PATCH 1/4] Fix LPA_ID issue

---
 CHANGELOG.md                                  |  1 +
 fivetran/framework/core/model/connector.go    | 10 +---
 .../core/model/destination_resource_model.go  |  2 +-
 fivetran/tests/e2e/fivetran_test.go           |  3 +
 ...source_hybrid_deployment_agent_e2e_test.go | 56 ++++++++++++++++++-
 5 files changed, 62 insertions(+), 10 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index fc5e6ad2..3965576b 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
 
 ## Fixed
 - Problem with `schema_change_handling` validation for SAP connectors
+- Issue with `local_processing_agent_id` in `fivetran_connector` and `fivetran_destination` resources
 
 ## [1.4.0](https://github.com/fivetran/terraform-provider-fivetran/compare/v1.3.2...v1.4.0)
 
diff --git a/fivetran/framework/core/model/connector.go b/fivetran/framework/core/model/connector.go
index aa0146a2..483ed25b 100644
--- a/fivetran/framework/core/model/connector.go
+++ b/fivetran/framework/core/model/connector.go
@@ -206,7 +206,7 @@ func (d *ConnectorResourceModel) ReadFromContainer(c ConnectorModelContainer, fo
 	d.GroupId = types.StringValue(c.GroupId)
 	d.Service = types.StringValue(c.Service)
 
-	if c.LocalProcessingAgentId != "" {
+	if c.LocalProcessingAgentId != "" && !d.LocalProcessingAgentId.IsUnknown() {
 		d.LocalProcessingAgentId = types.StringValue(c.HybridDeploymentAgentId)
 	} else {
 		d.LocalProcessingAgentId = types.StringNull()
@@ -251,12 +251,6 @@ func (d *ConnectorDatasourceModel) ReadFromContainer(c ConnectorModelContainer)
 	d.GroupId = types.StringValue(c.GroupId)
 	d.Service = types.StringValue(c.Service)
 
-	if c.LocalProcessingAgentId != "" {
-		d.LocalProcessingAgentId = types.StringValue(c.LocalProcessingAgentId)
-	} else {
-		d.LocalProcessingAgentId = types.StringNull()
-	}
-
     d.DestinationSchema = getDestinationSchemaValue(c.Service, c.Schema)
     
     if c.PrivateLinkId != "" {
@@ -341,7 +335,7 @@ func (c *ConnectorModelContainer) ReadFromResponseData(data connectors.DetailsRe
         c.PrivateLinkId = data.PrivateLinkId
 	}
 
-	if data.HybridDeploymentAgentId != "" {
+	if data.HybridDeploymentAgentId != "" && c.LocalProcessingAgentId != "" {
 		c.LocalProcessingAgentId = data.HybridDeploymentAgentId
 	}
 
diff --git a/fivetran/framework/core/model/destination_resource_model.go b/fivetran/framework/core/model/destination_resource_model.go
index 2781fffe..ecfcae89 100644
--- a/fivetran/framework/core/model/destination_resource_model.go
+++ b/fivetran/framework/core/model/destination_resource_model.go
@@ -52,7 +52,7 @@ func (d *DestinationResourceModel) SetDaylightSavingTimeEnabled(value bool) {
 	d.DaylightSavingTimeEnabled = types.BoolValue(value)
 }
 func (d *DestinationResourceModel) SetLocalProcessingAgentId(value string) {
-    if value != "" {
+    if value != "" && !d.LocalProcessingAgentId.IsUnknown() {
         d.LocalProcessingAgentId = types.StringValue(value)
     } else {
         d.LocalProcessingAgentId = types.StringNull()
diff --git a/fivetran/tests/e2e/fivetran_test.go b/fivetran/tests/e2e/fivetran_test.go
index 2880411a..bbcf6007 100644
--- a/fivetran/tests/e2e/fivetran_test.go
+++ b/fivetran/tests/e2e/fivetran_test.go
@@ -299,6 +299,9 @@ func cleanupPrivateLinks() {
 }
 
 func cleanupHybridDeploymentAgents() {
+	// this methods hides under FF
+	return;
+
 	lpaList, err := client.NewHybridDeploymentAgentList().Do(context.Background())
 	if err != nil {
 		log.Fatal(err)
diff --git a/fivetran/tests/e2e/resource_hybrid_deployment_agent_e2e_test.go b/fivetran/tests/e2e/resource_hybrid_deployment_agent_e2e_test.go
index e91ae00a..07ae0715 100644
--- a/fivetran/tests/e2e/resource_hybrid_deployment_agent_e2e_test.go
+++ b/fivetran/tests/e2e/resource_hybrid_deployment_agent_e2e_test.go
@@ -6,6 +6,7 @@ import (
 	"fmt"
 	"strings"
 	"testing"
+	"regexp"
 
 	"github.com/hashicorp/terraform-plugin-testing/helper/resource"
 	"github.com/hashicorp/terraform-plugin-testing/terraform"
@@ -41,13 +42,66 @@ func TestResourceHybridDeploymentAgentE2E(t *testing.T) {
 	})
 }
 
+func TestResourceConnectorWithHybridDeploymentAgentE2E(t *testing.T) {
+	regexp, _ := regexp.Compile("[a-z]*_[a-z]*")
+	resource.Test(t, resource.TestCase{
+		PreCheck:                 func() {},
+		ProtoV6ProviderFactories: ProtoV6ProviderFactories,
+		CheckDestroy:             testFivetranConnectorResourceDestroy,
+		Steps: []resource.TestStep{
+			{
+				Config: `
+				resource "fivetran_group" "test_group" {
+					provider = fivetran-provider
+					name = "test_group_name"
+			    }
+
+            	resource "fivetran_hybrid_deployment_agent" "test_hda" {
+                	provider = fivetran-provider
+
+                 	display_name = "TestResourceHybridDeploymentAgentE2E"
+                 	group_id = fivetran_group.test_group.id
+                 	auth_type = "AUTO"
+            	}
+
+			    resource "fivetran_connector" "test_connector" {
+					provider = fivetran-provider
+					group_id = fivetran_group.test_group.id
+					service = "fivetran_log"
+					hybrid_deployment_agent_id = fivetran_hybrid_deployment_agent.test_hda.id
+					destination_schema {
+						name = "fivetran_log_schema"
+					}
+					
+					trust_certificates = false
+					trust_fingerprints = false
+					run_setup_tests = false
+				}
+		  `,
+				Check: resource.ComposeAggregateTestCheckFunc(
+					testFivetranConnectorResourceCreate(t, "fivetran_connector.test_connector"),
+					resource.TestCheckResourceAttr("fivetran_connector.test_connector", "service", "fivetran_log"),
+					resource.TestCheckResourceAttr("fivetran_connector.test_connector", "name", "fivetran_log_schema"),
+					resource.TestCheckResourceAttr("fivetran_connector.test_connector", "trust_certificates", "false"),
+					resource.TestCheckResourceAttr("fivetran_connector.test_connector", "trust_fingerprints", "false"),
+					resource.TestCheckResourceAttr("fivetran_connector.test_connector", "run_setup_tests", "false"),
+					resource.TestMatchResourceAttr("fivetran_connector.test_connector", "hybrid_deployment_agent_id", regexp),
+				),
+			},
+		},
+	})
+}
+
 func testFivetranHybridDeploymentAgentResourceCreate(t *testing.T, resourceName string) resource.TestCheckFunc {
+	fmt.Printf("sadasasas")
+
 	return func(s *terraform.State) error {
 		rs := GetResource(t, s, resourceName)
+		fmt.Printf("sdfsdsdsdf %v", rs.Primary.ID)
 
 		_, err := client.NewHybridDeploymentAgentDetails().AgentId(rs.Primary.ID).Do(context.Background())
 		if err != nil {
-			fmt.Println(err)
+			fmt.Printf("sdfsdsdsdf %v %v", rs.Primary.ID, err)
 			return err
 		}
 		//todo: check response _  fields if needed

From 0cdebb037c2c528982e1ad0cc7594c61d2bd6676 Mon Sep 17 00:00:00 2001
From: Aleksandr Boldyrev <aleksandr.boldyrev@fivetran.com>
Date: Mon, 18 Nov 2024 11:59:03 +0100
Subject: [PATCH 2/4] fix comments

---
 fivetran/framework/core/model/connector.go    |  2 +-
 .../core/model/destination_resource_model.go  |  2 +-
 fivetran/tests/e2e/fivetran_test.go           | 21 -------------------
 3 files changed, 2 insertions(+), 23 deletions(-)

diff --git a/fivetran/framework/core/model/connector.go b/fivetran/framework/core/model/connector.go
index 483ed25b..dbc829fb 100644
--- a/fivetran/framework/core/model/connector.go
+++ b/fivetran/framework/core/model/connector.go
@@ -206,7 +206,7 @@ func (d *ConnectorResourceModel) ReadFromContainer(c ConnectorModelContainer, fo
 	d.GroupId = types.StringValue(c.GroupId)
 	d.Service = types.StringValue(c.Service)
 
-	if c.LocalProcessingAgentId != "" && !d.LocalProcessingAgentId.IsUnknown() {
+	if c.LocalProcessingAgentId != "" && !d.LocalProcessingAgentId.IsUnknown() && !d.LocalProcessingAgentId.IsNull(){
 		d.LocalProcessingAgentId = types.StringValue(c.HybridDeploymentAgentId)
 	} else {
 		d.LocalProcessingAgentId = types.StringNull()
diff --git a/fivetran/framework/core/model/destination_resource_model.go b/fivetran/framework/core/model/destination_resource_model.go
index ecfcae89..a60f508f 100644
--- a/fivetran/framework/core/model/destination_resource_model.go
+++ b/fivetran/framework/core/model/destination_resource_model.go
@@ -52,7 +52,7 @@ func (d *DestinationResourceModel) SetDaylightSavingTimeEnabled(value bool) {
 	d.DaylightSavingTimeEnabled = types.BoolValue(value)
 }
 func (d *DestinationResourceModel) SetLocalProcessingAgentId(value string) {
-    if value != "" && !d.LocalProcessingAgentId.IsUnknown() {
+    if value != "" && !d.LocalProcessingAgentId.IsUnknown() && !d.LocalProcessingAgentId.IsNull() {
         d.LocalProcessingAgentId = types.StringValue(value)
     } else {
         d.LocalProcessingAgentId = types.StringNull()
diff --git a/fivetran/tests/e2e/fivetran_test.go b/fivetran/tests/e2e/fivetran_test.go
index bbcf6007..539e8377 100644
--- a/fivetran/tests/e2e/fivetran_test.go
+++ b/fivetran/tests/e2e/fivetran_test.go
@@ -93,7 +93,6 @@ func cleanupAccount() {
 	cleanupTeams()
 	cleanupProxyAgents()
 	cleanupPrivateLinks()
-	cleanupHybridDeploymentAgents()
 }
 
 func isPredefinedUserExist() bool {
@@ -297,23 +296,3 @@ func cleanupPrivateLinks() {
 		cleanupPrivateLinks()
 	}
 }
-
-func cleanupHybridDeploymentAgents() {
-	// this methods hides under FF
-	return;
-
-	lpaList, err := client.NewHybridDeploymentAgentList().Do(context.Background())
-	if err != nil {
-		log.Fatal(err)
-	}
-	for _, lpa := range lpaList.Data.Items {
-		_, err := client.NewHybridDeploymentAgentDelete().AgentId(lpa.Id).Do(context.Background())
-		if err != nil {
-			log.Fatal(err)
-		}
-	}
-
-	if lpaList.Data.NextCursor != "" {
-		cleanupHybridDeploymentAgents()
-	}
-}

From 4900febdfee785523503b52b07688d376007e278 Mon Sep 17 00:00:00 2001
From: Aleksandr Boldyrev <aleksandr.boldyrev@fivetran.com>
Date: Mon, 18 Nov 2024 12:47:21 +0100
Subject: [PATCH 3/4] fix

---
 ...source_hybrid_deployment_agent_e2e_test.go | 84 +++++++++++--------
 .../e2e/resource_private_link_e2e_test.go     |  2 +-
 2 files changed, 49 insertions(+), 37 deletions(-)

diff --git a/fivetran/tests/e2e/resource_hybrid_deployment_agent_e2e_test.go b/fivetran/tests/e2e/resource_hybrid_deployment_agent_e2e_test.go
index 07ae0715..4b1564a6 100644
--- a/fivetran/tests/e2e/resource_hybrid_deployment_agent_e2e_test.go
+++ b/fivetran/tests/e2e/resource_hybrid_deployment_agent_e2e_test.go
@@ -7,59 +7,36 @@ import (
 	"strings"
 	"testing"
 	"regexp"
+	"strconv"
 
 	"github.com/hashicorp/terraform-plugin-testing/helper/resource"
 	"github.com/hashicorp/terraform-plugin-testing/terraform"
 )
 
-func TestResourceHybridDeploymentAgentE2E(t *testing.T) {
-	resource.Test(t, resource.TestCase{
-		PreCheck:                 func() {},
-		ProtoV6ProviderFactories: ProtoV6ProviderFactories,
-		CheckDestroy:             testFivetranHybridDeploymentAgentResourceDestroy,
-		Steps: []resource.TestStep{
-			{
-				Config: `
+var hdaResourceConfig = `
 				resource "fivetran_group" "testgroup" {
 					provider = fivetran-provider
-					name = "TestResourceHybridDeploymentAgentE2E"
+					name = "%v"
 			    }
 
             	resource "fivetran_hybrid_deployment_agent" "test_lpa" {
                 	provider = fivetran-provider
 
-                 	display_name = "TestResourceHybridDeploymentAgentE2E"
+                 	display_name = "%v"
                  	group_id = fivetran_group.testgroup.id
                  	auth_type = "AUTO"
-            	}`,
-				Check: resource.ComposeAggregateTestCheckFunc(
-					testFivetranHybridDeploymentAgentResourceCreate(t, "fivetran_hybrid_deployment_agent.test_lpa"),
-					resource.TestCheckResourceAttr("fivetran_hybrid_deployment_agent.test_lpa", "display_name", "TestResourceHybridDeploymentAgentE2E"),
-					resource.TestCheckResourceAttrSet("fivetran_hybrid_deployment_agent.test_lpa", "token"),
-				),
-			},
-		},
-	})
-}
+            	}`
 
-func TestResourceConnectorWithHybridDeploymentAgentE2E(t *testing.T) {
-	regexp, _ := regexp.Compile("[a-z]*_[a-z]*")
-	resource.Test(t, resource.TestCase{
-		PreCheck:                 func() {},
-		ProtoV6ProviderFactories: ProtoV6ProviderFactories,
-		CheckDestroy:             testFivetranConnectorResourceDestroy,
-		Steps: []resource.TestStep{
-			{
-				Config: `
+var connectorWithHdaResourceConfig = `
 				resource "fivetran_group" "test_group" {
 					provider = fivetran-provider
-					name = "test_group_name"
+					name = "%v"
 			    }
 
             	resource "fivetran_hybrid_deployment_agent" "test_hda" {
                 	provider = fivetran-provider
 
-                 	display_name = "TestResourceHybridDeploymentAgentE2E"
+                 	display_name = "%v"
                  	group_id = fivetran_group.test_group.id
                  	auth_type = "AUTO"
             	}
@@ -77,7 +54,46 @@ func TestResourceConnectorWithHybridDeploymentAgentE2E(t *testing.T) {
 					trust_fingerprints = false
 					run_setup_tests = false
 				}
-		  `,
+		  `
+
+func TestResourceHybridDeploymentAgentE2E(t *testing.T) {
+	hdaName := strconv.Itoa(seededRand.Int())
+	groupName := strconv.Itoa(seededRand.Int())
+
+	resourceConfig := fmt.Sprintf(hdaResourceConfig, groupName, hdaName)
+
+	resource.Test(t, resource.TestCase{
+		PreCheck:                 func() {},
+		ProtoV6ProviderFactories: ProtoV6ProviderFactories,
+		CheckDestroy:             testFivetranHybridDeploymentAgentResourceDestroy,
+		Steps: []resource.TestStep{
+			{
+				Config:resourceConfig,
+				Check: resource.ComposeAggregateTestCheckFunc(
+					testFivetranHybridDeploymentAgentResourceCreate(t, "fivetran_hybrid_deployment_agent.test_lpa"),
+					resource.TestCheckResourceAttr("fivetran_hybrid_deployment_agent.test_lpa", "display_name", hdaName),
+					resource.TestCheckResourceAttrSet("fivetran_hybrid_deployment_agent.test_lpa", "token"),
+				),
+			},
+		},
+	})
+}
+
+func TestResourceConnectorWithHybridDeploymentAgentE2E(t *testing.T) {
+	regexp, _ := regexp.Compile("[a-z]*_[a-z]*")
+	
+	hdaName := strconv.Itoa(seededRand.Int())
+	groupName := strconv.Itoa(seededRand.Int())
+
+	resourceConfig := fmt.Sprintf(connectorWithHdaResourceConfig, groupName, hdaName)
+
+	resource.Test(t, resource.TestCase{
+		PreCheck:                 func() {},
+		ProtoV6ProviderFactories: ProtoV6ProviderFactories,
+		CheckDestroy:             testFivetranConnectorResourceDestroy,
+		Steps: []resource.TestStep{
+			{
+				Config: resourceConfig,
 				Check: resource.ComposeAggregateTestCheckFunc(
 					testFivetranConnectorResourceCreate(t, "fivetran_connector.test_connector"),
 					resource.TestCheckResourceAttr("fivetran_connector.test_connector", "service", "fivetran_log"),
@@ -93,15 +109,11 @@ func TestResourceConnectorWithHybridDeploymentAgentE2E(t *testing.T) {
 }
 
 func testFivetranHybridDeploymentAgentResourceCreate(t *testing.T, resourceName string) resource.TestCheckFunc {
-	fmt.Printf("sadasasas")
-
 	return func(s *terraform.State) error {
 		rs := GetResource(t, s, resourceName)
-		fmt.Printf("sdfsdsdsdf %v", rs.Primary.ID)
 
 		_, err := client.NewHybridDeploymentAgentDetails().AgentId(rs.Primary.ID).Do(context.Background())
 		if err != nil {
-			fmt.Printf("sdfsdsdsdf %v %v", rs.Primary.ID, err)
 			return err
 		}
 		//todo: check response _  fields if needed
diff --git a/fivetran/tests/e2e/resource_private_link_e2e_test.go b/fivetran/tests/e2e/resource_private_link_e2e_test.go
index 4b362e98..dcd41250 100644
--- a/fivetran/tests/e2e/resource_private_link_e2e_test.go
+++ b/fivetran/tests/e2e/resource_private_link_e2e_test.go
@@ -25,7 +25,7 @@ var privateLinkResourceConfig = `
             	}`
 
 func TestResourcePrivateLinkE2E(t *testing.T) {
-	//t.Skip("Private links have a strict limit on the number of entities created. This test should only be used for intermediate tests when changes are made directly to Private links.")
+	t.Skip("Private links have a strict limit on the number of entities created. This test should only be used for intermediate tests when changes are made directly to Private links.")
 	suffix := strconv.Itoa(seededRand.Int())
 	privateLinkName := suffix
 	privateLinkCfgValue := "privatelink_" + suffix

From 7606bba5cc22eb44c2b9a3168ca8fa198f7870af Mon Sep 17 00:00:00 2001
From: Aleksandr Boldyrev <aleksandr.boldyrev@fivetran.com>
Date: Mon, 18 Nov 2024 13:41:28 +0100
Subject: [PATCH 4/4] Update resource_hybrid_deployment_agent_e2e_test.go

---
 .../tests/e2e/resource_hybrid_deployment_agent_e2e_test.go    | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/fivetran/tests/e2e/resource_hybrid_deployment_agent_e2e_test.go b/fivetran/tests/e2e/resource_hybrid_deployment_agent_e2e_test.go
index 4b1564a6..e9285995 100644
--- a/fivetran/tests/e2e/resource_hybrid_deployment_agent_e2e_test.go
+++ b/fivetran/tests/e2e/resource_hybrid_deployment_agent_e2e_test.go
@@ -58,7 +58,7 @@ var connectorWithHdaResourceConfig = `
 
 func TestResourceHybridDeploymentAgentE2E(t *testing.T) {
 	hdaName := strconv.Itoa(seededRand.Int())
-	groupName := strconv.Itoa(seededRand.Int())
+	groupName := "group" + strconv.Itoa(seededRand.Int())
 
 	resourceConfig := fmt.Sprintf(hdaResourceConfig, groupName, hdaName)
 
@@ -83,7 +83,7 @@ func TestResourceConnectorWithHybridDeploymentAgentE2E(t *testing.T) {
 	regexp, _ := regexp.Compile("[a-z]*_[a-z]*")
 	
 	hdaName := strconv.Itoa(seededRand.Int())
-	groupName := strconv.Itoa(seededRand.Int())
+	groupName := "group" + strconv.Itoa(seededRand.Int())
 
 	resourceConfig := fmt.Sprintf(connectorWithHdaResourceConfig, groupName, hdaName)