Merge branch '16997-sort-config-for-diff'
[arvados.git] / lib / controller / localdb / conn.go
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 package localdb
6
7 import (
8         "context"
9         "fmt"
10         "strings"
11
12         "git.arvados.org/arvados.git/lib/controller/railsproxy"
13         "git.arvados.org/arvados.git/lib/controller/rpc"
14         "git.arvados.org/arvados.git/sdk/go/arvados"
15 )
16
17 type railsProxy = rpc.Conn
18
19 type Conn struct {
20         cluster     *arvados.Cluster
21         *railsProxy // handles API methods that aren't defined on Conn itself
22         loginController
23 }
24
25 func NewConn(cluster *arvados.Cluster) *Conn {
26         railsProxy := railsproxy.NewConn(cluster)
27         railsProxy.RedactHostInErrors = true
28         var conn Conn
29         conn = Conn{
30                 cluster:    cluster,
31                 railsProxy: railsProxy,
32         }
33         conn.loginController = chooseLoginController(cluster, &conn)
34         return &conn
35 }
36
37 // Logout handles the logout of conn giving to the appropriate loginController
38 func (conn *Conn) Logout(ctx context.Context, opts arvados.LogoutOptions) (arvados.LogoutResponse, error) {
39         return conn.loginController.Logout(ctx, opts)
40 }
41
42 // Login handles the login of conn giving to the appropriate loginController
43 func (conn *Conn) Login(ctx context.Context, opts arvados.LoginOptions) (arvados.LoginResponse, error) {
44         return conn.loginController.Login(ctx, opts)
45 }
46
47 // UserAuthenticate handles the User Authentication of conn giving to the appropriate loginController
48 func (conn *Conn) UserAuthenticate(ctx context.Context, opts arvados.UserAuthenticateOptions) (arvados.APIClientAuthorization, error) {
49         return conn.loginController.UserAuthenticate(ctx, opts)
50 }
51
52 func (conn *Conn) GroupContents(ctx context.Context, options arvados.GroupContentsOptions) (arvados.ObjectList, error) {
53         // The requested UUID can be a user (virtual home project), which we just pass on to
54         // the API server.
55         if strings.Index(options.UUID, "-j7d0g-") != 5 {
56                 return conn.railsProxy.GroupContents(ctx, options)
57         }
58
59         var resp arvados.ObjectList
60
61         // Get the group object
62         respGroup, err := conn.GroupGet(ctx, arvados.GetOptions{UUID: options.UUID})
63         if err != nil {
64                 return resp, err
65         }
66
67         // If the group has groupClass 'filter', apply the filters before getting the contents.
68         if respGroup.GroupClass == "filter" {
69                 if filters, ok := respGroup.Properties["filters"].([]interface{}); ok {
70                         for _, f := range filters {
71                                 // f is supposed to be a []string
72                                 tmp, ok2 := f.([]interface{})
73                                 if !ok2 || len(tmp) < 3 {
74                                         return resp, fmt.Errorf("filter unparsable: %T, %+v, original field: %T, %+v\n", tmp, tmp, f, f)
75                                 }
76                                 var filter arvados.Filter
77                                 if attr, ok2 := tmp[0].(string); ok2 {
78                                         filter.Attr = attr
79                                 } else {
80                                         return resp, fmt.Errorf("filter unparsable: attribute must be string: %T, %+v, filter: %T, %+v\n", tmp[0], tmp[0], f, f)
81                                 }
82                                 if operator, ok2 := tmp[1].(string); ok2 {
83                                         filter.Operator = operator
84                                 } else {
85                                         return resp, fmt.Errorf("filter unparsable: operator must be string: %T, %+v, filter: %T, %+v\n", tmp[1], tmp[1], f, f)
86                                 }
87                                 filter.Operand = tmp[2]
88                                 options.Filters = append(options.Filters, filter)
89                         }
90                 } else {
91                         return resp, fmt.Errorf("filter unparsable: not an array\n")
92                 }
93                 // Use the generic /groups/contents endpoint for filter groups
94                 options.UUID = ""
95         }
96
97         return conn.railsProxy.GroupContents(ctx, options)
98 }