9005: De-duplicate arvadosclient setup.
authorTom Clegg <tom@curoverse.com>
Tue, 30 May 2017 19:24:45 +0000 (15:24 -0400)
committerTom Clegg <tom@curoverse.com>
Wed, 31 May 2017 20:14:55 +0000 (16:14 -0400)
Fix ignored KeepServiceURIs when using an arvados.Client to create an
arvadosclient.ArvadosClient.

Fix ARVADOS_API_HOST_INSECURE=no or =false interpreted as "insecure
mode" in arvados.Client setup.

Log & ignore invalid entries in ARVADOS_KEEP_SERVICES in
arvados.Client setup. Handle extra separator chars more gracefully.

sdk/go/arvados/client.go
sdk/go/arvadosclient/arvadosclient.go

index 9691e7a07e475668cc73a80dffba466addb3b4ef..d7eb811b8a7a26a08931b07b9c66fcfec83d78a3 100644 (file)
@@ -6,6 +6,7 @@ import (
        "fmt"
        "io"
        "io/ioutil"
+       "log"
        "math"
        "net/http"
        "net/url"
@@ -63,13 +64,25 @@ var DefaultSecureClient = &http.Client{
 // ARVADOS_API_* environment variables.
 func NewClientFromEnv() *Client {
        var svcs []string
-       if s := os.Getenv("ARVADOS_KEEP_SERVICES"); s != "" {
-               svcs = strings.Split(s, " ")
+       for _, s := range strings.Split(os.Getenv("ARVADOS_KEEP_SERVICES"), " ") {
+               if s == "" {
+                       continue
+               } else if u, err := url.Parse(s); err != nil {
+                       log.Printf("ARVADOS_KEEP_SERVICES: %q: %s", s, err)
+               } else if !u.IsAbs() {
+                       log.Printf("ARVADOS_KEEP_SERVICES: %q: not an absolute URI", s)
+               } else {
+                       svcs = append(svcs, s)
+               }
+       }
+       var insecure bool
+       if s := strings.ToLower(os.Getenv("ARVADOS_API_HOST_INSECURE")); s == "1" || s == "yes" || s == "true" {
+               insecure = true
        }
        return &Client{
                APIHost:         os.Getenv("ARVADOS_API_HOST"),
                AuthToken:       os.Getenv("ARVADOS_API_TOKEN"),
-               Insecure:        os.Getenv("ARVADOS_API_HOST_INSECURE") != "",
+               Insecure:        insecure,
                KeepServiceURIs: svcs,
        }
 }
index af7f028e0725635fefbae6b880d1506e788b2dd1..7e33ec0ce6fc3b2a5ba50ce4294916f6601f1280 100644 (file)
@@ -163,6 +163,7 @@ func New(c *arvados.Client) (*ArvadosClient, error) {
                        TLSClientConfig: MakeTLSConfig(c.Insecure)}},
                External:          false,
                Retries:           2,
+               KeepServiceURIs:   c.KeepServiceURIs,
                lastClosedIdlesAt: time.Now(),
        }
 
@@ -175,41 +176,12 @@ func New(c *arvados.Client) (*ArvadosClient, error) {
 // ARVADOS_KEEP_SERVICES.
 func MakeArvadosClient() (ac *ArvadosClient, err error) {
        var matchTrue = regexp.MustCompile("^(?i:1|yes|true)$")
-       insecure := matchTrue.MatchString(os.Getenv("ARVADOS_API_HOST_INSECURE"))
-       external := matchTrue.MatchString(os.Getenv("ARVADOS_EXTERNAL_CLIENT"))
-
-       ac = &ArvadosClient{
-               Scheme:      "https",
-               ApiServer:   os.Getenv("ARVADOS_API_HOST"),
-               ApiToken:    os.Getenv("ARVADOS_API_TOKEN"),
-               ApiInsecure: insecure,
-               Client: &http.Client{Transport: &http.Transport{
-                       TLSClientConfig: MakeTLSConfig(insecure)}},
-               External: external,
-               Retries:  2}
-
-       for _, s := range strings.Split(os.Getenv("ARVADOS_KEEP_SERVICES"), " ") {
-               if s == "" {
-                       continue
-               }
-               if u, err := url.Parse(s); err != nil {
-                       return ac, fmt.Errorf("ARVADOS_KEEP_SERVICES: %q: %s", s, err)
-               } else if !u.IsAbs() {
-                       return ac, fmt.Errorf("ARVADOS_KEEP_SERVICES: %q: not an absolute URI", s)
-               }
-               ac.KeepServiceURIs = append(ac.KeepServiceURIs, s)
-       }
-
-       if ac.ApiServer == "" {
-               return ac, MissingArvadosApiHost
-       }
-       if ac.ApiToken == "" {
-               return ac, MissingArvadosApiToken
+       ac, err = New(arvados.NewClientFromEnv())
+       if err != nil {
+               return
        }
-
-       ac.lastClosedIdlesAt = time.Now()
-
-       return ac, err
+       ac.External = matchTrue.MatchString(os.Getenv("ARVADOS_EXTERNAL_CLIENT"))
+       return
 }
 
 // CallRaw is the same as Call() but returns a Reader that reads the