github.com/GoogleCloudPlatform/terraformer@v0.8.18/providers/github/teams.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 "strconv" 21 22 "github.com/GoogleCloudPlatform/terraformer/terraformutils" 23 24 githubAPI "github.com/google/go-github/v35/github" 25 ) 26 27 type TeamsGenerator struct { 28 GithubService 29 } 30 31 func (g *TeamsGenerator) createTeamsResources(ctx context.Context, teams []*githubAPI.Team, client *githubAPI.Client) []terraformutils.Resource { 32 resources := []terraformutils.Resource{} 33 for _, team := range teams { 34 resource := terraformutils.NewSimpleResource( 35 strconv.FormatInt(team.GetID(), 10), 36 team.GetName(), 37 "github_team", 38 "github", 39 []string{}, 40 ) 41 resource.SlowQueryRequired = true 42 resources = append(resources, resource) 43 resources = append(resources, g.createTeamMembersResources(ctx, team, client)...) 44 resources = append(resources, g.createTeamRepositoriesResources(ctx, team, client)...) 45 } 46 return resources 47 } 48 49 func (g *TeamsGenerator) createTeamMembersResources(ctx context.Context, team *githubAPI.Team, client *githubAPI.Client) []terraformutils.Resource { 50 resources := []terraformutils.Resource{} 51 members, _, err := client.Teams.ListTeamMembersBySlug(ctx, g.Args["owner"].(string), team.GetSlug(), nil) 52 if err != nil { 53 log.Println(err) 54 } 55 for _, member := range members { 56 resources = append(resources, terraformutils.NewSimpleResource( 57 strconv.FormatInt(team.GetID(), 10)+":"+member.GetLogin(), 58 team.GetName()+"_"+member.GetLogin(), 59 "github_team_membership", 60 "github", 61 []string{}, 62 )) 63 } 64 return resources 65 } 66 67 func (g *TeamsGenerator) createTeamRepositoriesResources(ctx context.Context, team *githubAPI.Team, client *githubAPI.Client) []terraformutils.Resource { 68 resources := []terraformutils.Resource{} 69 repos, _, err := client.Teams.ListTeamReposBySlug(ctx, g.Args["owner"].(string), team.GetSlug(), nil) 70 if err != nil { 71 log.Println(err) 72 } 73 for _, repo := range repos { 74 resources = append(resources, terraformutils.NewSimpleResource( 75 strconv.FormatInt(team.GetID(), 10)+":"+repo.GetName(), 76 team.GetName()+"_"+repo.GetName(), 77 "github_team_repository", 78 "github", 79 []string{}, 80 )) 81 } 82 return resources 83 } 84 85 // InitResources generates TerraformResources from Github API, 86 func (g *TeamsGenerator) InitResources() error { 87 ctx := context.Background() 88 client, err := g.createClient() 89 if err != nil { 90 return err 91 } 92 93 opt := &githubAPI.ListOptions{PerPage: 1} 94 95 for { 96 teams, resp, err := client.Teams.ListTeams(ctx, g.Args["owner"].(string), opt) 97 if err != nil { 98 log.Println(err) 99 return nil 100 } 101 102 g.Resources = append(g.Resources, g.createTeamsResources(ctx, teams, client)...) 103 104 if resp.NextPage == 0 { 105 break 106 } 107 opt.Page = resp.NextPage 108 } 109 110 return nil 111 } 112 113 // PostConvertHook for connect between team and members 114 func (g *TeamsGenerator) PostConvertHook() error { 115 for _, team := range g.Resources { 116 if team.InstanceInfo.Type != "github_team" { 117 continue 118 } 119 for i, member := range g.Resources { 120 if member.InstanceInfo.Type != "github_team_membership" { 121 continue 122 } 123 if member.InstanceState.Attributes["team_id"] == team.InstanceState.Attributes["id"] { 124 g.Resources[i].Item["team_id"] = "${github_team." + team.ResourceName + ".id}" 125 } 126 } 127 for i, repo := range g.Resources { 128 if repo.InstanceInfo.Type != "github_team_repository" { 129 continue 130 } 131 if repo.InstanceState.Attributes["team_id"] == team.InstanceState.Attributes["id"] { 132 g.Resources[i].Item["team_id"] = "${github_team." + team.ResourceName + ".id}" 133 } 134 } 135 } 136 return nil 137 }