Skip to content

Commit

Permalink
Merge branch 'main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
kfcampbell committed Jan 22, 2024
2 parents b33cc48 + 6e507a6 commit 1874558
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 6 deletions.
19 changes: 15 additions & 4 deletions github/data_source_github_rest_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package github

import (
"context"
"encoding/json"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)
Expand All @@ -25,11 +26,11 @@ func dataSourceGithubRestApi() *schema.Resource {
Computed: true,
},
"headers": {
Type: schema.TypeMap,
Type: schema.TypeString,
Computed: true,
},
"body": {
Type: schema.TypeMap,
Type: schema.TypeString,
Computed: true,
},
},
Expand All @@ -51,11 +52,21 @@ func dataSourceGithubRestApiRead(d *schema.ResourceData, meta interface{}) error

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

h, err := json.Marshal(resp.Header)
if err != nil {
return err
}

b, err := json.Marshal(body)
if err != nil {
return err
}

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)
d.Set("headers", string(h))
d.Set("body", string(b))

return nil
}
10 changes: 10 additions & 0 deletions github/data_source_github_rest_api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ func TestAccGithubRestApiDataSource(t *testing.T) {
resource.TestMatchResourceAttr(
"data.github_rest_api.test", "code", regexp.MustCompile("200"),
),
resource.TestMatchResourceAttr(
"data.github_rest_api.test", "status", regexp.MustCompile("200 OK"),
),
resource.TestCheckResourceAttrSet("data.github_rest_api.test", "body"),
resource.TestCheckResourceAttrSet("data.github_rest_api.test", "headers"),
)

testCase := func(t *testing.T, mode string) {
Expand Down Expand Up @@ -76,6 +81,11 @@ func TestAccGithubRestApiDataSource(t *testing.T) {
resource.TestMatchResourceAttr(
"data.github_rest_api.test", "code", regexp.MustCompile("404"),
),
resource.TestMatchResourceAttr(
"data.github_rest_api.test", "status", regexp.MustCompile("404 Not Found"),
),
resource.TestCheckResourceAttrSet("data.github_rest_api.test", "body"),
resource.TestCheckResourceAttrSet("data.github_rest_api.test", "headers"),
)

testCase := func(t *testing.T, mode string) {
Expand Down
5 changes: 3 additions & 2 deletions website/docs/d/rest_api.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ data "github_rest_api" "example" {

## Attributes Reference

* `id` - The GitHub API Request ID
* `code` - A response status code.
* `status` - A response status string.
* `headers` - A map of response headers.
* `body` - A map of response body.
* `headers` - A JSON string containing response headers.
* `body` - A JSON string containing response body.

0 comments on commit 1874558

Please sign in to comment.