github.com/nathanielks/terraform@v0.6.1-0.20170509030759-13e1a62319dc/builtin/providers/github/resource_github_team.go (about)

     1  package github
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/google/go-github/github"
     7  	"github.com/hashicorp/terraform/helper/schema"
     8  )
     9  
    10  func resourceGithubTeam() *schema.Resource {
    11  
    12  	return &schema.Resource{
    13  		Create: resourceGithubTeamCreate,
    14  		Read:   resourceGithubTeamRead,
    15  		Update: resourceGithubTeamUpdate,
    16  		Delete: resourceGithubTeamDelete,
    17  		Importer: &schema.ResourceImporter{
    18  			State: schema.ImportStatePassthrough,
    19  		},
    20  
    21  		Schema: map[string]*schema.Schema{
    22  			"name": {
    23  				Type:     schema.TypeString,
    24  				Required: true,
    25  			},
    26  			"description": {
    27  				Type:     schema.TypeString,
    28  				Optional: true,
    29  			},
    30  			"privacy": {
    31  				Type:         schema.TypeString,
    32  				Optional:     true,
    33  				Default:      "secret",
    34  				ValidateFunc: validateValueFunc([]string{"secret", "closed"}),
    35  			},
    36  		},
    37  	}
    38  }
    39  
    40  func resourceGithubTeamCreate(d *schema.ResourceData, meta interface{}) error {
    41  	client := meta.(*Organization).client
    42  	n := d.Get("name").(string)
    43  	desc := d.Get("description").(string)
    44  	p := d.Get("privacy").(string)
    45  	githubTeam, _, err := client.Organizations.CreateTeam(context.TODO(), meta.(*Organization).name, &github.Team{
    46  		Name:        &n,
    47  		Description: &desc,
    48  		Privacy:     &p,
    49  	})
    50  	if err != nil {
    51  		return err
    52  	}
    53  	d.SetId(fromGithubID(githubTeam.ID))
    54  	return resourceGithubTeamRead(d, meta)
    55  }
    56  
    57  func resourceGithubTeamRead(d *schema.ResourceData, meta interface{}) error {
    58  	client := meta.(*Organization).client
    59  
    60  	team, err := getGithubTeam(d, client)
    61  	if err != nil {
    62  		d.SetId("")
    63  		return nil
    64  	}
    65  	d.Set("description", team.Description)
    66  	d.Set("name", team.Name)
    67  	d.Set("privacy", team.Privacy)
    68  	return nil
    69  }
    70  
    71  func resourceGithubTeamUpdate(d *schema.ResourceData, meta interface{}) error {
    72  	client := meta.(*Organization).client
    73  	team, err := getGithubTeam(d, client)
    74  
    75  	if err != nil {
    76  		d.SetId("")
    77  		return nil
    78  	}
    79  
    80  	name := d.Get("name").(string)
    81  	description := d.Get("description").(string)
    82  	privacy := d.Get("privacy").(string)
    83  	team.Description = &description
    84  	team.Name = &name
    85  	team.Privacy = &privacy
    86  
    87  	team, _, err = client.Organizations.EditTeam(context.TODO(), *team.ID, team)
    88  	if err != nil {
    89  		return err
    90  	}
    91  	d.SetId(fromGithubID(team.ID))
    92  	return resourceGithubTeamRead(d, meta)
    93  }
    94  
    95  func resourceGithubTeamDelete(d *schema.ResourceData, meta interface{}) error {
    96  	client := meta.(*Organization).client
    97  	id := toGithubID(d.Id())
    98  	_, err := client.Organizations.DeleteTeam(context.TODO(), id)
    99  	return err
   100  }
   101  
   102  func getGithubTeam(d *schema.ResourceData, github *github.Client) (*github.Team, error) {
   103  	id := toGithubID(d.Id())
   104  	team, _, err := github.Organizations.GetTeam(context.TODO(), id)
   105  	return team, err
   106  }