Skip to content
Open
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
6 changes: 6 additions & 0 deletions docs/data-sources/ip_ranges.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@
data "github_ip_ranges" "test" {}
```

## Timeouts

The `timeouts` block allows you to configure [timeouts](https://developer.hashicorp.com/terraform/language/resources/syntax#operation-timeouts) for certain actions:

* `read` - (Defaults to 5 minutes) Used when reading the GitHub IP ranges from the metadata API.

Check warning on line 21 in docs/data-sources/ip_ranges.md

View workflow job for this annotation

GitHub Actions / Documentation

MD004

List marker '*' does not match expected style '-'

Check warning on line 21 in docs/data-sources/ip_ranges.md

View workflow job for this annotation

GitHub Actions / Documentation

MD004

List marker '*' does not match expected style '-'

## Attributes Reference

- `actions` - An array of IP addresses in CIDR format specifying the addresses that incoming requests from GitHub Actions will originate from.
Expand Down
4 changes: 4 additions & 0 deletions github/data_source_github_ip_ranges.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"net"
"time"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
Expand All @@ -13,6 +14,9 @@ func dataSourceGithubIpRanges() *schema.Resource {
return &schema.Resource{
Description: "Get the GitHub IP ranges used by various GitHub services.",
ReadContext: dataSourceGithubIpRangesRead,
Timeouts: &schema.ResourceTimeout{
Read: schema.DefaultTimeout(5 * time.Minute),
},
Schema: map[string]*schema.Schema{
"hooks": {
Type: schema.TypeList,
Expand Down
100 changes: 100 additions & 0 deletions github/data_source_github_ip_ranges_test.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,117 @@
package github

import (
"context"
"net/http"
"net/http/httptest"
"testing"
"time"

"github.com/google/go-github/v89/github"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/knownvalue"
"github.com/hashicorp/terraform-plugin-testing/statecheck"
"github.com/hashicorp/terraform-plugin-testing/tfjsonpath"
)

func TestGithubIpRangesDataSourceRead(t *testing.T) {
t.Parallel()

t.Run("gives up on a stalled metadata request once the deadline expires", func(t *testing.T) {
t.Parallel()

serverDelay := 5 * time.Second
readDeadline := 200 * time.Millisecond

ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
select {
case <-r.Context().Done():
case <-time.After(serverDelay):
mustWrite(w, `{}`)
}
}))
defer ts.Close()

// StopContext mirrors the provider configuration and never expires, so the
// read has to use the context it is called with to respect the deadline.
meta := &Owner{
v3client: mustCreateTestGitHubClient(t, ts.URL),
StopContext: context.Background(),
}

ctx, cancel := context.WithTimeout(t.Context(), readDeadline)
defer cancel()

start := time.Now()
diags := dataSourceGithubIpRangesRead(ctx, dataSourceGithubIpRanges().TestResourceData(), meta)
elapsed := time.Since(start)

if !diags.HasError() {
t.Fatal("expected an error when the read deadline expires before the response arrives")
}
if elapsed >= serverDelay {
t.Fatalf("read waited %s for the metadata response instead of honoring the %s deadline", elapsed, readDeadline)
}
})

t.Run("populates IP ranges when the response arrives before the deadline", func(t *testing.T) {
t.Parallel()

ts := githubApiMock([]*mockResponse{
mustGetTestMockResponse(t, "/meta", http.StatusOK, &github.APIMeta{
Hooks: []string{"192.0.2.0/24", "2001:db8::/32"},
}),
})
defer ts.Close()

meta := &Owner{v3client: mustCreateTestGitHubClient(t, ts.URL)}

ctx, cancel := context.WithTimeout(t.Context(), time.Minute)
defer cancel()

d := dataSourceGithubIpRanges().TestResourceData()
diags := dataSourceGithubIpRangesRead(ctx, d, meta)
if diags.HasError() {
t.Fatalf("unexpected error: %v", diags)
}

if got, want := d.Get("hooks_ipv4").([]any), "192.0.2.0/24"; len(got) != 1 || got[0] != want {

Check failure on line 78 in github/data_source_github_ip_ranges_test.go

View workflow job for this annotation

GitHub Actions / Strict linting of new code

right hand must be only type assertion (forcetypeassert)
t.Errorf("expected hooks_ipv4 to be [%s], got %v", want, got)
}
if got, want := d.Get("hooks_ipv6").([]any), "2001:db8::/32"; len(got) != 1 || got[0] != want {

Check failure on line 81 in github/data_source_github_ip_ranges_test.go

View workflow job for this annotation

GitHub Actions / Strict linting of new code

right hand must be only type assertion (forcetypeassert)
t.Errorf("expected hooks_ipv6 to be [%s], got %v", want, got)
}
})
}

func TestAccGithubIpRangesDataSource(t *testing.T) {
t.Parallel()

t.Run("reads IP ranges with a configured read timeout", func(t *testing.T) {
t.Parallel()

config := `
data "github_ip_ranges" "test" {
timeouts {
read = "2m"
}
}
`

resource.Test(t, resource.TestCase{
ProviderFactories: providerFactories,
Steps: []resource.TestStep{
{
Config: config,
ConfigStateChecks: []statecheck.StateCheck{
statecheck.ExpectKnownValue("data.github_ip_ranges.test", tfjsonpath.New("actions_ipv4"), knownvalue.NotNull()),
statecheck.ExpectKnownValue("data.github_ip_ranges.test", tfjsonpath.New("actions_ipv6"), knownvalue.NotNull()),
},
},
},
})
})

t.Run("reads IP ranges without error", func(t *testing.T) {
t.Parallel()

Expand Down
6 changes: 6 additions & 0 deletions templates/data-sources/ip_ranges.md.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ Use this data source to retrieve information about GitHub's IP addresses.

{{ tffile "examples/data-sources/ip_ranges/example_1.tf" }}

## Timeouts

The `timeouts` block allows you to configure [timeouts](https://developer.hashicorp.com/terraform/language/resources/syntax#operation-timeouts) for certain actions:

* `read` - (Defaults to 5 minutes) Used when reading the GitHub IP ranges from the metadata API.

## Attributes Reference

- `actions` - An array of IP addresses in CIDR format specifying the addresses that incoming requests from GitHub Actions will originate from.
Expand Down
Loading