github.com/richardbowden/terraform@v0.6.12-0.20160901200758-30ea22c25211/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  
    16  		Schema: map[string]*schema.Schema{
    17  			"username": &schema.Schema{
    18  				Type:     schema.TypeString,
    19  				Required: true,
    20  				ForceNew: true,
    21  			},
    22  			"role": &schema.Schema{
    23  				Type:         schema.TypeString,
    24  				Optional:     true,
    25  				ValidateFunc: validateValueFunc([]string{"member", "admin"}),
    26  				Default:      "member",
    27  			},
    28  		},
    29  	}
    30  }
    31  
    32  func resourceGithubMembershipCreate(d *schema.ResourceData, meta interface{}) error {
    33  	client := meta.(*Organization).client
    34  	n := d.Get("username").(string)
    35  	r := d.Get("role").(string)
    36  
    37  	membership, _, err := client.Organizations.EditOrgMembership(n, meta.(*Organization).name,
    38  		&github.Membership{Role: &r})
    39  	if err != nil {
    40  		return err
    41  	}
    42  
    43  	d.SetId(buildTwoPartID(membership.Organization.Login, membership.User.Login))
    44  
    45  	return resourceGithubMembershipRead(d, meta)
    46  }
    47  
    48  func resourceGithubMembershipRead(d *schema.ResourceData, meta interface{}) error {
    49  	client := meta.(*Organization).client
    50  
    51  	membership, _, err := client.Organizations.GetOrgMembership(d.Get("username").(string), meta.(*Organization).name)
    52  	if err != nil {
    53  		d.SetId("")
    54  		return nil
    55  	}
    56  
    57  	d.Set("username", membership.User.Login)
    58  	d.Set("role", membership.Role)
    59  	return nil
    60  }
    61  
    62  func resourceGithubMembershipUpdate(d *schema.ResourceData, meta interface{}) error {
    63  	client := meta.(*Organization).client
    64  	n := d.Get("username").(string)
    65  	r := d.Get("role").(string)
    66  
    67  	_, _, err := client.Organizations.EditOrgMembership(n, meta.(*Organization).name, &github.Membership{
    68  		Role: &r,
    69  	})
    70  	if err != nil {
    71  		return err
    72  	}
    73  	return nil
    74  }
    75  
    76  func resourceGithubMembershipDelete(d *schema.ResourceData, meta interface{}) error {
    77  	client := meta.(*Organization).client
    78  	n := d.Get("username").(string)
    79  
    80  	_, err := client.Organizations.RemoveOrgMembership(n, meta.(*Organization).name)
    81  
    82  	return err
    83  }