aboutsummaryrefslogtreecommitdiffstats
path: root/client/go/cmd/target.go
diff options
context:
space:
mode:
Diffstat (limited to 'client/go/cmd/target.go')
-rw-r--r--client/go/cmd/target.go102
1 files changed, 40 insertions, 62 deletions
diff --git a/client/go/cmd/target.go b/client/go/cmd/target.go
index d6c01acf5b1..97ded047284 100644
--- a/client/go/cmd/target.go
+++ b/client/go/cmd/target.go
@@ -5,44 +5,20 @@
package cmd
import (
+ "crypto/tls"
+ "fmt"
"log"
+ "path/filepath"
"strings"
-)
-
-const (
- cloudApi = "https://api.vespa-external.aws.oath.cloud:4443"
-)
-
-type target struct {
- deploy string
- query string
- document string
-}
-
-type context int32
+ "time"
-const (
- deployContext context = 0
- queryContext context = 1
- documentContext context = 2
+ "github.com/vespa-engine/vespa/vespa"
)
-func deployTarget() string {
- return getTarget(deployContext).deploy
-}
-
-func queryTarget() string {
- return getTarget(queryContext).query
-}
-
-func documentTarget() string {
- return getTarget(documentContext).document
-}
-
func getApplication() string {
app, err := getOption(applicationFlag)
if err != nil {
- log.Fatalf("a valid application must be specified")
+ printErr(err, "A valid application must be specified")
}
return app
}
@@ -50,45 +26,47 @@ func getApplication() string {
func getTargetType() string {
target, err := getOption(targetFlag)
if err != nil {
- log.Fatalf("a valid target must be specified")
+ printErr(err, "A valid target must be specified")
}
return target
}
-func getTarget(targetContext context) *target {
- targetValue := getTargetType()
- if strings.HasPrefix(targetValue, "http") {
- // TODO: Add default ports if missing
- switch targetContext {
- case deployContext:
- return &target{
- deploy: targetValue,
- }
- case queryContext:
- return &target{
- query: targetValue,
- }
- case documentContext:
- return &target{
- document: targetValue,
- }
- }
+func getService(service string) *vespa.Service {
+ t := getTarget()
+ timeout := time.Duration(waitSecsArg) * time.Second
+ if timeout > 0 {
+ log.Printf("Waiting %d %s for service discovery to complete ...", color.Cyan(waitSecsArg), color.Cyan("seconds"))
}
-
- // Otherwise, target is a name
-
- if targetValue == "" || targetValue == "local" {
- return &target{
- deploy: "http://127.0.0.1:19071",
- query: "http://127.0.0.1:8080",
- document: "http://127.0.0.1:8080",
- }
+ if err := t.DiscoverServices(timeout); err != nil {
+ printErr(err, "Failed to discover services")
}
-
- if targetValue == "cloud" {
- return &target{deploy: cloudApi}
+ s, err := t.Service(service)
+ if err != nil {
+ printErr(err, "Invalid service")
}
+ return s
+}
- log.Printf("Unknown target '%s': Use %s, %s or an URL", color.Red(targetValue), color.Cyan("local"), color.Cyan("cloud"))
+func getTarget() vespa.Target {
+ targetType := getTargetType()
+ if strings.HasPrefix(targetType, "http") {
+ return vespa.CustomTarget(targetType)
+ }
+ switch targetType {
+ case "local":
+ return vespa.LocalTarget()
+ case "cloud":
+ deployment := deploymentFromArgs()
+ apiKey := readAPIKey(deployment.Application.Tenant)
+ configDir := configDir(deployment.Application.String())
+ privateKeyFile := filepath.Join(configDir, "data-plane-private-key.pem")
+ certificateFile := filepath.Join(configDir, "data-plane-public-cert.pem")
+ kp, err := tls.LoadX509KeyPair(certificateFile, privateKeyFile)
+ if err != nil {
+ printErr(err, "Could not read key pair")
+ }
+ return vespa.CloudTarget(deployment, kp, apiKey)
+ }
+ printErrHint(fmt.Errorf("Invalid target: %s", targetType), "Valid targets are 'local', 'cloud' or an URL")
return nil
}