github.com/koding/terraform@v0.6.4-0.20170608090606-5d7e0339779d/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  
    59  	return nil
    60  }
    61  
    62  func getGithubTeamBySlug(client *github.Client, org string, slug string) (team *github.Team, err error) {
    63  	opt := &github.ListOptions{PerPage: 10}
    64  	for {
    65  		teams, resp, err := client.Organizations.ListTeams(context.TODO(), org, opt)
    66  		if err != nil {
    67  			return team, err
    68  		}
    69  
    70  		for _, t := range teams {
    71  			if *t.Slug == slug {
    72  				return t, nil
    73  			}
    74  		}
    75  
    76  		if resp.NextPage == 0 {
    77  			break
    78  		}
    79  		opt.Page = resp.NextPage
    80  	}
    81  
    82  	return team, fmt.Errorf("Could not find team with slug: %s", slug)
    83  }