Skip to content

Commit 2023ffb

Browse files
committed
feat: Added data source for org security managers
Signed-off-by: Steve Hipwell <steve.hipwell@gmail.com>
1 parent 1c11053 commit 2023ffb

File tree

4 files changed

+161
-0
lines changed

4 files changed

+161
-0
lines changed
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package github
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
"github.com/google/go-github/v66/github"
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
9+
)
10+
11+
func dataSourceGithubOrganizationSecurityManagers() *schema.Resource {
12+
return &schema.Resource{
13+
Read: dataSourceGithubOrganizationSecurityManagersRead,
14+
15+
Schema: map[string]*schema.Schema{
16+
"teams": {
17+
Type: schema.TypeList,
18+
Computed: true,
19+
Elem: &schema.Resource{
20+
Schema: map[string]*schema.Schema{
21+
"id": {
22+
Type: schema.TypeInt,
23+
Computed: true,
24+
},
25+
"slug": {
26+
Type: schema.TypeString,
27+
Computed: true,
28+
},
29+
"name": {
30+
Type: schema.TypeString,
31+
Computed: true,
32+
},
33+
"permission": {
34+
Type: schema.TypeString,
35+
Computed: true,
36+
},
37+
},
38+
},
39+
},
40+
},
41+
}
42+
}
43+
44+
func dataSourceGithubOrganizationSecurityManagersRead(d *schema.ResourceData, meta interface{}) error {
45+
client := meta.(*Owner).v3client
46+
ctx := context.Background()
47+
48+
orgName := meta.(*Owner).name
49+
options := &github.ListIDPGroupsOptions{
50+
ListCursorOptions: github.ListCursorOptions{
51+
PerPage: maxPerPage,
52+
},
53+
}
54+
55+
allTeams := make([]interface{}, 0)
56+
for {
57+
teams, resp, err := client.Organizations.ListSecurityManagerTeams(ctx, orgName)
58+
if err != nil {
59+
return err
60+
}
61+
62+
for _, team := range teams {
63+
t := map[string]any{
64+
"id": team.GetID(),
65+
"slug": team.GetSlug(),
66+
"name": team.GetName(),
67+
"permission": team.GetPermission(),
68+
}
69+
allTeams = append(allTeams, t)
70+
}
71+
72+
if resp.NextPageToken == "" {
73+
break
74+
}
75+
options.Page = resp.NextPageToken
76+
}
77+
78+
d.SetId(fmt.Sprintf("%s/github-org-security-managers", orgName))
79+
if err := d.Set("teams", allTeams); err != nil {
80+
return fmt.Errorf("error setting teams: %s", err)
81+
}
82+
83+
return nil
84+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package github
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
9+
)
10+
11+
func TestAccDataSourceGithubOrganizationSecurityManagers(t *testing.T) {
12+
t.Run("get the organization security managers without error", func(t *testing.T) {
13+
randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum)
14+
teamName := fmt.Sprintf("tf-acc-%s", randomID)
15+
16+
config := fmt.Sprintf(`
17+
resource "github_team" "test" {
18+
name = "%s"
19+
}
20+
21+
resource "github_organization_security_manager" "test" {
22+
team_slug = github_team.test.slug
23+
}
24+
25+
data "github_organization_security_managers" "test" {
26+
depends_on = [
27+
github_organization_security_manager.test
28+
]
29+
}
30+
`, teamName)
31+
32+
resource.Test(t, resource.TestCase{
33+
PreCheck: func() { skipUnlessMode(t, organization) },
34+
Providers: testAccProviders,
35+
Steps: []resource.TestStep{
36+
{
37+
Config: config,
38+
Check: resource.ComposeTestCheckFunc(
39+
resource.TestCheckResourceAttrSet("data.github_organization_security_managers.test", "teams.#"),
40+
resource.TestCheckResourceAttr("data.github_organization_security_managers.test", "teams.#", "1"),
41+
resource.TestCheckResourceAttr("data.github_organization_security_managers.test", "teams.0.name", teamName),
42+
),
43+
},
44+
},
45+
})
46+
})
47+
}

github/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,7 @@ func Provider() *schema.Provider {
232232
"github_organization_custom_role": dataSourceGithubOrganizationCustomRole(),
233233
"github_organization_external_identities": dataSourceGithubOrganizationExternalIdentities(),
234234
"github_organization_ip_allow_list": dataSourceGithubOrganizationIpAllowList(),
235+
"github_organization_security_managers": dataSourceGithubOrganizationSecurityManagers(),
235236
"github_organization_team_sync_groups": dataSourceGithubOrganizationTeamSyncGroups(),
236237
"github_organization_teams": dataSourceGithubOrganizationTeams(),
237238
"github_organization_webhooks": dataSourceGithubOrganizationWebhooks(),
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
---
2+
layout: "github"
3+
page_title: "GitHub: github_organization_security_managers"
4+
description: |-
5+
Get the security managers for an organization.
6+
---
7+
8+
# github_organization_security_managers
9+
10+
Use this data source to retrieve the security managers for an organization.
11+
12+
## Example Usage
13+
14+
```hcl
15+
data "github_organization_security_managers" "test" {}
16+
```
17+
18+
## Attributes Reference
19+
20+
* `teams` - An list of GitHub teams. Each `team` block consists of the fields documented below.
21+
22+
___
23+
24+
The `team` block consists of:
25+
26+
* `id` - the ID of the team.
27+
* `slug` - the slug of the team.
28+
* `name` - the team's full name.
29+
* `permission` - the team's permission

0 commit comments

Comments
 (0)