@@ -8,16 +8,11 @@ package cvm
8
8
9
9
import (
10
10
"context"
11
- "encoding/json"
12
11
"fmt"
13
- "io/ioutil"
14
12
"os"
15
- "runtime"
16
13
"strconv"
17
- "strings"
18
14
19
15
"github.com/hashicorp/packer-plugin-sdk/template/interpolate"
20
- "github.com/mitchellh/go-homedir"
21
16
cvm "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cvm/v20170312"
22
17
vpc "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vpc/v20170312"
23
18
)
@@ -222,35 +217,23 @@ func (cf *TencentCloudAccessConfig) Config() error {
222
217
cf .SharedCredentialsDir = os .Getenv (PACKER_SHARED_CREDENTIALS_DIR )
223
218
}
224
219
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
-
237
220
if cf .SecretId == "" || cf .SecretKey == "" {
238
- value , err = loadConfigProfile (cf )
221
+ profile , err : = loadConfigProfile (cf )
239
222
if err != nil {
240
223
return err
241
224
}
242
225
243
226
if cf .SecretId == "" {
244
- cf .SecretId = getProviderConfig ( "secretId" )
227
+ cf .SecretId = profile . SecretId
245
228
}
246
229
if cf .SecretKey == "" {
247
- cf .SecretKey = getProviderConfig ( "secretKey" )
230
+ cf .SecretKey = profile . SecretKey
248
231
}
249
232
if cf .SecurityToken == "" {
250
- cf .SecurityToken = getProviderConfig ( "securityToken" )
233
+ cf .SecurityToken = profile . Token
251
234
}
252
235
if cf .Region == "" {
253
- cf .Region = getProviderConfig ( "region" )
236
+ cf .Region = profile . Region
254
237
}
255
238
}
256
239
@@ -293,33 +276,29 @@ func (cf *TencentCloudAccessConfig) Config() error {
293
276
}
294
277
295
278
if cf .AssumeRole .RoleArn == "" || cf .AssumeRole .SessionName == "" {
296
- value , err = loadConfigProfile (cf )
279
+ profile , err : = loadConfigProfile (cf )
297
280
if err != nil {
298
281
return err
299
282
}
300
283
301
284
if cf .AssumeRole .RoleArn == "" {
302
- roleArn := getProviderConfig ( "role-arn" )
285
+ roleArn := profile . RoleArn
303
286
if roleArn != "" {
304
287
cf .AssumeRole .RoleArn = roleArn
305
288
}
306
289
}
307
290
308
291
if cf .AssumeRole .SessionName == "" {
309
- sessionName := getProviderConfig ( "role-session-name" )
292
+ sessionName := profile . RoleSessionName
310
293
if sessionName != "" {
311
294
cf .AssumeRole .SessionName = sessionName
312
295
}
313
296
}
314
297
315
298
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 )
323
302
}
324
303
}
325
304
}
@@ -344,104 +323,3 @@ func validRegion(region string) error {
344
323
345
324
return fmt .Errorf ("unknown region: %s" , region )
346
325
}
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