github.com/GoogleCloudPlatform/terraformer@v0.8.18/providers/github/user_ssh_keys.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 UserSSHKeyGenerator struct {
    28  	GithubService
    29  }
    30  
    31  // Generate TerraformResources from Github API,
    32  func (g *UserSSHKeyGenerator) InitResources() error {
    33  	ctx := context.Background()
    34  	client, err := g.createClient()
    35  	if err != nil {
    36  		return err
    37  	}
    38  
    39  	opt := &githubAPI.ListOptions{PerPage: 100}
    40  
    41  	// List all ssh keys for the authenticated user
    42  	for {
    43  		keys, resp, err := client.Users.ListKeys(ctx, "", opt)
    44  		if err != nil {
    45  			log.Println(err)
    46  			return nil
    47  		}
    48  
    49  		for _, key := range keys {
    50  			resource := terraformutils.NewSimpleResource(
    51  				strconv.FormatInt(key.GetID(), 10),
    52  				strconv.FormatInt(key.GetID(), 10),
    53  				"github_user_ssh_key",
    54  				"github",
    55  				[]string{},
    56  			)
    57  			resource.SlowQueryRequired = true
    58  			g.Resources = append(g.Resources, resource)
    59  		}
    60  
    61  		if resp.NextPage == 0 {
    62  			break
    63  		}
    64  		opt.Page = resp.NextPage
    65  	}
    66  	return nil
    67  }