github.com/GoogleCloudPlatform/terraformer@v0.8.18/providers/tencentcloud/tencentcloud_provider.go (about) 1 // Copyright 2021 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 tencentcloud 16 17 import ( 18 "errors" 19 "os" 20 21 "github.com/GoogleCloudPlatform/terraformer/terraformutils" 22 "github.com/GoogleCloudPlatform/terraformer/terraformutils/providerwrapper" 23 "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common" 24 "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/profile" 25 ) 26 27 type TencentCloudProvider struct { //nolint 28 terraformutils.Provider 29 region string 30 credential common.Credential 31 } 32 33 func (p *TencentCloudProvider) getCredential() error { 34 secretID := os.Getenv("TENCENTCLOUD_SECRET_ID") 35 if secretID == "" { 36 return errors.New("TENCENTCLOUD_SECRET_ID must be set") 37 } 38 secretKey := os.Getenv("TENCENTCLOUD_SECRET_KEY") 39 if secretKey == "" { 40 return errors.New("TENCENTCLOUD_SECRET_KEY must be set") 41 } 42 token := os.Getenv("TENCENTCLOUD_SECURITY_TOKEN") 43 44 p.credential = common.Credential{ 45 SecretId: secretID, 46 SecretKey: secretKey, 47 Token: token, 48 } 49 return nil 50 } 51 52 func (p *TencentCloudProvider) GetName() string { 53 return "tencentcloud" 54 } 55 56 func (p *TencentCloudProvider) Init(args []string) error { 57 err := p.getCredential() 58 if err != nil { 59 return err 60 } 61 p.region = args[0] 62 return nil 63 } 64 65 func (p *TencentCloudProvider) InitService(serviceName string, verbose bool) error { 66 var isSupported bool 67 if _, isSupported = p.GetSupportedService()[serviceName]; !isSupported { 68 return errors.New("tencentcloud: " + serviceName + " not supported service") 69 } 70 p.Service = p.GetSupportedService()[serviceName] 71 p.Service.SetName(serviceName) 72 p.Service.SetVerbose(verbose) 73 p.Service.SetProviderName(p.GetName()) 74 p.Service.SetArgs(map[string]interface{}{ 75 "region": p.region, 76 "credential": p.credential, 77 }) 78 return nil 79 } 80 81 func (p *TencentCloudProvider) GetSupportedService() map[string]terraformutils.ServiceGenerator { 82 return map[string]terraformutils.ServiceGenerator{ 83 "cvm": &CvmGenerator{}, 84 "vpc": &VpcGenerator{}, 85 "subnet": &SubnetGenerator{}, 86 "cdn": &CdnGenerator{}, 87 "as": &AsGenerator{}, 88 "clb": &ClbGenerator{}, 89 "cos": &CosGenerator{}, 90 "key_pair": &KeyPairGenerator{}, 91 "security_group": &SecurityGroupGenerator{}, 92 "cbs": &CbsGenerator{}, 93 "cfs": &CfsGenerator{}, 94 "elasticsearch": &EsGenerator{}, 95 "gaap": &GaapGenerator{}, 96 "mongodb": &MongodbGenerator{}, 97 "mysql": &MysqlGenerator{}, 98 "redis": &RedisGenerator{}, 99 "ssl": &SslGenerator{}, 100 "scf": &ScfGenerator{}, 101 "tcaplus": &TcaplusGenerator{}, 102 "vpn": &VpnGenerator{}, 103 } 104 } 105 106 func (p *TencentCloudProvider) GetResourceConnections() map[string]map[string][]string { 107 return map[string]map[string][]string{ 108 "cvm": { 109 "vpc": []string{"vpc_id", "id"}, 110 "subnet": []string{"subnet_id", "id"}, 111 "security_group": []string{"security_groups", "id"}, 112 "key_pair": []string{"key_name", "id"}, 113 }, 114 "subnet": { 115 "vpc": []string{"vpc_id", "id"}, 116 }, 117 "as": { 118 "vpc": []string{"vpc_id", "id"}, 119 "subnet": []string{"subnet_ids", "id"}, 120 "clb": []string{"forward_balancer_ids", "id"}, 121 }, 122 "clb": { 123 "vpc": []string{"vpc_id", "id"}, 124 "subnet": []string{"subnet_id", "id"}, 125 "security_group": []string{"security_groups", "id"}, 126 }, 127 "cfs": { 128 "vpc": []string{"vpc_id", "id"}, 129 "subnet": []string{"subnet_id", "id"}, 130 }, 131 "elasticsearch": { 132 "vpc": []string{"vpc_id", "id"}, 133 "subnet": []string{"subnet_id", "id"}, 134 }, 135 "mongodb": { 136 "vpc": []string{"vpc_id", "id"}, 137 "subnet": []string{"subnet_id", "id"}, 138 }, 139 "mysql": { 140 "vpc": []string{"vpc_id", "id"}, 141 "subnet": []string{"subnet_id", "id"}, 142 "security_group": []string{"security_groups", "id"}, 143 }, 144 "redis": { 145 "vpc": []string{"vpc_id", "id"}, 146 "subnet": []string{"subnet_id", "id"}, 147 }, 148 "scf": { 149 "vpc": []string{"vpc_id", "id"}, 150 "subnet": []string{"subnet_id", "id"}, 151 "cos": []string{"cos_bucket_name", "id"}, 152 }, 153 "tcaplus": { 154 "vpc": []string{"vpc_id", "id"}, 155 "subnet": []string{"subnet_id", "id"}, 156 }, 157 "vpn": { 158 "vpc": []string{"vpc_id", "id"}, 159 }, 160 } 161 } 162 163 func (p *TencentCloudProvider) GetProviderData(arg ...string) map[string]interface{} { 164 return map[string]interface{}{ 165 "provider": map[string]interface{}{ 166 p.GetName(): map[string]interface{}{ 167 "version": providerwrapper.GetProviderVersion(p.GetName()), 168 }, 169 }, 170 } 171 } 172 173 func NewTencentCloudClientProfile() *profile.ClientProfile { 174 cpf := profile.NewClientProfile() 175 176 // all request use method POST 177 cpf.HttpProfile.ReqMethod = "POST" 178 // request timeout 179 cpf.HttpProfile.ReqTimeout = 300 180 // default language 181 cpf.Language = "en-US" 182 183 return cpf 184 }