diff --git a/github/data_source_github_rest_api.go b/github/data_source_github_rest_api.go index 1bbd22f023..bc1b5f1a50 100644 --- a/github/data_source_github_rest_api.go +++ b/github/data_source_github_rest_api.go @@ -2,6 +2,7 @@ package github import ( "context" + "encoding/json" "github.com/hashicorp/terraform-plugin-sdk/helper/schema" ) @@ -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, }, }, @@ -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 } diff --git a/github/data_source_github_rest_api_test.go b/github/data_source_github_rest_api_test.go index b69bc6b398..d309323cd4 100644 --- a/github/data_source_github_rest_api_test.go +++ b/github/data_source_github_rest_api_test.go @@ -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) { @@ -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) { diff --git a/website/docs/d/rest_api.html.markdown b/website/docs/d/rest_api.html.markdown index 4535d79508..de628acef6 100644 --- a/website/docs/d/rest_api.html.markdown +++ b/website/docs/d/rest_api.html.markdown @@ -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.