github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/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  		Importer: &schema.ResourceImporter{
    18  			State: schema.ImportStatePassthrough,
    19  		},
    20  
    21  		Schema: map[string]*schema.Schema{
    22  			"team_id": &schema.Schema{
    23  				Type:     schema.TypeString,
    24  				Required: true,
    25  				ForceNew: true,
    26  			},
    27  			"username": &schema.Schema{
    28  				Type:     schema.TypeString,
    29  				Required: true,
    30  				ForceNew: true,
    31  			},
    32  			"role": &schema.Schema{
    33  				Type:         schema.TypeString,
    34  				Optional:     true,
    35  				ForceNew:     true,
    36  				Default:      "member",
    37  				ValidateFunc: validateValueFunc([]string{"member", "maintainer"}),
    38  			},
    39  		},
    40  	}
    41  }
    42  
    43  func resourceGithubTeamMembershipCreate(d *schema.ResourceData, meta interface{}) error {
    44  	client := meta.(*Organization).client
    45  	t := d.Get("team_id").(string)
    46  	n := d.Get("username").(string)
    47  	r := d.Get("role").(string)
    48  
    49  	_, _, err := client.Organizations.AddTeamMembership(toGithubID(t), n,
    50  		&github.OrganizationAddTeamMembershipOptions{Role: r})
    51  
    52  	if err != nil {
    53  		return err
    54  	}
    55  
    56  	d.SetId(buildTwoPartID(&t, &n))
    57  
    58  	return resourceGithubTeamMembershipRead(d, meta)
    59  }
    60  
    61  func resourceGithubTeamMembershipRead(d *schema.ResourceData, meta interface{}) error {
    62  	client := meta.(*Organization).client
    63  	t, n := parseTwoPartID(d.Id())
    64  
    65  	membership, _, err := client.Organizations.GetTeamMembership(toGithubID(t), n)
    66  
    67  	if err != nil {
    68  		d.SetId("")
    69  		return nil
    70  	}
    71  	team, user := getTeamAndUserFromURL(membership.URL)
    72  
    73  	d.Set("username", user)
    74  	d.Set("role", membership.Role)
    75  	d.Set("team_id", team)
    76  	return nil
    77  }
    78  
    79  func resourceGithubTeamMembershipDelete(d *schema.ResourceData, meta interface{}) error {
    80  	client := meta.(*Organization).client
    81  	t := d.Get("team_id").(string)
    82  	n := d.Get("username").(string)
    83  
    84  	_, err := client.Organizations.RemoveTeamMembership(toGithubID(t), n)
    85  
    86  	return err
    87  }
    88  
    89  func getTeamAndUserFromURL(url *string) (string, string) {
    90  	var team, user string
    91  
    92  	urlSlice := strings.Split(*url, "/")
    93  	for v := range urlSlice {
    94  		if urlSlice[v] == "teams" {
    95  			team = urlSlice[v+1]
    96  		}
    97  		if urlSlice[v] == "memberships" {
    98  			user = urlSlice[v+1]
    99  		}
   100  	}
   101  	return team, user
   102  }