Skip to content

Commit 9cb0a3e

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

4 files changed

Lines changed: 148 additions & 0 deletions
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package github
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
8+
)
9+
10+
func dataSourceGithubOrganizationSecurityManagers() *schema.Resource {
11+
return &schema.Resource{
12+
Read: dataSourceGithubOrganizationSecurityManagersRead,
13+
14+
Schema: map[string]*schema.Schema{
15+
"teams": {
16+
Type: schema.TypeList,
17+
Computed: true,
18+
Elem: &schema.Resource{
19+
Schema: map[string]*schema.Schema{
20+
"id": {
21+
Type: schema.TypeInt,
22+
Computed: true,
23+
},
24+
"slug": {
25+
Type: schema.TypeString,
26+
Computed: true,
27+
},
28+
"name": {
29+
Type: schema.TypeString,
30+
Computed: true,
31+
},
32+
"permission": {
33+
Type: schema.TypeString,
34+
Computed: true,
35+
},
36+
},
37+
},
38+
},
39+
},
40+
}
41+
}
42+
43+
func dataSourceGithubOrganizationSecurityManagersRead(d *schema.ResourceData, meta interface{}) error {
44+
client := meta.(*Owner).v3client
45+
ctx := context.Background()
46+
orgName := meta.(*Owner).name
47+
48+
allTeams := make([]interface{}, 0)
49+
50+
teams, _, err := client.Organizations.ListSecurityManagerTeams(ctx, orgName)
51+
if err != nil {
52+
return err
53+
}
54+
55+
for _, team := range teams {
56+
t := map[string]any{
57+
"id": team.GetID(),
58+
"slug": team.GetSlug(),
59+
"name": team.GetName(),
60+
"permission": team.GetPermission(),
61+
}
62+
allTeams = append(allTeams, t)
63+
}
64+
65+
d.SetId(fmt.Sprintf("%s/github-org-security-managers", orgName))
66+
if err := d.Set("teams", allTeams); err != nil {
67+
return fmt.Errorf("error setting teams: %s", err)
68+
}
69+
70+
return nil
71+
}
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)