From 940ba40edf17437761a2f71d8dc7418ffe9f4e43 Mon Sep 17 00:00:00 2001
From: Tomer Heber <theber@zscaler.com>
Date: Mon, 7 Oct 2024 08:08:24 -0500
Subject: [PATCH] Fix: variable set assignment drifts with parent projects

---
 env0/resource_variable_set.go                 |  2 ++
 env0/resource_variable_set_assignment.go      |  6 ++++++
 env0/resource_variable_set_assignment_test.go | 13 +++++++++++++
 3 files changed, 21 insertions(+)

diff --git a/env0/resource_variable_set.go b/env0/resource_variable_set.go
index c604c607..9930784a 100644
--- a/env0/resource_variable_set.go
+++ b/env0/resource_variable_set.go
@@ -300,11 +300,13 @@ func mergeVariables(schema []client.ConfigurationVariable, api []client.Configur
 		for _, avariable := range api {
 			if svariable.Name == avariable.Name && *svariable.Type == *avariable.Type {
 				found = true
+
 				if avariable.IsSensitive != nil && *avariable.IsSensitive {
 					// Sensitive - to avoid drift use the value from the schema
 					avariable.Value = svariable.Value
 				}
 				res.currentVariables = append(res.currentVariables, avariable)
+
 				break
 			}
 		}
diff --git a/env0/resource_variable_set_assignment.go b/env0/resource_variable_set_assignment.go
index 26f08ee5..7615e2c3 100644
--- a/env0/resource_variable_set_assignment.go
+++ b/env0/resource_variable_set_assignment.go
@@ -188,10 +188,16 @@ func resourceVariableSetAssignmentRead(ctx context.Context, d *schema.ResourceDa
 	}
 
 	for _, apiConfigurationSet := range apiConfigurationSets {
+		// Filter out irrelevant scopes.
 		if !strings.EqualFold(apiConfigurationSet.AssignmentScope, assignmentSchema.Scope) {
 			continue
 		}
 
+		// Filter out inherited scopes (e.g parent project).
+		if apiConfigurationSet.CreationScopeId != assignmentSchema.ScopeId {
+			continue
+		}
+
 		apiSetId := apiConfigurationSet.Id
 		found := false
 
diff --git a/env0/resource_variable_set_assignment_test.go b/env0/resource_variable_set_assignment_test.go
index 52263728..179c2df2 100644
--- a/env0/resource_variable_set_assignment_test.go
+++ b/env0/resource_variable_set_assignment_test.go
@@ -22,10 +22,17 @@ func TestUnitVariableSetAssignmentResource(t *testing.T) {
 			{
 				Id:              "a1",
 				AssignmentScope: scope,
+				CreationScopeId: scopeId,
 			},
 			{
 				Id:              "a2",
 				AssignmentScope: scope,
+				CreationScopeId: scopeId,
+			},
+			{
+				Id:              "filtered_out",
+				AssignmentScope: scope,
+				CreationScopeId: "otherCreationScopeId",
 			},
 		}
 		// Validates that drifts do not occur due to ordering.
@@ -33,10 +40,12 @@ func TestUnitVariableSetAssignmentResource(t *testing.T) {
 			{
 				Id:              "a2",
 				AssignmentScope: scope,
+				CreationScopeId: scopeId,
 			},
 			{
 				Id:              "a1",
 				AssignmentScope: scope,
+				CreationScopeId: scopeId,
 			},
 		}
 
@@ -45,10 +54,12 @@ func TestUnitVariableSetAssignmentResource(t *testing.T) {
 			{
 				Id:              "a3",
 				AssignmentScope: scope,
+				CreationScopeId: scopeId,
 			},
 			{
 				Id:              "a1",
 				AssignmentScope: scope,
+				CreationScopeId: scopeId,
 			},
 		}
 
@@ -105,10 +116,12 @@ func TestUnitVariableSetAssignmentResource(t *testing.T) {
 			{
 				Id:              "a1",
 				AssignmentScope: scope,
+				CreationScopeId: scopeId,
 			},
 			{
 				Id:              "a2",
 				AssignmentScope: scope,
+				CreationScopeId: scopeId,
 			},
 		}