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

     1  // Copyright 2020 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  type OrganizationBlockGenerator struct {
    27  	GithubService
    28  }
    29  
    30  // Generate TerraformResources from Github API,
    31  func (g *OrganizationBlockGenerator) InitResources() error {
    32  	ctx := context.Background()
    33  	client, err := g.createClient()
    34  	if err != nil {
    35  		return err
    36  	}
    37  
    38  	owner := g.Args["owner"].(string)
    39  	g.Resources = append(g.Resources, createOrganizationBlocksResources(ctx, client, owner)...)
    40  
    41  	return nil
    42  }
    43  
    44  func createOrganizationBlocksResources(ctx context.Context, client *githubAPI.Client, owner string) []terraformutils.Resource {
    45  	resources := []terraformutils.Resource{}
    46  
    47  	opt := &githubAPI.ListOptions{PerPage: 100}
    48  
    49  	// List all organization blocks for the authenticated user
    50  	for {
    51  		blocks, resp, err := client.Organizations.ListBlockedUsers(ctx, owner, opt)
    52  		if err != nil {
    53  			log.Println(err)
    54  			return nil
    55  		}
    56  
    57  		for _, block := range blocks {
    58  			resource := terraformutils.NewSimpleResource(
    59  				block.GetLogin(),
    60  				block.GetLogin(),
    61  				"github_organization_block",
    62  				"github",
    63  				[]string{},
    64  			)
    65  			resource.SlowQueryRequired = true
    66  
    67  			resources = append(resources, resource)
    68  		}
    69  
    70  		if resp.NextPage == 0 {
    71  			break
    72  		}
    73  		opt.Page = resp.NextPage
    74  	}
    75  	return resources
    76  }