github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/github/resource_github_repository_collaborator.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 resourceGithubRepositoryCollaborator() *schema.Resource {
     9  	return &schema.Resource{
    10  		Create: resourceGithubRepositoryCollaboratorCreate,
    11  		Read:   resourceGithubRepositoryCollaboratorRead,
    12  		// editing repository collaborators are not supported by github api so forcing new on any changes
    13  		Delete: resourceGithubRepositoryCollaboratorDelete,
    14  		Importer: &schema.ResourceImporter{
    15  			State: schema.ImportStatePassthrough,
    16  		},
    17  
    18  		Schema: map[string]*schema.Schema{
    19  			"username": &schema.Schema{
    20  				Type:     schema.TypeString,
    21  				Required: true,
    22  				ForceNew: true,
    23  			},
    24  			"repository": &schema.Schema{
    25  				Type:     schema.TypeString,
    26  				Required: true,
    27  				ForceNew: true,
    28  			},
    29  			"permission": &schema.Schema{
    30  				Type:         schema.TypeString,
    31  				Optional:     true,
    32  				ForceNew:     true,
    33  				Default:      "push",
    34  				ValidateFunc: validateValueFunc([]string{"pull", "push", "admin"}),
    35  			},
    36  		},
    37  	}
    38  }
    39  
    40  func resourceGithubRepositoryCollaboratorCreate(d *schema.ResourceData, meta interface{}) error {
    41  	client := meta.(*Organization).client
    42  	u := d.Get("username").(string)
    43  	r := d.Get("repository").(string)
    44  	p := d.Get("permission").(string)
    45  
    46  	_, err := client.Repositories.AddCollaborator(meta.(*Organization).name, r, u,
    47  		&github.RepositoryAddCollaboratorOptions{Permission: p})
    48  
    49  	if err != nil {
    50  		return err
    51  	}
    52  
    53  	d.SetId(buildTwoPartID(&r, &u))
    54  
    55  	return resourceGithubRepositoryCollaboratorRead(d, meta)
    56  }
    57  
    58  func resourceGithubRepositoryCollaboratorRead(d *schema.ResourceData, meta interface{}) error {
    59  	client := meta.(*Organization).client
    60  	r, u := parseTwoPartID(d.Id())
    61  
    62  	isCollaborator, _, err := client.Repositories.IsCollaborator(meta.(*Organization).name, r, u)
    63  
    64  	if !isCollaborator || err != nil {
    65  		d.SetId("")
    66  		return nil
    67  	}
    68  
    69  	collaborators, _, err := client.Repositories.ListCollaborators(meta.(*Organization).name, r,
    70  		&github.ListOptions{})
    71  
    72  	if err != nil {
    73  		return err
    74  	}
    75  
    76  	for _, c := range collaborators {
    77  		if *c.Login == u {
    78  			permName, err := getRepoPermission(c.Permissions)
    79  
    80  			if err != nil {
    81  				return err
    82  			}
    83  
    84  			d.Set("repository", r)
    85  			d.Set("username", u)
    86  			d.Set("permission", permName)
    87  
    88  			return nil
    89  		}
    90  	}
    91  
    92  	return nil
    93  }
    94  
    95  func resourceGithubRepositoryCollaboratorDelete(d *schema.ResourceData, meta interface{}) error {
    96  	client := meta.(*Organization).client
    97  	u := d.Get("username").(string)
    98  	r := d.Get("repository").(string)
    99  
   100  	_, err := client.Repositories.RemoveCollaborator(meta.(*Organization).name, r, u)
   101  
   102  	return err
   103  }