Search code examples
pythonconnectionopenstacksnapshotvolumes

How to create a volume from snapshot openstack using connection in python?


I am trying to create a volume from a snapshot in openstack using the python api. Using the UI this is easily doable by pressing the action Create Volume in the snapshot overview.

When looking at the python documentation though, I can't find the required method. I found create_volume_snapshot which creates the snapshot, but I am unable to find a method that creates a volume from a snapshot. I also found the parameter where you can handover an image to create a volume, but nothing for snapshot. I must be blind.

This is the documentation: https://docs.openstack.org/openstacksdk/latest/user/connection.html


Solution

  • It is impossible to create a volume from snapshot using only connection. However, one can use the cinderclient (python api) for it. A minimal example would be:

    from keystoneauth1 import loading
    from keystoneauth1 import session
    from cinderclient import client
    def create_cinder(self, version=3):
        loader = loading.get_plugin_loader('password')
        auth = loader.load_from_options(auth_url=os.environ["OS_AUTH_URL"],
                                        username=os.environ["OS_USERNAME"],
                                        password=os.environ["OS_PASSWORD"],
                                        project_id=os.environ["OS_PROJECT_ID"],
                                        user_domain_name=os.environ["OS_USER_DOMAIN_NAME"])
        sess = session.Session(auth=auth)
        return client.Client(version, session=sess)
    cinder = create_cinder()
    cinder.volumes.create(size="size", snapshot_id="id", name="name",
                                               description="description")
    

    This example only works if you enter possible values for size id name and description and set the environment variables correctly.