github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/github/resource_github_team_membership.go (about)

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