github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/github/resource_github_membership.go (about)

     1  package github
     2  
     3  import (
     4  	"github.com/google/go-github/github"
     5  	"github.com/hashicorp/terraform/helper/schema"
     6  )
     7  
     8  func resourceGithubMembership() *schema.Resource {
     9  
    10  	return &schema.Resource{
    11  		Create: resourceGithubMembershipCreate,
    12  		Read:   resourceGithubMembershipRead,
    13  		Update: resourceGithubMembershipUpdate,
    14  		Delete: resourceGithubMembershipDelete,
    15  		Importer: &schema.ResourceImporter{
    16  			State: schema.ImportStatePassthrough,
    17  		},
    18  
    19  		Schema: map[string]*schema.Schema{
    20  			"username": &schema.Schema{
    21  				Type:     schema.TypeString,
    22  				Required: true,
    23  				ForceNew: true,
    24  			},
    25  			"role": &schema.Schema{
    26  				Type:         schema.TypeString,
    27  				Optional:     true,
    28  				ValidateFunc: validateValueFunc([]string{"member", "admin"}),
    29  				Default:      "member",
    30  			},
    31  		},
    32  	}
    33  }
    34  
    35  func resourceGithubMembershipCreate(d *schema.ResourceData, meta interface{}) error {
    36  	client := meta.(*Organization).client
    37  	n := d.Get("username").(string)
    38  	r := d.Get("role").(string)
    39  
    40  	membership, _, err := client.Organizations.EditOrgMembership(n, meta.(*Organization).name,
    41  		&github.Membership{Role: &r})
    42  	if err != nil {
    43  		return err
    44  	}
    45  
    46  	d.SetId(buildTwoPartID(membership.Organization.Login, membership.User.Login))
    47  
    48  	return resourceGithubMembershipRead(d, meta)
    49  }
    50  
    51  func resourceGithubMembershipRead(d *schema.ResourceData, meta interface{}) error {
    52  	client := meta.(*Organization).client
    53  	_, n := parseTwoPartID(d.Id())
    54  
    55  	membership, _, err := client.Organizations.GetOrgMembership(n, meta.(*Organization).name)
    56  	if err != nil {
    57  		d.SetId("")
    58  		return nil
    59  	}
    60  
    61  	d.Set("username", membership.User.Login)
    62  	d.Set("role", membership.Role)
    63  	return nil
    64  }
    65  
    66  func resourceGithubMembershipUpdate(d *schema.ResourceData, meta interface{}) error {
    67  	client := meta.(*Organization).client
    68  	n := d.Get("username").(string)
    69  	r := d.Get("role").(string)
    70  
    71  	membership, _, err := client.Organizations.EditOrgMembership(n, meta.(*Organization).name, &github.Membership{
    72  		Role: &r,
    73  	})
    74  	if err != nil {
    75  		return err
    76  	}
    77  	d.SetId(buildTwoPartID(membership.Organization.Login, membership.User.Login))
    78  
    79  	return nil
    80  }
    81  
    82  func resourceGithubMembershipDelete(d *schema.ResourceData, meta interface{}) error {
    83  	client := meta.(*Organization).client
    84  	n := d.Get("username").(string)
    85  
    86  	_, err := client.Organizations.RemoveOrgMembership(n, meta.(*Organization).name)
    87  
    88  	return err
    89  }