0
  8   Íˆl–Ðz¾”Šj"Tâ‚À½q¶”Å£�þ[d,¬ãe#qº•à?ã„Dã¿âÕÅà ?÷     Í# 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.
"""
GiG G8 Driver

"""

import json

from libcloud.compute.base import (
    Node,
    NodeSize,
    NodeImage,
    UuidMixin,
    NodeDriver,
    StorageVolume,
    NodeAuthSSHKey,
)
from libcloud.common.gig_g8 import G8Connection
from libcloud.compute.types import Provider, NodeState
from libcloud.common.exceptions import BaseHTTPError


class G8ProvisionError(Exception):
    pass


class G8PortForward(UuidMixin):
    def __init__(self, network, node_id, publicport, privateport, protocol, driver):
        self.node_id = node_id
        self.network = network
        self.publicport = int(publicport)
        self.privateport = int(privateport)
        self.protocol = protocol
        self.driver = driver
        UuidMixin.__init__(self)

    def destroy(self):
        self.driver.ex_delete_portforward(self)


class G8Network(UuidMixin):
    """
    G8 Network object class.

    This class maps to a cloudspace

    """

    def __init__(self, id, name, cidr, publicipaddress, driver, extra=None):
        self.id = id
        self.name = name
        self._cidr = cidr
        self.driver = driver
        self.publicipaddress = publicipaddress
        self.extra = extra
        UuidMixin.__init__(self)

    @property
    def cidr(self):
        """
        Cidr is not part of the list result
        we will lazily fetch it with a get request
        """
        if self._cidr is None:
            networkdata = self.driver._api_request("/cloudspaces/get", {"cloudspaceId": self.id})
            self._cidr = networkdata["privatenetwork"]
        return self._cidr

    def list_nodes(self):
        return self.driver.list_nodes(self)

    def destroy(self):
        return self.driver.ex_destroy_network(self)

    def list_portforwards(self):
        return self.driver.ex_list_portforwards(self)

    def create_portforward(self, node, publicport, privateport, protocol="tcp"):
        return self.driver.ex_create_portforward(self, node, publicport, privateport, protocol)


class G8NodeDriver(NodeDriver):
    """
    GiG G8 node driver

    """

    NODE_STATE_MAP = {
        "VIRTUAL": NodeState.PENDING,
        "HALTED": NodeState.STOPPED,
        "RUNNING": NodeState.RUNNING,
        "DESTROYED": NodeState.TERMINATED,
        "DELETED": NodeState.TERMINATED,
        "PAUSED": NodeState.PAUSED,
        "ERROR": NodeState.ERROR,
        # transition states
        "DEPLOYING": NodeState.PENDING,
        "STOPPING": NodeState.STOPPING,
        "MOVING": NodeState.MIGRATING,
        "RESTORING": NodeState.PENDING,
        "STARTING": NodeState.STARTING,
        "PAUSING": NodeState.PENDING,
        "RESUMING": NodeState.PENDING,
        "RESETTING": NodeState.REBOOTING,
        "DELETING": NodeState.TERMINATED,
        "DESTROYING": NodeState.TERMINATED,
        "ADDING_DISK": NodeState.RECONFIGURING,
        "ATTACHING_DISK": NodeState.RECONFIGURING,
        "DETACHING_DISK": NodeState.RECONFIGURING,
        "ATTACHING_NIC": NodeState.RECONFIGURING,
        "DETTACHING_NIC": NodeState.RECONFIGURING,
        "DELETING_DISK": NodeState.RECONFIGURING,
        "CHANGING_DISK_LIMITS": NodeState.RECONFIGURING,
        "CLONING": NodeState.PENDING,
        "RESIZING": NodeState.RECONFIGURING,
     