github.com/federicobaldo/terraform@v0.6.15-0.20160323222747-b20f680cbf05/builtin/providers/github/resource_github_team.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 resourceGithubTeam() *schema.Resource {
     9  
    10  	return &schema.Resource{
    11  		Create: resourceGithubTeamCreate,
    12  		Read:   resourceGithubTeamRead,
    13  		Update: resourceGithubTeamUpdate,
    14  		Delete: resourceGithubTeamDelete,
    15  
    16  		Schema: map[string]*schema.Schema{
    17  			"name": &schema.Schema{
    18  				Type:     schema.TypeString,
    19  				Required: true,
    20  			},
    21  			"description": &schema.Schema{
    22  				Type:     schema.TypeString,
    23  				Optional: true,
    24  			},
    25  		},
    26  	}
    27  }
    28  
    29  func resourceGithubTeamCreate(d *schema.ResourceData, meta interface{}) error {
    30  	client := meta.(*Organization).client
    31  	n := d.Get("name").(string)
    32  	desc := d.Get("description").(string)
    33  	githubTeam, _, err := client.Organizations.CreateTeam(meta.(*Organization).name, &github.Team{
    34  		Name:        &n,
    35  		Description: &desc,
    36  	})
    37  	if err != nil {
    38  		return err
    39  	}
    40  	d.SetId(fromGithubID(githubTeam.ID))
    41  	return resourceGithubTeamRead(d, meta)
    42  }
    43  
    44  func resourceGithubTeamRead(d *schema.ResourceData, meta interface{}) error {
    45  	client := meta.(*Organization).client
    46  
    47  	team, err := getGithubTeam(d, client)
    48  	if err != nil {
    49  		d.SetId("")
    50  		return nil
    51  	}
    52  	d.Set("description", team.Description)
    53  	d.Set("name", team.Name)
    54  	return nil
    55  }
    56  
    57  func resourceGithubTeamUpdate(d *schema.ResourceData, meta interface{}) error {
    58  	client := meta.(*Organization).client
    59  	team, err := getGithubTeam(d, client)
    60  
    61  	if err != nil {
    62  		d.SetId("")
    63  		return nil
    64  	}
    65  
    66  	name := d.Get("name").(string)
    67  	description := d.Get("description").(string)
    68  	team.Description = &description
    69  	team.Name = &name
    70  
    71  	team, _, err = client.Organizations.EditTeam(*team.ID, team)
    72  	if err != nil {
    73  		return err
    74  	}
    75  	d.SetId(fromGithubID(team.ID))
    76  	return resourceGithubTeamRead(d, meta)
    77  }
    78  
    79  func resourceGithubTeamDelete(d *schema.ResourceData, meta interface{}) error {
    80  	client := meta.(*Organization).client
    81  	id := toGithubID(d.Id())
    82  	_, err := client.Organizations.DeleteTeam(id)
    83  	return err
    84  }
    85  
    86  func getGithubTeam(d *schema.ResourceData, github *github.Client) (*github.Team, error) {
    87  	id := toGithubID(d.Id())
    88  	team, _, err := github.Organizations.GetTeam(id)
    89  	return team, err
    90  }