added a test that fails to prove rpc.Conn is not DTRT
[arvados.git] / lib / controller / rpc / conn.go
index f4bc1733eaff7112caf4bdfc7c69f62ddae5c4a8..b3fd541bbb28672d12b1f97e756d6af5897d5bff 100644 (file)
@@ -5,30 +5,33 @@
 package rpc
 
 import (
+       "bytes"
        "context"
        "crypto/tls"
        "encoding/json"
        "errors"
        "fmt"
        "io"
+       "log"
        "net"
        "net/http"
        "net/url"
+       "strconv"
        "strings"
        "time"
 
-       "git.curoverse.com/arvados.git/sdk/go/arvados"
-       "git.curoverse.com/arvados.git/sdk/go/auth"
+       "git.arvados.org/arvados.git/sdk/go/arvados"
+       "git.arvados.org/arvados.git/sdk/go/auth"
 )
 
 type TokenProvider func(context.Context) ([]string, error)
 
 func PassthroughTokenProvider(ctx context.Context) ([]string, error) {
-       if incoming, ok := auth.FromContext(ctx); !ok {
+       incoming, ok := auth.FromContext(ctx)
+       if !ok {
                return nil, errors.New("no token provided")
-       } else {
-               return incoming.Tokens, nil
        }
+       return incoming.Tokens, nil
 }
 
 type Conn struct {
@@ -100,19 +103,23 @@ func (conn *Conn) requestAndDecode(ctx context.Context, dst interface{}, ep arva
                return fmt.Errorf("%T: requestAndDecode: Marshal opts: %s", conn, err)
        }
        var params map[string]interface{}
-       err = json.Unmarshal(j, &params)
+       dec := json.NewDecoder(bytes.NewBuffer(j))
+       dec.UseNumber()
+       err = dec.Decode(&params)
        if err != nil {
-               return fmt.Errorf("%T: requestAndDecode: Unmarshal opts: %s", conn, err)
+               return fmt.Errorf("%T: requestAndDecode: Decode opts: %s", conn, err)
        }
        if attrs, ok := params["attrs"]; ok && ep.AttrsKey != "" {
                params[ep.AttrsKey] = attrs
                delete(params, "attrs")
        }
-       if limit, ok := params["limit"].(float64); ok && limit < 0 {
-               // Negative limit means "not specified" here, but some
-               // servers/versions do not accept that, so we need to
-               // remove it entirely.
-               delete(params, "limit")
+       if limitStr, ok := params["limit"]; ok {
+               if limit, err := strconv.ParseInt(string(limitStr.(json.Number)), 10, 64); err == nil && limit < 0 {
+                       // Negative limit means "not specified" here, but some
+                       // servers/versions do not accept that, so we need to
+                       // remove it entirely.
+                       delete(params, "limit")
+               }
        }
        if len(tokens) > 1 {
                params["reader_tokens"] = tokens[1:]
@@ -145,6 +152,14 @@ func (conn *Conn) Login(ctx context.Context, options arvados.LoginOptions) (arva
        return resp, err
 }
 
+func (conn *Conn) Logout(ctx context.Context, options arvados.LogoutOptions) (arvados.LogoutResponse, error) {
+       ep := arvados.EndpointLogout
+       var resp arvados.LogoutResponse
+       err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
+       resp.RedirectLocation = conn.relativeToBaseURL(resp.RedirectLocation)
+       return resp, err
+}
+
 // If the given location is a valid URL and its origin is the same as
 // conn.baseURL, return it as a relative URL. Otherwise, return it
 // unmodified.
@@ -156,9 +171,8 @@ func (conn *Conn) relativeToBaseURL(location string) string {
                u.User = nil
                u.Host = ""
                return u.String()
-       } else {
-               return location
        }
+       return location
 }
 
 func (conn *Conn) CollectionCreate(ctx context.Context, options arvados.CreateOptions) (arvados.Collection, error) {
@@ -273,6 +287,42 @@ func (conn *Conn) ContainerUnlock(ctx context.Context, options arvados.GetOption
        return resp, err
 }
 
+func (conn *Conn) ContainerRequestCreate(ctx context.Context, options arvados.CreateOptions) (arvados.ContainerRequest, error) {
+       log.Printf("THIS IS THE rcp.Conn.ContainerRequestCreate(): %s", options.ClusterID)
+       ep := arvados.EndpointContainerRequestCreate
+       var resp arvados.ContainerRequest
+       err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
+       return resp, err
+}
+
+func (conn *Conn) ContainerRequestUpdate(ctx context.Context, options arvados.UpdateOptions) (arvados.ContainerRequest, error) {
+       ep := arvados.EndpointContainerRequestUpdate
+       var resp arvados.ContainerRequest
+       err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
+       return resp, err
+}
+
+func (conn *Conn) ContainerRequestGet(ctx context.Context, options arvados.GetOptions) (arvados.ContainerRequest, error) {
+       ep := arvados.EndpointContainerRequestGet
+       var resp arvados.ContainerRequest
+       err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
+       return resp, err
+}
+
+func (conn *Conn) ContainerRequestList(ctx context.Context, options arvados.ListOptions) (arvados.ContainerRequestList, error) {
+       ep := arvados.EndpointContainerRequestList
+       var resp arvados.ContainerRequestList
+       err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
+       return resp, err
+}
+
+func (conn *Conn) ContainerRequestDelete(ctx context.Context, options arvados.DeleteOptions) (arvados.ContainerRequest, error) {
+       ep := arvados.EndpointContainerRequestDelete
+       var resp arvados.ContainerRequest
+       err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
+       return resp, err
+}
+
 func (conn *Conn) SpecimenCreate(ctx context.Context, options arvados.CreateOptions) (arvados.Specimen, error) {
        ep := arvados.EndpointSpecimenCreate
        var resp arvados.Specimen
@@ -327,25 +377,25 @@ func (conn *Conn) UserUpdateUUID(ctx context.Context, options arvados.UpdateUUID
        return resp, err
 }
 func (conn *Conn) UserMerge(ctx context.Context, options arvados.UserMergeOptions) (arvados.User, error) {
-       ep := arvados.EndpointUserUpdateUUID
+       ep := arvados.EndpointUserMerge
        var resp arvados.User
        err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
        return resp, err
 }
 func (conn *Conn) UserActivate(ctx context.Context, options arvados.UserActivateOptions) (arvados.User, error) {
-       ep := arvados.EndpointUserUpdateUUID
+       ep := arvados.EndpointUserActivate
        var resp arvados.User
        err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
        return resp, err
 }
 func (conn *Conn) UserSetup(ctx context.Context, options arvados.UserSetupOptions) (map[string]interface{}, error) {
-       ep := arvados.EndpointUserUpdateUUID
+       ep := arvados.EndpointUserSetup
        var resp map[string]interface{}
        err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
        return resp, err
 }
 func (conn *Conn) UserUnsetup(ctx context.Context, options arvados.GetOptions) (arvados.User, error) {
-       ep := arvados.EndpointUserUpdateUUID
+       ep := arvados.EndpointUserUnsetup
        var resp arvados.User
        err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
        return resp, err
@@ -389,6 +439,7 @@ func (conn *Conn) APIClientAuthorizationCurrent(ctx context.Context, options arv
 }
 
 type UserSessionAuthInfo struct {
+       UserUUID        string   `json:"user_uuid"`
        Email           string   `json:"email"`
        AlternateEmails []string `json:"alternate_emails"`
        FirstName       string   `json:"first_name"`
@@ -409,8 +460,15 @@ func (conn *Conn) UserSessionCreate(ctx context.Context, options UserSessionCrea
 }
 
 func (conn *Conn) UserBatchUpdate(ctx context.Context, options arvados.UserBatchUpdateOptions) (arvados.UserList, error) {
-       ep := arvados.APIEndpoint{Method: "PATCH", Path: "arvados/v1/users/batch_update"}
+       ep := arvados.EndpointUserBatchUpdate
        var resp arvados.UserList
        err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
        return resp, err
 }
+
+func (conn *Conn) UserAuthenticate(ctx context.Context, options arvados.UserAuthenticateOptions) (arvados.APIClientAuthorization, error) {
+       ep := arvados.EndpointUserAuthenticate
+       var resp arvados.APIClientAuthorization
+       err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
+       return resp, err
+}