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