Don Newton | 379ae25 | 2019-04-01 12:17:06 -0400 | [diff] [blame^] | 1 | // Copyright (C) MongoDB, Inc. 2017-present. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 4 | // not use this file except in compliance with the License. You may obtain |
| 5 | // a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | |
| 7 | package driver |
| 8 | |
| 9 | import ( |
| 10 | "context" |
| 11 | |
| 12 | "github.com/mongodb/mongo-go-driver/mongo/options" |
| 13 | "github.com/mongodb/mongo-go-driver/x/bsonx" |
| 14 | "github.com/mongodb/mongo-go-driver/x/mongo/driver/session" |
| 15 | "github.com/mongodb/mongo-go-driver/x/mongo/driver/topology" |
| 16 | "github.com/mongodb/mongo-go-driver/x/mongo/driver/uuid" |
| 17 | "github.com/mongodb/mongo-go-driver/x/network/command" |
| 18 | "github.com/mongodb/mongo-go-driver/x/network/description" |
| 19 | "github.com/mongodb/mongo-go-driver/x/network/result" |
| 20 | ) |
| 21 | |
| 22 | // ListDatabases handles the full cycle dispatch and execution of a listDatabases command against the provided |
| 23 | // topology. |
| 24 | func ListDatabases( |
| 25 | ctx context.Context, |
| 26 | cmd command.ListDatabases, |
| 27 | topo *topology.Topology, |
| 28 | selector description.ServerSelector, |
| 29 | clientID uuid.UUID, |
| 30 | pool *session.Pool, |
| 31 | opts ...*options.ListDatabasesOptions, |
| 32 | ) (result.ListDatabases, error) { |
| 33 | |
| 34 | ss, err := topo.SelectServer(ctx, selector) |
| 35 | if err != nil { |
| 36 | return result.ListDatabases{}, err |
| 37 | } |
| 38 | |
| 39 | conn, err := ss.Connection(ctx) |
| 40 | if err != nil { |
| 41 | return result.ListDatabases{}, err |
| 42 | } |
| 43 | defer conn.Close() |
| 44 | |
| 45 | // If no explicit session and deployment supports sessions, start implicit session. |
| 46 | if cmd.Session == nil && topo.SupportsSessions() { |
| 47 | cmd.Session, err = session.NewClientSession(pool, clientID, session.Implicit) |
| 48 | if err != nil { |
| 49 | return result.ListDatabases{}, err |
| 50 | } |
| 51 | defer cmd.Session.EndSession() |
| 52 | } |
| 53 | |
| 54 | ld := options.MergeListDatabasesOptions(opts...) |
| 55 | if ld.NameOnly != nil { |
| 56 | cmd.Opts = append(cmd.Opts, bsonx.Elem{"nameOnly", bsonx.Boolean(*ld.NameOnly)}) |
| 57 | } |
| 58 | |
| 59 | return cmd.RoundTrip(ctx, ss.Description(), conn) |
| 60 | } |