-
Notifications
You must be signed in to change notification settings - Fork 741
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a data source for custom GET requests to REST API. (#1119)
Co-authored-by: Keegan Campbell <me@kfcampbell.com>
- Loading branch information
1 parent
33f858f
commit 2d7e32c
Showing
5 changed files
with
201 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
|
||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters