github.com/bradfeehan/terraform@v0.7.0-rc3.0.20170529055808-34b45c5ad841/builtin/providers/github/data_source_github_team.go (about)

     1  package github
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"log"
     7  	"strconv"
     8  
     9  	"github.com/google/go-github/github"
    10  	"github.com/hashicorp/terraform/helper/schema"
    11  )
    12  
    13  func dataSourceGithubTeam() *schema.Resource {
    14  	return &schema.Resource{
    15  		Read: dataSourceGithubTeamRead,
    16  
    17  		Schema: map[string]*schema.Schema{
    18  			"slug": &schema.Schema{
    19  				Type:     schema.TypeString,
    20  				Required: true,
    21  			},
    22  			"name": &schema.Schema{
    23  				Type:     schema.TypeString,
    24  				Computed: true,
    25  			},
    26  			"description": &schema.Schema{
    27  				Type:     schema.TypeString,
    28  				Computed: true,
    29  			},
    30  			"privacy": &schema.Schema{
    31  				Type:     schema.TypeString,
    32  				Computed: true,
    33  			},
    34  			"permission": &schema.Schema{
    35  				Type:     schema.TypeString,
    36  				Computed: true,
    37  			},
    38  		},
    39  	}
    40  }
    41  
    42  func dataSourceGithubTeamRead(d *schema.ResourceData, meta interface{}) error {
    43  	slug := d.Get("slug").(string)
    44  	log.Printf("[INFO] Refreshing Gitub Team: %s", slug)
    45  
    46  	client := meta.(*Organization).client
    47  
    48  	team, err := getGithubTeamBySlug(client, meta.(*Organization).name, slug)
    49  	if err != nil {
    50  		return err
    51  	}
    52  
    53  	d.SetId(strconv.Itoa(*team.ID))
    54  	d.Set("name", *team.Name)
    55  	d.Set("description", *team.Description)
    56  	d.Set("privacy", *team.Privacy)
    57  	d.Set("permission", *team.Permission)
    58  	d.Set("members_count", *team.MembersCount)
    59  	d.Set("repos_count", *team.ReposCount)
    60  
    61  	return nil
    62  }
    63  
    64  func getGithubTeamBySlug(client *github.Client, org string, slug string) (team *github.Team, err error) {
    65  	opt := &github.ListOptions{PerPage: 10}
    66  	for {
    67  		teams, resp, err := client.Organizations.ListTeams(context.TODO(), org, opt)
    68  		if err != nil {
    69  			return team, err
    70  		}
    71  
    72  		for _, t := range teams {
    73  			if *t.Slug == slug {
    74  				return t, nil
    75  			}
    76  		}
    77  
    78  		if resp.NextPage == 0 {
    79  			break
    80  		}
    81  		opt.Page = resp.NextPage
    82  	}
    83  
    84  	return team, fmt.Errorf("Could not find team with slug: %s", slug)
    85  }