Red Hat Training

A Red Hat training course is available for Red Hat Gluster Storage

A.4. Python SDK Example: Volumes

Create Volume
When a volume is created, the volume object needs a Red Hat Storage volume name, volume type and the bricks to be associated with the volume. Before creating a volume, you need to build a corresponding cluster object and associate that cluster with brick objects.
try:
    api = API(url=URL,
              username=USERNAME,
              password=PASSWORD,
              insecure=True)

    brick1 = params.GlusterBrick(
        brick_dir='/exports/music_1',
        server_id='0f5a7016-bb36-4f2b-a226-3b16c3eeca0c')

    brick2 = params.GlusterBrick(
        brick_dir='/exports/music_2',
        server_id='0f5a7016-bb36-4f2b-a226-3b16c3eeca0c')

    bricksParam = params.GlusterBricks()
    bricksParam.add_brick(brick1)
    bricksParam.add_brick(brick2)

    volume1 = params.GlusterVolume(
        name="music",
        cluster=api.clusters.get(name="Gluster"),
        volume_type="distribute",
        bricks=bricksParam)

    api.clusters.get("Gluster").glustervolumes.add(volume1)
    api.disconnect()

except Exception as e:
    print "Unexpected error: %s" % e
You can retrieve the server ID using the api.host collection.
glusterVolume Delete
def glusterVolumeDelete(clusterName, volumeName):
    try:
        API.clusters.get(clusterName).glustervolumes.get(volumeName).delete()
        return True
    except:
        return False
glusterVolumeStart
To start a volume, you need to pass the cluster name with which the Red Hat Storage volume is associated and the corresponding volume name to the glusterVolumeStart() function.
def glusterVolumeStart(clusterName, volumeName):
    try:
        API.clusters.get(clusterName).glustervolumes.get(volumeName).start()
        return True
    except:
        return False
glusterVolumeStop
To stop a volume, you need to pass the cluster name with which the Red Hat Storage volume is associated and the corresponding volume name to the glusterVolumeStart() function.
def glusterVolumeStop(clusterName, volumeName):
    try:
        API.clusters.get(clusterName).glustervolumes.get(volumeName).stop()
        return True
    except:
        return False
Rebalance operations: startRebalance, stopRebalance, and rebalanceStatus
To rebalance data among the servers, you need to pass the cluster name and the volume name to the startRebalance and stopRebalance functions and the jobID to the rebalanceStatus function.
import ovirtsdk
from ovirtsdk.api import API
from ovirtsdk.xml import params

# This function returns a stop rebalance task instance. Using this
# we can reterive the status of the stop rebalance.
# ex. volume = stopRebalance(clusterName, volumeName)
#     print volume.status.state

def stopRebalance(clusterName, volumeName):
    volume = api.clusters.get(clusterName).glustervolumes.get(volumeName)
    try:
        return volume.stoprebalance()
    except ovirtsdk.infrastructure.errors.RequestError, e:
        print "Error"

def startRebalance(clusterName, volumeName):
    volume = api.clusters.get(clusterName).glustervolumes.get(volumeName)
    return volume.rebalance()

def rebalanceStatus(jobId):
    # jobs.get accepts job name and job id as an optional
    # parameters to provide the job details.
    return api.jobs.get(None, jobId)

clusterName="Default"   # name of the cluser
volumeName="music"      # name of the volume which belongs
                        # to the purticular cluster
try:
    api = API (url="https://HOST",
               username="USER",
               password="PASS",
               ca_file="ca.crt")

    status = startRebalance(clusterName, volumeName)
    jobId = status.job.get_id()
    # jobId return by startRebalance can be stored somewhere
    # for later use. With out the jobId it would be very
    # difficult to find out the actual status of this task.
    print jobId
    privStatus = None
    while True:
        status = rebalanceStatus(jobId)
        jobStatus = status.get_status().get_state()
        if jobStatus != privStatus:
            print "Description: ", status.get_description()
            print "Status:      ", jobStatus
        if "FINISHED" == jobStatus:
            break
        privStatus = jobStatus

    api.disconnect()
except Exception as ex:
    print "Unexpected error: %s" % ex
22632%2C+Console+Developer+Guide-322-09-2014+17%3A11%3A35Report a bug