From 52377c42e5b3227c4fda0f7e3e19efdddcec86f8 Mon Sep 17 00:00:00 2001 From: Steve Hipwell Date: Thu, 28 Nov 2024 19:38:59 +0000 Subject: [PATCH] feat: Added data source for org security managers Signed-off-by: Steve Hipwell --- ...ce_github_organization_security_manager.go | 75 +++++++++++++++++++ ...thub_organization_security_manager_test.go | 47 ++++++++++++ github/provider.go | 1 + ...ganization_security_managers.html.markdown | 29 +++++++ 4 files changed, 152 insertions(+) create mode 100644 github/data_source_github_organization_security_manager.go create mode 100644 github/data_source_github_organization_security_manager_test.go create mode 100644 website/docs/d/organization_security_managers.html.markdown diff --git a/github/data_source_github_organization_security_manager.go b/github/data_source_github_organization_security_manager.go new file mode 100644 index 0000000000..3167f6b5d1 --- /dev/null +++ b/github/data_source_github_organization_security_manager.go @@ -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 +} diff --git a/github/data_source_github_organization_security_manager_test.go b/github/data_source_github_organization_security_manager_test.go new file mode 100644 index 0000000000..ce32902ed0 --- /dev/null +++ b/github/data_source_github_organization_security_manager_test.go @@ -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), + ), + }, + }, + }) + }) +} diff --git a/github/provider.go b/github/provider.go index a9a04d0474..a5bbdec8cf 100644 --- a/github/provider.go +++ b/github/provider.go @@ -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(), diff --git a/website/docs/d/organization_security_managers.html.markdown b/website/docs/d/organization_security_managers.html.markdown new file mode 100644 index 0000000000..3ebea2770d --- /dev/null +++ b/website/docs/d/organization_security_managers.html.markdown @@ -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.