Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

datasource/github_ip_ranges: implement ip datasource via the GH Meta api #82

Merged
merged 3 commits into from
Mar 29, 2018
Merged
Show file tree
Hide file tree
Changes from 2 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: 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
53 changes: 53 additions & 0 deletions github/data_source_github_ip_ranges.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
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},
},
},
}
}

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")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know what to set here? I suppose it doesn't matter in this case? Anyone let me know!

}
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