Managing Clusters Using the Go SDK with Couchbase Server

The primary means for managing clusters is through the Couchbase Web UI which provides an easy to use interface for adding, removing, monitoring and modifying buckets.
In some instances you may wish to have a programmatic interface.
For example, if you wish to manage a cluster from a setup script, or if you are setting up buckets in test scaffolding.

The Go SDK also comes with some convenience functionality for common Couchbase management requests.

Management operations in the Go SDK may be performed through several interfaces depending on the object:

Creating and Removing Buckets

The ClusterManager interface may be used to create and delete buckets from the Couchbase cluster.
It is instantiated through the Cluster's Manager method, providing the administrative username and password.

To create a bucket, use the ClusterManager's InsertBucket(settings *BucketSettings) method.
The BucketSettings struct is also used to expose information about existing buckets (ClusterManager.GetBuckets() returns a slice of settings) or to update an existing bucket (ClusterManager.UpdateBucket(settings *BucketSettings)).

Note that any property that is not explicitly set when building the BucketSettings will use the default value.
In the case of the update, this is not necessarily the currently configured value, so you should be careful to set all properties to their correct expected values when updating an existing bucket configuration.

Only name and type parameters are mandatory for the BucketSettings.
Here is the list of parameters available:

Name: The name of the bucket (mandatory to create one, cannot be updated).

Type: The type of the bucket (mandatory to create one, cannot be changed).
Defaults to BucketType.Couchbase, but can also be BucketType.Memcached to create a cache bucket.

Quota: How much memory should each node use for the bucket.
This number is specified in megabytes.

Once you no longer need to use the bucket, you may delete the bucket using the ClusterManager.RemoveBucket(name string) method:

clusterManager.RemoveBucket("hello");

Flushing Buckets

When a bucket is flushed, all content is removed.
Because this operation is potentially dangerous it is disabled by default for each bucket.
Bucket flushing may be useful in test environments where it becomes a simpler alternative to removing and creating a test bucket.
You may enable bucket flushing on a per-bucket basis using the Couchbase Web Console or when creating a bucket.

You may flush a bucket in the Go SDK by using the BucketManager.Flush() method.

The flush operation may fail if the bucket does not have flush enabled:

{
"_": "Flush is disabled for the bucket"
}

Index Management

You can create and drop N1QL indexes using the SDK.
This is especially useful when setting up new applications, or simply when ensuring that a given bucket has certain indexes defined.
Indexes can be defined using actual N1QL statements or by using convenience functions within the SDK.

You can manage indexes in the Go SDK using the BucketManager interface, with its various N1QL related methods: GetIndexes(), CreateIndex(...), etc...

The following example creates a N1QL secondary index named "fooBar" on the "test" bucket, indexing fields "foo" and "bar":

View Management

Views are stored in design documents.
The SDK provides convenient methods to create, retrieve, and remove design documents.
To set up views, you create design documents that contain one or more view definitions, and then insert the design documents into a bucket.
Each view in a design document is represented by a name and a set of MapReduce functions.
The mandatory map function describes how to select and transform the data from the bucket, and the optional reduce function describes how to aggregate the results.

In the Go SDK, design documents are represented by the DesignDocument and View structs.
All operations on design documents are performed on a BucketManager instance.

To inspect design documents, you can either retrieve them by name (bucketManager.GetDesignDocument("landmarks")) or iterate through a slice of documents (bucketManager.GetDesignDocuments()).

To create or update design documents, use the InsertDesignDocument(ddoc *DesignDocument) and UpsertDesignDocument(ddoc *DesignDocument) methods.

The following example inserts a design document with two regular views and one spatial view into a bucket named travel-sample:

When you want to update an existing document with a new view (or a modification of a view’s definition), you can use the upsertDesignDocument method.

However, this method needs the list of views in the document to be exhaustive, meaning that if you just create the new view definition as previously and add it to a new DesignDocument that you upsert, all your other views will be erased!

The solution is to perform a getDesignDocument, add your view definition to the DesignDocument’s views list, then upsert it.
This also works with view modifications, provided the change is in the map or reduce functions (just reuse the same name for the modified View), or for deletion of one out of several views in the document.