Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

r/github_repository_webhook: Add import #29

Merged
merged 2 commits into from
Jul 5, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
```