Skip to content

Commit

Permalink
Check team ID format at plan (#253)
Browse files Browse the repository at this point in the history
* Add validateTeamIDFunc function

* Add validation of teamID at plan time
  • Loading branch information
tgermain authored and tracypholmes committed Jul 16, 2019
1 parent 526ac62 commit bcd632e
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 6 deletions.
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

0 comments on commit bcd632e

Please sign in to comment.