@@ -20,7 +20,10 @@ import (
20
20
)
21
21
22
22
const (
23
- instanceActionTimeout = 20 * time .Minute
23
+ instanceActionTimeout = 20 * time .Minute
24
+ errorMessagePublicEndpointNotFound = "public endpoint not found"
25
+ errorMessagePrivateEndpointNotFound = "private endpoint not found"
26
+ errorMessageEndpointNotFound = "any endpoint is associated on your instance"
24
27
)
25
28
26
29
var (
@@ -396,11 +399,12 @@ func instanceWaitCommand() *core.Command {
396
399
}
397
400
398
401
type instanceConnectArgs struct {
399
- Region scw.Region
400
- InstanceID string
401
- Username string
402
- Database * string
403
- CliDB * string
402
+ Region scw.Region
403
+ PrivateNetwork bool
404
+ InstanceID string
405
+ Username string
406
+ Database * string
407
+ CliDB * string
404
408
}
405
409
406
410
type engineFamily string
@@ -463,7 +467,27 @@ func detectEngineFamily(instance *rdb.Instance) (engineFamily, error) {
463
467
return Unknown , fmt .Errorf ("unknown engine: %s" , instance .Engine )
464
468
}
465
469
466
- func createConnectCommandLineArgs (instance * rdb.Instance , family engineFamily , args * instanceConnectArgs ) ([]string , error ) {
470
+ func getPublicEndpoint (endpoints []* rdb.Endpoint ) (* rdb.Endpoint , error ) {
471
+ for _ , e := range endpoints {
472
+ if e .LoadBalancer != nil {
473
+ return e , nil
474
+ }
475
+ }
476
+
477
+ return nil , fmt .Errorf (errorMessagePublicEndpointNotFound )
478
+ }
479
+
480
+ func getPrivateEndpoint (endpoints []* rdb.Endpoint ) (* rdb.Endpoint , error ) {
481
+ for _ , e := range endpoints {
482
+ if e .PrivateNetwork != nil {
483
+ return e , nil
484
+ }
485
+ }
486
+
487
+ return nil , fmt .Errorf (errorMessagePrivateEndpointNotFound )
488
+ }
489
+
490
+ func createConnectCommandLineArgs (endpoint * rdb.Endpoint , family engineFamily , args * instanceConnectArgs ) ([]string , error ) {
467
491
database := "rdb"
468
492
if args .Database != nil {
469
493
database = * args .Database
@@ -479,8 +503,8 @@ func createConnectCommandLineArgs(instance *rdb.Instance, family engineFamily, a
479
503
// psql -h 51.159.25.206 --port 13917 -d rdb -U username
480
504
return []string {
481
505
clidb ,
482
- "--host" , instance . Endpoints [ 0 ] .IP .String (),
483
- "--port" , fmt .Sprintf ("%d" , instance . Endpoints [ 0 ] .Port ),
506
+ "--host" , endpoint .IP .String (),
507
+ "--port" , fmt .Sprintf ("%d" , endpoint .Port ),
484
508
"--username" , args .Username ,
485
509
"--dbname" , database ,
486
510
}, nil
@@ -493,14 +517,14 @@ func createConnectCommandLineArgs(instance *rdb.Instance, family engineFamily, a
493
517
// mysql -h 195.154.69.163 --port 12210 -p -u username
494
518
return []string {
495
519
clidb ,
496
- "--host" , instance . Endpoints [ 0 ] .IP .String (),
497
- "--port" , fmt .Sprintf ("%d" , instance . Endpoints [ 0 ] .Port ),
520
+ "--host" , endpoint .IP .String (),
521
+ "--port" , fmt .Sprintf ("%d" , endpoint .Port ),
498
522
"--database" , database ,
499
523
"--user" , args .Username ,
500
524
}, nil
501
525
}
502
526
503
- return nil , fmt .Errorf ("unrecognize database engine: %s" , instance . Engine )
527
+ return nil , fmt .Errorf ("unrecognize database engine: %s" , family )
504
528
}
505
529
506
530
func instanceConnectCommand () * core.Command {
@@ -512,6 +536,12 @@ func instanceConnectCommand() *core.Command {
512
536
Long : "Connect to an instance using locally installed CLI such as psql or mysql." ,
513
537
ArgsType : reflect .TypeOf (instanceConnectArgs {}),
514
538
ArgSpecs : core.ArgSpecs {
539
+ {
540
+ Name : "private-network" ,
541
+ Short : `Connect by the private network endpoint attached.` ,
542
+ Required : false ,
543
+ Default : core .DefaultValueSetter ("false" ),
544
+ },
515
545
{
516
546
Name : "instance-id" ,
517
547
Short : `UUID of the instance` ,
@@ -552,7 +582,25 @@ func instanceConnectCommand() *core.Command {
552
582
return nil , err
553
583
}
554
584
555
- cmdArgs , err := createConnectCommandLineArgs (instance , engineFamily , args )
585
+ if len (instance .Endpoints ) == 0 {
586
+ return nil , fmt .Errorf (errorMessageEndpointNotFound )
587
+ }
588
+
589
+ var endpoint * rdb.Endpoint
590
+ switch {
591
+ case args .PrivateNetwork :
592
+ endpoint , err = getPrivateEndpoint (instance .Endpoints )
593
+ if err != nil {
594
+ return nil , err
595
+ }
596
+ default :
597
+ endpoint , err = getPublicEndpoint (instance .Endpoints )
598
+ if err != nil {
599
+ return nil , err
600
+ }
601
+ }
602
+
603
+ cmdArgs , err := createConnectCommandLineArgs (endpoint , engineFamily , args )
556
604
if err != nil {
557
605
return nil , err
558
606
}
0 commit comments