From 233d517e0ab7987abc87796a4a0e03e18431f4b8 Mon Sep 17 00:00:00 2001 From: Elie Date: Tue, 11 May 2021 14:32:33 +0200 Subject: [PATCH] Refacto github resources --- .../result.golden.json | 169 +++++++++--------- .../test/github_membership/result.golden.json | 20 ++- .../test/github_repository/result.golden.json | 126 ++++++------- .../state/test/github_team/result.golden.json | 70 ++++---- .../github_team_membership/result.golden.json | 28 +-- .../github/github_branch_protection.go | 11 +- pkg/resource/github/github_membership.go | 11 +- pkg/resource/github/github_repository.go | 12 +- pkg/resource/github/github_team.go | 14 +- pkg/resource/github/github_team_membership.go | 11 +- pkg/resource/github/metadatas.go | 6 +- pkg/resource/resource.go | 6 + 12 files changed, 275 insertions(+), 209 deletions(-) diff --git a/pkg/iac/terraform/state/test/github_branch_protection/result.golden.json b/pkg/iac/terraform/state/test/github_branch_protection/result.golden.json index 78238d35..ca900e09 100755 --- a/pkg/iac/terraform/state/test/github_branch_protection/result.golden.json +++ b/pkg/iac/terraform/state/test/github_branch_protection/result.golden.json @@ -1,93 +1,98 @@ [ { - "AllowsDeletions": true, - "AllowsForcePushes": true, - "EnforceAdmins": true, "Id": "MDIwOkJyYW5jaFByb3RlY3Rpb25SdWxlMTk1NDg0NzI=", - "Pattern": "main", - "PushRestrictions": [ - "MDQ6VXNlcjYxNTQ5ODc=" - ], - "RepositoryId": "MDEwOlJlcG9zaXRvcnkzNDI1MzIzNzE=", - "RequireSignedCommits": false, - "RequiredPullRequestReviews": [ - { - "DismissStaleReviews": true, - "DismissalRestrictions": [ - "MDQ6VXNlcjYxNTQ5ODc=" - ], - "RequireCodeOwnerReviews": false, - "RequiredApprovingReviewCount": 1 - } - ], - "RequiredStatusChecks": [ - { - "Contexts": [ - "ci/travis" - ], - "Strict": false - } - ], - "CtyVal": {} + "Type": "github_branch_protection", + "Attrs": { + "allows_deletions": true, + "allows_force_pushes": true, + "enforce_admins": true, + "id": "MDIwOkJyYW5jaFByb3RlY3Rpb25SdWxlMTk1NDg0NzI=", + "pattern": "main", + "push_restrictions": [ + "MDQ6VXNlcjYxNTQ5ODc=" + ], + "require_signed_commits": false, + "required_pull_request_reviews": [ + { + "dismiss_stale_reviews": true, + "dismissal_restrictions": [ + "MDQ6VXNlcjYxNTQ5ODc=" + ], + "require_code_owner_reviews": false, + "required_approving_review_count": 1 + } + ], + "required_status_checks": [ + { + "contexts": [ + "ci/travis" + ], + "strict": false + } + ] + } }, { - "AllowsDeletions": true, - "AllowsForcePushes": true, - "EnforceAdmins": true, "Id": "MDIwOkJyYW5jaFByb3RlY3Rpb25SdWxlMTk1NDg0NzQ=", - "Pattern": "main", - "PushRestrictions": [ - "MDQ6VXNlcjYxNTQ5ODc=" - ], - "RepositoryId": "MDEwOlJlcG9zaXRvcnkzNDI1MzIzODI=", - "RequireSignedCommits": false, - "RequiredPullRequestReviews": [ - { - "DismissStaleReviews": true, - "DismissalRestrictions": [ - "MDQ6VXNlcjYxNTQ5ODc=" - ], - "RequireCodeOwnerReviews": false, - "RequiredApprovingReviewCount": 1 - } - ], - "RequiredStatusChecks": [ - { - "Contexts": [ - "ci/travis" - ], - "Strict": false - } - ], - "CtyVal": {} + "Type": "github_branch_protection", + "Attrs": { + "allows_deletions": true, + "allows_force_pushes": true, + "enforce_admins": true, + "id": "MDIwOkJyYW5jaFByb3RlY3Rpb25SdWxlMTk1NDg0NzQ=", + "pattern": "main", + "push_restrictions": [ + "MDQ6VXNlcjYxNTQ5ODc=" + ], + "require_signed_commits": false, + "required_pull_request_reviews": [ + { + "dismiss_stale_reviews": true, + "dismissal_restrictions": [ + "MDQ6VXNlcjYxNTQ5ODc=" + ], + "require_code_owner_reviews": false, + "required_approving_review_count": 1 + } + ], + "required_status_checks": [ + { + "contexts": [ + "ci/travis" + ], + "strict": false + } + ] + } }, { - "AllowsDeletions": true, - "AllowsForcePushes": true, - "EnforceAdmins": true, "Id": "MDIwOkJyYW5jaFByb3RlY3Rpb25SdWxlMTk1NDg0NzE=", - "Pattern": "main", - "PushRestrictions": null, - "RepositoryId": "MDEwOlJlcG9zaXRvcnkzNDI1MzIzOTE=", - "RequireSignedCommits": false, - "RequiredPullRequestReviews": [ - { - "DismissStaleReviews": true, - "DismissalRestrictions": [ - "MDQ6VXNlcjYxNTQ5ODc=" - ], - "RequireCodeOwnerReviews": false, - "RequiredApprovingReviewCount": 1 - } - ], - "RequiredStatusChecks": [ - { - "Contexts": [ - "ci/travis" - ], - "Strict": false - } - ], - "CtyVal": {} + "Type": "github_branch_protection", + "Attrs": { + "allows_deletions": true, + "allows_force_pushes": true, + "enforce_admins": true, + "id": "MDIwOkJyYW5jaFByb3RlY3Rpb25SdWxlMTk1NDg0NzE=", + "pattern": "main", + "require_signed_commits": false, + "required_pull_request_reviews": [ + { + "dismiss_stale_reviews": true, + "dismissal_restrictions": [ + "MDQ6VXNlcjYxNTQ5ODc=" + ], + "require_code_owner_reviews": false, + "required_approving_review_count": 1 + } + ], + "required_status_checks": [ + { + "contexts": [ + "ci/travis" + ], + "strict": false + } + ] + } } ] \ No newline at end of file diff --git a/pkg/iac/terraform/state/test/github_membership/result.golden.json b/pkg/iac/terraform/state/test/github_membership/result.golden.json index 2069cb62..6685c26e 100755 --- a/pkg/iac/terraform/state/test/github_membership/result.golden.json +++ b/pkg/iac/terraform/state/test/github_membership/result.golden.json @@ -1,16 +1,20 @@ [ { - "Etag": "W/\"2d1cae5b1a58be39bd09bad91fb225b3b589ddc47ff101cf4b8a86d3d8b6f65d\"", "Id": "driftctl-test:driftctl-acceptance-tester", - "Role": "admin", - "Username": "driftctl-acceptance-tester", - "CtyVal": {} + "Type": "github_membership", + "Attrs": { + "id": "driftctl-test:driftctl-acceptance-tester", + "role": "admin", + "username": "driftctl-acceptance-tester" + } }, { - "Etag": "W/\"d2a4eb901334f41629f5ead98a4b6e02489ae2f13b2152630f00e4dfa6488586\"", "Id": "driftctl-test:eliecharra", - "Role": "admin", - "Username": "eliecharra", - "CtyVal": {} + "Type": "github_membership", + "Attrs": { + "id": "driftctl-test:eliecharra", + "role": "admin", + "username": "eliecharra" + } } ] \ No newline at end of file diff --git a/pkg/iac/terraform/state/test/github_repository/result.golden.json b/pkg/iac/terraform/state/test/github_repository/result.golden.json index 85536acd..6ff62edf 100755 --- a/pkg/iac/terraform/state/test/github_repository/result.golden.json +++ b/pkg/iac/terraform/state/test/github_repository/result.golden.json @@ -1,76 +1,66 @@ [ { - "AllowMergeCommit": true, - "AllowRebaseMerge": true, - "AllowSquashMerge": true, - "ArchiveOnDestroy": null, - "Archived": false, - "AutoInit": null, - "DefaultBranch": "main", - "DeleteBranchOnMerge": false, - "Description": "this is a private repo", - "Etag": "W/\"04420b05933b55f136bb12b9c8d1748e67e143b290d72ecadd0fd2d4f4a3048a\"", - "FullName": "driftctl-test/private-repo", - "GitCloneUrl": "git://github.com/driftctl-test/private-repo.git", - "GitignoreTemplate": null, - "HasDownloads": false, - "HasIssues": false, - "HasProjects": false, - "HasWiki": false, - "HomepageUrl": "", - "HtmlUrl": "https://github.com/driftctl-test/private-repo", - "HttpCloneUrl": "https://github.com/driftctl-test/private-repo.git", "Id": "private-repo", - "IsTemplate": false, - "LicenseTemplate": null, - "Name": "private-repo", - "NodeId": "MDEwOlJlcG9zaXRvcnkzMzkwNzY5NjQ=", - "Private": true, - "RepoId": 339076964, - "SshCloneUrl": "git@github.com:driftctl-test/private-repo.git", - "SvnUrl": "https://github.com/driftctl-test/private-repo", - "Topics": null, - "Visibility": "private", - "VulnerabilityAlerts": false, - "Pages": [], - "Template": [], - "CtyVal": {} + "Type": "github_repository", + "Attrs": { + "allow_merge_commit": true, + "allow_rebase_merge": true, + "allow_squash_merge": true, + "archived": false, + "default_branch": "main", + "delete_branch_on_merge": false, + "description": "this is a private repo", + "full_name": "driftctl-test/private-repo", + "git_clone_url": "git://github.com/driftctl-test/private-repo.git", + "has_downloads": false, + "has_issues": false, + "has_projects": false, + "has_wiki": false, + "homepage_url": "", + "html_url": "https://github.com/driftctl-test/private-repo", + "http_clone_url": "https://github.com/driftctl-test/private-repo.git", + "id": "private-repo", + "is_template": false, + "name": "private-repo", + "node_id": "MDEwOlJlcG9zaXRvcnkzMzkwNzY5NjQ=", + "private": true, + "repo_id": 339076964, + "ssh_clone_url": "git@github.com:driftctl-test/private-repo.git", + "svn_url": "https://github.com/driftctl-test/private-repo", + "visibility": "private", + "vulnerability_alerts": false + } }, { - "AllowMergeCommit": true, - "AllowRebaseMerge": true, - "AllowSquashMerge": true, - "ArchiveOnDestroy": null, - "Archived": false, - "AutoInit": null, - "DefaultBranch": "main", - "DeleteBranchOnMerge": false, - "Description": "", - "Etag": "W/\"3e7a2583fb97097c8acbcb4c289e46fc0db70341071c5f55972bbd3270a2b957\"", - "FullName": "driftctl-test/public-repo", - "GitCloneUrl": "git://github.com/driftctl-test/public-repo.git", - "GitignoreTemplate": null, - "HasDownloads": false, - "HasIssues": false, - "HasProjects": false, - "HasWiki": false, - "HomepageUrl": "", - "HtmlUrl": "https://github.com/driftctl-test/public-repo", - "HttpCloneUrl": "https://github.com/driftctl-test/public-repo.git", "Id": "public-repo", - "IsTemplate": false, - "LicenseTemplate": null, - "Name": "public-repo", - "NodeId": "MDEwOlJlcG9zaXRvcnkzMzkwNzY5Nzg=", - "Private": false, - "RepoId": 339076978, - "SshCloneUrl": "git@github.com:driftctl-test/public-repo.git", - "SvnUrl": "https://github.com/driftctl-test/public-repo", - "Topics": null, - "Visibility": "public", - "VulnerabilityAlerts": false, - "Pages": [], - "Template": [], - "CtyVal": {} + "Type": "github_repository", + "Attrs": { + "allow_merge_commit": true, + "allow_rebase_merge": true, + "allow_squash_merge": true, + "archived": false, + "default_branch": "main", + "delete_branch_on_merge": false, + "description": "", + "full_name": "driftctl-test/public-repo", + "git_clone_url": "git://github.com/driftctl-test/public-repo.git", + "has_downloads": false, + "has_issues": false, + "has_projects": false, + "has_wiki": false, + "homepage_url": "", + "html_url": "https://github.com/driftctl-test/public-repo", + "http_clone_url": "https://github.com/driftctl-test/public-repo.git", + "id": "public-repo", + "is_template": false, + "name": "public-repo", + "node_id": "MDEwOlJlcG9zaXRvcnkzMzkwNzY5Nzg=", + "private": false, + "repo_id": 339076978, + "ssh_clone_url": "git@github.com:driftctl-test/public-repo.git", + "svn_url": "https://github.com/driftctl-test/public-repo", + "visibility": "public", + "vulnerability_alerts": false + } } ] \ No newline at end of file diff --git a/pkg/iac/terraform/state/test/github_team/result.golden.json b/pkg/iac/terraform/state/test/github_team/result.golden.json index 740f95f2..94bc7e94 100755 --- a/pkg/iac/terraform/state/test/github_team/result.golden.json +++ b/pkg/iac/terraform/state/test/github_team/result.golden.json @@ -1,44 +1,48 @@ [ { - "CreateDefaultMaintainer": false, - "Description": "test", - "Etag": "W/\"04b608322c60381373485f1154b7670f1daeb6bdfa9062f7eba05739171fcac0\"", "Id": "4556715", - "LdapDn": "", - "MembersCount": 1, - "Name": "team1", - "NodeId": "MDQ6VGVhbTQ1NTY3MTU=", - "ParentTeamId": null, - "Privacy": "closed", - "Slug": "team1", - "CtyVal": {} + "Type": "github_team", + "Attrs": { + "create_default_maintainer": false, + "description": "test", + "id": "4556715", + "ldap_dn": "", + "members_count": 1, + "name": "team1", + "node_id": "MDQ6VGVhbTQ1NTY3MTU=", + "privacy": "closed", + "slug": "team1" + } }, { - "CreateDefaultMaintainer": false, - "Description": "test 2", - "Etag": "W/\"1af373c3f173859e7f06690e8e353c21a9a1a90224963e8f162546f1022af224\"", "Id": "4556719", - "LdapDn": "", - "MembersCount": 1, - "Name": "team2", - "NodeId": "MDQ6VGVhbTQ1NTY3MTk=", - "ParentTeamId": null, - "Privacy": "secret", - "Slug": "team2", - "CtyVal": {} + "Type": "github_team", + "Attrs": { + "create_default_maintainer": false, + "description": "test 2", + "id": "4556719", + "ldap_dn": "", + "members_count": 1, + "name": "team2", + "node_id": "MDQ6VGVhbTQ1NTY3MTk=", + "privacy": "secret", + "slug": "team2" + } }, { - "CreateDefaultMaintainer": false, - "Description": "test parent team", - "Etag": "W/\"d6fded1b23237d988a0914455547a2e66789bb625fc0a519babe993429facd37\"", "Id": "4556747", - "LdapDn": "", - "MembersCount": 1, - "Name": "new team with parent", - "NodeId": "MDQ6VGVhbTQ1NTY3NDc=", - "ParentTeamId": 4556715, - "Privacy": "closed", - "Slug": "new-team-with-parent", - "CtyVal": {} + "Type": "github_team", + "Attrs": { + "create_default_maintainer": false, + "description": "test parent team", + "id": "4556747", + "ldap_dn": "", + "members_count": 1, + "name": "new team with parent", + "node_id": "MDQ6VGVhbTQ1NTY3NDc=", + "parent_team_id": 4556715, + "privacy": "closed", + "slug": "new-team-with-parent" + } } ] \ No newline at end of file diff --git a/pkg/iac/terraform/state/test/github_team_membership/result.golden.json b/pkg/iac/terraform/state/test/github_team_membership/result.golden.json index dba52ae5..24e8998a 100755 --- a/pkg/iac/terraform/state/test/github_team_membership/result.golden.json +++ b/pkg/iac/terraform/state/test/github_team_membership/result.golden.json @@ -1,18 +1,22 @@ [ { - "Etag": "W/\"7d708fe20649b975d112449eb87681183b03ec1eee387e9cad2559fcc6b1fb3b\"", - "Id": "4564046:wbeuil", - "Role": "member", - "TeamId": "4564046", - "Username": "wbeuil", - "CtyVal": {} + "Id": "4564046:driftctl-acceptance-tester", + "Type": "github_team_membership", + "Attrs": { + "id": "4564046:driftctl-acceptance-tester", + "role": "maintainer", + "team_id": "4564046", + "username": "driftctl-acceptance-tester" + } }, { - "Etag": "W/\"1ecb16111126288558af7851ad1fb6eb99a57393235bd0600bf39c85dcd98563\"", - "Id": "4564046:driftctl-acceptance-tester", - "Role": "maintainer", - "TeamId": "4564046", - "Username": "driftctl-acceptance-tester", - "CtyVal": {} + "Id": "4564046:wbeuil", + "Type": "github_team_membership", + "Attrs": { + "id": "4564046:wbeuil", + "role": "member", + "team_id": "4564046", + "username": "wbeuil" + } } ] \ No newline at end of file diff --git a/pkg/resource/github/github_branch_protection.go b/pkg/resource/github/github_branch_protection.go index 8e73a5af..e74483b1 100644 --- a/pkg/resource/github/github_branch_protection.go +++ b/pkg/resource/github/github_branch_protection.go @@ -1,7 +1,10 @@ // GENERATED, DO NOT EDIT THIS FILE package github -import "github.com/zclconf/go-cty/cty" +import ( + "github.com/cloudskiff/driftctl/pkg/resource" + "github.com/zclconf/go-cty/cty" +) const GithubBranchProtectionResourceType = "github_branch_protection" @@ -38,3 +41,9 @@ func (r *GithubBranchProtection) TerraformType() string { func (r *GithubBranchProtection) CtyValue() *cty.Value { return r.CtyVal } + +func initGithubBranchProtectionMetaData(resourceSchemaRepository resource.SchemaRepositoryInterface) { + resourceSchemaRepository.SetNormalizeFunc(GithubBranchProtectionResourceType, func(val *resource.Attributes) { + val.SafeDelete([]string{"repository_id"}) // Terraform provider is always returning nil + }) +} diff --git a/pkg/resource/github/github_membership.go b/pkg/resource/github/github_membership.go index 134ec274..ea6a4902 100644 --- a/pkg/resource/github/github_membership.go +++ b/pkg/resource/github/github_membership.go @@ -1,7 +1,10 @@ // GENERATED, DO NOT EDIT THIS FILE package github -import "github.com/zclconf/go-cty/cty" +import ( + "github.com/cloudskiff/driftctl/pkg/resource" + "github.com/zclconf/go-cty/cty" +) const GithubMembershipResourceType = "github_membership" @@ -24,3 +27,9 @@ func (r *GithubMembership) TerraformType() string { func (r *GithubMembership) CtyValue() *cty.Value { return r.CtyVal } + +func initGithubMembershipMetaData(resourceSchemaRepository resource.SchemaRepositoryInterface) { + resourceSchemaRepository.SetNormalizeFunc(GithubMembershipResourceType, func(val *resource.Attributes) { + val.SafeDelete([]string{"etag"}) + }) +} diff --git a/pkg/resource/github/github_repository.go b/pkg/resource/github/github_repository.go index 42a8d43d..b3bd53a2 100644 --- a/pkg/resource/github/github_repository.go +++ b/pkg/resource/github/github_repository.go @@ -1,6 +1,9 @@ package github -import "github.com/zclconf/go-cty/cty" +import ( + "github.com/cloudskiff/driftctl/pkg/resource" + "github.com/zclconf/go-cty/cty" +) const GithubRepositoryResourceType = "github_repository" @@ -66,3 +69,10 @@ func (r *GithubRepository) TerraformType() string { func (r *GithubRepository) CtyValue() *cty.Value { return r.CtyVal } + +func initGithubRepositoryMetaData(resourceSchemaRepository resource.SchemaRepositoryInterface) { + resourceSchemaRepository.SetNormalizeFunc(GithubRepositoryResourceType, func(val *resource.Attributes) { + val.SafeDelete([]string{"auto_init"}) + val.SafeDelete([]string{"etag"}) + }) +} diff --git a/pkg/resource/github/github_team.go b/pkg/resource/github/github_team.go index 3dae1c39..84429bf8 100644 --- a/pkg/resource/github/github_team.go +++ b/pkg/resource/github/github_team.go @@ -1,7 +1,10 @@ // GENERATED, DO NOT EDIT THIS FILE package github -import "github.com/zclconf/go-cty/cty" +import ( + "github.com/cloudskiff/driftctl/pkg/resource" + "github.com/zclconf/go-cty/cty" +) const GithubTeamResourceType = "github_team" @@ -31,3 +34,12 @@ func (r *GithubTeam) TerraformType() string { func (r *GithubTeam) CtyValue() *cty.Value { return r.CtyVal } + +func initGithubTeamMetaData(resourceSchemaRepository resource.SchemaRepositoryInterface) { + resourceSchemaRepository.SetNormalizeFunc(GithubTeamResourceType, func(val *resource.Attributes) { + if defaultMaintainer, exist := val.Get("create_default_maintainer"); !exist || defaultMaintainer == nil { + (*val)["create_default_maintainer"] = false + } + val.SafeDelete([]string{"etag"}) + }) +} diff --git a/pkg/resource/github/github_team_membership.go b/pkg/resource/github/github_team_membership.go index 8257ec8b..d314eb2a 100644 --- a/pkg/resource/github/github_team_membership.go +++ b/pkg/resource/github/github_team_membership.go @@ -1,7 +1,10 @@ // GENERATED, DO NOT EDIT THIS FILE package github -import "github.com/zclconf/go-cty/cty" +import ( + "github.com/cloudskiff/driftctl/pkg/resource" + "github.com/zclconf/go-cty/cty" +) const GithubTeamMembershipResourceType = "github_team_membership" @@ -25,3 +28,9 @@ func (r *GithubTeamMembership) TerraformType() string { func (r *GithubTeamMembership) CtyValue() *cty.Value { return r.CtyVal } + +func initGithubTeamMembershipMetaData(resourceSchemaRepository resource.SchemaRepositoryInterface) { + resourceSchemaRepository.SetNormalizeFunc(GithubTeamMembershipResourceType, func(val *resource.Attributes) { + val.SafeDelete([]string{"etag"}) + }) +} diff --git a/pkg/resource/github/metadatas.go b/pkg/resource/github/metadatas.go index eb9d6075..49ae0455 100644 --- a/pkg/resource/github/metadatas.go +++ b/pkg/resource/github/metadatas.go @@ -3,5 +3,9 @@ package github import "github.com/cloudskiff/driftctl/pkg/resource" func InitResourcesMetadata(resourceSchemaRepository resource.SchemaRepositoryInterface) { - + initGithubBranchProtectionMetaData(resourceSchemaRepository) + initGithubMembershipMetaData(resourceSchemaRepository) + initGithubRepositoryMetaData(resourceSchemaRepository) + initGithubTeamMetaData(resourceSchemaRepository) + initGithubTeamMembershipMetaData(resourceSchemaRepository) } diff --git a/pkg/resource/resource.go b/pkg/resource/resource.go index a4e5fc2a..cef2e77c 100644 --- a/pkg/resource/resource.go +++ b/pkg/resource/resource.go @@ -71,6 +71,12 @@ var refactoredResources = []string{ // "aws_sqs_queue_policy", // "aws_subnet", // "aws_vpc", + + "github_branch_protection", + "github_membership", + "github_repository", + "github_team", + "github_team_membership", } func IsRefactoredResource(typ string) bool {