github.com/GoogleCloudPlatform/terraformer@v0.8.18/providers/github/github_provider.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 "os" 19 20 "github.com/GoogleCloudPlatform/terraformer/terraformutils" 21 "github.com/pkg/errors" 22 "github.com/zclconf/go-cty/cty" 23 ) 24 25 type GithubProvider struct { //nolint 26 terraformutils.Provider 27 owner string 28 token string 29 baseURL string 30 } 31 32 func (p GithubProvider) GetResourceConnections() map[string]map[string][]string { 33 return map[string]map[string][]string{} 34 } 35 36 func (p GithubProvider) GetProviderData(arg ...string) map[string]interface{} { 37 return map[string]interface{}{ 38 "provider": map[string]interface{}{ 39 "github": map[string]interface{}{ 40 "owner": p.owner, 41 }, 42 }, 43 } 44 } 45 46 func (p *GithubProvider) GetConfig() cty.Value { 47 return cty.ObjectVal(map[string]cty.Value{ 48 "owner": cty.StringVal(p.owner), 49 "token": cty.StringVal(p.token), 50 "base_url": cty.StringVal(p.baseURL), 51 }) 52 } 53 54 // Init GithubProvider with owner 55 func (p *GithubProvider) Init(args []string) error { 56 p.owner = args[0] 57 if len(args) < 2 { 58 if os.Getenv("GITHUB_TOKEN") == "" { 59 return errors.New("token requirement") 60 } 61 p.token = os.Getenv("GITHUB_TOKEN") 62 } else { 63 p.token = args[1] 64 } 65 if len(args) > 2 { 66 if args[2] != "" { 67 p.baseURL = args[2] 68 } else { 69 p.baseURL = githubDefaultURL 70 } 71 } 72 return nil 73 } 74 75 func (p *GithubProvider) GetName() string { 76 return "github" 77 } 78 79 func (p *GithubProvider) InitService(serviceName string, verbose bool) error { 80 var isSupported bool 81 if _, isSupported = p.GetSupportedService()[serviceName]; !isSupported { 82 return errors.New(p.GetName() + ": " + serviceName + " not supported service") 83 } 84 p.Service = p.GetSupportedService()[serviceName] 85 p.Service.SetName(serviceName) 86 p.Service.SetVerbose(verbose) 87 p.Service.SetProviderName(p.GetName()) 88 p.Service.SetArgs(map[string]interface{}{ 89 "owner": p.owner, 90 "token": p.token, 91 "base_url": p.baseURL, 92 }) 93 return nil 94 } 95 96 // GetSupportedService return map of support service for Github 97 func (p *GithubProvider) GetSupportedService() map[string]terraformutils.ServiceGenerator { 98 return map[string]terraformutils.ServiceGenerator{ 99 "members": &MembersGenerator{}, 100 "organization": &OrganizationGenerator{}, 101 "organization_blocks": &OrganizationBlockGenerator{}, 102 "organization_projects": &OrganizationProjectGenerator{}, 103 "organization_webhooks": &OrganizationWebhooksGenerator{}, 104 "repositories": &RepositoriesGenerator{}, 105 "teams": &TeamsGenerator{}, 106 "user_ssh_keys": &UserSSHKeyGenerator{}, 107 } 108 }