Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Added data source for org security managers #2486

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions github/data_source_github_organization_security_manager.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package github

import (
"context"
"fmt"

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

func dataSourceGithubOrganizationSecurityManagers() *schema.Resource {
return &schema.Resource{
Read: dataSourceGithubOrganizationSecurityManagersRead,

Schema: map[string]*schema.Schema{
"teams": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Description: "Unique identifier of the team.",
Type: schema.TypeInt,
Computed: true,
},
"slug": {
Description: "Name based identifier of the team.",
Type: schema.TypeString,
Computed: true,
},
"name": {
Description: "Name of the team.",
Type: schema.TypeString,
Computed: true,
},
"permission": {
Description: "Permission that the team will have for its repositories.",
Type: schema.TypeString,
Computed: true,
},
},
},
},
},
}
}

func dataSourceGithubOrganizationSecurityManagersRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*Owner).v3client
ctx := context.Background()
orgName := meta.(*Owner).name

allTeams := make([]interface{}, 0)

teams, _, err := client.Organizations.ListSecurityManagerTeams(ctx, orgName)
if err != nil {
return err
}

for _, team := range teams {
t := map[string]any{
"id": team.GetID(),
"slug": team.GetSlug(),
"name": team.GetName(),
"permission": team.GetPermission(),
}
allTeams = append(allTeams, t)
}

d.SetId(fmt.Sprintf("%s/github-org-security-managers", orgName))
if err := d.Set("teams", allTeams); err != nil {
return fmt.Errorf("error setting teams: %s", err)
}

return nil
}
47 changes: 47 additions & 0 deletions github/data_source_github_organization_security_manager_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package github

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

func TestAccDataSourceGithubOrganizationSecurityManagers(t *testing.T) {
t.Run("get the organization security managers without error", func(t *testing.T) {
randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum)
teamName := fmt.Sprintf("tf-acc-%s", randomID)

config := fmt.Sprintf(`
resource "github_team" "test" {
name = "%s"
}

resource "github_organization_security_manager" "test" {
team_slug = github_team.test.slug
}

data "github_organization_security_managers" "test" {
depends_on = [
github_organization_security_manager.test
]
}
`, teamName)

resource.Test(t, resource.TestCase{
PreCheck: func() { skipUnlessMode(t, organization) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: config,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet("data.github_organization_security_managers.test", "teams.#"),
resource.TestCheckResourceAttr("data.github_organization_security_managers.test", "teams.#", "1"),
resource.TestCheckResourceAttr("data.github_organization_security_managers.test", "teams.0.name", teamName),
),
},
},
})
})
}
1 change: 1 addition & 0 deletions github/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ func Provider() *schema.Provider {
"github_organization_custom_role": dataSourceGithubOrganizationCustomRole(),
"github_organization_external_identities": dataSourceGithubOrganizationExternalIdentities(),
"github_organization_ip_allow_list": dataSourceGithubOrganizationIpAllowList(),
"github_organization_security_managers": dataSourceGithubOrganizationSecurityManagers(),
"github_organization_team_sync_groups": dataSourceGithubOrganizationTeamSyncGroups(),
"github_organization_teams": dataSourceGithubOrganizationTeams(),
"github_organization_webhooks": dataSourceGithubOrganizationWebhooks(),
Expand Down
29 changes: 29 additions & 0 deletions website/docs/d/organization_security_managers.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
---
layout: "github"
page_title: "GitHub: github_organization_security_managers"
description: |-
Get the security managers for an organization.
---

# github_organization_security_managers

Use this data source to retrieve the security managers for an organization.

## Example Usage

```hcl
data "github_organization_security_managers" "test" {}
```

## Attributes Reference

* `teams` - An list of GitHub teams. Each `team` block consists of the fields documented below.

___

The `team` block consists of:

* `id` - Unique identifier of the team.
* `slug` - Name based identifier of the team.
* `name` - Name of the team.
* `permission` - Permission that the team will have for its repositories.
Loading