Skip to content

Commit

Permalink
(Fix integrations#883) Additionally expose ipv4/6 cidrs in github_ip_…
Browse files Browse the repository at this point in the history
…ranges data source

Fixes integrations#883
  • Loading branch information
rymir committed Aug 18, 2021
1 parent 97a100b commit e52dcd3
Show file tree
Hide file tree
Showing 2 changed files with 137 additions and 1 deletion.
126 changes: 125 additions & 1 deletion github/data_source_github_ip_ranges.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package github

import (
"fmt"
"net"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)

Expand Down Expand Up @@ -39,6 +42,66 @@ func dataSourceGithubIpRanges() *schema.Resource {
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"hooks_ipv4": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"git_ipv4": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"pages_ipv4": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"importer_ipv4": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"actions_ipv4": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"dependabot_ipv4": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"hooks_ipv6": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"git_ipv6": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"pages_ipv6": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"importer_ipv6": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"actions_ipv6": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"dependabot_ipv6": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
}
}
Expand All @@ -51,27 +114,88 @@ func dataSourceGithubIpRangesRead(d *schema.ResourceData, meta interface{}) erro
return err
}

if len(api.Hooks)+len(api.Git)+len(api.Pages)+len(api.Importer) > 0 {
cidrHooksIpv4, cidrHooksIpv6, err := splitIpv4Ipv6Cidrs(&api.Hooks)
if err != nil {
return err
}

cidrGitIpv4, cidrGitIpv6, err := splitIpv4Ipv6Cidrs(&api.Git)
if err != nil {
return err
}

cidrPagesIpv4, cidrPagesIpv6, err := splitIpv4Ipv6Cidrs(&api.Pages)
if err != nil {
return err
}

cidrImporterIpv4, cidrImporterIpv6, err := splitIpv4Ipv6Cidrs(&api.Importer)
if err != nil {
return err
}

cidrActionsIpv4, cidrActionsIpv6, err := splitIpv4Ipv6Cidrs(&api.Actions)
if err != nil {
return err
}

cidrDependabotIpv4, cidrDependabotIpv6, err := splitIpv4Ipv6Cidrs(&api.Dependabot)
if err != nil {
return err
}

if len(api.Hooks)+len(api.Git)+len(api.Pages)+len(api.Importer)+len(api.Actions)+len(api.Dependabot) > 0 {
d.SetId("github-ip-ranges")
}
if len(api.Hooks) > 0 {
d.Set("hooks", api.Hooks)
d.Set("hooks_ipv4", cidrHooksIpv4)
d.Set("hooks_ipv6", cidrHooksIpv6)
}
if len(api.Git) > 0 {
d.Set("git", api.Git)
d.Set("git_ipv4", cidrGitIpv4)
d.Set("git_ipv6", cidrGitIpv6)
}
if len(api.Pages) > 0 {
d.Set("pages", api.Pages)
d.Set("pages_ipv4", cidrPagesIpv4)
d.Set("pages_ipv6", cidrPagesIpv6)
}
if len(api.Importer) > 0 {
d.Set("importer", api.Importer)
d.Set("importer_ipv4", cidrImporterIpv4)
d.Set("importer_ipv6", cidrImporterIpv6)
}
if len(api.Actions) > 0 {
d.Set("actions", api.Actions)
d.Set("actions_ipv4", cidrActionsIpv4)
d.Set("actions_ipv6", cidrActionsIpv6)
}
if len(api.Dependabot) > 0 {
d.Set("dependabot", api.Dependabot)
d.Set("dependabot_ipv4", cidrDependabotIpv4)
d.Set("dependabot_ipv6", cidrDependabotIpv6)
}

return nil
}

func splitIpv4Ipv6Cidrs(cidrs *[]string) (*[]string, *[]string, error) {
cidrIpv4 := []string{}
cidrIpv6 := []string{}

for _, cidr := range *cidrs {
cidrHost, _, err := net.ParseCIDR(cidr)
if err != nil {
return nil, nil, fmt.Errorf("Failed parsing cidr %s (%v)", cidr, err)
}
if cidrHost.To4() != nil {
cidrIpv4 = append(cidrIpv4, cidr)
} else {
cidrIpv6 = append(cidrIpv6, cidr)
}
}

return &cidrIpv4, &cidrIpv6, nil
}
12 changes: 12 additions & 0 deletions github/data_source_github_ip_ranges_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,18 @@ func TestAccGithubIpRangesDataSource(t *testing.T) {
resource.TestCheckResourceAttrSet("data.github_ip_ranges.test", "importer.#"),
resource.TestCheckResourceAttrSet("data.github_ip_ranges.test", "actions.#"),
resource.TestCheckResourceAttrSet("data.github_ip_ranges.test", "dependabot.#"),
resource.TestCheckResourceAttrSet("data.github_ip_ranges.test", "hooks_ipv4.#"),
resource.TestCheckResourceAttrSet("data.github_ip_ranges.test", "git_ipv4.#"),
resource.TestCheckResourceAttrSet("data.github_ip_ranges.test", "pages_ipv4.#"),
resource.TestCheckResourceAttrSet("data.github_ip_ranges.test", "importer_ipv4.#"),
resource.TestCheckResourceAttrSet("data.github_ip_ranges.test", "actions_ipv4.#"),
resource.TestCheckResourceAttrSet("data.github_ip_ranges.test", "dependabot_ipv4.#"),
resource.TestCheckResourceAttrSet("data.github_ip_ranges.test", "hooks_ipv6.#"),
resource.TestCheckResourceAttrSet("data.github_ip_ranges.test", "git_ipv6.#"),
resource.TestCheckResourceAttrSet("data.github_ip_ranges.test", "pages_ipv6.#"),
resource.TestCheckResourceAttrSet("data.github_ip_ranges.test", "importer_ipv6.#"),
resource.TestCheckResourceAttrSet("data.github_ip_ranges.test", "actions_ipv6.#"),
resource.TestCheckResourceAttrSet("data.github_ip_ranges.test", "dependabot_ipv6.#"),
)

testCase := func(t *testing.T, mode string) {
Expand Down

0 comments on commit e52dcd3

Please sign in to comment.