Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
Size: Mime:
# Copyright (c) 2014 TrilioData, Inc.
# All Rights Reserved.

"""
Snapshots interface (1.1 extension).
"""

import six

try:
    from urllib import urlencode
except ImportError:
    from urllib.parse import urlencode

from workloadmgrclient import base


class Snapshot(base.Resource):
    """A workload snapshot either full or incremental"""

    def __repr__(self):
        return "<Snapshot: %s>" % self.id

    def delete(self):
        """Delete this snapshot."""
        return self.manager.delete(self)

    def cancel(self):
        """Cancels this snapshot."""
        return self.manager.cancel(self)

    def restore(self, test, name=None, description=None, options=None):
        """Restore the snapshot """
        return self.manager.restore(self, test, name, description, options)

    def mount(self, mount_vm_id, options=None):
        """Mount the snapshot """
        return self.manager.mount(self, mount_vm_id, options)

    def dismount(self):
        """DisMount the snapshot """
        return self.manager.dismount(self)

    def restore_network_topology(self, name=None, description=None):
        return self.manager.restore_network_topology(self, name, description)

    def restore_security_groups(self, name=None, description=None):
        return self.manager.restore_security_groups(self, name, description)


class SnapshotManager(base.ManagerWithFind):
    """Manage :class:`Snapshot` resources."""

    resource_class = Snapshot

    def get(self, snapshot_id, query_string=None):
        """Show details of a workload snapshot.
        :param snapshot_id: The ID of the workload snapshot to display.
        :rtype: :class:`Snapshot`
        """
        return self._get("/snapshots/%s" % snapshot_id, "snapshot")

    def set(self, search_opts=None):
        """
        """
        qparams = {}

        for opt, val in six.iteritems(search_opts):
            if val:
                qparams[opt] = val

        query_string = "?%s" % urlencode(qparams) if qparams else ""
        action = "/set"
        body = qparams
        return self._update("/snapshots/%s/%s" % (qparams.get('snapshot_id', ''), action), body)

    def list(self, detailed=False, search_opts=None):
        """Get a list of all workload snapshots
        :rtype: list of :class:`Snapshot`
        """
        if search_opts is None:
            search_opts = {}

        qparams = {}

        for opt, val in six.iteritems(search_opts):
            if val:
                qparams[opt] = val

        query_string = "?%s" % urlencode(qparams) if qparams else ""

        detail = ""
        if detailed:
            detail = "/detail"
        return self._list("/snapshots%s%s" % (detail, query_string), "snapshots")

    def delete(self, snapshot_id):
        """Delete a workload snapshot.
        :param snapshot_id: The :class:`Snapshot` to delete.
        """
        self._delete("/snapshots/%s" % base.getid(snapshot_id))

    def cancel(self, snapshot_id):
        """Cancel a workload snapshot.
        :param snapshot_id: The :class:`Snapshot` to cancel.
        """
        self._cancel("/snapshots/%s/cancel" % base.getid(snapshot_id))

    def restore(
        self, snapshot_id, test=False, name=None, description=None, options=None
    ):
        """restores a workload snapshot.
        :param snapshot_id: The :class:`Snapshot` to restore.
        """

        if test == True:
            body = {
                "testbubble": {
                    "name": name,
                    "description": description,
                    "options": options,
                }
            }
            self._post("/snapshots/%s?test=1" % base.getid(snapshot_id), body)
        else:
            body = {
                "restore": {
                    "name": name,
                    "description": description,
                    "options": options,
                }
            }
            self._post("/snapshots/%s" % base.getid(snapshot_id), body)

    def mount(self, snapshot_id, mount_vm_id, options=None):
        """mounts a workload snapshot.
        :param snapshot_id: The :class:`Snapshot` to mount.
        """

        body = {"mount": {"mount_vm_id": mount_vm_id, "options": options}}
        self._mount("/snapshots/%s/mount" % base.getid(snapshot_id), body)

    def dismount(self, snapshot_id, options=None):
        """dismounts a workload snapshot.
        :param snapshot_id: The :class:`Snapshot` to dismount.
        """

        body = {"mount": {"options": options}}
        self._post("/snapshots/%s/dismount" % base.getid(snapshot_id), body)

    def snapshot_mounted_list(self, workload_id):
        """
        get mounted snapshots list
        """
        if workload_id is not None:
            return self._snapshot_mounted_list(
                "/workloads/%s/snapshots/mounted/list" % workload_id
            )
        else:
            return self._snapshot_mounted_list("/snapshots/mounted/list")

    def restore_network_topology(
            self, snapshot_obj, name=None, description=None
    ):
        body = {"restore": {"name": name, "description": description}}
        self._post(
            "/snapshots/%s/restore/networktopology" % base.getid(snapshot_obj),
            body
        )

    def restore_security_groups(
            self, snapshot_obj, name=None, description=None
    ):
        body = {"restore": {"name": name, "description": description}}
        self._post(
            "/snapshots/%s/restore/securitygroups" % base.getid(snapshot_obj),
            body
        )

    def get_next_snapshot_id(self, workload_id, snapshot_id):
        """

            * API Call to get next Snapshot
            * Request_url:- /workloads/<uuid>/snapshots/<uuid>/next
            * Response_url:- {
                                "id": ["Snapshot_id", "next_snapshot_id"]
                                }
        Args:
            workload_id: int, uuid
            snapshot_id: int, uuid

        Returns: dict

        """
        url = "/workloads/{}/snapshots/{}/next".format(workload_id, snapshot_id)
        return self._get(url)

    def get_prev_snapshot_id(self, workload_id, snapshot_id):
        """
            * API Call to get previous Snapshot
            * Request_url:- /workloads/<uuid>/snapshots/<uuid>/prev
            * Response_url:- {
                                "id": ["Snapshot_id", "previous_snapshot_id"]
                                }
        Args:
            workload_id: int, uuid
            snapshot_id: int, uuid

        Returns: dict

        """
        url = "/workloads/{}/snapshots/{}/prev".format(workload_id, snapshot_id)
        return self._get(url)