forked from integrations/terraform-provider-github
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create data source for organization ip allow list (integrations#1275)
* feat: add data source to get org's ip allow list * test: add test for org ip allow list data source * doc: document org ip allow list data source * chore: backmerge from main * chore: sort resources
- Loading branch information
1 parent
81855e0
commit 75c59f8
Showing
5 changed files
with
217 additions
and
3 deletions.
There are no files selected for viewing
125 changes: 125 additions & 0 deletions
125
github/data_source_github_organization_ip_allow_list.go
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,125 @@ | ||
package github | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/helper/schema" | ||
"github.com/shurcooL/githubv4" | ||
) | ||
|
||
func dataSourceGithubOrganizationIpAllowList() *schema.Resource { | ||
return &schema.Resource{ | ||
Read: dataSourceGithubOrganizationIpAllowListRead, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"ip_allow_list": { | ||
Type: schema.TypeList, | ||
Computed: true, | ||
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"id": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"name": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"allow_list_value": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"is_active": { | ||
Type: schema.TypeBool, | ||
Computed: true, | ||
}, | ||
"created_at": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"updated_at": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func dataSourceGithubOrganizationIpAllowListRead(d *schema.ResourceData, meta interface{}) error { | ||
err := checkOrganization(meta) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
ctx := context.Background() | ||
client := meta.(*Owner).v4client | ||
orgName := meta.(*Owner).name | ||
|
||
type PageInfo struct { | ||
StartCursor githubv4.String | ||
EndCursor githubv4.String | ||
HasNextPage githubv4.Boolean | ||
HasPreviousPage githubv4.Boolean | ||
} | ||
|
||
type IpAllowListEntry struct { | ||
ID githubv4.String | ||
Name githubv4.String | ||
AllowListValue githubv4.String | ||
IsActive githubv4.Boolean | ||
CreatedAt githubv4.String | ||
UpdatedAt githubv4.String | ||
} | ||
|
||
type IpAllowListEntries struct { | ||
Nodes []IpAllowListEntry | ||
PageInfo PageInfo | ||
TotalCount githubv4.Int | ||
} | ||
|
||
var query struct { | ||
Organization struct { | ||
ID githubv4.String | ||
IpAllowListEntries IpAllowListEntries `graphql:"ipAllowListEntries(first: 100, after: $entriesCursor)"` | ||
} `graphql:"organization(login: $login)"` | ||
} | ||
|
||
variables := map[string]interface{}{ | ||
"login": githubv4.String(orgName), | ||
"entriesCursor": (*githubv4.String)(nil), | ||
} | ||
|
||
var ipAllowList []interface{} | ||
var ipAllowListEntries []IpAllowListEntry | ||
|
||
for { | ||
err := client.Query(ctx, &query, variables) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
ipAllowListEntries = append(ipAllowListEntries, query.Organization.IpAllowListEntries.Nodes...) | ||
if !query.Organization.IpAllowListEntries.PageInfo.HasNextPage { | ||
break | ||
} | ||
variables["entriesCursor"] = githubv4.NewString(query.Organization.IpAllowListEntries.PageInfo.EndCursor) | ||
} | ||
for index := range ipAllowListEntries { | ||
ipAllowList = append(ipAllowList, map[string]interface{}{ | ||
"id": ipAllowListEntries[index].ID, | ||
"name": ipAllowListEntries[index].Name, | ||
"allow_list_value": ipAllowListEntries[index].AllowListValue, | ||
"is_active": ipAllowListEntries[index].IsActive, | ||
"created_at": ipAllowListEntries[index].CreatedAt, | ||
"updated_at": ipAllowListEntries[index].UpdatedAt, | ||
}) | ||
} | ||
|
||
d.SetId(string(query.Organization.ID)) | ||
d.Set("ip_allow_list", ipAllowList) | ||
|
||
return nil | ||
} |
53 changes: 53 additions & 0 deletions
53
github/data_source_github_organization_ip_allow_list_test.go
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,53 @@ | ||
package github | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/helper/resource" | ||
) | ||
|
||
func TestAccGithubOrganizationIpAllowListDataSource(t *testing.T) { | ||
|
||
t.Run("queries without error", func(t *testing.T) { | ||
|
||
config := ` | ||
data "github_organization_ip_allow_list" "all" {} | ||
` | ||
|
||
check := resource.ComposeAggregateTestCheckFunc( | ||
resource.TestCheckResourceAttrSet("data.github_organization_ip_allow_list.all", "ip_allow_list.0.id"), | ||
resource.TestCheckResourceAttrSet("data.github_organization_ip_allow_list.all", "ip_allow_list.0.name"), | ||
resource.TestCheckResourceAttrSet("data.github_organization_ip_allow_list.all", "ip_allow_list.0.allow_list_value"), | ||
resource.TestCheckResourceAttrSet("data.github_organization_ip_allow_list.all", "ip_allow_list.0.is_active"), | ||
resource.TestCheckResourceAttrSet("data.github_organization_ip_allow_list.all", "ip_allow_list.0.created_at"), | ||
resource.TestCheckResourceAttrSet("data.github_organization_ip_allow_list.all", "ip_allow_list.0.updated_at"), | ||
) | ||
|
||
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) { | ||
t.Skip("individual account not supported for this operation") | ||
}) | ||
|
||
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,32 @@ | ||
--- | ||
layout: "github" | ||
page_title: "GitHub: github_organization_ip_allow_list" | ||
description: |- | ||
Get the IP allow list of an organization. | ||
--- | ||
|
||
# github_organization_ip_allow_list | ||
|
||
Use this data source to retrieve information about the IP allow list of an organization. | ||
The allow list for IP addresses will block access to private resources via the web, API, | ||
and Git from any IP addresses that are not on the allow list. | ||
|
||
## Example Usage | ||
|
||
```hcl | ||
data "github_organization_ip_allow_list" "all" {} | ||
``` | ||
|
||
## Attributes Reference | ||
|
||
* `ip_allow_list` - An Array of allowed IP addresses. | ||
___ | ||
|
||
Each element in the `ip_allow_list` block consists of: | ||
|
||
* `id` - The ID of the IP allow list entry. | ||
* `name` - The name of the IP allow list entry. | ||
* `allow_list_value` - A single IP address or range of IP addresses in CIDR notation. | ||
* `is_active` - Whether the entry is currently active. | ||
* `created_at` - Identifies the date and time when the object was created. | ||
* `updated_at` - Identifies the date and time when the object was last updated. |
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