Skip to content

Commit

Permalink
Add a data source for custom GET requests to REST API. (#1119)
Browse files Browse the repository at this point in the history
Co-authored-by: Keegan Campbell <me@kfcampbell.com>
  • Loading branch information
galargh and kfcampbell authored May 19, 2023
1 parent 33f858f commit 2d7e32c
Show file tree
Hide file tree
Showing 5 changed files with 201 additions and 0 deletions.
61 changes: 61 additions & 0 deletions github/data_source_github_rest_api.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package github

import (
"context"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)

func dataSourceGithubRestApi() *schema.Resource {
return &schema.Resource{
Read: dataSourceGithubRestApiRead,

Schema: map[string]*schema.Schema{
"endpoint": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"code": {
Type: schema.TypeInt,
Computed: true,
},
"status": {
Type: schema.TypeString,
Computed: true,
},
"headers": {
Type: schema.TypeMap,
Computed: true,
},
"body": {
Type: schema.TypeMap,
Computed: true,
},
},
}
}

func dataSourceGithubRestApiRead(d *schema.ResourceData, meta interface{}) error {
u := d.Get("endpoint").(string)

client := meta.(*Owner).v3client
ctx := context.Background()

var body map[string]interface{}

req, err := client.NewRequest("GET", u, nil)
if err != nil {
return err
}

resp, _ := client.Do(ctx, req, &body)

d.SetId(resp.Header.Get("x-github-request-id"))
d.Set("code", resp.StatusCode)
d.Set("status", resp.Status)
d.Set("headers", resp.Header)
d.Set("body", body)

return nil
}
107 changes: 107 additions & 0 deletions github/data_source_github_rest_api_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package github

import (
"fmt"
"regexp"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
)

func TestAccGithubRestApiDataSource(t *testing.T) {

randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum)

t.Run("queries an existing branch without error", func(t *testing.T) {

config := fmt.Sprintf(`
resource "github_repository" "test" {
name = "tf-acc-test-%[1]s"
auto_init = true
}
data "github_rest_api" "test" {
endpoint = "repos/${github_repository.test.full_name}/git/refs/heads/${github_repository.test.default_branch}"
}
`, randomID)

check := resource.ComposeTestCheckFunc(
resource.TestMatchResourceAttr(
"data.github_rest_api.test", "code", regexp.MustCompile("200"),
),
)

testCase := func(t *testing.T, mode string) {
resource.Test(t, resource.TestCase{
PreCheck: func() { skipUnlessMode(t, mode) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: config,
Check: check,
},
},
})
}

t.Run("with an anonymous account", func(t *testing.T) {
t.Skip("anonymous account not supported for this operation")
})

t.Run("with an individual account", func(t *testing.T) {
testCase(t, individual)
})

t.Run("with an organization account", func(t *testing.T) {
testCase(t, organization)
})

})

t.Run("queries an invalid branch without error", func(t *testing.T) {

config := fmt.Sprintf(`
resource "github_repository" "test" {
name = "tf-acc-test-%[1]s"
auto_init = true
}
data "github_rest_api" "test" {
endpoint = "repos/${github_repository.test.full_name}/git/refs/heads/xxxxxx"
}
`, randomID)

check := resource.ComposeTestCheckFunc(
resource.TestMatchResourceAttr(
"data.github_rest_api.test", "code", regexp.MustCompile("404"),
),
)

testCase := func(t *testing.T, mode string) {
resource.Test(t, resource.TestCase{
PreCheck: func() { skipUnlessMode(t, mode) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: config,
Check: check,
},
},
})
}

t.Run("with an anonymous account", func(t *testing.T) {
t.Skip("anonymous account not supported for this operation")
})

t.Run("with an individual account", func(t *testing.T) {
testCase(t, individual)
})

t.Run("with an organization account", func(t *testing.T) {
testCase(t, organization)
})

})
}
1 change: 1 addition & 0 deletions github/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ func Provider() terraform.ResourceProvider {
"github_repository_pull_requests": dataSourceGithubRepositoryPullRequests(),
"github_repository_teams": dataSourceGithubRepositoryTeams(),
"github_repository_webhooks": dataSourceGithubRepositoryWebhooks(),
"github_rest_api": dataSourceGithubRestApi(),
"github_ssh_keys": dataSourceGithubSshKeys(),
"github_team": dataSourceGithubTeam(),
"github_tree": dataSourceGithubTree(),
Expand Down
29 changes: 29 additions & 0 deletions website/docs/d/rest_api.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
---
layout: "github"
page_title: "GitHub: github_rest_api"
description: |-
Get information on a GitHub resource with a custom GET request to GitHub REST API.
---

# github_rest_api

Use this data source to retrieve information about a GitHub resource through REST API.

## Example Usage

```hcl
data "github_rest_api" "example" {
endpoint = "repos/example_repo/git/refs/heads/main"
}
```

## Argument Reference

* `endpoint` - (Required) REST API endpoint to send the GET request to.

## Attributes Reference

* `code` - A response status code.
* `status` - A response status string.
* `headers` - A map of response headers.
* `body` - A map of response body.
3 changes: 3 additions & 0 deletions website/github.erb
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,9 @@
<li>
<a href="/docs/providers/github/d/repository_webhooks.html">github_repository_webhooks</a>
</li>
<li>
<a href="/docs/providers/github/d/rest_api.html">github_rest_api</a>
</li>
<li>
<a href="/docs/providers/github/d/ssh_keys.html">github_ssh_keys</a>
</li>
Expand Down

0 comments on commit 2d7e32c

Please sign in to comment.