Skip to content

Commit

Permalink
Merge pull request #82 from appilon/github-ip-ranges
Browse files Browse the repository at this point in the history
datasource/github_ip_ranges: implement ip datasource via the GH Meta api
  • Loading branch information
appilon authored Mar 29, 2018
2 parents 0efddbe + 05dd175 commit 8613fc6
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 15 deletions.
6 changes: 4 additions & 2 deletions github/config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package github

import (
"context"
"net/url"

"github.com/google/go-github/github"
Expand All @@ -15,8 +16,9 @@ type Config struct {
}

type Organization struct {
name string
client *github.Client
name string
client *github.Client
StopContext context.Context
}

// Client configures and returns a fully initialized GithubClient
Expand Down
55 changes: 55 additions & 0 deletions github/data_source_github_ip_ranges.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package github

import (
"github.com/hashicorp/terraform/helper/schema"
)

func dataSourceGithubIpRanges() *schema.Resource {
return &schema.Resource{
Read: dataSourceGithubIpRangesRead,

Schema: map[string]*schema.Schema{
"hooks": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"git": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"pages": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
// TODO: importer IPs coming once this is merged
// https://github.com/google/go-github/pull/881
},
}
}

func dataSourceGithubIpRangesRead(d *schema.ResourceData, meta interface{}) error {
org := meta.(*Organization)

api, _, err := org.client.APIMeta(org.StopContext)
if err != nil {
return err
}

if len(api.Hooks)+len(api.Git)+len(api.Pages) > 0 {
d.SetId("github-ip-ranges")
}
if len(api.Hooks) > 0 {
d.Set("hooks", api.Hooks)
}
if len(api.Git) > 0 {
d.Set("git", api.Git)
}
if len(api.Pages) > 0 {
d.Set("pages", api.Pages)
}

return nil
}
28 changes: 28 additions & 0 deletions github/data_source_github_ip_ranges_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package github

import (
"testing"

"github.com/hashicorp/terraform/helper/resource"
)

func TestAccGithubIpRangesDataSource_existing(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
},
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: `
data "github_ip_ranges" "test" {}
`,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet("data.github_ip_ranges.test", "hooks.#"),
resource.TestCheckResourceAttrSet("data.github_ip_ranges.test", "git.#"),
resource.TestCheckResourceAttrSet("data.github_ip_ranges.test", "pages.#"),
),
},
},
})
}
38 changes: 25 additions & 13 deletions github/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import (

// Provider returns a terraform.ResourceProvider.
func Provider() terraform.ResourceProvider {

var p *schema.Provider
// The actual provider
return &schema.Provider{
p = &schema.Provider{
Schema: map[string]*schema.Schema{
"token": &schema.Schema{
Type: schema.TypeString,
Expand Down Expand Up @@ -46,12 +46,15 @@ func Provider() terraform.ResourceProvider {
},

DataSourcesMap: map[string]*schema.Resource{
"github_user": dataSourceGithubUser(),
"github_team": dataSourceGithubTeam(),
"github_user": dataSourceGithubUser(),
"github_team": dataSourceGithubTeam(),
"github_ip_ranges": dataSourceGithubIpRanges(),
},

ConfigureFunc: providerConfigure,
}

p.ConfigureFunc = providerConfigure(p)

return p
}

var descriptions map[string]string
Expand All @@ -66,12 +69,21 @@ func init() {
}
}

func providerConfigure(d *schema.ResourceData) (interface{}, error) {
config := Config{
Token: d.Get("token").(string),
Organization: d.Get("organization").(string),
BaseURL: d.Get("base_url").(string),
}
func providerConfigure(p *schema.Provider) schema.ConfigureFunc {
return func(d *schema.ResourceData) (interface{}, error) {
config := Config{
Token: d.Get("token").(string),
Organization: d.Get("organization").(string),
BaseURL: d.Get("base_url").(string),
}

meta, err := config.Client()
if err != nil {
return nil, err
}

return config.Client()
meta.(*Organization).StopContext = p.StopContext()

return meta, nil
}
}
22 changes: 22 additions & 0 deletions website/docs/d/ip_ranges.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
---
layout: "github"
page_title: "Github: github_ip_ranges"
sidebar_current: "docs-github-datasource-ip-ranges"
description: |-
Get information on a Github's IP addresses.
---

# github_ip_ranges

Use this data source to retrieve information about a Github's IP addresses.
## Example Usage

```
data "github_ip_ranges" "test" {}
```

## Attributes Reference

* `hooks` - An Array of IP addresses in CIDR format specifying the addresses that incoming service hooks will originate from.
* `git` - An Array of IP addresses in CIDR format specifying the Git servers.
* `pages` - An Array of IP addresses in CIDR format specifying the A records for GitHub Pages.
3 changes: 3 additions & 0 deletions website/github.erb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
<li<%= sidebar_current("docs-github-datasource") %>>
<a href="#">Data Sources</a>
<ul class="nav nav-visible">
<li<%= sidebar_current("docs-github-datasource-ip-ranges") %>>
<a href="/docs/providers/github/d/ip_ranges.html">github_ip_ranges</a>
</li>
<li<%= sidebar_current("docs-github-datasource-user") %>>
<a href="/docs/providers/github/d/user.html">github_user</a>
</li>
Expand Down

0 comments on commit 8613fc6

Please sign in to comment.