github.com/richardbowden/terraform@v0.6.12-0.20160901200758-30ea22c25211/builtin/providers/github/resource_github_team_membership.go (about)

     1  package github
     2  
     3  import (
     4  	"strings"
     5  
     6  	"github.com/google/go-github/github"
     7  	"github.com/hashicorp/terraform/helper/schema"
     8  )
     9  
    10  func resourceGithubTeamMembership() *schema.Resource {
    11  
    12  	return &schema.Resource{
    13  		Create: resourceGithubTeamMembershipCreate,
    14  		Read:   resourceGithubTeamMembershipRead,
    15  		// editing team memberships are not supported by github api so forcing new on any changes
    16  		Delete: resourceGithubTeamMembershipDelete,
    17  
    18  		Schema: map[string]*schema.Schema{
    19  			"team_id": &schema.Schema{
    20  				Type:     schema.TypeString,
    21  				Required: true,
    22  				ForceNew: true,
    23  			},
    24  			"username": &schema.Schema{
    25  				Type:     schema.TypeString,
    26  				Required: true,
    27  				ForceNew: true,
    28  			},
    29  			"role": &schema.Schema{
    30  				Type:         schema.TypeString,
    31  				Optional:     true,
    32  				ForceNew:     true,
    33  				Default:      "member",
    34  				ValidateFunc: validateValueFunc([]string{"member", "maintainer"}),
    35  			},
    36  		},
    37  	}
    38  }
    39  
    40  func resourceGithubTeamMembershipCreate(d *schema.ResourceData, meta interface{}) error {
    41  	client := meta.(*Organization).client
    42  	t := d.Get("team_id").(string)
    43  	n := d.Get("username").(string)
    44  	r := d.Get("role").(string)
    45  
    46  	_, _, err := client.Organizations.AddTeamMembership(toGithubID(t), n,
    47  		&github.OrganizationAddTeamMembershipOptions{Role: r})
    48  
    49  	if err != nil {
    50  		return err
    51  	}
    52  
    53  	d.SetId(buildTwoPartID(&t, &n))
    54  
    55  	return resourceGithubTeamMembershipRead(d, meta)
    56  }
    57  
    58  func resourceGithubTeamMembershipRead(d *schema.ResourceData, meta interface{}) error {
    59  	client := meta.(*Organization).client
    60  	t := d.Get("team_id").(string)
    61  	n := d.Get("username").(string)
    62  
    63  	membership, _, err := client.Organizations.GetTeamMembership(toGithubID(t), n)
    64  
    65  	if err != nil {
    66  		d.SetId("")
    67  		return nil
    68  	}
    69  	team, user := getTeamAndUserFromURL(membership.URL)
    70  
    71  	d.Set("username", user)
    72  	d.Set("role", membership.Role)
    73  	d.Set("team_id", team)
    74  	return nil
    75  }
    76  
    77  func resourceGithubTeamMembershipDelete(d *schema.ResourceData, meta interface{}) error {
    78  	client := meta.(*Organization).client
    79  	t := d.Get("team_id").(string)
    80  	n := d.Get("username").(string)
    81  
    82  	_, err := client.Organizations.RemoveTeamMembership(toGithubID(t), n)
    83  
    84  	return err
    85  }
    86  
    87  func getTeamAndUserFromURL(url *string) (string, string) {
    88  	var team, user string
    89  
    90  	urlSlice := strings.Split(*url, "/")
    91  	for v := range urlSlice {
    92  		if urlSlice[v] == "teams" {
    93  			team = urlSlice[v+1]
    94  		}
    95  		if urlSlice[v] == "memberships" {
    96  			user = urlSlice[v+1]
    97  		}
    98  	}
    99  	return team, user
   100  }