diff --git a/builtin/providers/github/provider_test.go b/builtin/providers/github/provider_test.go index 07b2a79d6..b227d5271 100644 --- a/builtin/providers/github/provider_test.go +++ b/builtin/providers/github/provider_test.go @@ -10,6 +10,9 @@ import ( const testRepo string = "test-repo" +var testUser string = os.Getenv("GITHUB_TEST_USER") +var testCollaborator string = os.Getenv("GITHUB_TEST_COLLABORATOR") + var testAccProviders map[string]terraform.ResourceProvider var testAccProvider *schema.Provider diff --git a/builtin/providers/github/resource_github_issue_label.go b/builtin/providers/github/resource_github_issue_label.go index f519182b9..f0b5e2b8d 100644 --- a/builtin/providers/github/resource_github_issue_label.go +++ b/builtin/providers/github/resource_github_issue_label.go @@ -11,6 +11,9 @@ func resourceGithubIssueLabel() *schema.Resource { Read: resourceGithubIssueLabelRead, Update: resourceGithubIssueLabelUpdate, Delete: resourceGithubIssueLabelDelete, + Importer: &schema.ResourceImporter{ + State: schema.ImportStatePassthrough, + }, Schema: map[string]*schema.Schema{ "repository": &schema.Schema{ @@ -55,8 +58,7 @@ func resourceGithubIssueLabelCreate(d *schema.ResourceData, meta interface{}) er func resourceGithubIssueLabelRead(d *schema.ResourceData, meta interface{}) error { client := meta.(*Organization).client - r := d.Get("repository").(string) - n := d.Get("name").(string) + r, n := parseTwoPartID(d.Id()) githubLabel, _, err := client.Issues.GetLabel(meta.(*Organization).name, r, n) if err != nil { @@ -64,6 +66,8 @@ func resourceGithubIssueLabelRead(d *schema.ResourceData, meta interface{}) erro return nil } + d.Set("repository", r) + d.Set("name", n) d.Set("color", githubLabel.Color) d.Set("url", githubLabel.URL) diff --git a/builtin/providers/github/resource_github_issue_label_test.go b/builtin/providers/github/resource_github_issue_label_test.go index f3882767c..f279bc00a 100644 --- a/builtin/providers/github/resource_github_issue_label_test.go +++ b/builtin/providers/github/resource_github_issue_label_test.go @@ -35,6 +35,24 @@ func TestAccGithubIssueLabel_basic(t *testing.T) { }) } +func TestAccGithubIssueLabel_importBasic(t *testing.T) { + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccGithubIssueLabelDestroy, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testAccGithubIssueLabelConfig, + }, + resource.TestStep{ + ResourceName: "github_issue_label.test", + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} + func testAccCheckGithubIssueLabelExists(n string, label *github.Label) resource.TestCheckFunc { return func(s *terraform.State) error { rs, ok := s.RootModule().Resources[n] @@ -101,42 +119,16 @@ func testAccGithubIssueLabelDestroy(s *terraform.State) error { } var testAccGithubIssueLabelConfig string = fmt.Sprintf(` -resource "github_repository" "foo" { - name = "%s" - description = "Terraform acceptance tests!" - homepage_url = "http://example.com/" - - # So that acceptance tests can be run in a github organization - # with no billing - private = false - - has_issues = false - has_wiki = false - has_downloads = false -} resource "github_issue_label" "test" { - repository = "${github_repository.foo.name}" + repository = "%s" name = "foo" color = "000000" } `, testRepo) var testAccGithubIssueLabelUpdateConfig string = fmt.Sprintf(` -resource "github_repository" "foo" { - name = "%s" - description = "Terraform acceptance tests!" - homepage_url = "http://example.com/" - - # So that acceptance tests can be run in a github organization - # with no billing - private = false - - has_issues = false - has_wiki = false - has_downloads = false -} resource "github_issue_label" "test" { - repository = "${github_repository.foo.name}" + repository = "%s" name = "bar" color = "FFFFFF" } diff --git a/builtin/providers/github/resource_github_membership.go b/builtin/providers/github/resource_github_membership.go index 77716e554..e13b0025c 100644 --- a/builtin/providers/github/resource_github_membership.go +++ b/builtin/providers/github/resource_github_membership.go @@ -12,6 +12,9 @@ func resourceGithubMembership() *schema.Resource { Read: resourceGithubMembershipRead, Update: resourceGithubMembershipUpdate, Delete: resourceGithubMembershipDelete, + Importer: &schema.ResourceImporter{ + State: schema.ImportStatePassthrough, + }, Schema: map[string]*schema.Schema{ "username": &schema.Schema{ @@ -47,8 +50,9 @@ func resourceGithubMembershipCreate(d *schema.ResourceData, meta interface{}) er func resourceGithubMembershipRead(d *schema.ResourceData, meta interface{}) error { client := meta.(*Organization).client + _, n := parseTwoPartID(d.Id()) - membership, _, err := client.Organizations.GetOrgMembership(d.Get("username").(string), meta.(*Organization).name) + membership, _, err := client.Organizations.GetOrgMembership(n, meta.(*Organization).name) if err != nil { d.SetId("") return nil @@ -64,12 +68,14 @@ func resourceGithubMembershipUpdate(d *schema.ResourceData, meta interface{}) er n := d.Get("username").(string) r := d.Get("role").(string) - _, _, err := client.Organizations.EditOrgMembership(n, meta.(*Organization).name, &github.Membership{ + membership, _, err := client.Organizations.EditOrgMembership(n, meta.(*Organization).name, &github.Membership{ Role: &r, }) if err != nil { return err } + d.SetId(buildTwoPartID(membership.Organization.Login, membership.User.Login)) + return nil } diff --git a/builtin/providers/github/resource_github_membership_test.go b/builtin/providers/github/resource_github_membership_test.go index cebad98da..b6e1f19f5 100644 --- a/builtin/providers/github/resource_github_membership_test.go +++ b/builtin/providers/github/resource_github_membership_test.go @@ -2,7 +2,6 @@ package github import ( "fmt" - "os" "testing" "github.com/google/go-github/github" @@ -13,14 +12,6 @@ import ( func TestAccGithubMembership_basic(t *testing.T) { var membership github.Membership - testUser := os.Getenv("GITHUB_TEST_USER") - testAccGithubMembershipConfig := fmt.Sprintf(` - resource "github_membership" "test_org_membership" { - username = "%s" - role = "member" - } - `, testUser) - resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, Providers: testAccProviders, @@ -37,6 +28,24 @@ func TestAccGithubMembership_basic(t *testing.T) { }) } +func TestAccGithubMembership_importBasic(t *testing.T) { + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckGithubMembershipDestroy, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testAccGithubMembershipConfig, + }, + resource.TestStep{ + ResourceName: "github_membership.test_org_membership", + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} + func testAccCheckGithubMembershipDestroy(s *terraform.State) error { conn := testAccProvider.Meta().(*Organization).client @@ -113,3 +122,10 @@ func testAccCheckGithubMembershipRoleState(n string, membership *github.Membersh return nil } } + +var testAccGithubMembershipConfig string = fmt.Sprintf(` + resource "github_membership" "test_org_membership" { + username = "%s" + role = "member" + } +`, testUser) diff --git a/builtin/providers/github/resource_github_repository.go b/builtin/providers/github/resource_github_repository.go index a449b1d83..726fe1776 100644 --- a/builtin/providers/github/resource_github_repository.go +++ b/builtin/providers/github/resource_github_repository.go @@ -14,6 +14,9 @@ func resourceGithubRepository() *schema.Resource { Read: resourceGithubRepositoryRead, Update: resourceGithubRepositoryUpdate, Delete: resourceGithubRepositoryDelete, + Importer: &schema.ResourceImporter{ + State: schema.ImportStatePassthrough, + }, Schema: map[string]*schema.Schema{ "name": &schema.Schema{ @@ -134,6 +137,7 @@ func resourceGithubRepositoryRead(d *schema.ResourceData, meta interface{}) erro } return err } + d.Set("name", repoName) d.Set("description", repo.Description) d.Set("homepage_url", repo.Homepage) d.Set("private", repo.Private) @@ -154,10 +158,12 @@ func resourceGithubRepositoryUpdate(d *schema.ResourceData, meta interface{}) er repoReq := resourceGithubRepositoryObject(d) repoName := d.Id() log.Printf("[DEBUG] update github repository %s/%s", meta.(*Organization).name, repoName) - _, _, err := client.Repositories.Edit(meta.(*Organization).name, repoName, repoReq) + repo, _, err := client.Repositories.Edit(meta.(*Organization).name, repoName, repoReq) if err != nil { return err } + d.SetId(*repo.Name) + return resourceGithubRepositoryRead(d, meta) } diff --git a/builtin/providers/github/resource_github_repository_collaborator.go b/builtin/providers/github/resource_github_repository_collaborator.go index fdbca0cb6..cde09c6f8 100644 --- a/builtin/providers/github/resource_github_repository_collaborator.go +++ b/builtin/providers/github/resource_github_repository_collaborator.go @@ -11,6 +11,9 @@ func resourceGithubRepositoryCollaborator() *schema.Resource { Read: resourceGithubRepositoryCollaboratorRead, // editing repository collaborators are not supported by github api so forcing new on any changes Delete: resourceGithubRepositoryCollaboratorDelete, + Importer: &schema.ResourceImporter{ + State: schema.ImportStatePassthrough, + }, Schema: map[string]*schema.Schema{ "username": &schema.Schema{ @@ -54,8 +57,7 @@ func resourceGithubRepositoryCollaboratorCreate(d *schema.ResourceData, meta int func resourceGithubRepositoryCollaboratorRead(d *schema.ResourceData, meta interface{}) error { client := meta.(*Organization).client - u := d.Get("username").(string) - r := d.Get("repository").(string) + r, u := parseTwoPartID(d.Id()) isCollaborator, _, err := client.Repositories.IsCollaborator(meta.(*Organization).name, r, u) @@ -79,6 +81,8 @@ func resourceGithubRepositoryCollaboratorRead(d *schema.ResourceData, meta inter return err } + d.Set("repository", r) + d.Set("username", u) d.Set("permission", permName) return nil diff --git a/builtin/providers/github/resource_github_repository_collaborator_test.go b/builtin/providers/github/resource_github_repository_collaborator_test.go index cecee96c0..18f5cb9ef 100644 --- a/builtin/providers/github/resource_github_repository_collaborator_test.go +++ b/builtin/providers/github/resource_github_repository_collaborator_test.go @@ -2,7 +2,6 @@ package github import ( "fmt" - "os" "testing" "github.com/google/go-github/github" @@ -13,15 +12,6 @@ import ( const expectedPermission string = "admin" func TestAccGithubRepositoryCollaborator_basic(t *testing.T) { - testCollaborator := os.Getenv("GITHUB_TEST_COLLABORATOR") - testAccGithubRepositoryCollaboratorConfig := fmt.Sprintf(` - resource "github_repository_collaborator" "test_repo_collaborator" { - repository = "%s" - username = "%s" - permission = "%s" - } - `, testRepo, testCollaborator, expectedPermission) - resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, Providers: testAccProviders, @@ -38,6 +28,24 @@ func TestAccGithubRepositoryCollaborator_basic(t *testing.T) { }) } +func TestAccGithubRepositoryCollaborator_importBasic(t *testing.T) { + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckGithubRepositoryCollaboratorDestroy, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testAccGithubRepositoryCollaboratorConfig, + }, + resource.TestStep{ + ResourceName: "github_repository_collaborator.test_repo_collaborator", + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} + func testAccCheckGithubRepositoryCollaboratorDestroy(s *terraform.State) error { conn := testAccProvider.Meta().(*Organization).client @@ -133,3 +141,11 @@ func testAccCheckGithubRepositoryCollaboratorPermission(n string) resource.TestC return fmt.Errorf("Repository collaborator did not appear in list of collaborators on repository") } } + +var testAccGithubRepositoryCollaboratorConfig string = fmt.Sprintf(` + resource "github_repository_collaborator" "test_repo_collaborator" { + repository = "%s" + username = "%s" + permission = "%s" + } +`, testRepo, testCollaborator, expectedPermission) diff --git a/builtin/providers/github/resource_github_repository_test.go b/builtin/providers/github/resource_github_repository_test.go index 01ccdb08e..685337195 100644 --- a/builtin/providers/github/resource_github_repository_test.go +++ b/builtin/providers/github/resource_github_repository_test.go @@ -49,6 +49,24 @@ func TestAccGithubRepository_basic(t *testing.T) { }) } +func TestAccGithubRepository_importBasic(t *testing.T) { + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckGithubRepositoryDestroy, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testAccGithubRepositoryConfig, + }, + resource.TestStep{ + ResourceName: "github_repository.foo", + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} + func testAccCheckGithubRepositoryExists(n string, repo *github.Repository) resource.TestCheckFunc { return func(s *terraform.State) error { rs, ok := s.RootModule().Resources[n] diff --git a/builtin/providers/github/resource_github_team.go b/builtin/providers/github/resource_github_team.go index 77c4fdbe6..dc6ad2c5e 100644 --- a/builtin/providers/github/resource_github_team.go +++ b/builtin/providers/github/resource_github_team.go @@ -12,6 +12,9 @@ func resourceGithubTeam() *schema.Resource { Read: resourceGithubTeamRead, Update: resourceGithubTeamUpdate, Delete: resourceGithubTeamDelete, + Importer: &schema.ResourceImporter{ + State: schema.ImportStatePassthrough, + }, Schema: map[string]*schema.Schema{ "name": &schema.Schema{ diff --git a/builtin/providers/github/resource_github_team_membership.go b/builtin/providers/github/resource_github_team_membership.go index 3bd99cbc8..e6f38b675 100644 --- a/builtin/providers/github/resource_github_team_membership.go +++ b/builtin/providers/github/resource_github_team_membership.go @@ -14,6 +14,9 @@ func resourceGithubTeamMembership() *schema.Resource { Read: resourceGithubTeamMembershipRead, // editing team memberships are not supported by github api so forcing new on any changes Delete: resourceGithubTeamMembershipDelete, + Importer: &schema.ResourceImporter{ + State: schema.ImportStatePassthrough, + }, Schema: map[string]*schema.Schema{ "team_id": &schema.Schema{ @@ -57,8 +60,7 @@ func resourceGithubTeamMembershipCreate(d *schema.ResourceData, meta interface{} func resourceGithubTeamMembershipRead(d *schema.ResourceData, meta interface{}) error { client := meta.(*Organization).client - t := d.Get("team_id").(string) - n := d.Get("username").(string) + t, n := parseTwoPartID(d.Id()) membership, _, err := client.Organizations.GetTeamMembership(toGithubID(t), n) diff --git a/builtin/providers/github/resource_github_team_membership_test.go b/builtin/providers/github/resource_github_team_membership_test.go index 074112b4b..9cf2dd788 100644 --- a/builtin/providers/github/resource_github_team_membership_test.go +++ b/builtin/providers/github/resource_github_team_membership_test.go @@ -2,7 +2,6 @@ package github import ( "fmt" - "os" "testing" "github.com/google/go-github/github" @@ -13,25 +12,6 @@ import ( func TestAccGithubTeamMembership_basic(t *testing.T) { var membership github.Membership - testUser := os.Getenv("GITHUB_TEST_USER") - testAccGithubTeamMembershipConfig := fmt.Sprintf(` - resource "github_membership" "test_org_membership" { - username = "%s" - role = "member" - } - - resource "github_team" "test_team" { - name = "foo" - description = "Terraform acc test group" - } - - resource "github_team_membership" "test_team_membership" { - team_id = "${github_team.test_team.id}" - username = "%s" - role = "member" - } - `, testUser, testUser) - testAccGithubTeamMembershipUpdateConfig := fmt.Sprintf(` resource "github_membership" "test_org_membership" { username = "%s" @@ -73,6 +53,24 @@ func TestAccGithubTeamMembership_basic(t *testing.T) { }) } +func TestAccGithubTeamMembership_importBasic(t *testing.T) { + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckGithubTeamMembershipDestroy, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testAccGithubTeamMembershipConfig, + }, + resource.TestStep{ + ResourceName: "github_team_membership.test_team_membership", + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} + func testAccCheckGithubTeamMembershipDestroy(s *terraform.State) error { conn := testAccProvider.Meta().(*Organization).client @@ -152,3 +150,21 @@ func testAccCheckGithubTeamMembershipRoleState(n, expected string, membership *g return nil } } + +var testAccGithubTeamMembershipConfig string = fmt.Sprintf(` + resource "github_membership" "test_org_membership" { + username = "%s" + role = "member" + } + + resource "github_team" "test_team" { + name = "foo" + description = "Terraform acc test group" + } + + resource "github_team_membership" "test_team_membership" { + team_id = "${github_team.test_team.id}" + username = "%s" + role = "member" + } +`, testUser, testUser) diff --git a/builtin/providers/github/resource_github_team_repository.go b/builtin/providers/github/resource_github_team_repository.go index b9e50a09b..fa8b70e75 100644 --- a/builtin/providers/github/resource_github_team_repository.go +++ b/builtin/providers/github/resource_github_team_repository.go @@ -11,6 +11,9 @@ func resourceGithubTeamRepository() *schema.Resource { Read: resourceGithubTeamRepositoryRead, Update: resourceGithubTeamRepositoryUpdate, Delete: resourceGithubTeamRepositoryDelete, + Importer: &schema.ResourceImporter{ + State: schema.ImportStatePassthrough, + }, Schema: map[string]*schema.Schema{ "team_id": &schema.Schema{ @@ -53,8 +56,7 @@ func resourceGithubTeamRepositoryCreate(d *schema.ResourceData, meta interface{} func resourceGithubTeamRepositoryRead(d *schema.ResourceData, meta interface{}) error { client := meta.(*Organization).client - t := d.Get("team_id").(string) - r := d.Get("repository").(string) + t, r := parseTwoPartID(d.Id()) repo, _, repoErr := client.Organizations.IsTeamRepo(toGithubID(t), meta.(*Organization).name, r) @@ -92,6 +94,8 @@ func resourceGithubTeamRepositoryUpdate(d *schema.ResourceData, meta interface{} if err != nil { return err } + d.SetId(buildTwoPartID(&t, &r)) + return resourceGithubTeamRepositoryRead(d, meta) } diff --git a/builtin/providers/github/resource_github_team_repository_test.go b/builtin/providers/github/resource_github_team_repository_test.go index fd90a8fc1..3d764305b 100644 --- a/builtin/providers/github/resource_github_team_repository_test.go +++ b/builtin/providers/github/resource_github_team_repository_test.go @@ -35,6 +35,24 @@ func TestAccGithubTeamRepository_basic(t *testing.T) { }) } +func TestAccGithubTeamRepository_importBasic(t *testing.T) { + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckGithubTeamRepositoryDestroy, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testAccGithubTeamRepositoryConfig, + }, + resource.TestStep{ + ResourceName: "github_team_repository.test_team_test_repo", + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} + func TestAccCheckGetPermissions(t *testing.T) { pullMap := map[string]bool{"pull": true, "push": false, "admin": false} pushMap := map[string]bool{"pull": true, "push": true, "admin": false} diff --git a/builtin/providers/github/resource_github_team_test.go b/builtin/providers/github/resource_github_team_test.go index 19b58b18c..1077e9655 100644 --- a/builtin/providers/github/resource_github_team_test.go +++ b/builtin/providers/github/resource_github_team_test.go @@ -35,6 +35,24 @@ func TestAccGithubTeam_basic(t *testing.T) { }) } +func TestAccGithubTeam_importBasic(t *testing.T) { + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckGithubTeamDestroy, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testAccGithubTeamConfig, + }, + resource.TestStep{ + ResourceName: "github_team.foo", + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} + func testAccCheckGithubTeamExists(n string, team *github.Team) resource.TestCheckFunc { return func(s *terraform.State) error { rs, ok := s.RootModule().Resources[n]