diff --git a/github/resource_github_repository_webhook.go b/github/resource_github_repository_webhook.go index 3c77e10328..653ce105c1 100644 --- a/github/resource_github_repository_webhook.go +++ b/github/resource_github_repository_webhook.go @@ -2,7 +2,9 @@ package github import ( "context" + "fmt" "strconv" + "strings" "github.com/google/go-github/github" "github.com/hashicorp/terraform/helper/schema" @@ -14,6 +16,17 @@ func resourceGithubRepositoryWebhook() *schema.Resource { Read: resourceGithubRepositoryWebhookRead, Update: resourceGithubRepositoryWebhookUpdate, Delete: resourceGithubRepositoryWebhookDelete, + Importer: &schema.ResourceImporter{ + 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 /") + } + d.Set("repository", parts[0]) + d.SetId(parts[1]) + return []*schema.ResourceData{d}, nil + }, + }, Schema: map[string]*schema.Schema{ "name": { @@ -26,7 +39,7 @@ func resourceGithubRepositoryWebhook() *schema.Resource { Required: true, ForceNew: true, }, - "events": &schema.Schema{ + "events": { Type: schema.TypeSet, Required: true, Elem: &schema.Schema{Type: schema.TypeString}, diff --git a/github/resource_github_repository_webhook_test.go b/github/resource_github_repository_webhook_test.go index 189cae5c3e..06693a2748 100644 --- a/github/resource_github_repository_webhook_test.go +++ b/github/resource_github_repository_webhook_test.go @@ -59,6 +59,27 @@ func TestAccGithubRepositoryWebhook_basic(t *testing.T) { }) } +func TestAccGithubRepositoryWebhook_importBasic(t *testing.T) { + randString := acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckGithubRepositoryDestroy, + Steps: []resource.TestStep{ + { + Config: testAccGithubRepositoryWebhookConfig(randString), + }, + { + ResourceName: "github_repository_webhook.foo", + ImportState: true, + ImportStateVerify: true, + ImportStateIdPrefix: fmt.Sprintf("foo-%s/", randString), + }, + }, + }) +} + func testAccCheckGithubRepositoryWebhookExists(n string, repoName string, hook *github.Hook) resource.TestCheckFunc { return func(s *terraform.State) error { rs, ok := s.RootModule().Resources[n] diff --git a/website/docs/r/repository_webhook.html.markdown b/website/docs/r/repository_webhook.html.markdown index 71f1ba427e..b0cf1670c5 100644 --- a/website/docs/r/repository_webhook.html.markdown +++ b/website/docs/r/repository_webhook.html.markdown @@ -61,3 +61,14 @@ The following arguments are supported: The following additional attributes are exported: * `url` - URL of the webhook + +## Import + +Repository Webhooks can be imported using the `name` of the repository, combined with the `id` of the webhook, separated by a `/` character. +The `id` of the webhook can be found in the URL of the webhook. For example: `"https://github.com/foo-org/foo-repo/settings/hooks/14711452"`. + +Importing uses the name of the repository, as well as the ID of the webhook, e.g. + +``` +$ terraform import github_repository_webhook.terraform terraform/11235813 +``` \ No newline at end of file