Skip to content

Commit f9300e7

Browse files
committed
fixup! feat: Refactor team data sources to latest pattern
1 parent cf0a8e7 commit f9300e7

9 files changed

Lines changed: 72 additions & 41 deletions

docs/data-sources/team.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,4 +103,4 @@ Read-Only:
103103

104104
- `repo_id` (Number) ID of the repository.
105105
- `repo_name` (String) Name of the repository.
106-
- `role_name` (String) Role of the team for the repository; one of `admin`, `maintain`, `push`, `triage`, or `pull`.
106+
- `role_name` (String) Role the team has for the repository.

github/acc_checks_test.go

Lines changed: 42 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,55 @@ package github
22

33
import (
44
"fmt"
5-
"strings"
65

7-
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
8-
"github.com/hashicorp/terraform-plugin-testing/terraform"
6+
"github.com/hashicorp/terraform-plugin-testing/knownvalue"
97
)
108

11-
// checkCollectionItemAbsent checks that a collection attribute does not contain an item with a specific field value.
12-
func checkCollectionItemAbsent(resourceName, collectionAttr, field, forbidden string) resource.TestCheckFunc {
13-
return func(s *terraform.State) error {
14-
rs, ok := s.RootModule().Resources[resourceName]
15-
if !ok {
16-
return fmt.Errorf("resource not found: %s", resourceName)
17-
}
9+
var _ knownvalue.Check = setAbsent{}
1810

19-
prefix := collectionAttr + "."
20-
suffix := "." + field
11+
// setAbsent is a knownvalue.Check implementation that asserts that a specific value is not present in a set.
12+
type setAbsent struct {
13+
value []knownvalue.Check
14+
}
2115

22-
for k, v := range rs.Primary.Attributes {
23-
if !strings.HasPrefix(k, prefix) {
24-
continue
25-
}
26-
// skip collection metadata
27-
if strings.HasSuffix(k, ".#") || strings.HasSuffix(k, ".%") {
28-
continue
29-
}
30-
if strings.HasSuffix(k, suffix) && v == forbidden {
31-
return fmt.Errorf("%s contains forbidden %s=%q (key %s)", collectionAttr, field, forbidden, k)
16+
// CheckValue determines whether the passed value of type []any, and does not contain the expected value.
17+
func (v setAbsent) CheckValue(other any) error {
18+
otherVals, ok := other.([]any)
19+
if !ok {
20+
return fmt.Errorf("expected []any value for SetAbsent check, got: %T", other)
21+
}
22+
23+
for _, otherVal := range otherVals {
24+
match := true
25+
for _, check := range v.value {
26+
if err := check.CheckValue(otherVal); err != nil {
27+
match = false
28+
break
3229
}
3330
}
3431

35-
return nil
32+
if match {
33+
return fmt.Errorf("found unexpected value %s for SetAbsent check", v.String())
34+
}
35+
}
36+
37+
return nil
38+
}
39+
40+
// String returns the string representation of the value.
41+
func (v setAbsent) String() string {
42+
var setVals []string
43+
44+
for _, val := range v.value {
45+
setVals = append(setVals, val.String())
46+
}
47+
48+
return fmt.Sprintf("%s", setVals)
49+
}
50+
51+
// SetAbsent returns a knownvalue.Check for asserting the value defined by the []knownvalue.Check slice is not present in the set.
52+
func SetAbsent(value []knownvalue.Check) setAbsent {
53+
return setAbsent{
54+
value: value,
3655
}
3756
}

github/data_source_github_organization_repository_role.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66

77
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
88
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
9+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
910
)
1011

1112
func dataSourceGithubOrganizationRepositoryRole() *schema.Resource {
@@ -16,9 +17,10 @@ func dataSourceGithubOrganizationRepositoryRole() *schema.Resource {
1617

1718
Schema: map[string]*schema.Schema{
1819
"role_id": {
19-
Description: "ID of the organization repository role.",
20-
Type: schema.TypeInt,
21-
Required: true,
20+
Description: "ID of the organization repository role.",
21+
Type: schema.TypeInt,
22+
Required: true,
23+
ValidateDiagFunc: validation.ToDiagFunc(validation.IntAtLeast(1)),
2224
},
2325
"name": {
2426
Description: "Name of the organization repository role.",

github/data_source_github_organization_role.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66

77
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
88
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
9+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
910
)
1011

1112
func dataSourceGithubOrganizationRole() *schema.Resource {
@@ -16,9 +17,10 @@ func dataSourceGithubOrganizationRole() *schema.Resource {
1617

1718
Schema: map[string]*schema.Schema{
1819
"role_id": {
19-
Description: "ID of the organization role.",
20-
Type: schema.TypeInt,
21-
Required: true,
20+
Description: "ID of the organization role.",
21+
Type: schema.TypeInt,
22+
Required: true,
23+
ValidateDiagFunc: validation.ToDiagFunc(validation.IntAtLeast(1)),
2224
},
2325
"name": {
2426
Description: "Name of the organization role.",

github/data_source_github_organization_role_teams.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"github.com/google/go-github/v89/github"
88
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
99
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
10+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
1011
)
1112

1213
func dataSourceGithubOrganizationRoleTeams() *schema.Resource {
@@ -17,10 +18,11 @@ func dataSourceGithubOrganizationRoleTeams() *schema.Resource {
1718

1819
Schema: map[string]*schema.Schema{
1920
"role_id": {
20-
Description: "ID of the organization role.",
21-
Type: schema.TypeInt,
22-
Required: true,
23-
ForceNew: true,
21+
Description: "ID of the organization role.",
22+
Type: schema.TypeInt,
23+
Required: true,
24+
ForceNew: true,
25+
ValidateDiagFunc: validation.ToDiagFunc(validation.IntAtLeast(1)),
2426
},
2527
"teams": {
2628
Description: "Teams assigned to the organization role.",

github/data_source_github_organization_role_users.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"github.com/google/go-github/v89/github"
88
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
99
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
10+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
1011
)
1112

1213
func dataSourceGithubOrganizationRoleUsers() *schema.Resource {
@@ -17,10 +18,11 @@ func dataSourceGithubOrganizationRoleUsers() *schema.Resource {
1718

1819
Schema: map[string]*schema.Schema{
1920
"role_id": {
20-
Description: "ID of the organization role.",
21-
Type: schema.TypeInt,
22-
Required: true,
23-
ForceNew: true,
21+
Description: "ID of the organization role.",
22+
Type: schema.TypeInt,
23+
Required: true,
24+
ForceNew: true,
25+
ValidateDiagFunc: validation.ToDiagFunc(validation.IntAtLeast(1)),
2426
},
2527
"users": {
2628
Description: "Users assigned to the organization role.",

github/data_source_github_organization_teams_test.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,12 @@ data "github_organization_teams" "test" {
123123
"parent_team_slug": knownvalue.StringExact(""),
124124
}),
125125
})),
126+
statecheck.ExpectKnownValue("data.github_organization_teams.test", tfjsonpath.New("teams"), SetAbsent([]knownvalue.Check{
127+
knownvalue.MapPartial(map[string]knownvalue.Check{
128+
"slug": knownvalue.StringExact(team2.GetSlug()),
129+
}),
130+
})),
126131
},
127-
Check: checkCollectionItemAbsent("data.github_organization_teams.test", "teams", "slug", team2.GetSlug()),
128132
},
129133
},
130134
})

github/data_source_github_team.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ func dataSourceGithubTeam() *schema.Resource {
130130
Computed: true,
131131
},
132132
"role_name": {
133-
Description: "Role of the team for the repository; one of `admin`, `maintain`, `push`, `triage`, or `pull`.",
133+
Description: "Role the team has for the repository.",
134134
Type: schema.TypeString,
135135
Computed: true,
136136
},

templates/data-sources/team.md.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,4 @@ Read-Only:
6868

6969
- `repo_id` (Number) ID of the repository.
7070
- `repo_name` (String) Name of the repository.
71-
- `role_name` (String) Role of the team for the repository; one of `admin`, `maintain`, `push`, `triage`, or `pull`.
71+
- `role_name` (String) Role the team has for the repository.

0 commit comments

Comments
 (0)