@@ -8,16 +8,11 @@ package cvm
88
99import (
1010 "context"
11- "encoding/json"
1211 "fmt"
13- "io/ioutil"
1412 "os"
15- "runtime"
1613 "strconv"
17- "strings"
1814
1915 "github.com/hashicorp/packer-plugin-sdk/template/interpolate"
20- "github.com/mitchellh/go-homedir"
2116 cvm "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cvm/v20170312"
2217 vpc "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vpc/v20170312"
2318)
@@ -222,35 +217,23 @@ func (cf *TencentCloudAccessConfig) Config() error {
222217 cf .SharedCredentialsDir = os .Getenv (PACKER_SHARED_CREDENTIALS_DIR )
223218 }
224219
225- var value map [string ]interface {}
226- var err error
227- getProviderConfig := func (key string ) string {
228- var str string
229- if value != nil {
230- if v , ok := value [key ]; ok {
231- str = v .(string )
232- }
233- }
234- return str
235- }
236-
237220 if cf .SecretId == "" || cf .SecretKey == "" {
238- value , err = loadConfigProfile (cf )
221+ profile , err : = loadConfigProfile (cf )
239222 if err != nil {
240223 return err
241224 }
242225
243226 if cf .SecretId == "" {
244- cf .SecretId = getProviderConfig ( "secretId" )
227+ cf .SecretId = profile . SecretId
245228 }
246229 if cf .SecretKey == "" {
247- cf .SecretKey = getProviderConfig ( "secretKey" )
230+ cf .SecretKey = profile . SecretKey
248231 }
249232 if cf .SecurityToken == "" {
250- cf .SecurityToken = getProviderConfig ( "securityToken" )
233+ cf .SecurityToken = profile . Token
251234 }
252235 if cf .Region == "" {
253- cf .Region = getProviderConfig ( "region" )
236+ cf .Region = profile . Region
254237 }
255238 }
256239
@@ -293,33 +276,29 @@ func (cf *TencentCloudAccessConfig) Config() error {
293276 }
294277
295278 if cf .AssumeRole .RoleArn == "" || cf .AssumeRole .SessionName == "" {
296- value , err = loadConfigProfile (cf )
279+ profile , err : = loadConfigProfile (cf )
297280 if err != nil {
298281 return err
299282 }
300283
301284 if cf .AssumeRole .RoleArn == "" {
302- roleArn := getProviderConfig ( "role-arn" )
285+ roleArn := profile . RoleArn
303286 if roleArn != "" {
304287 cf .AssumeRole .RoleArn = roleArn
305288 }
306289 }
307290
308291 if cf .AssumeRole .SessionName == "" {
309- sessionName := getProviderConfig ( "role-session-name" )
292+ sessionName := profile . RoleSessionName
310293 if sessionName != "" {
311294 cf .AssumeRole .SessionName = sessionName
312295 }
313296 }
314297
315298 if cf .AssumeRole .SessionDuration == 0 {
316- duration := getProviderConfig ("role-session-duration" )
317- if duration != "" {
318- durationInt , err := strconv .Atoi (duration )
319- if err != nil {
320- return err
321- }
322- cf .AssumeRole .SessionDuration = durationInt
299+ duration := profile .RoleSessionDuration
300+ if duration != 0 {
301+ cf .AssumeRole .SessionDuration = int (duration )
323302 }
324303 }
325304 }
@@ -344,104 +323,3 @@ func validRegion(region string) error {
344323
345324 return fmt .Errorf ("unknown region: %s" , region )
346325}
347-
348- func getProfilePatch (cf * TencentCloudAccessConfig ) (string , string , error ) {
349- var (
350- profile string
351- sharedCredentialsDir string
352- credentialPath string
353- configurePath string
354- )
355-
356- if cf .Profile != "" {
357- profile = cf .Profile
358- } else {
359- profile = DEFAULT_PROFILE
360- }
361-
362- if cf .SharedCredentialsDir != "" {
363- sharedCredentialsDir = cf .SharedCredentialsDir
364- }
365-
366- tmpSharedCredentialsDir , err := homedir .Expand (sharedCredentialsDir )
367- if err != nil {
368- return "" , "" , err
369- }
370-
371- if tmpSharedCredentialsDir == "" {
372- credentialPath = fmt .Sprintf ("%s/.tccli/%s.credential" , os .Getenv ("HOME" ), profile )
373- configurePath = fmt .Sprintf ("%s/.tccli/%s.configure" , os .Getenv ("HOME" ), profile )
374- if runtime .GOOS == "windows" {
375- credentialPath = fmt .Sprintf ("%s/.tccli/%s.credential" , os .Getenv ("USERPROFILE" ), profile )
376- configurePath = fmt .Sprintf ("%s/.tccli/%s.configure" , os .Getenv ("USERPROFILE" ), profile )
377- }
378- } else {
379- credentialPath = fmt .Sprintf ("%s/%s.credential" , tmpSharedCredentialsDir , profile )
380- configurePath = fmt .Sprintf ("%s/%s.configure" , tmpSharedCredentialsDir , profile )
381- }
382-
383- return credentialPath , configurePath , nil
384- }
385-
386- func loadConfigProfile (cf * TencentCloudAccessConfig ) (map [string ]interface {}, error ) {
387- var (
388- credentialPath string
389- configurePath string
390- )
391-
392- credentialPath , configurePath , err := getProfilePatch (cf )
393- if err != nil {
394- return nil , err
395- }
396-
397- packerConfig := make (map [string ]interface {})
398- _ , err = os .Stat (credentialPath )
399- if ! os .IsNotExist (err ) {
400- data , err := ioutil .ReadFile (credentialPath )
401- if err != nil {
402- return nil , err
403- }
404-
405- config := map [string ]interface {}{}
406- err = json .Unmarshal (data , & config )
407- if err != nil {
408- return nil , fmt .Errorf ("credential file unmarshal failed, %s" , err )
409- }
410-
411- for k , v := range config {
412- packerConfig [k ] = strings .TrimSpace (v .(string ))
413- }
414- } else {
415- return nil , fmt .Errorf ("please set a valid secret_id and secret_key or shared_credentials_dir, %s" , err )
416- }
417- _ , err = os .Stat (configurePath )
418- if ! os .IsNotExist (err ) {
419- data , err := ioutil .ReadFile (configurePath )
420- if err != nil {
421- return nil , err
422- }
423-
424- config := map [string ]interface {}{}
425- err = json .Unmarshal (data , & config )
426- if err != nil {
427- return nil , fmt .Errorf ("configure file unmarshal failed, %s" , err )
428- }
429-
430- outerLoop:
431- for k , v := range config {
432- if k == "_sys_param" {
433- tmpMap := v .(map [string ]interface {})
434- for tmpK , tmpV := range tmpMap {
435- if tmpK == "region" {
436- packerConfig [tmpK ] = strings .TrimSpace (tmpV .(string ))
437- break outerLoop
438- }
439- }
440- }
441- }
442- } else {
443- return nil , fmt .Errorf ("please set a valid region or shared_credentials_dir, %s" , err )
444- }
445-
446- return packerConfig , nil
447- }
0 commit comments