Skip to content

Commit

Permalink
Merge pull request integrations#29 from terraform-providers/f-import-…
Browse files Browse the repository at this point in the history
…webhook

r/github_repository_webhook: Add import
  • Loading branch information
grubernaut authored Jul 5, 2017
2 parents bdf4bed + e75f9d2 commit 2bdefde
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
15 changes: 14 additions & 1 deletion github/resource_github_repository_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package github

import (
"context"
"fmt"
"strconv"
"strings"

"github.com/google/go-github/github"
"github.com/hashicorp/terraform/helper/schema"
Expand All @@ -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 <repository>/<webhook_id>")
}
d.Set("repository", parts[0])
d.SetId(parts[1])
return []*schema.ResourceData{d}, nil
},
},

Schema: map[string]*schema.Schema{
"name": {
Expand All @@ -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},
Expand Down
21 changes: 21 additions & 0 deletions github/resource_github_repository_webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
11 changes: 11 additions & 0 deletions website/docs/r/repository_webhook.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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
```

0 comments on commit 2bdefde

Please sign in to comment.