Skip to content

Commit

Permalink
Merge pull request #35 from kishaningithub/add-resources
Browse files Browse the repository at this point in the history
Add support for more resources and upgrade go version
  • Loading branch information
kishaningithub authored Oct 2, 2024
2 parents 007788d + d0e022c commit 9c466e1
Show file tree
Hide file tree
Showing 7 changed files with 176 additions and 40 deletions.
15 changes: 2 additions & 13 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,5 @@ jobs:
with:
go-version-file: "go.mod"

- name: Test
run: make build

- name: Lint
uses: golangci/golangci-lint-action@v6
with:
version: latest

- name: Check go releaser config
uses: goreleaser/goreleaser-action@v5
with:
version: latest
args: check
- name: Build
run: make build
2 changes: 2 additions & 0 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
version: 2

project_name: tf-import-gen

before:
Expand Down
10 changes: 9 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
# Binary dependencies
golangci-lint := go run github.com/golangci/golangci-lint/cmd/golangci-lint@latest
goreleaser := go run github.com/goreleaser/goreleaser/v2@latest

test:
go test -race -v ./...

build: download-deps tidy-deps compile test
build: download-deps tidy-deps compile test lint

fmt:
gofmt -l -s -w .
Expand All @@ -18,3 +22,7 @@ update-deps:

compile:
go build -v ./...

lint:
$(golangci-lint) run
$(goreleaser) check
8 changes: 4 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
module github.com/kishaningithub/tf-import-gen

go 1.22
go 1.23

require (
github.com/hashicorp/terraform-json v0.22.1
github.com/spf13/cobra v1.8.0
github.com/spf13/cobra v1.8.1
github.com/stretchr/testify v1.9.0
)

Expand All @@ -15,7 +15,7 @@ require (
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/zclconf/go-cty v1.14.4 // indirect
golang.org/x/text v0.15.0 // indirect
github.com/zclconf/go-cty v1.15.0 // indirect
golang.org/x/text v0.18.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
14 changes: 7 additions & 7 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
github.com/apparentlymart/go-textseg/v15 v15.0.0 h1:uYvfpb3DyLSCGWnctWKGj857c6ew1u1fNQOlOtuGxQY=
github.com/apparentlymart/go-textseg/v15 v15.0.0/go.mod h1:K8XmNZdhEBkdlyDdvbmmsvpAG721bKi0joRfFdHIWJ4=
github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
Expand All @@ -14,16 +14,16 @@ github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLf
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/spf13/cobra v1.8.0 h1:7aJaZx1B85qltLMc546zn58BxxfZdR/W22ej9CFoEf0=
github.com/spf13/cobra v1.8.0/go.mod h1:WXLWApfZ71AjXPya3WOlMsY9yMs7YeiHhFVlvLyhcho=
github.com/spf13/cobra v1.8.1 h1:e5/vxKd/rZsfSJMUX1agtjeTDf+qv1/JdBF8gg5k9ZM=
github.com/spf13/cobra v1.8.1/go.mod h1:wHxEcudfqmLYa8iTfL+OuZPbBZkmvliBWKIezN3kD9Y=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/zclconf/go-cty v1.14.4 h1:uXXczd9QDGsgu0i/QFR/hzI5NYCHLf6NQw/atrbnhq8=
github.com/zclconf/go-cty v1.14.4/go.mod h1:VvMs5i0vgZdhYawQNq5kePSpLAoz8u1xvZgrPIxfnZE=
golang.org/x/text v0.15.0 h1:h1V/4gjBv8v9cjcR6+AR5+/cIYK5N/WAgiv4xlsEtAk=
golang.org/x/text v0.15.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
github.com/zclconf/go-cty v1.15.0 h1:tTCRWxsexYUmtt/wVxgDClUe+uQusuI443uL6e+5sXQ=
github.com/zclconf/go-cty v1.15.0/go.mod h1:VvMs5i0vgZdhYawQNq5kePSpLAoz8u1xvZgrPIxfnZE=
golang.org/x/text v0.18.0 h1:XvMDiNzPAl0jr17s6W9lcaIhGUfUORdGCNsuLmPG224=
golang.org/x/text v0.18.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
Expand Down
53 changes: 38 additions & 15 deletions pkg/convertor.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ func computeTerraformImportForResource(resource parser.TerraformResource) Terraf
"aws_lakeformation_data_lake_settings",
"aws_lakeformation_permissions",
"aws_iam_policy_attachment",
"aws_acm_certificate_validation",
"aws_ami_copy",
}
if slices.Contains(resourcesWhichDoNotSupportImport, resource.Type) {
return TerraformImport{
Expand All @@ -30,43 +32,64 @@ func computeTerraformImportForResource(resource parser.TerraformResource) Terraf
}

func computeResourceID(resource parser.TerraformResource) string {
getValue := func(name string) string {
v := func(name string) string {
return fmt.Sprint(resource.AttributeValues[name])
}
switch resource.Type {
case "aws_iam_role_policy_attachment":
return fmt.Sprintf("%s/%s", getValue("role"), getValue("policy_arn"))
return fmt.Sprintf("%s/%s", v("role"), v("policy_arn"))
case "aws_cloudwatch_event_target":
return fmt.Sprintf("%s/%s", getValue("rule"), getValue("target_id"))
return fmt.Sprintf("%s/%s", v("rule"), v("target_id"))
case "aws_lambda_permission":
return fmt.Sprintf("%s/%s", getValue("function_name"), getValue("statement_id"))
return fmt.Sprintf("%s/%s", v("function_name"), v("statement_id"))
case "aws_security_group_rule":
return computeResourceIDForAWSSecurityGroupRole(resource)
case "aws_api_gateway_resource", "aws_api_gateway_deployment":
return fmt.Sprintf("%s/%s", getValue("rest_api_id"), getValue("id"))
return fmt.Sprintf("%s/%s", v("rest_api_id"), v("id"))
case "aws_api_gateway_stage":
return fmt.Sprintf("%s/%s", getValue("rest_api_id"), getValue("stage_name"))
return fmt.Sprintf("%s/%s", v("rest_api_id"), v("stage_name"))
case "aws_api_gateway_method_settings":
return fmt.Sprintf("%s/%s/%s", getValue("rest_api_id"), getValue("stage_name"), getValue("method_path"))
return fmt.Sprintf("%s/%s/%s", v("rest_api_id"), v("stage_name"), v("method_path"))
case "aws_api_gateway_method", "aws_api_gateway_integration":
return fmt.Sprintf("%s/%s/%s", getValue("rest_api_id"), getValue("resource_id"), getValue("http_method"))
return fmt.Sprintf("%s/%s/%s", v("rest_api_id"), v("resource_id"), v("http_method"))
case "aws_route_table_association":
return fmt.Sprintf("%s/%s", getValue("subnet_id"), getValue("route_table_id"))
return fmt.Sprintf("%s/%s", v("subnet_id"), v("route_table_id"))
case "aws_iam_user_policy_attachment":
return fmt.Sprintf("%s/%s", getValue("user"), getValue("policy_arn"))
return fmt.Sprintf("%s/%s", v("user"), v("policy_arn"))
case "aws_emr_instance_group":
return fmt.Sprintf("%s/%s", getValue("cluster_id"), getValue("id"))
return fmt.Sprintf("%s/%s", v("cluster_id"), v("id"))
case "aws_backup_selection":
return fmt.Sprintf("%s|%s", getValue("plan_id"), getValue("id"))
return fmt.Sprintf("%s|%s", v("plan_id"), v("id"))
case "aws_vpc_endpoint_route_table_association":
return fmt.Sprintf("%s/%s", getValue("vpc_endpoint_id"), getValue("route_table_id"))
return fmt.Sprintf("%s/%s", v("vpc_endpoint_id"), v("route_table_id"))
case "aws_cognito_user_pool_client":
return fmt.Sprintf("%s/%s", getValue("user_pool_id"), getValue("id"))
return fmt.Sprintf("%s/%s", v("user_pool_id"), v("id"))
case "aws_ecs_cluster":
return v("name")
case "aws_ecs_task_definition":
return v("arn")
case "aws_wafv2_web_acl":
return fmt.Sprintf("%s/%s/%s", v("id"), v("name"), v("scope"))
case "aws_autoscaling_schedule":
return fmt.Sprintf("%s/%s", v("autoscaling_group_name"), v("scheduled_action_name"))
case "aws_appautoscaling_target":
return fmt.Sprintf("%s/%s/%s", v("service_namespace"), v("resource_id"), v("scalable_dimension"))
case "aws_appautoscaling_policy":
return fmt.Sprintf("%s/%s/%s/%s", v("service_namespace"), v("resource_id"), v("scalable_dimension"), v("name"))
case "aws_ecs_service":
return fmt.Sprintf("%s/%s", getEcsClusterNameFromARN(v("cluster")), v("name"))
default:
return getValue("id")
return v("id")
}
}

func getEcsClusterNameFromARN(arn string) string {
if parts := strings.Split(arn, "/"); len(parts) == 2 {
return parts[1]
}
return ""
}

func computeResourceIDForAWSSecurityGroupRole(resource parser.TerraformResource) string {
// Required Fields
securityGroupId := fmt.Sprint(resource.AttributeValues["security_group_id"])
Expand Down
114 changes: 114 additions & 0 deletions pkg/convertor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,120 @@ func Test_ComputeTerraformImportForResource(t *testing.T) {
SupportsImport: true,
},
},
{
name: "For aws_ecs_cluster",
terraformResource: parser.TerraformResource{
Address: "aws_ecs_cluster.test",
Type: "aws_ecs_cluster",
AttributeValues: map[string]any{
"name": "name",
},
},
expected: TerraformImport{
ResourceAddress: "aws_ecs_cluster.test",
ResourceID: "name",
SupportsImport: true,
},
},
{
name: "For aws_ecs_task_definition",
terraformResource: parser.TerraformResource{
Address: "aws_ecs_task_definition.test",
Type: "aws_ecs_task_definition",
AttributeValues: map[string]any{
"arn": "arn",
},
},
expected: TerraformImport{
ResourceAddress: "aws_ecs_task_definition.test",
ResourceID: "arn",
SupportsImport: true,
},
},
{
name: "For aws_wafv2_web_acl",
terraformResource: parser.TerraformResource{
Address: "aws_wafv2_web_acl.test",
Type: "aws_wafv2_web_acl",
AttributeValues: map[string]any{
"id": "id",
"name": "name",
"scope": "scope",
},
},
expected: TerraformImport{
ResourceAddress: "aws_wafv2_web_acl.test",
ResourceID: "id/name/scope",
SupportsImport: true,
},
},
{
name: "For aws_autoscaling_schedule",
terraformResource: parser.TerraformResource{
Address: "aws_autoscaling_schedule.test",
Type: "aws_autoscaling_schedule",
AttributeValues: map[string]any{
"autoscaling_group_name": "autoscaling_group_name",
"scheduled_action_name": "scheduled_action_name",
},
},
expected: TerraformImport{
ResourceAddress: "aws_autoscaling_schedule.test",
ResourceID: "autoscaling_group_name/scheduled_action_name",
SupportsImport: true,
},
},
{
name: "For aws_appautoscaling_target",
terraformResource: parser.TerraformResource{
Address: "aws_appautoscaling_target.test",
Type: "aws_appautoscaling_target",
AttributeValues: map[string]any{
"service_namespace": "service_namespace",
"resource_id": "resource_id",
"scalable_dimension": "scalable_dimension",
},
},
expected: TerraformImport{
ResourceAddress: "aws_appautoscaling_target.test",
ResourceID: "service_namespace/resource_id/scalable_dimension",
SupportsImport: true,
},
},
{
name: "For aws_appautoscaling_policy",
terraformResource: parser.TerraformResource{
Address: "aws_appautoscaling_policy.test",
Type: "aws_appautoscaling_policy",
AttributeValues: map[string]any{
"service_namespace": "service_namespace",
"resource_id": "resource_id",
"scalable_dimension": "scalable_dimension",
"name": "policy_name",
},
},
expected: TerraformImport{
ResourceAddress: "aws_appautoscaling_policy.test",
ResourceID: "service_namespace/resource_id/scalable_dimension/policy_name",
SupportsImport: true,
},
},
{
name: "For aws_ecs_service",
terraformResource: parser.TerraformResource{
Address: "aws_ecs_service.test",
Type: "aws_ecs_service",
AttributeValues: map[string]any{
"name": "service_name",
"cluster": "arn:aws:ecs:us-west-2:0123456789:cluster/cluster_name",
},
},
expected: TerraformImport{
ResourceAddress: "aws_ecs_service.test",
ResourceID: "cluster_name/service_name",
SupportsImport: true,
},
},
{
name: "For everything else",
terraformResource: parser.TerraformResource{
Expand Down

0 comments on commit 9c466e1

Please sign in to comment.