-
Notifications
You must be signed in to change notification settings - Fork 766
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 52377c4
Showing
4 changed files
with
152 additions
and
0 deletions.
There are no files selected for viewing
75 changes: 75 additions & 0 deletions
75
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,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
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` - 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. |