summaryrefslogtreecommitdiffstats
path: root/client
diff options
context:
space:
mode:
authorMartin Polden <mpolden@mpolden.no>2021-09-08 12:45:09 +0200
committerMartin Polden <mpolden@mpolden.no>2021-09-08 12:55:37 +0200
commitde3fa4f928ad39863afdb75f61373de9eea3f90d (patch)
tree8e2ad624cd75431412b64bd9d997e993ed459a1c /client
parentdd26caaa5cc05e9cacaa280a4bee5d9ddb56ecbc (diff)
Remove unnecessary localTarget struct
Diffstat (limited to 'client')
-rw-r--r--client/go/vespa/target.go44
-rw-r--r--client/go/vespa/target_test.go23
2 files changed, 50 insertions, 17 deletions
diff --git a/client/go/vespa/target.go b/client/go/vespa/target.go
index faf80736293..1d6cc0f0366 100644
--- a/client/go/vespa/target.go
+++ b/client/go/vespa/target.go
@@ -6,6 +6,7 @@ import (
"fmt"
"io/ioutil"
"net/http"
+ "net/url"
"time"
"github.com/vespa-engine/vespa/client/go/util"
@@ -51,8 +52,6 @@ type customTarget struct {
baseURL string
}
-type localTarget struct{ targetType string }
-
// Do sends request to this service. Any required authentication happens automatically.
func (s *Service) Do(request *http.Request, timeout time.Duration) (*http.Response, error) {
if s.certificate.Certificate != nil {
@@ -97,27 +96,36 @@ func (t *customTarget) Type() string { return t.targetType }
func (t *customTarget) Service(name string) (*Service, error) {
switch name {
case deployService, queryService, documentService:
- // TODO: Add default ports if missing
- return &Service{BaseURL: t.baseURL, Name: name}, nil
+ url, err := t.urlWithPort(name)
+ if err != nil {
+ return nil, err
+ }
+ return &Service{BaseURL: url, Name: name}, nil
}
return nil, fmt.Errorf("unknown service: %s", name)
}
-func (t *customTarget) DiscoverServices(timeout time.Duration, sessionID int64) error { return nil }
-
-func (t *localTarget) Type() string { return t.targetType }
-
-func (t *localTarget) Service(name string) (*Service, error) {
- switch name {
- case deployService:
- return &Service{Name: name, BaseURL: "http://127.0.0.1:19071"}, nil
- case queryService, documentService:
- return &Service{Name: name, BaseURL: "http://127.0.0.1:8080"}, nil
+func (t *customTarget) urlWithPort(serviceName string) (string, error) {
+ u, err := url.Parse(t.baseURL)
+ if err != nil {
+ return "", err
}
- return nil, fmt.Errorf("unknown service: %s", name)
+ port := u.Port()
+ if port == "" {
+ switch serviceName {
+ case deployService:
+ port = "19071"
+ case queryService, documentService:
+ port = "8080"
+ default:
+ return "", fmt.Errorf("unknown service: %s", serviceName)
+ }
+ u.Host = u.Host + ":" + port
+ }
+ return u.String(), nil
}
-func (t *localTarget) DiscoverServices(timeout time.Duration, sessionID int64) error { return nil }
+func (t *customTarget) DiscoverServices(timeout time.Duration, sessionID int64) error { return nil }
type cloudTarget struct {
cloudAPI string
@@ -233,7 +241,9 @@ func (t *cloudTarget) discoverEndpoints(signer *RequestSigner, timeout time.Dura
}
// LocalTarget creates a target for a Vespa platform running locally.
-func LocalTarget() Target { return &localTarget{targetType: localTargetType} }
+func LocalTarget() Target {
+ return &customTarget{targetType: localTargetType, baseURL: "http://127.0.0.1"}
+}
// CustomTarget creates a Target for a Vespa platform running at baseURL.
func CustomTarget(baseURL string) Target {
diff --git a/client/go/vespa/target_test.go b/client/go/vespa/target_test.go
index 1f46cc83178..41932a4aee7 100644
--- a/client/go/vespa/target_test.go
+++ b/client/go/vespa/target_test.go
@@ -42,6 +42,23 @@ func (v *mockVespaApi) mockVespaHandler(w http.ResponseWriter, req *http.Request
}
}
+func TestCustomTarget(t *testing.T) {
+ lt := LocalTarget()
+ assertServiceURL(t, "http://127.0.0.1:19071", lt, "deploy")
+ assertServiceURL(t, "http://127.0.0.1:8080", lt, "query")
+ assertServiceURL(t, "http://127.0.0.1:8080", lt, "document")
+
+ ct := CustomTarget("http://192.0.2.42")
+ assertServiceURL(t, "http://192.0.2.42:19071", ct, "deploy")
+ assertServiceURL(t, "http://192.0.2.42:8080", ct, "query")
+ assertServiceURL(t, "http://192.0.2.42:8080", ct, "document")
+
+ ct2 := CustomTarget("http://192.0.2.42:60000")
+ assertServiceURL(t, "http://192.0.2.42:60000", ct2, "deploy")
+ assertServiceURL(t, "http://192.0.2.42:60000", ct2, "query")
+ assertServiceURL(t, "http://192.0.2.42:60000", ct2, "document")
+}
+
func TestCustomTargetWait(t *testing.T) {
vc := mockVespaApi{}
srv := httptest.NewServer(http.HandlerFunc(vc.mockVespaHandler))
@@ -95,6 +112,12 @@ func TestCloudTargetWait(t *testing.T) {
assertServiceWait(t, 500, target, "document")
}
+func assertServiceURL(t *testing.T, url string, target Target, service string) {
+ s, err := target.Service(service)
+ assert.Nil(t, err)
+ assert.Equal(t, url, s.BaseURL)
+}
+
func assertServiceWait(t *testing.T, expectedStatus int, target Target, service string) {
s, err := target.Service(service)
assert.Nil(t, err)