github.com/mohanarpit/terraform@v0.6.16-0.20160909104007-291f29853544/builtin/providers/github/resource_github_team_repository.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 resourceGithubTeamRepository() *schema.Resource {
     9  	return &schema.Resource{
    10  		Create: resourceGithubTeamRepositoryCreate,
    11  		Read:   resourceGithubTeamRepositoryRead,
    12  		Update: resourceGithubTeamRepositoryUpdate,
    13  		Delete: resourceGithubTeamRepositoryDelete,
    14  
    15  		Schema: map[string]*schema.Schema{
    16  			"team_id": &schema.Schema{
    17  				Type:     schema.TypeString,
    18  				Required: true,
    19  				ForceNew: true,
    20  			},
    21  			"repository": &schema.Schema{
    22  				Type:     schema.TypeString,
    23  				Required: true,
    24  				ForceNew: true,
    25  			},
    26  			"permission": &schema.Schema{
    27  				Type:         schema.TypeString,
    28  				Optional:     true,
    29  				Default:      "pull",
    30  				ValidateFunc: validateValueFunc([]string{"pull", "push", "admin"}),
    31  			},
    32  		},
    33  	}
    34  }
    35  
    36  func resourceGithubTeamRepositoryCreate(d *schema.ResourceData, meta interface{}) error {
    37  	client := meta.(*Organization).client
    38  	t := d.Get("team_id").(string)
    39  	r := d.Get("repository").(string)
    40  	p := d.Get("permission").(string)
    41  
    42  	_, err := client.Organizations.AddTeamRepo(toGithubID(t), meta.(*Organization).name, r,
    43  		&github.OrganizationAddTeamRepoOptions{Permission: p})
    44  
    45  	if err != nil {
    46  		return err
    47  	}
    48  
    49  	d.SetId(buildTwoPartID(&t, &r))
    50  
    51  	return resourceGithubTeamRepositoryRead(d, meta)
    52  }
    53  
    54  func resourceGithubTeamRepositoryRead(d *schema.ResourceData, meta interface{}) error {
    55  	client := meta.(*Organization).client
    56  	t := d.Get("team_id").(string)
    57  	r := d.Get("repository").(string)
    58  
    59  	repo, _, repoErr := client.Organizations.IsTeamRepo(toGithubID(t), meta.(*Organization).name, r)
    60  
    61  	if repoErr != nil {
    62  		d.SetId("")
    63  		return nil
    64  	}
    65  
    66  	repositoryName := repo.Name
    67  
    68  	d.Set("team_id", t)
    69  	d.Set("repository", repositoryName)
    70  
    71  	permName, permErr := getRepoPermission(repo.Permissions)
    72  
    73  	if permErr != nil {
    74  		return permErr
    75  	}
    76  
    77  	d.Set("permission", permName)
    78  
    79  	return nil
    80  }
    81  
    82  func resourceGithubTeamRepositoryUpdate(d *schema.ResourceData, meta interface{}) error {
    83  	client := meta.(*Organization).client
    84  	t := d.Get("team_id").(string)
    85  	r := d.Get("repository").(string)
    86  	p := d.Get("permission").(string)
    87  
    88  	// the go-github library's AddTeamRepo method uses the add/update endpoint from Github API
    89  	_, err := client.Organizations.AddTeamRepo(toGithubID(t), meta.(*Organization).name, r,
    90  		&github.OrganizationAddTeamRepoOptions{Permission: p})
    91  
    92  	if err != nil {
    93  		return err
    94  	}
    95  	return resourceGithubTeamRepositoryRead(d, meta)
    96  }
    97  
    98  func resourceGithubTeamRepositoryDelete(d *schema.ResourceData, meta interface{}) error {
    99  	client := meta.(*Organization).client
   100  	t := d.Get("team_id").(string)
   101  	r := d.Get("repository").(string)
   102  
   103  	_, err := client.Organizations.RemoveTeamRepo(toGithubID(t), meta.(*Organization).name, r)
   104  
   105  	return err
   106  }