Repository: klothoplatform/klotho
Pull request #835 (2023-12-19 18:09:11)

rename resources
fixes bug in renaming resources


### Standard checks

- **Unit tests**: Any special considerations?
- **Docs**: Do we need to update any docs, internal or public?
- **Backwards compatibility**: Will this break existing apps? If so, what would be the extra work required to keep them working?



Patch:
diff --git a/pkg/engine2/operational_eval/evaluator.go b/pkg/engine2/operational_eval/evaluator.go
index 8a6f3791..8804ccae 100644
--- a/pkg/engine2/operational_eval/evaluator.go
+++ b/pkg/engine2/operational_eval/evaluator.go
@@ -400,7 +400,12 @@ func (eval *Evaluator) UpdateId(oldId, newId construct.ResourceId) error {
 		return err
 	}
 	v.ID = newId
-	err = construct.PropagateUpdatedId(eval.Solution.RawView(), oldId)
+	// We have to operate on these graphs separately since the deployment graph can store edges based on property references.
+	// since these edges wont exist in the dataflow graph they would never get cleaned up if we passed in the raw view.
+	err = errors.Join(
+		construct.PropagateUpdatedId(eval.Solution.DataflowGraph(), oldId),
+		graph_addons.ReplaceVertex(eval.Solution.DeploymentGraph(), oldId, v, construct.ResourceHasher),
+	)
 	if err != nil {
 		return err
 	}


Test patch:
diff --git a/pkg/engine2/testdata/rename.dataflow-viz.yaml b/pkg/engine2/testdata/rename.dataflow-viz.yaml
new file mode 100644
index 00000000..2af05c54
--- /dev/null
+++ b/pkg/engine2/testdata/rename.dataflow-viz.yaml
@@ -0,0 +1,11 @@
+provider: aws
+resources:
+  dynamodb_table/dynamodb_table_1:
+
+  lambda_function/new_lambda_name:
+    children: aws:ecr_image:lambda_function_0-image,aws:iam_role:lambda_function_0-ExecutionRole,aws:log_group:new_lambda_name-log-group
+
+  lambda_function/new_lambda_name -> dynamodb_table/dynamodb_table_1:
+    path: aws:SERVICE_API:lambda_function_0-lambda_function_0-log-group
+
+
diff --git a/pkg/engine2/testdata/rename.expect.yaml b/pkg/engine2/testdata/rename.expect.yaml
new file mode 100644
index 00000000..742a5329
--- /dev/null
+++ b/pkg/engine2/testdata/rename.expect.yaml
@@ -0,0 +1,65 @@
+resources:
+    aws:lambda_function:new_lambda_name:
+        EnvironmentVariables:
+            DYNAMODB_TABLE_1_TABLE_NAME: aws:dynamodb_table:dynamodb_table_1#Name
+        ExecutionRole: aws:iam_role:lambda_function_0-ExecutionRole
+        Image: aws:ecr_image:lambda_function_0-image
+        LogGroup: aws:log_group:new_lambda_name-log-group
+        MemorySize: 512
+        Timeout: 180
+    aws:SERVICE_API:lambda_function_0-lambda_function_0-log-group:
+    aws:ecr_image:lambda_function_0-image:
+        Context: .
+        Dockerfile: lambda_function_0-image.Dockerfile
+        Repo: aws:ecr_repo:ecr_repo-0
+    aws:iam_role:lambda_function_0-ExecutionRole:
+        AssumeRolePolicyDoc:
+            Statement:
+                - Action:
+                    - sts:AssumeRole
+                  Effect: Allow
+                  Principal:
+                    Service:
+                        - lambda.amazonaws.com
+            Version: "2012-10-17"
+        InlinePolicies:
+            - Name: dynamodb_table_1-policy
+              Policy:
+                Statement:
+                    - Action:
+                        - dynamodb:*
+                      Effect: Allow
+                      Resource:
+                        - aws:dynamodb_table:dynamodb_table_1#Arn
+                        - aws:dynamodb_table:dynamodb_table_1#DynamoTableStreamArn
+                        - aws:dynamodb_table:dynamodb_table_1#DynamoTableBackupArn
+                        - aws:dynamodb_table:dynamodb_table_1#DynamoTableExportArn
+                        - aws:dynamodb_table:dynamodb_table_1#DynamoTableIndexArn
+                Version: "2012-10-17"
+        ManagedPolicies:
+            - arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
+    aws:ecr_repo:ecr_repo-0:
+        ForceDelete: true
+    aws:dynamodb_table:dynamodb_table_1:
+        Attributes:
+            - Name: id
+              Type: S
+        BillingMode: PAY_PER_REQUEST
+        HashKey: id
+    aws:log_group:lambda_function_0-log-group:
+        LogGroupName: /aws/lambda/lambda_function_0
+        RetentionInDays: 5
+    aws:log_group:new_lambda_name-log-group:
+        LogGroupName: /aws/lambda/new_lambda_name
+        RetentionInDays: 5
+edges:
+    aws:lambda_function:new_lambda_name -> aws:SERVICE_API:lambda_function_0-lambda_function_0-log-group:
+    aws:lambda_function:new_lambda_name -> aws:ecr_image:lambda_function_0-image:
+    aws:lambda_function:new_lambda_name -> aws:iam_role:lambda_function_0-ExecutionRole:
+    aws:SERVICE_API:lambda_function_0-lambda_function_0-log-group -> aws:dynamodb_table:dynamodb_table_1:
+    aws:SERVICE_API:lambda_function_0-lambda_function_0-log-group -> aws:log_group:lambda_function_0-log-group:
+    aws:SERVICE_API:lambda_function_0-lambda_function_0-log-group -> aws:log_group:new_lambda_name-log-group:
+    aws:ecr_image:lambda_function_0-image -> aws:ecr_repo:ecr_repo-0:
+    aws:iam_role:lambda_function_0-ExecutionRole -> aws:dynamodb_table:dynamodb_table_1:
+    aws:iam_role:lambda_function_0-ExecutionRole -> aws:log_group:lambda_function_0-log-group:
+    aws:iam_role:lambda_function_0-ExecutionRole -> aws:log_group:new_lambda_name-log-group:
diff --git a/pkg/engine2/testdata/rename.iac-viz.yaml b/pkg/engine2/testdata/rename.iac-viz.yaml
new file mode 100644
index 00000000..d11a68dc
--- /dev/null
+++ b/pkg/engine2/testdata/rename.iac-viz.yaml
@@ -0,0 +1,23 @@
+provider: aws
+resources:
+  ecr_repo/ecr_repo-0:
+
+  dynamodb_table/dynamodb_table_1:
+
+  log_group/lambda_function_0-log-group:
+
+  log_group/new_lambda_name-log-group:
+
+  ecr_image/lambda_function_0-image:
+  ecr_image/lambda_function_0-image -> ecr_repo/ecr_repo-0:
+
+  iam_role/lambda_function_0-executionrole:
+  iam_role/lambda_function_0-executionrole -> dynamodb_table/dynamodb_table_1:
+  iam_role/lambda_function_0-executionrole -> log_group/lambda_function_0-log-group:
+  iam_role/lambda_function_0-executionrole -> log_group/new_lambda_name-log-group:
+
+  lambda_function/new_lambda_name:
+  lambda_function/new_lambda_name -> dynamodb_table/dynamodb_table_1:
+  lambda_function/new_lambda_name -> ecr_image/lambda_function_0-image:
+  lambda_function/new_lambda_name -> iam_role/lambda_function_0-executionrole:
+
diff --git a/pkg/engine2/testdata/rename.input.yaml b/pkg/engine2/testdata/rename.input.yaml
new file mode 100644
index 00000000..d5826e97
--- /dev/null
+++ b/pkg/engine2/testdata/rename.input.yaml
@@ -0,0 +1,65 @@
+constraints:
+- node: aws:lambda_function:lambda_function_0
+  operator: replace
+  replacement_node: aws:lambda_function:new_lambda_name
+  scope: application
+resources:
+    aws:lambda_function:lambda_function_0:
+        EnvironmentVariables:
+            DYNAMODB_TABLE_1_TABLE_NAME: aws:dynamodb_table:dynamodb_table_1#Name
+        ExecutionRole: aws:iam_role:lambda_function_0-ExecutionRole
+        Image: aws:ecr_image:lambda_function_0-image
+        LogGroup: aws:log_group:lambda_function_0-log-group
+        MemorySize: 512
+        Timeout: 180
+    aws:SERVICE_API:lambda_function_0-lambda_function_0-log-group:
+    aws:ecr_image:lambda_function_0-image:
+        Context: .
+        Dockerfile: lambda_function_0-image.Dockerfile
+        Repo: aws:ecr_repo:ecr_repo-0
+    aws:iam_role:lambda_function_0-ExecutionRole:
+        AssumeRolePolicyDoc:
+            Statement:
+                - Action:
+                    - sts:AssumeRole
+                  Effect: Allow
+                  Principal:
+                    Service:
+                        - lambda.amazonaws.com
+            Version: "2012-10-17"
+        InlinePolicies:
+            - Name: dynamodb_table_1-policy
+              Policy:
+                Statement:
+                    - Action:
+                        - dynamodb:*
+                      Effect: Allow
+                      Resource:
+                        - aws:dynamodb_table:dynamodb_table_1#Arn
+                        - aws:dynamodb_table:dynamodb_table_1#DynamoTableStreamArn
+                        - aws:dynamodb_table:dynamodb_table_1#DynamoTableBackupArn
+                        - aws:dynamodb_table:dynamodb_table_1#DynamoTableExportArn
+                        - aws:dynamodb_table:dynamodb_table_1#DynamoTableIndexArn
+                Version: "2012-10-17"
+        ManagedPolicies:
+            - arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
+    aws:ecr_repo:ecr_repo-0:
+        ForceDelete: true
+    aws:dynamodb_table:dynamodb_table_1:
+        Attributes:
+            - Name: id
+              Type: S
+        BillingMode: PAY_PER_REQUEST
+        HashKey: id
+    aws:log_group:lambda_function_0-log-group:
+        LogGroupName: /aws/lambda/lambda_function_0
+        RetentionInDays: 5
+edges:
+    aws:lambda_function:lambda_function_0 -> aws:SERVICE_API:lambda_function_0-lambda_function_0-log-group:
+    aws:lambda_function:lambda_function_0 -> aws:ecr_image:lambda_function_0-image:
+    aws:lambda_function:lambda_function_0 -> aws:iam_role:lambda_function_0-ExecutionRole:
+    aws:SERVICE_API:lambda_function_0-lambda_function_0-log-group -> aws:dynamodb_table:dynamodb_table_1:
+    aws:SERVICE_API:lambda_function_0-lambda_function_0-log-group -> aws:log_group:lambda_function_0-log-group:
+    aws:ecr_image:lambda_function_0-image -> aws:ecr_repo:ecr_repo-0:
+    aws:iam_role:lambda_function_0-ExecutionRole -> aws:dynamodb_table:dynamodb_table_1:
+    aws:iam_role:lambda_function_0-ExecutionRole -> aws:log_group:lambda_function_0-log-group: