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

Check team ID format at plan #253

Merged
merged 2 commits into from
Jul 16, 2019
Merged
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
7 changes: 4 additions & 3 deletions github/resource_github_team_membership.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ func resourceGithubTeamMembership() *schema.Resource {

Schema: map[string]*schema.Schema{
"team_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validateTeamIDFunc,
},
"username": {
Type: schema.TypeString,
Expand Down
7 changes: 4 additions & 3 deletions github/resource_github_team_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ func resourceGithubTeamRepository() *schema.Resource {

Schema: map[string]*schema.Schema{
"team_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validateTeamIDFunc,
},
"repository": {
Type: schema.TypeString,
Expand Down
14 changes: 14 additions & 0 deletions github/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package github

import (
"fmt"
"strconv"
"strings"

"github.com/hashicorp/terraform/helper/schema"
Expand Down Expand Up @@ -91,3 +92,16 @@ func (e *unconvertibleIdError) Error() string {
return fmt.Sprintf("Unexpected ID format (%q), expected numerical ID. %s",
e.OriginalId, e.OriginalError.Error())
}

func validateTeamIDFunc(v interface{}, keyName string) (we []string, errors []error) {
teamIDString, ok := v.(string)
if !ok {
return nil, []error{fmt.Errorf("expected type of %s to be string", keyName)}
}
// Check that the team ID can be converted to an int
if _, err := strconv.ParseInt(teamIDString, 10, 64); err != nil {
return nil, []error{unconvertibleIdErr(teamIDString, err)}
}

return
}
31 changes: 31 additions & 0 deletions github/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,37 @@ import (
"unicode"
)

func TestAccValidateTeamIDFunc(t *testing.T) {
// warnings, errors := validateTeamIDFunc(interface{"1234567"})

cases := []struct {
TeamID interface{}
ErrCount int
}{
{

TeamID: "1234567",
ErrCount: 0,
},
{
// an int cannot be cast to a string
TeamID: 1234567,
ErrCount: 1,
},
{
TeamID: "notAnInt",
ErrCount: 1,
},
}

for _, tc := range cases {
_, errors := validateTeamIDFunc(tc.TeamID, "keyName")
if len(errors) != tc.ErrCount {
t.Fatalf("Expected %d validation error but got %d", tc.ErrCount, len(errors))
}
}
}

func TestAccGithubUtilRole_validation(t *testing.T) {
cases := []struct {
Value string
Expand Down