libcloud-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From GitBox <...@apache.org>
Subject [GitHub] [libcloud] Kami commented on a change in pull request #1305: adding gridscale driver to libcloud
Date Tue, 09 Jul 2019 15:41:00 GMT
Kami commented on a change in pull request #1305: adding gridscale driver to libcloud
URL: https://github.com/apache/libcloud/pull/1305#discussion_r301657824
 
 

 ##########
 File path: libcloud/compute/drivers/gridscale.py
 ##########
 @@ -0,0 +1,977 @@
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License.  You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+import json
+import time
+
+from libcloud.common.gridscale import GridscaleBaseDriver
+from libcloud.common.gridscale import GridscaleConnection
+from libcloud.compute.base import NodeImage, NodeLocation, VolumeSnapshot, \
+    Node, StorageVolume, KeyPair, NodeState, StorageVolumeState, NodeAuthSSHKey
+from libcloud.compute.providers import Provider
+from libcloud.utils.iso8601 import parse_date
+
+
+class GridscaleIp(object):
+    """
+    Ip Object
+
+    :param id: uuid
+    :type id: ``str``
+    :param family: family of ip (v4 or v6)
+    :type family: ``str``
+    :param prefix: prefix of ip
+    :type prefix: ``str``
+    :param ip_address: Ip address
+    :type ip_address: ``str``
+    :param create_time: Time ip was created
+    :type create_time: ``str``
+    """
+
+    def __init__(self, id, family, prefix, create_time, address):
+        self.id = id
+        self.family = family
+        self.prefix = prefix
+        self.create_time = create_time
+        self.ip_address = address
+
+    def __repr__(self):
+        return ('Ip: id={}, family={}, prefix={}, create_time={},Ip_address={}'
+                .format(self.id, self.family,
+                        self.prefix,
+                        self.create_time,
+                        self.ip_address))
+
+
+class GridscaleNetwork(object):
+    """
+    Network Object
+
+    :param id: uuid
+    :type id: ``str``
+    :param name: Name of Network
+    :type name: ``str``
+    :param status: Network status
+    :type status: ``str``
+    :param relations: object related to network
+    :type relations: ``object``
+    :param create_time: Time Network was created
+    :type create_time: ``str``
+    """
+
+    def __init__(self, id, name, status, create_time, relations):
+        self.id = id
+        self.name = name
+        self.status = status
+        self.create_time = create_time
+        self.relations = relations
+
+    def __repr__(self):
+        return ('Network: id={}, name={}, status={}, create_time={}, '
+                'relations={}'.format(self.id, self.name, self.status,
+                                      self.create_time, self.relations))
+
+
+class GridscaleNodeDriver(GridscaleBaseDriver):
+    """
+    create and entry in libcloud/compute/providers for gridscale
+    """
+    connectionCls = GridscaleConnection
+    type = Provider.GRIDSCALE
+    name = 'gridscale'
+    website = 'https://gridscale.io'
+
+    def __init__(self, user_id, key, **kwargs):
+        super(GridscaleNodeDriver, self).__init__(user_id, key, **kwargs)
+
+    def list_nodes(self):
+        """
+        List all nodes.
+
+        :return: List of node objects
+        :rtype: ``list`` of :class:`.Node`
+        """
+        result = self._sync_request(data=None, endpoint='objects/servers/')
+        nodes = []
+        for key, value in self._get_response_dict(result).items():
+            node = self._to_node(value)
+            nodes.append(node)
+            continue
+
+        return sorted(nodes, key=lambda sort: sort.created_at)
+
+    def list_locations(self):
+        """
+        List all available data centers.
+
+        :return: List of node location objects
+        :rtype: ``list`` of :class:`.NodeLocation`
+        """
+        locations = []
+        result = self._sync_request(endpoint='objects/locations/')
+        for key, value in self._get_response_dict(result).items():
+            location = self._to_location(value)
+            locations.append(location)
+            continue
+        return sorted(locations, key=lambda nod: nod.id)
+
+    def list_volumes(self):
+        """
+        List all volumes.
+
+        :return: List of StorageVolume object
+        :rtype: ``list`` of :class:`.StorageVolume`
+        """
+        volumes = []
+        result = self._sync_request(endpoint='objects/storages/')
+        for key, value in self._get_response_dict(result).items():
+            volume = self._to_volume(value)
+            volumes.append(volume)
+        return sorted(volumes, key=lambda sort: sort.extra['create_time'])
+
+    def ex_list_networks(self):
+        """
+        List all networks.
+
+        :return: List of objects.
+        :rtype: ``list`` of :class:`.GridscaleNetwork`
+        """
+        networks = []
+        result = self._sync_request(endpoint='objects/networks/')
+        for key, value in self._get_response_dict(result).items():
+            network = self._to_network(value)
+            networks.append(network)
+        return sorted(networks, key=lambda sort: sort.create_time)
+
+    def list_volume_snapshots(self, volume):
+        """
+        Lists all snapshots for storage volume.
+
+        :param volume: storage the snapshot is attached to
+        :type volume: :class:`.StorageVolume`
+
+        :return: Snapshots
+        :rtype: ``list`` of :class:`.VolumeSnapshot`
+        """
+        snapshots = []
+        result = self._sync_request(
+            endpoint='objects/storages/'
+                     '{}/snapshots'.format(volume.id))
+        for key, value in self._get_response_dict(result).items():
+            snapshot = self._to_volume_snapshot(value)
+            snapshots.append(snapshot)
+        return sorted(snapshots, key=lambda snapshot: snapshot.created)
+
+    def ex_list_ips(self):
+        """
+        Lists all IPs available.
+
+        :return: List of IP objects.
+        :rtype: ``list`` of :class:`.GridscaleIp`
+        """
+        ips = []
+        result = self._sync_request(endpoint='objects/ips/')
+        for key, value in self._get_response_dict(result).items():
+            ip = self._to_ip(value)
+            ips.append(ip)
+        return ips
+
+    def list_images(self):
+        """
+        List images.
+
+        :return: List of node image objects
+        :rtype: ``list`` of :class:`.NodeImage`
+        """
+        templates = []
+        result = self._sync_request(endpoint='objects/templates')
+        for key, value in self._get_response_dict(result).items():
+            template = self._to_node_image(value)
+            templates.append(template)
+        return sorted(templates, key=lambda sort: sort.name)
+
+    def create_node(self, name, size, image, location, auth):
+        """
+        Create a simple node  with a name, cores, memory at the designated
+        location.
+
+        :param name: Name of the server.
+        :type name: ``str``
+
+        :param size: Nodesize object.
+        :type size: :class:`.NodeSize`
+
+        :param image: OS image to attach to the storage.
+        :type image: :class:`.GridscaleTemplate`
+
+        :param location: The data center to create a node in.
+        :type location: :class:`.NodeLocation`
+
+        :param auth: sshkey uuid.
+        :type auth: :class:`.NodeAuthSSHKey`
+
+        :return: The newly created Node.
+        :rtype: :class:`.Node`
+
+        """
+        auth = NodeAuthSSHKey(auth)
+
+        if size.ram % 1024 != 0:
+            raise Exception('Value not accepted. Use a multiple of 1024 e.g.'
+                            '1024, 2048, 3096...')
+        data = {
+            'name': name,
+            'cores': size.extra['cores'],
+            'memory': int(size.ram / 1024),
+            'location_uuid':
+                location.id}
+        self.connection.async_request('objects/servers/',
+                                      data=json.dumps(data),
+                                      method='POST')
+
+        node = self._to_node(self._get_resource('servers', self.connection
+                                                .poll_response_initial
+                                                .object['object_uuid']))
+
+        volume = self._create_volume_from_template(name=image.extra['ostype'],
+                                                   size=size.disk,
+                                                   location=location,
+                                                   template={
+                                                   'template_uuid': image.id,
+                                                   'sshkeys': auth.pubkey})
+
+        ip = self.ex_create_ip(4, location, name + '_ip')
+
+        self.attach_volume(node, volume)
+        self.ex_link_ip_to_node(node, ip)
+        self.ex_link_network_to_node(node, self.ex_list_networks()[0])
+        self.ex_start_node(node)
+
+        return self._to_node(self._get_resource('servers', node.id))
+
+    def ex_create_ip(self, family, location, name):
+        """
+        Create either an ip_v4 ip or a ip_v6.
+
+        :param family: Defines if the ip is v4 or v6 with int 4 or int 6.
+        :type family: ``int``
+
+        :param location: Defines which datacenter the created ip
+                         responds with.
+        :type location: :class:`.NodeLocation`
+
+        :param name: Name of your Ip.
+        :type name: ``str``
+
+        :return: Ip
+        :rtype: :class:`.GridscaleIp`
+        """
+        self.connection.async_request(
+            'objects/ips/',
+            data=json.dumps({
+                'name': name,
+                'family': family,
+                'location_uuid': location.id}),
+            method='POST')
+
+        return self._to_ip(self._get_resource('ips', self.connection
+                                              .poll_response_initial
+                                              .object['object_uuid']))
+
+    def ex_create_networks(self, name, location):
+        """
+        Create a network at the data center location.
+
+        :param name: Name of the network.
+        :type name: ``str``
+
+        :param location: Location.
+        :type location: :class:`.NodeLocation`
+
+        :return: Network.
+        :rtype: :class:`.GridscaleNetwork`
+        """
+        self.connection.async_request(
+            'objects/networks',
+            data=json.dumps({
+                'name': name,
+                'location_uuid': location.id}),
+            method='POST')
+
+        return self._to_network(self._get_resource('network', self.connection
+                                                   .poll_response_initial
+                                                   .object['object_uuid']))
+
+    def create_volume(self, size, name, location=None, snapshot=None):
+        """
+        Create a new volume.
+
+        :param size: Integer in GB.
+        :type size: ``int``
+
+        :param name: Name of the volume.
+        :type name: ``str``
+
+        :param location: The server location.
+        :type location: :class:`.NodeLocation`
+
+        :param snapshot:  Snapshot from which to create the new
+                          volume.  (optional)
+        :type snapshot: :class:`.VolumeSnapshot`
+
+        :return: Newly created StorageVolume.
+        :rtype: :class:`.StorageVolume`
+        """
+
+        return self._create_volume_from_template(size, name, location)
+
+    def _create_volume_from_template(self, size, name, location=None,
+                                     template={}):
+        """
+        create Storage
+
+        :param name: name of your Storage unit
+        :type name: ``str``
+
+        :param size: Integer in GB.
+        :type size: ``int``
+
+        :param location: your server location
+        :type location: :class:`.NodeLocation`
+
+        :param template: template to shape the storage capacity to
+        :type template: ``dict``
+
+        :return: newly created StorageVolume
+        :rtype: :class:`.GridscaleVolumeStorage`
+        """
+        self.connection.async_request(
+            'objects/storages/',
+            data=json.dumps({
+                'name': name,
+                'capacity': size,
+                'location_uuid': location.id,
+                'template': template}),
+            method='POST')
+
+        return self._to_volume(self._get_resource('storages',
+                                                  self.connection
+                                                  .poll_response_initial
+                                                  .object['object_uuid']))
+
+    def create_volume_snapshot(self, volume, name):
+        """
+        Creates a snapshot of the current state of your volume,
+        you can rollback to.
+
+        :param volume: Volume you want to create a snapshot of.
+        :type volume: :class:`.StorageVolume`
+
+        :param name: Name of the snapshot.
+        :type name: ``str``
+
+        :return: VolumeSnapshot.
+        :rtype: :class:`.VolumeSnapshot`
+        """
+        self.connection.async_request(
+            'objects/storages/{}/snapshots'.format(volume.id),
+            data=json.dumps({
+                'name': name}),
+            method='POST')
+
+        return self._to_volume_snapshot(self._get_resource(
+            'storages/{}/snapshots'.format(volume.id), self.connection
+            .poll_response_initial.object['object_uuid']))
+
+    def create_image(self, node, name):
+        """
+        Creates an image from a node object.
+
+        :param node: Node to run the task on.
+        :type node: :class:`.Node`
+
+        :param name: Name for new image.
+        :type name: ``str``
+
+        :return: NodeImage.
+        :rtype: :class:`.NodeImage`
+        """
+        storage_dict = node.extra['relations']['storages'][0]
+        snapshot_uuid = ''
+        if storage_dict['bootdevice'] is True:
+            self.connection.async_request(
+                'objects/storages/{}/snapshots/'
+                .format(storage_dict['object_uuid']),
+                data=json.dumps({'name': name + '_snapshot'}),
+                method='POST')
+
+            snapshot_uuid = self.connection.poll_response_initial.object[
+                'object_uuid']
+
+            self.connection.async_request(
+                'objects/templates/',
+                data=json.dumps({
+                    'name': name,
+                    'snapshot_uuid': snapshot_uuid}),
+                method='POST')
+
+            snapshot_dict = self._get_response_dict(self._sync_request(
+                endpoint='objects/storages/{}/snapshots/{}'
+                .format(storage_dict['object_uuid'], snapshot_uuid)))
+
+            self.destroy_volume_snapshot(
+                self._to_volume_snapshot(snapshot_dict))
+
+        return self._to_node_image(self._get_resource(
+            'templates', self.connection.poll_response_initial.object[
+                'object_uuid']))
+
+    def destroy_node(self, node):
+        """
+        Destroy node.
+
+        :param node: Node object.
+        :type node: :class:`.Node`
+
+        :return: True if the destroy was successful, otherwise False.
+        :rtype: ``bool``
+        """
+        result = self._sync_request(endpoint='objects/servers/{}'
+                                    .format(node.id),
+                                    method='DELETE')
+        return result.status == 204
+
+    def destroy_volume(self, volume):
+        """
+        Delete volume.
+
+        :param volume: Volume to be destroyed.
+        :type volume: :class:`.StorageVolume`
+
+        :return: True if the destroy was successful, otherwise False.
+        :rtype: ``bool``
+        """
+        result = self._sync_request(endpoint='objects/storages/{}'
+                                    .format(volume.id),
+                                    method='DELETE')
+        return result.status == 204
+
+    def ex_destroy_ip(self, ip):
+        """
+        Delete an ip.
+
+        :param ip: IP object.
+        :type ip: :class:`.GridscaleIp`
+
+        :return: ``True`` if delete_image was successful, ``False`` otherwise.
+        :rtype: ``bool``
+        """
+        result = self._sync_request(endpoint='objects/ips/{}'
+                                    .format(ip.id),
+                                    method='DELETE')
+        return result.status == 204
+
+    def destroy_volume_snapshot(self, snapshot):
+        """
+        Destroy a snapshot.
+
+        :param snapshot: The snapshot to delete.
+        :type snapshot: :class:'.VolumeSnapshot`
+
+        :return: True if the destroy was successful, otherwise False.
+        :rtype: ``bool``
+        """
+        result = self._sync_request(endpoint='objects/storages/'
+                                             '{}/snapshots/{}/'
+                                    .format(snapshot.extra['parent_uuid'],
+                                            snapshot.id),
+                                    method='DELETE')
+        return result.status == 204
+
+    def ex_destroy_networks(self, network):
+        """
+        Delete network.
+
+        :param network: Network object.
+        :type network: :class:`.GridscaleNetwork`
+
+        :return: ``True`` if destroyed successfully, otherwise ``False``
+        :rtype: ``bool``
+        """
+        result = self._sync_request(endpoint='objects/networks/{}'
+                                    .format(network.id),
+                                    method='DELETE')
+        return result.status == 204
+
+    def delete_image(self, node_image):
+        """
+        Destroy an image.
+
+        :param node_image: Node image object.
+        :type node_image: :class:`.NodeImage`
+
+        :return: True if the destroy was successful, otherwise False
+        :rtype: ``bool``
+
+        """
+        result = self._sync_request(endpoint='objects/templates/{}'
+                                    .format(node_image.id),
+                                    method='DELETE')
+        return result.status == 204
+
+    def patch_node(self, node, name=None):
 
 Review comment:
   This method and ``patch_storage`` is not part of the standard Libcloud API, so it should
be prefixed with ``ex_`` (``ex_patch_node``, ``ex_patch_storage``).
   
   Actually, it looks like all those two methods do is rename a node / storage volume, so
they should probably be called something along the lines of ``ex_rename_node`` and ``ex_rename_volume``.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

Mime
View raw message