Skip to content

Commit

Permalink
r/membership: Fix import crash on incorrect ID
Browse files Browse the repository at this point in the history
An incorrect two-part ID supplied to github_membership during import
results in a crash - this is because the import functionality is passing
this directly to the Read function of the resource, for which IDs are
programatically controlled and a panic there would be more telling of a
much more serious error worthy of panicking over.

This adds an internal validation function for two-part IDs, and wraps
the resource's import functionality to use it to validate incoming IDs
before passing them to Read.
  • Loading branch information
vancluever committed Feb 7, 2018
1 parent f531217 commit 6273c49
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion github/resource_github_membership.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package github

import (
"context"
"errors"
"strings"

"github.com/google/go-github/github"
"github.com/hashicorp/terraform/helper/schema"
Expand All @@ -15,7 +17,7 @@ func resourceGithubMembership() *schema.Resource {
Update: resourceGithubMembershipUpdate,
Delete: resourceGithubMembershipDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
State: resourceGithubMembershipImport,
},

Schema: map[string]*schema.Schema{
Expand Down Expand Up @@ -89,3 +91,19 @@ func resourceGithubMembershipDelete(d *schema.ResourceData, meta interface{}) er

return err
}

func resourceGithubMembershipImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
// All we do here is validate that the import string is in a correct enough
// format to be parsed. parseTwoPartID will panic if it's missing elements,
// and is used otherwise in places where that should never happen, so we want
// to keep it that way.
id := d.Id()
if id == "" {
return nil, errors.New("please supply an ID format matching organization:username")
}
parts := strings.Split(id, ":")
if len(parts) < 2 {
return nil, errors.New("incorrect ID format. Please supply an ID format matching organization:username")
}
return []*schema.ResourceData{d}, nil
}

0 comments on commit 6273c49

Please sign in to comment.