Creating Clone

The following script creates a Clone. This script is used to create a regular full backup using QuickOPC and is used to build the environment. For more information, see the description of Creating Regular Full Backups with QuickOPC.

The sequence of script processing is as follows:

  1. Create a Clone using POST /volume/{volume_id}/clone API.

quickopc_create.py


import sys

from eternus_rest import EtdxBaseApi

# Change the following values for your environment.
storage_url = "https://192.168.0.1:5665"
storage_user_name = "username"
storage_password = "password"

# Change the following values as required.
# Volume ID to create clone.
volume_id = 100001
# TPP ID to create a clone.
tpp_id = 0
# Name of the clone.
clone_name = "clone#0"


class EtdxApi(EtdxBaseApi):
    def __init__(self, base_url, boxid=None, proxies=None):
        super().__init__(base_url, boxid=boxid, proxies=proxies)

    def quickopc_create(self, volume_id, tpp_id, clone_name):
        """Create clone using QuickOPC

        Create clone volume and QuickOPC session.

        Args:
            volume_id (int): Volume ID to create clone.
            tpp_id (int): TPP ID to create the clone.
            clone_name (str): Name of the clone.

        Returns:
            int: Clone volume ID. If failed, returns a value of -1.
              This value cannot be used as volume ID.
        """
        body = {
            "name": clone_name,
            "tpp_id": tpp_id,
            "is_data_tracking_disabled": False
        }
        r = super().post("/api/v1/volume/{0!s}/clone".format(volume_id), body)
        if r.status_code != 202:
            print("Failed to request a clone creation.")
            print(r)
            print(r.json())
            return -1

        # Wait for the job completion
        job_r = super().wait_job(r.json()["job_id"])
        if job_r.json()["status"] != "Success":
            print("Failed to create a clone.")
            print(job_r)
            print(job_r.json())
            return -1
        backup_volume_href = job_r.json()["resource_href_list"][0]
        backup_volume_id = int(backup_volume_href.split("/")[4])

        return backup_volume_id


def main():
    storage = EtdxApi(storage_url)

    # Login
    if not storage.login(storage_user_name, storage_password):
        return False

    print("Creating a clone of the volume (ID: {0!s}) using QuickOPC."
          .format(volume_id))

    # Create clone using QuickOPC
    clone_volume_id = storage.quickopc_create(volume_id, tpp_id, clone_name)
    if clone_volume_id == -1:
        return False
    print("Created clone volume ID:", clone_volume_id)

    # Get the clone information
    r = storage.get("/api/v1/volume/{0!s}/copysession".format(clone_volume_id))
    print(r.json())

    # Variable declaration for other QuickOPC script
    print()
    print("--- Variable declaration for other QuickOPC script ---")
    print("clone_pair_list = [({0!s}, {1!s})]"
          .format(volume_id, clone_volume_id))

    # Logout
    storage.logout()
    return True


if __name__ == '__main__':
    if not main():
        sys.exit(1)