github.com/andresvia/terraform@v0.6.15-0.20160412045437-d51c75946785/builtin/providers/github/resource_github_team_repository.go (about)

     1  package github
     2  
     3  import (
     4  	"errors"
     5  
     6  	"github.com/google/go-github/github"
     7  	"github.com/hashicorp/terraform/helper/schema"
     8  )
     9  
    10  const pullPermission string = "pull"
    11  const pushPermission string = "push"
    12  const adminPermission string = "admin"
    13  
    14  func resourceGithubTeamRepository() *schema.Resource {
    15  	return &schema.Resource{
    16  		Create: resourceGithubTeamRepositoryCreate,
    17  		Read:   resourceGithubTeamRepositoryRead,
    18  		Update: resourceGithubTeamRepositoryUpdate,
    19  		Delete: resourceGithubTeamRepositoryDelete,
    20  
    21  		Schema: map[string]*schema.Schema{
    22  			"team_id": &schema.Schema{
    23  				Type:     schema.TypeString,
    24  				Required: true,
    25  				ForceNew: true,
    26  			},
    27  			"repository": &schema.Schema{
    28  				Type:     schema.TypeString,
    29  				Required: true,
    30  				ForceNew: true,
    31  			},
    32  			"permission": &schema.Schema{
    33  				Type:         schema.TypeString,
    34  				Optional:     true,
    35  				Default:      "pull",
    36  				ValidateFunc: validateValueFunc([]string{"pull", "push", "admin"}),
    37  			},
    38  		},
    39  	}
    40  }
    41  
    42  func resourceGithubTeamRepositoryCreate(d *schema.ResourceData, meta interface{}) error {
    43  	client := meta.(*Organization).client
    44  	t := d.Get("team_id").(string)
    45  	r := d.Get("repository").(string)
    46  	p := d.Get("permission").(string)
    47  
    48  	_, err := client.Organizations.AddTeamRepo(toGithubID(t), meta.(*Organization).name, r,
    49  		&github.OrganizationAddTeamRepoOptions{Permission: p})
    50  
    51  	if err != nil {
    52  		return err
    53  	}
    54  
    55  	d.SetId(buildTwoPartID(&t, &r))
    56  
    57  	return resourceGithubTeamRepositoryRead(d, meta)
    58  }
    59  
    60  func resourceGithubTeamRepositoryRead(d *schema.ResourceData, meta interface{}) error {
    61  	client := meta.(*Organization).client
    62  	t := d.Get("team_id").(string)
    63  	r := d.Get("repository").(string)
    64  
    65  	repo, _, repoErr := client.Organizations.IsTeamRepo(toGithubID(t), meta.(*Organization).name, r)
    66  
    67  	if repoErr != nil {
    68  		d.SetId("")
    69  		return nil
    70  	}
    71  
    72  	repositoryName := repo.Name
    73  
    74  	d.Set("team_id", t)
    75  	d.Set("repository", repositoryName)
    76  
    77  	permName, permErr := getRepoPermission(repo.Permissions)
    78  
    79  	if permErr != nil {
    80  		return permErr
    81  	}
    82  
    83  	d.Set("permission", permName)
    84  
    85  	return nil
    86  }
    87  
    88  func resourceGithubTeamRepositoryUpdate(d *schema.ResourceData, meta interface{}) error {
    89  	client := meta.(*Organization).client
    90  	t := d.Get("team_id").(string)
    91  	r := d.Get("repository").(string)
    92  	p := d.Get("permission").(string)
    93  
    94  	// the go-github library's AddTeamRepo method uses the add/update endpoint from Github API
    95  	_, err := client.Organizations.AddTeamRepo(toGithubID(t), meta.(*Organization).name, r,
    96  		&github.OrganizationAddTeamRepoOptions{Permission: p})
    97  
    98  	if err != nil {
    99  		return err
   100  	}
   101  	return resourceGithubTeamRepositoryRead(d, meta)
   102  }
   103  
   104  func resourceGithubTeamRepositoryDelete(d *schema.ResourceData, meta interface{}) error {
   105  	client := meta.(*Organization).client
   106  	t := d.Get("team_id").(string)
   107  	r := d.Get("repository").(string)
   108  
   109  	_, err := client.Organizations.RemoveTeamRepo(toGithubID(t), meta.(*Organization).name, r)
   110  
   111  	return err
   112  }
   113  
   114  func getRepoPermission(p *map[string]bool) (string, error) {
   115  
   116  	// Permissions are returned in this map format such that if you have a certain level
   117  	// of permission, all levels below are also true. For example, if a team has push
   118  	// permission, the map will be: {"pull": true, "push": true, "admin": false}
   119  	if (*p)[adminPermission] {
   120  		return adminPermission, nil
   121  	} else if (*p)[pushPermission] {
   122  		return pushPermission, nil
   123  	} else {
   124  		if (*p)[pullPermission] {
   125  			return pullPermission, nil
   126  		}
   127  		return "", errors.New("At least one permission expected from permissions map.")
   128  	}
   129  }