github.com/GoogleCloudPlatform/terraformer@v0.8.18/providers/github/members.go (about)

     1  // Copyright 2018 The Terraformer Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package github
    16  
    17  import (
    18  	"context"
    19  	"log"
    20  
    21  	"github.com/GoogleCloudPlatform/terraformer/terraformutils"
    22  
    23  	githubAPI "github.com/google/go-github/v35/github"
    24  )
    25  
    26  // MembersGenerator holds GithubService struct of Terraform service information
    27  type MembersGenerator struct {
    28  	GithubService
    29  }
    30  
    31  // InitResources generates TerraformResources from Github API,
    32  func (g *MembersGenerator) InitResources() error {
    33  	ctx := context.Background()
    34  	client, err := g.createClient()
    35  	if err != nil {
    36  		return err
    37  	}
    38  
    39  	owner := g.Args["owner"].(string)
    40  	g.Resources = append(g.Resources, createMembershipsResources(ctx, client, owner)...)
    41  
    42  	return nil
    43  }
    44  
    45  func createMembershipsResources(ctx context.Context, client *githubAPI.Client, owner string) []terraformutils.Resource {
    46  	resources := []terraformutils.Resource{}
    47  
    48  	opt := &githubAPI.ListMembersOptions{
    49  		ListOptions: githubAPI.ListOptions{PerPage: 100},
    50  	}
    51  
    52  	// List all organization members for the authenticated user
    53  	for {
    54  		members, resp, err := client.Organizations.ListMembers(ctx, owner, opt)
    55  		if err != nil {
    56  			log.Println(err)
    57  			return nil
    58  		}
    59  
    60  		for _, member := range members {
    61  			resource := terraformutils.NewSimpleResource(
    62  				owner+":"+member.GetLogin(),
    63  				member.GetLogin(),
    64  				"github_membership",
    65  				"github",
    66  				[]string{},
    67  			)
    68  			resource.SlowQueryRequired = true
    69  
    70  			resources = append(resources, resource)
    71  		}
    72  
    73  		if resp.NextPage == 0 {
    74  			break
    75  		}
    76  		opt.Page = resp.NextPage
    77  	}
    78  
    79  	return resources
    80  }