-
Notifications
You must be signed in to change notification settings - Fork 769
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Added data source for org security managers
Signed-off-by: Steve Hipwell <[email protected]>
- Loading branch information
1 parent
1c11053
commit 2023ffb
Showing
4 changed files
with
161 additions
and
0 deletions.
There are no files selected for viewing
84 changes: 84 additions & 0 deletions
84
github/data_source_github_organization_security_manager.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package github | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/google/go-github/v66/github" | ||
"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": { | ||
Type: schema.TypeInt, | ||
Computed: true, | ||
}, | ||
"slug": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"name": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"permission": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func dataSourceGithubOrganizationSecurityManagersRead(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*Owner).v3client | ||
ctx := context.Background() | ||
|
||
orgName := meta.(*Owner).name | ||
options := &github.ListIDPGroupsOptions{ | ||
ListCursorOptions: github.ListCursorOptions{ | ||
PerPage: maxPerPage, | ||
}, | ||
} | ||
|
||
allTeams := make([]interface{}, 0) | ||
for { | ||
teams, resp, 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) | ||
} | ||
|
||
if resp.NextPageToken == "" { | ||
break | ||
} | ||
options.Page = resp.NextPageToken | ||
} | ||
|
||
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
47
github/data_source_github_organization_security_manager_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
), | ||
}, | ||
}, | ||
}) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
website/docs/d/organization_security_managers.html.markdown
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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` - the ID of the team. | ||
* `slug` - the slug of the team. | ||
* `name` - the team's full name. | ||
* `permission` - the team's permission |