github.com/mohanarpit/terraform@v0.6.16-0.20160909104007-291f29853544/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  			"privacy": &schema.Schema{
    26  				Type:         schema.TypeString,
    27  				Optional:     true,
    28  				Default:      "secret",
    29  				ValidateFunc: validateValueFunc([]string{"secret", "closed"}),
    30  			},
    31  		},
    32  	}
    33  }
    34  
    35  func resourceGithubTeamCreate(d *schema.ResourceData, meta interface{}) error {
    36  	client := meta.(*Organization).client
    37  	n := d.Get("name").(string)
    38  	desc := d.Get("description").(string)
    39  	p := d.Get("privacy").(string)
    40  	githubTeam, _, err := client.Organizations.CreateTeam(meta.(*Organization).name, &github.Team{
    41  		Name:        &n,
    42  		Description: &desc,
    43  		Privacy:     &p,
    44  	})
    45  	if err != nil {
    46  		return err
    47  	}
    48  	d.SetId(fromGithubID(githubTeam.ID))
    49  	return resourceGithubTeamRead(d, meta)
    50  }
    51  
    52  func resourceGithubTeamRead(d *schema.ResourceData, meta interface{}) error {
    53  	client := meta.(*Organization).client
    54  
    55  	team, err := getGithubTeam(d, client)
    56  	if err != nil {
    57  		d.SetId("")
    58  		return nil
    59  	}
    60  	d.Set("description", team.Description)
    61  	d.Set("name", team.Name)
    62  	d.Set("privacy", team.Privacy)
    63  	return nil
    64  }
    65  
    66  func resourceGithubTeamUpdate(d *schema.ResourceData, meta interface{}) error {
    67  	client := meta.(*Organization).client
    68  	team, err := getGithubTeam(d, client)
    69  
    70  	if err != nil {
    71  		d.SetId("")
    72  		return nil
    73  	}
    74  
    75  	name := d.Get("name").(string)
    76  	description := d.Get("description").(string)
    77  	privacy := d.Get("privacy").(string)
    78  	team.Description = &description
    79  	team.Name = &name
    80  	team.Privacy = &privacy
    81  
    82  	team, _, err = client.Organizations.EditTeam(*team.ID, team)
    83  	if err != nil {
    84  		return err
    85  	}
    86  	d.SetId(fromGithubID(team.ID))
    87  	return resourceGithubTeamRead(d, meta)
    88  }
    89  
    90  func resourceGithubTeamDelete(d *schema.ResourceData, meta interface{}) error {
    91  	client := meta.(*Organization).client
    92  	id := toGithubID(d.Id())
    93  	_, err := client.Organizations.DeleteTeam(id)
    94  	return err
    95  }
    96  
    97  func getGithubTeam(d *schema.ResourceData, github *github.Client) (*github.Team, error) {
    98  	id := toGithubID(d.Id())
    99  	team, _, err := github.Organizations.GetTeam(id)
   100  	return team, err
   101  }