Skip to content

Commit 52377c4

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

File tree

4 files changed

+152
-0
lines changed

4 files changed

+152
-0
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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+
Description: "Unique identifier of the team.",
22+
Type: schema.TypeInt,
23+
Computed: true,
24+
},
25+
"slug": {
26+
Description: "Name based identifier of the team.",
27+
Type: schema.TypeString,
28+
Computed: true,
29+
},
30+
"name": {
31+
Description: "Name of the team.",
32+
Type: schema.TypeString,
33+
Computed: true,
34+
},
35+
"permission": {
36+
Description: "Permission that the team will have for its repositories.",
37+
Type: schema.TypeString,
38+
Computed: true,
39+
},
40+
},
41+
},
42+
},
43+
},
44+
}
45+
}
46+
47+
func dataSourceGithubOrganizationSecurityManagersRead(d *schema.ResourceData, meta interface{}) error {
48+
client := meta.(*Owner).v3client
49+
ctx := context.Background()
50+
orgName := meta.(*Owner).name
51+
52+
allTeams := make([]interface{}, 0)
53+
54+
teams, _, err := client.Organizations.ListSecurityManagerTeams(ctx, orgName)
55+
if err != nil {
56+
return err
57+
}
58+
59+
for _, team := range teams {
60+
t := map[string]any{
61+
"id": team.GetID(),
62+
"slug": team.GetSlug(),
63+
"name": team.GetName(),
64+
"permission": team.GetPermission(),
65+
}
66+
allTeams = append(allTeams, t)
67+
}
68+
69+
d.SetId(fmt.Sprintf("%s/github-org-security-managers", orgName))
70+
if err := d.Set("teams", allTeams); err != nil {
71+
return fmt.Errorf("error setting teams: %s", err)
72+
}
73+
74+
return nil
75+
}
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` - Unique identifier of the team.
27+
* `slug` - Name based identifier of the team.
28+
* `name` - Name of the team.
29+
* `permission` - Permission that the team will have for its repositories.

0 commit comments

Comments
 (0)