github.com/go-kivik/kivik/v4@v4.3.2/cluster.go (about) 1 // Licensed under the Apache License, Version 2.0 (the "License"); you may not 2 // use this file except in compliance with the License. You may obtain a copy of 3 // the License at 4 // 5 // http://www.apache.org/licenses/LICENSE-2.0 6 // 7 // Unless required by applicable law or agreed to in writing, software 8 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 9 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 10 // License for the specific language governing permissions and limitations under 11 // the License. 12 13 package kivik 14 15 import ( 16 "context" 17 18 "github.com/go-kivik/kivik/v4/driver" 19 ) 20 21 // ClusterStatus returns the current [cluster status]. 22 // 23 // [cluster status]: http://docs.couchdb.org/en/stable/api/server/common.html#cluster-setup 24 func (c *Client) ClusterStatus(ctx context.Context, options ...Option) (string, error) { 25 endQuery, err := c.startQuery() 26 if err != nil { 27 return "", err 28 } 29 defer endQuery() 30 cluster, ok := c.driverClient.(driver.Cluster) 31 if !ok { 32 return "", errClusterNotImplemented 33 } 34 return cluster.ClusterStatus(ctx, multiOptions(options)) 35 } 36 37 // ClusterSetup performs the requested [cluster action]. action should be 38 // an object understood by the driver. For the CouchDB driver, this means an 39 // object which is marshalable to a JSON object of the expected format. 40 // 41 // [cluster action]: http://docs.couchdb.org/en/stable/api/server/common.html#post--_cluster_setup 42 func (c *Client) ClusterSetup(ctx context.Context, action interface{}) error { 43 endQuery, err := c.startQuery() 44 if err != nil { 45 return err 46 } 47 defer endQuery() 48 cluster, ok := c.driverClient.(driver.Cluster) 49 if !ok { 50 return errClusterNotImplemented 51 } 52 return cluster.ClusterSetup(ctx, action) 53 } 54 55 // ClusterMembership contains the list of known nodes, and cluster nodes, as returned 56 // by [Client.Membership]. 57 type ClusterMembership struct { 58 AllNodes []string `json:"all_nodes"` 59 ClusterNodes []string `json:"cluster_nodes"` 60 } 61 62 // Membership returns a list of known CouchDB [nodes in the cluster]. 63 // 64 // [nodes in the cluster]: https://docs.couchdb.org/en/latest/api/server/common.html#get--_membership 65 func (c *Client) Membership(ctx context.Context) (*ClusterMembership, error) { 66 endQuery, err := c.startQuery() 67 if err != nil { 68 return nil, err 69 } 70 defer endQuery() 71 cluster, ok := c.driverClient.(driver.Cluster) 72 if !ok { 73 return nil, errClusterNotImplemented 74 } 75 nodes, err := cluster.Membership(ctx) 76 return (*ClusterMembership)(nodes), err 77 }