Skip to content

Commit

Permalink
repository_project: fix import and add test
Browse files Browse the repository at this point in the history
  • Loading branch information
shihanng committed Aug 9, 2018
1 parent d3099f1 commit dd35451
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
11 changes: 10 additions & 1 deletion github/resource_github_repository_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"strconv"
"strings"

"github.com/google/go-github/github"
"github.com/hashicorp/terraform/helper/schema"
Expand All @@ -16,7 +17,15 @@ func resourceGithubRepositoryProject() *schema.Resource {
Update: resourceGithubRepositoryProjectUpdate,
Delete: resourceGithubRepositoryProjectDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
State: func(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
parts := strings.Split(d.Id(), "/")
if len(parts) != 2 {
return nil, fmt.Errorf("Invalid ID specified. Supplied ID must be written as <repository>/<project_id>")
}
d.Set("repository", parts[0])
d.SetId(parts[1])
return []*schema.ResourceData{d}, nil
},
},

Schema: map[string]*schema.Schema{
Expand Down
21 changes: 21 additions & 0 deletions github/resource_github_repository_project_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,27 @@ func TestAccGithubRepositoryProject_basic(t *testing.T) {
})
}

func TestAccGithubRepositoryProject_importBasic(t *testing.T) {
randRepoName := acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccGithubRepositoryProjectDestroy,
Steps: []resource.TestStep{
{
Config: testAccGithubRepositoryProjectConfig(randRepoName),
},
{
ResourceName: "github_repository_project.test",
ImportState: true,
ImportStateVerify: true,
ImportStateIdPrefix: randRepoName + `/`,
},
},
})
}

func testAccGithubRepositoryProjectDestroy(s *terraform.State) error {
conn := testAccProvider.Meta().(*Organization).client

Expand Down

0 comments on commit dd35451

Please sign in to comment.