From f94f0ad922ecb8b4e512270c30f9b393b6be9e52 Mon Sep 17 00:00:00 2001 From: Jeremy Udit Date: Wed, 11 Nov 2020 10:26:15 -0500 Subject: [PATCH] Resolve repo:pattern on `github_branch_protection` import --- github/resource_github_branch_protection.go | 23 ++++++++++++++++++- .../resource_github_branch_protection_test.go | 15 ++++++++++++ .../docs/r/branch_protection.html.markdown | 4 ++-- 3 files changed, 39 insertions(+), 3 deletions(-) diff --git a/github/resource_github_branch_protection.go b/github/resource_github_branch_protection.go index feaa23b002..f5b784fe62 100644 --- a/github/resource_github_branch_protection.go +++ b/github/resource_github_branch_protection.go @@ -95,7 +95,7 @@ func resourceGithubBranchProtection() *schema.Resource { Delete: resourceGithubBranchProtectionDelete, Importer: &schema.ResourceImporter{ - State: schema.ImportStatePassthrough, + State: resourceGithubBranchProtectionImport, }, StateUpgraders: []schema.StateUpgrader{ @@ -269,3 +269,24 @@ func resourceGithubBranchProtectionDelete(d *schema.ResourceData, meta interface return err } + +func resourceGithubBranchProtectionImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { + repoName, pattern, err := parseTwoPartID(d.Id(), "repository", "pattern") + if err != nil { + return nil, err + } + + repoID, err := getRepositoryID(repoName, meta) + if err != nil { + return nil, err + } + d.Set("repository_id", repoID) + + id, err := getBranchProtectionID(repoName, pattern, meta) + if err != nil { + return nil, err + } + d.SetId(fmt.Sprintf("%s", id)) + + return []*schema.ResourceData{d}, resourceGithubBranchProtectionRead(d, meta) +} diff --git a/github/resource_github_branch_protection_test.go b/github/resource_github_branch_protection_test.go index 0adb43467a..7309b1f45c 100644 --- a/github/resource_github_branch_protection_test.go +++ b/github/resource_github_branch_protection_test.go @@ -6,6 +6,7 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/helper/acctest" "github.com/hashicorp/terraform-plugin-sdk/helper/resource" + "github.com/hashicorp/terraform-plugin-sdk/terraform" ) func TestAccGithubBranchProtection(t *testing.T) { @@ -57,6 +58,14 @@ func TestAccGithubBranchProtection(t *testing.T) { Config: config, Check: check, }, + { + ResourceName: "github_branch_protection.test", + ImportState: true, + ImportStateVerify: true, + ImportStateIdFunc: branchProtectionImportStateIdFunc( + fmt.Sprintf("tf-acc-test-%s", randomID), "main", + ), + }, }, }) } @@ -253,3 +262,9 @@ func TestAccGithubBranchProtection(t *testing.T) { }) } + +func branchProtectionImportStateIdFunc(repo, pattern string) resource.ImportStateIdFunc { + return func(s *terraform.State) (string, error) { + return fmt.Sprintf("%s:%s", repo, pattern), nil + } +} diff --git a/website/docs/r/branch_protection.html.markdown b/website/docs/r/branch_protection.html.markdown index 2269cba049..c74fac620a 100644 --- a/website/docs/r/branch_protection.html.markdown +++ b/website/docs/r/branch_protection.html.markdown @@ -94,8 +94,8 @@ The following arguments are supported: ## Import -GitHub Branch Protection can be imported using an ID made up of `repository:branch`, e.g. +GitHub Branch Protection can be imported using an ID made up of `repository:pattern`, e.g. ``` -$ terraform import github_branch_protection.terraform terraform:master +$ terraform import github_branch_protection.terraform terraform:main ```