github.com/GoogleCloudPlatform/terraformer@v0.8.18/providers/github/organization_project.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  	"strconv"
    21  
    22  	"github.com/GoogleCloudPlatform/terraformer/terraformutils"
    23  
    24  	githubAPI "github.com/google/go-github/v35/github"
    25  )
    26  
    27  type OrganizationProjectGenerator struct {
    28  	GithubService
    29  }
    30  
    31  // Generate TerraformResources from Github API,
    32  func (g *OrganizationProjectGenerator) 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, createOrganizationProjects(ctx, client, owner)...)
    41  
    42  	return nil
    43  }
    44  
    45  func createOrganizationProjects(ctx context.Context, client *githubAPI.Client, owner string) []terraformutils.Resource {
    46  	resources := []terraformutils.Resource{}
    47  
    48  	opt := &githubAPI.ProjectListOptions{
    49  		ListOptions: githubAPI.ListOptions{PerPage: 100},
    50  	}
    51  
    52  	// List all organization projects for the authenticated user
    53  	for {
    54  		projects, resp, err := client.Organizations.ListProjects(ctx, owner, opt)
    55  		if err != nil {
    56  			log.Println(err)
    57  			return nil
    58  		}
    59  
    60  		for _, project := range projects {
    61  			resource := terraformutils.NewSimpleResource(
    62  				strconv.FormatInt(project.GetID(), 10),
    63  				strconv.FormatInt(project.GetID(), 10),
    64  				"github_organization_project",
    65  				"github",
    66  				[]string{},
    67  			)
    68  			resource.SlowQueryRequired = true
    69  			resources = append(resources, resource)
    70  		}
    71  
    72  		if resp.NextPage == 0 {
    73  			break
    74  		}
    75  		opt.Page = resp.NextPage
    76  	}
    77  	return resources
    78  }