Provisioning Cluster Resources

    +
    Provisioning cluster resources is managed at the collection or bucket level, depending upon the service affected. Common use cases are outlined here, less common use cases are covered in the API docs.

    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 PHP SDK also comes with some convenience functionality for common Couchbase management requests.

    When using a Couchbase version earlier than 6.5, you must create a valid Bucket connection using cluster.Bucket(name) before you can use cluster level managers.

    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.Buckets() method.

    The CreateBucketSettings and BucketSettings structs are used for creating and updating buckets, BucketSettings is also used for exposing information about existing buckets.

    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.

    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 can flush a bucket in the SDK by using the flush() method:

    The flush() operation may fail if the bucket does not have flush enabled, in that case it will return an ErrBucketNotFlushable.

    Collection Management

    The CollectionManager interface may be used to create and delete scopes and collections from the Couchbase cluster. It is instantiated through the Bucket.collections() method. Refer to the CollectionManager API documentation for further details.

    $options = new \Couchbase\ClusterOptions();
    $options->credentials("scopeAdmin", "password");
    $cluster = new \Couchbase\Cluster("localhost", $options);
    $bucket = $cluster->bucket("travel-sample");
    
    $collections = $bucket->collections();

    You can create a scope:

    try {
        $collections->createScope("example-scope");
    }
    catch (Exception $e) {
        print $e;
    }

    You can then create a collection within that scope:

    $collection = new \Couchbase\CollectionSpec();
    $collection->setName("example-collection");
    $collection->setScopeName("example-scope");
    
    try {
        $collections->createCollection($collection);
    }
    catch (Exception $e) {
        print $e;
    }

    Finally, you can drop unneeded collections and scopes:

    $collections->dropCollection($collection);
    
    $collections->dropScope("example-scope");

    Note that the most minimal permissions to create and drop a Scope or Collection is Manage Scopes along with Data Reader.

    You can create users with the appropriate RBAC programmatically:

    $users = $cluster->users();
    
    $user = new \Couchbase\User();
    $user->setUsername("scopeAdmin");
    $user->setDisplayName("Manage Scopes [travel-sample]");
    $user->setPassword("password");
    $user->setRoles([
        (new \Couchbase\Role)->setName("scope_admin")->setBucket("travel-sample"),
        (new \Couchbase\Role)->setName("data_reader")->setBucket("travel-sample")
    ]);
    
    $users->upsertUser($user);

    Listing the Scopes and Collections available

    You can enumerate Scopes and Collections using the CollectionManager and the properties of the ScopeSpec and CollectionSpec objects retrieved.

    $scopes = $collections->getAllScopes();
    foreach ($scopes as $scope) {
        print "Scope {$scope->name()}\n";
        
        foreach ($scope->collections() as $collection) {
            print " - {$collection->name()}\n";
        }
    }

    Index Management

    In general,you will rarely need to work with Index Managers from the SDK. For those occasions when you do, please see the relevant API docs:

    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 SDK, design documents are represented by the DesignDocument and View structs. All operations on design documents are performed on the ViewIndexManager instance: