-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from kishaningithub/add-tests-for-custom-id-logic
Add support for aws_security_group_rule resource type and fix id generation logic
- Loading branch information
Showing
11 changed files
with
223 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package tfimportgen | ||
|
||
import ( | ||
"fmt" | ||
"github.com/kishaningithub/tf-import-gen/pkg/internal/parser" | ||
"strings" | ||
) | ||
|
||
func computeTerraformImportForResource(resource parser.TerraformResource) TerraformImport { | ||
return TerraformImport{ | ||
ResourceAddress: resource.Address, | ||
ResourceID: computeResourceID(resource), | ||
} | ||
} | ||
|
||
func computeResourceID(resource parser.TerraformResource) string { | ||
switch resource.Type { | ||
case "aws_iam_role_policy_attachment": | ||
role := fmt.Sprint(resource.AttributeValues["role"]) | ||
policyArn := fmt.Sprint(resource.AttributeValues["policy_arn"]) | ||
return fmt.Sprintf("%s/%s", role, policyArn) | ||
case "aws_lambda_permission": | ||
functionName := fmt.Sprint(resource.AttributeValues["function_name"]) | ||
statementId := fmt.Sprint(resource.AttributeValues["statement_id"]) | ||
return fmt.Sprintf("%s/%s", functionName, statementId) | ||
case "aws_security_group_rule": | ||
return computeResourceIDForAWSSecurityGroupRole(resource) | ||
default: | ||
return fmt.Sprint(resource.AttributeValues["id"]) | ||
} | ||
} | ||
|
||
func computeResourceIDForAWSSecurityGroupRole(resource parser.TerraformResource) string { | ||
// Required Fields | ||
securityGroupId := fmt.Sprint(resource.AttributeValues["security_group_id"]) | ||
securityGroupType := fmt.Sprint(resource.AttributeValues["type"]) | ||
protocol := fmt.Sprint(resource.AttributeValues["protocol"]) | ||
fromPort := fmt.Sprint(resource.AttributeValues["from_port"]) | ||
toPort := fmt.Sprint(resource.AttributeValues["to_port"]) | ||
|
||
// Optional Fields | ||
sourceSecurityGroupId, isSourceSecurityGroupIdValid := resource.AttributeValues["source_security_group_id"].(string) | ||
if isSourceSecurityGroupIdValid { | ||
isSourceSecurityGroupIdValid = len(sourceSecurityGroupId) > 0 | ||
} | ||
cidrBlocks, isCidrBlocksValid := resource.AttributeValues["cidr_blocks"].([]any) | ||
if isCidrBlocksValid { | ||
isCidrBlocksValid = len(cidrBlocks) > 0 | ||
} | ||
resourceID := fmt.Sprintf("%s_%s_%s_%s_%s", securityGroupId, securityGroupType, protocol, fromPort, toPort) | ||
if isSourceSecurityGroupIdValid { | ||
return fmt.Sprintf("%s_%s", resourceID, sourceSecurityGroupId) | ||
} | ||
if isCidrBlocksValid { | ||
var cidrStringBlocks []string | ||
for _, cidrBlock := range cidrBlocks { | ||
cidrStringBlocks = append(cidrStringBlocks, fmt.Sprint(cidrBlock)) | ||
} | ||
return fmt.Sprintf("%s_%s", resourceID, strings.Join(cidrStringBlocks, "_")) | ||
} | ||
return resourceID | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
package tfimportgen | ||
|
||
import ( | ||
"github.com/kishaningithub/tf-import-gen/pkg/internal/parser" | ||
"github.com/stretchr/testify/require" | ||
"testing" | ||
) | ||
|
||
func Test_ComputeTerraformImportForResource(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
terraformResource parser.TerraformResource | ||
expected TerraformImport | ||
}{ | ||
{ | ||
name: "For aws_iam_role_policy_attachment", | ||
terraformResource: parser.TerraformResource{ | ||
Address: "aws_iam_role_policy_attachment.test", | ||
Type: "aws_iam_role_policy_attachment", | ||
AttributeValues: map[string]any{ | ||
"role": "test-role", | ||
"policy_arn": "test-policy-arn", | ||
}, | ||
}, | ||
expected: TerraformImport{ | ||
ResourceAddress: "aws_iam_role_policy_attachment.test", | ||
ResourceID: "test-role/test-policy-arn", | ||
}, | ||
}, | ||
{ | ||
name: "For aws_lambda_permission", | ||
terraformResource: parser.TerraformResource{ | ||
Address: "aws_lambda_permission.test", | ||
Type: "aws_lambda_permission", | ||
AttributeValues: map[string]any{ | ||
"statement_id": "test-statement-id", | ||
"function_name": "test-function-name", | ||
}, | ||
}, | ||
expected: TerraformImport{ | ||
ResourceAddress: "aws_lambda_permission.test", | ||
ResourceID: "test-function-name/test-statement-id", | ||
}, | ||
}, | ||
{ | ||
name: "For aws_security_group_rule with source_security_group_id", | ||
terraformResource: parser.TerraformResource{ | ||
Address: "aws_security_group_rule.test", | ||
Type: "aws_security_group_rule", | ||
AttributeValues: map[string]any{ | ||
"security_group_id": "security-group-id", | ||
"type": "type", | ||
"protocol": "protocol", | ||
"from_port": 1234, | ||
"to_port": 5678, | ||
"source_security_group_id": "source-security-group-id", | ||
}, | ||
}, | ||
expected: TerraformImport{ | ||
ResourceAddress: "aws_security_group_rule.test", | ||
ResourceID: "security-group-id_type_protocol_1234_5678_source-security-group-id", | ||
}, | ||
}, | ||
{ | ||
name: "For aws_security_group_rule with cidr_blocks", | ||
terraformResource: parser.TerraformResource{ | ||
Address: "aws_security_group_rule.test", | ||
Type: "aws_security_group_rule", | ||
AttributeValues: map[string]any{ | ||
"security_group_id": "security-group-id", | ||
"type": "type", | ||
"protocol": "protocol", | ||
"from_port": 1234, | ||
"to_port": 5678, | ||
"cidr_blocks": []any{"cidr-block-1", "cidr-block-2"}, | ||
}, | ||
}, | ||
expected: TerraformImport{ | ||
ResourceAddress: "aws_security_group_rule.test", | ||
ResourceID: "security-group-id_type_protocol_1234_5678_cidr-block-1_cidr-block-2", | ||
}, | ||
}, | ||
{ | ||
name: "For everything else", | ||
terraformResource: parser.TerraformResource{ | ||
Address: "example.address", | ||
Type: "example_type", | ||
AttributeValues: map[string]any{ | ||
"id": "test_id", | ||
}, | ||
}, | ||
expected: TerraformImport{ | ||
ResourceAddress: "example.address", | ||
ResourceID: "test_id", | ||
}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
actual := computeTerraformImportForResource(tt.terraformResource) | ||
require.Equal(t, tt.expected, actual) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package tfimportgen | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
var _ fmt.Stringer = TerraformImport{} | ||
|
||
type TerraformImport struct { | ||
ResourceAddress string | ||
ResourceID string | ||
} | ||
|
||
func (terraformImport TerraformImport) String() string { | ||
importTemplate := `import { | ||
to = %s | ||
id = "%s" | ||
}` | ||
return fmt.Sprintln(fmt.Sprintf(importTemplate, terraformImport.ResourceAddress, terraformImport.ResourceID)) | ||
} | ||
|
||
var _ fmt.Stringer = (TerraformImports)(nil) | ||
|
||
type TerraformImports []TerraformImport | ||
|
||
func (terraformImports TerraformImports) String() string { | ||
var terraformImportsStr strings.Builder | ||
for _, terraformImport := range terraformImports { | ||
terraformImportsStr.WriteString(fmt.Sprintln(terraformImport)) | ||
} | ||
return terraformImportsStr.String() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.