Skip to content

Commit

Permalink
Create data source for external groups (integrations#1185)
Browse files Browse the repository at this point in the history
* Initial commit of EMU data source

* Set groups in state

* Some renames

* Correct google/go-github client version to v45 in new data source

* Rename data source file to something more appropriate

* Set a sensible ID

* Add documentation for new data source

* Fix website formatting
  • Loading branch information
kfcampbell authored and kazaker committed Dec 28, 2022
1 parent 87f9339 commit dde1156
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 1 deletion.
1 change: 0 additions & 1 deletion github/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ func (c *Config) NewRESTClient(client *http.Client) (*github.Client, error) {
}

func (c *Config) ConfigureOwner(owner *Owner) (*Owner, error) {

ctx := context.Background()
owner.name = c.Owner
if owner.name == "" {
Expand Down
74 changes: 74 additions & 0 deletions github/data_source_github_external_groups.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package github

import (
"context"
"encoding/json"
"fmt"

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

func dataSourceGithubExternalGroups() *schema.Resource {
return &schema.Resource{
Read: dataSourceGithubExternalGroupsRead,
Schema: map[string]*schema.Schema{
"external_groups": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"group_id": {
Type: schema.TypeInt,
Computed: true,
},
"group_name": {
Type: schema.TypeString,
Computed: true,
},
"updated_at": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
},
}
}

func dataSourceGithubExternalGroupsRead(d *schema.ResourceData, meta interface{}) error {
err := checkOrganization(meta)
if err != nil {
return err
}
client := meta.(*Owner).v3client
orgName := meta.(*Owner).name

ctx := context.WithValue(context.Background(), ctxId, d.Id())
opts := &github.ListExternalGroupsOptions{}

externalGroups, _, err := client.Teams.ListExternalGroups(ctx, orgName, opts)
if err != nil {
return err
}

// convert to JSON in order to martial to format we can return
jsonGroups, err := json.Marshal(externalGroups.Groups)
if err != nil {
return err
}

groupsState := make([]map[string]interface{}, 0)
err = json.Unmarshal(jsonGroups, &groupsState)
if err != nil {
return err
}

if err := d.Set("external_groups", groupsState); err != nil {
return err
}

d.SetId(fmt.Sprintf("/orgs/%v/external-groups", orgName))
return nil
}
1 change: 1 addition & 0 deletions github/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ func Provider() terraform.ResourceProvider {
"github_branch": dataSourceGithubBranch(),
"github_collaborators": dataSourceGithubCollaborators(),
"github_dependabot_public_key": dataSourceGithubDependabotPublicKey(),
"github_external_groups": dataSourceGithubExternalGroups(),
"github_ip_ranges": dataSourceGithubIpRanges(),
"github_membership": dataSourceGithubMembership(),
"github_organization": dataSourceGithubOrganization(),
Expand Down
40 changes: 40 additions & 0 deletions website/docs/d/external_groups.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
---
layout: "github"
page_title: "GitHub: github_external_group"
description: |-
Retrieve external groups belonging to an organization.
---

# github\_external\_group

Use this data source to retrieve external groups belonging to an organization.

## Example Usage

```hcl
data "github_external_groups" "example_external_groups" {}
locals {
local_groups = "${data.github_external_groups.example_external_groups}"
}
output "groups" {
value = local.local_groups
}
```

## Argument Reference

N/A. This resource will retrieve all the external groups belonging to an organization.

## Attributes Reference

* `external_groups` - an array of external groups belonging to the organization. Each group consists of the fields documented below.

___


* `group_id` - the ID of the group.
* `group_name` - the name of the group.
* `updated_at` - the date the group was last updated.

3 changes: 3 additions & 0 deletions website/github.erb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
<li>
<a href="/docs/providers/github/d/collaborators.html">github_collaborators</a>
</li>
<li>
<a href="/docs/providers/github/d/external_groups.html">github_external_groups</a>
</li>
<li>
<a href="/docs/providers/github/d/ip_ranges.html">github_ip_ranges</a>
</li>
Expand Down

0 comments on commit dde1156

Please sign in to comment.