<commit_before># 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.

"""
Node driver for Aliyun.
"""

try:
    import simplejson as json
except ImportError:
    import json
import time
import base64
import hashlib

from libcloud.common.aliyun import AliyunXmlResponse, SignedAliyunConnection
from libcloud.common.types import LibcloudError
from libcloud.compute.base import Node, NodeDriver, NodeImage, NodeSize, \
    StorageVolume, VolumeSnapshot, NodeLocation, KeyPair
from libcloud.compute.types import NodeState, StorageVolumeState, \
    VolumeSnapshotState
from libcloud.utils.py3 import _real_unicode as u
from libcloud.utils.py3 import ensure_string, b
from libcloud.utils.xml import findall, findattr, findtext
from libcloud.utils.publickey import get_pubkey_ssh2_fingerprint
from libcloud.utils.publickey import get_pubkey_comment

__all__ = [
    'DiskCategory',
    'InternetChargeType',
    'ECS_API_VERSION',
    'ECSDriver',
    'ECSSecurityGroup',
    'ECSZone'
]

ECS_API_VERSION = '2014-05-26'
ECS_API_ENDPOINT = 'ecs.aliyuncs.com'
DEFAULT_SIGNATURE_VERSION = '1.0'


def _parse_bool(value):
    if isinstance(value, bool):
        return value
    if u(value).lower() == 'true':
        return True
    return False


"""
Define the extra dictionary for specific resources
"""
RESOURCE_EXTRA_ATTRIBUTES_MAP = {
    'node': {
        'description': {
            'xpath': 'Description',
            'transform_func': u
        },
        'image_id': {
            'xpath': 'ImageId',
            'transform_func': u
        },
        'zone_id': {
            'xpath': 'ZoneId',
            'transform_func': u
        },
        'instance_type': {
            'xpath': 'InstanceType',
            'transform_func': u
        },
        'instance_type_family': {
            'xpath': 'InstanceTypeFamily',
            'transform_func': u
        },
        'hostname': {
            'xpath': 'HostName',
            'transform_func': u
        },
        'serial_number': {
            'xpath': 'SerialNumber',
            'transform_func': u
        },
        'internet_charge_type': {
            'xpath': 'InternetChargeType',
            'transform_func': u
        },
        'creation_time': {
            'xpath': 'CreationTime',
            'transform_func': u
        },
        'instance_network_type': {
            'xpath': 'InstanceNetworkType',
            'transform_func': u
        },
        'instance_charge_type': {
            'xpath': 'InstanceChargeType',
            'transform_func': u
        },
        'device_available': {
            'xpath': 'DeviceAvailable',
            'transform_func': u
        },
        'io_optimized': {
            'xpath': 'IoOptimized',
            'transform_func': u
        },
        'expired_time': {
            'xpath': 'ExpiredTime',
            'transform_func': u
        }
    },
    'vpc_attributes': {
        'vpc_id': {
            'xpath': 'VpcId',
            'transform_func': u
        },
        'vswitch_id': {
            'xpath': 'VSwitchId',
            'transform_func': u
        },
        'private_ip_address': {
            'xpath': 'PrivateIpAddress/IpAddress',
            'transform_func': u
        },
        'nat_ip_address': {
            'xpath': 'NatIpAddress',
            'transform_func': u
        }
    },
    'eip_address_associate': {
        'allocation_id': {
            'xpath': 'AllocationId',
            'transform_func': u
        },
        'ip_address': {
            'xpath': 'IpAddress',
            'transform_func': u
        },
        'bandwidth': {
            'xpath': 'Bandwidth',
            'transform_func': int
        },
        'internet_charge_type': {
            'xpath': 'InternetChargeType',
            'transform_func': u
        }
    },
    'operation_locks': {
        'lock_reason': {
            'xpath': 'LockReason',
            'transform_func': u
        }
    },
    'volume': {
        'region_id': {
            'xpath': 'RegionId',
            'transform_func': u
        },
        'zone_id': {
            'xpath': 'ZoneId',
            'transform_func': u
        },
        'description': {
            'xpath': 'Description',
            'transform_func': u
        },
        'type': {
            'xpath': 'Type',
            'transform_func': u
        },
        'category': {
            'xpath': 'Category',
            'transform_func': u
        },
        'image_id': {
            'xpath': 'ImageId',
            'transform_func': u
        },
        'source_snapshot_id': {
            'xpath': 'SourceSnapshotId',
            'transform_func': u
        },
        'product_code': {
            'xpath': 'ProductCode',
            'transform_func': u
        },
        'portable': {
            'xpath': 'Portable',
            'transform_func': _parse_bool
        },
        'instance_id': {
            'xpath': 'InstanceId',
            'transform_func': u
        },
        'device': {
            'xpath': 'Device',
            'transform_func': u
        },
        'delete_with_instance': {
            'xpath': 'DeleteWithInstance',
            'transform_func': _parse_bool
        },
        'enable_auto_snapshot': {
            'xpath': 'EnableAutoSnapshot',
            'transform_func': _parse_bool
        },
        'creation_time': {
            'xpath': 'CreationTime',
            'transform_func': u
        },
        'attached_time': {
            'xpath': 'AttachedTime',
            'transform_func': u
        },
        'detached_time': {
            'xpath': 'DetachedTime',
            'transform_func': u
        },
        'disk_charge_type': {
            'xpath': 'DiskChargeType',
            'transform_func': u
        }
    },
    'snapshot': {
        'snapshot_name': {
            'xpath': 'SnapshotName',
            'transform_func': u
        },
        'description': {
            'xpath': 'Description',
            'transform_func': u
        },
        'progress': {
            'xpath': 'Progress',
            'transform_func': u
        },
        'source_disk_id': {
            'xpath': 'SourceDiskId',
            'transform_func': u
        },
        'source_disk_size': {
            'xpath': 'SourceDiskSize',
            'transform_func': int
        },
        'source_disk_type': {
            'xpath': 'SourceDiskType',
            'transform_func': u
        },
        'product_code': {
            'xpath': 'ProductCode',
            'transform_func': u
        },
        'usage': {
            'xpath': 'Usage',
            'transform_func': u
        }
    },
    'image': {
        'image_version': {
            'xpath': 'ImageVersion',
            'transform_func': u
        },
        'os_type': {
            'xpath': 'OSType',
            'transform_func': u
        },
        'platform': {
            'xpath': 'Platform',
            'transform_func': u
        },
        'architecture': {
            'xpath': 'Architecture',
            'transform_func': u
        },
        'description': {
            'xpath': 'Description',
            'transform_func': u
        },
        'size': {
            'xpath': 'Size',
            'transform_func': int
        },
        'image_owner_alias': {
            'xpath': 'ImageOwnerAlias',
            'transform_func': u
        },
        'os_name': {
            'xpath': 'OSName',
            'transform_func': u
        },
        'product_code': {
            'xpath': 'ProductCode',
            'transform_func': u
        },
        'is_subscribed': {
            'xpath': 'IsSubscribed',
            'transform_func': _parse_bool
        },
        'progress': {
            'xpath': 'Progress',
            'transform_func': u
        },
        'creation_time': {
            'xpath': 'CreationTime',
            'transform_func': u
        },
        'usage': {
            'xpath': 'Usage',
            'transform_func': u
        },
        'is_copied': {
            'xpath': 'IsCopied',
            'transform_func': _parse_bool
        }
    },
    'disk_device_mapping': {
        'snapshot_id': {
            'xpath': 'SnapshotId',
            'transform_func': u
        },
        'size': {
            'xpath': 'Size',
            'transform_func': int
        },
        'device': {
            'xpath': 'Device',
            'transform_func': u
        },
        'format': {
            'xpath': 'Format',
            'transform_func': u
        },
        'import_oss_bucket': {
            'xpath': 'ImportOSSBucket',
            'transform_func': u
        },
        'import_oss_object': {
            'xpath': 'ImportOSSObject',
            'transform_func': u
        }
    }
}


class ECSConnection(SignedAliyunConnection):
    """
    Represents a single connection to the Aliyun ECS Endpoint.
    """

    api_version = ECS_API_VERSION
    host = ECS_API_ENDPOINT
    responseCls = AliyunXmlResponse
    service_name = 'ecs'


class ECSSecurityGroup(object):
    """
    Security group used to control nodes internet and intranet accessibility.
    """
    def __init__(self, id, name, description=None, driver=None, vpc_id=None,
                 creation_time=None):
        self.id = id
        self.name = name
        self.description = description
        self.driver = driver
        self.vpc_id = vpc_id
        self.creation_time = creation_time

    def __repr__(self):
        return ('<ECSSecurityGroup: id=%s, name=%s, driver=%s ...>' %
                (self.id, self.name, self.driver.name))


class ECSSecurityGroupAttribute(object):

    """
    Security group attribute.
    """
    def __init__(self, ip_protocol=None, port_range=None,
                 source_group_id=None, policy=None, nic_type=None):
        self.ip_protocol = ip_protocol
        self.port_range = port_range
        self.source_group_id = source_group_id
        self.policy = policy
        self.nic_type = nic_type

    def __repr__(self):
        return ('<ECSSecurityGroupAttribute: ip_protocol=%s ...>' %
                (self.ip_protocol))


class ECSZone(object):
    """
    ECSZone used to represent an availability zone in a region.
    """
    def __init__(self, id, name, driver=None,
                 available_resource_types=None,
                 available_instance_types=None,
                 available_disk_categories=None):
        self.id = id
        self.name = name
        self.driver = driver
        self.available_resource_types = available_resource_types
        self.available_instance_types = available_instance_types
        self.available_disk_categories = available_disk_categories

    def __repr__(self):
        return ('<ECSZone: id=%s, name=%s, driver=%s>' %
                (self.id, self.name, self.driver))


class InternetChargeType(object):
    """
    Internet connection billing types for Aliyun Nodes.
    """
    BY_BANDWIDTH = 'PayByBandwidth'
    BY_TRAFFIC = 'PayByTraffic'


class DiskCategory(object):
    """
    Enum defined disk types supported by Aliyun system and data disks.
    """
    CLOUD = 'cloud'
    CLOUD_EFFICIENCY = 'cloud_efficiency'
    CLOUD_SSD = 'cloud_ssd'
    EPHEMERAL_SSD = 'ephemeral_ssd'


class Pagination(object):
    """
    Pagination used to describe the multiple pages results.
    """
    def __init__(self, total, size, current):
        """
        Create a pagination.

        :param total: the total count of the results
        :param size: the page size of each page
        :param current: the current page number, 1-based
        """
        self.total = total
        self.size = size
        self.current = current

    def next(self):
        """
        Switch to the next page.
        :return: the new pagination or None when no more page
        :rtype: ``Pagination``
        """
        if self.total is None or (self.size * self.current >= self.total):
            return None
        self.current += 1
        return self

    def to_dict(self):
        return {'PageNumber': self.current,
                'PageSize': self.size}

    def __repr__(self):
        return ('<Pagination total=%d, size=%d, current page=%d>' %
                (self.total, self.size, self.current))


class ECSDriver(NodeDriver):
    """
    Aliyun ECS node driver.

    Used for Aliyun ECS service.

    TODO:
    Get guest OS root password
    Adjust internet bandwidth settings
    Manage security groups and rules
    """

    name = 'Aliyun ECS'
    website = 'https://www.aliyun.com/product/ecs'
    connectionCls = ECSConnection
    features = {'create_node': ['password', 'ssh_key']}
    namespace = None
    path = '/'

    internet_charge_types = InternetChargeType
    disk_categories = DiskCategory

    NODE_STATE_MAPPING = {
        'Starting': NodeState.PENDING,
        'Running': NodeState.RUNNING,
        'Stopping': NodeState.PENDING,
        'Stopped': NodeState.STOPPED
    }

    VOLUME_STATE_MAPPING = {
        'In_use': StorageVolumeState.INUSE,
        'Available': StorageVolumeState.AVAILABLE,
        'Attaching': StorageVolumeState.ATTACHING,
        'Detaching': StorageVolumeState.INUSE,
        'Creating': StorageVolumeState.CREATING,
        'ReIniting': StorageVolumeState.CREATING}

    SNAPSHOT_STATE_MAPPING = {
        'progressing': VolumeSnapshotState.CREATING,
        'accomplished': VolumeSnapshotState.AVAILABLE,
        'failed': VolumeSnapshotState.ERROR}

    def list_nodes(self, ex_node_ids=None, ex_filters=None):
        """
        List all nodes.

        @inherits: :class:`NodeDriver.create_node`

        :keyword  ex_node_ids: a list of node's ids used to filter nodes.
                               Only the nodes which's id in this list
                               will be returned.
        :type   ex_node_ids: ``list`` of ``str``
        :keyword  ex_filters: node attribute and value pairs to filter nodes.
                              Only the nodes which matchs all the pairs will
                              be returned.
                              If the filter attribute need a json array value,
                              use ``list`` object, the driver will convert it.
        :type   ex_filters: ``dict``
        """

        params = {'Action': 'DescribeInstances',
                  'RegionId': self.region}

        if ex_node_ids:
            if isinstance(ex_node_ids, list):
                params['InstanceIds'] = self._list_to_json_array(ex_node_ids)
            else:
                raise AttributeError('ex_node_ids should be a list of '
                                     'node ids.')

        if ex_filters:
            if isinstance(ex_filters, dict):
                params.update(ex_filters)
            else:
                raise AttributeError('ex_filters should be a dict of '
                                     'node attributes.')

        nodes = self._request_multiple_pages(self.path, params,
                                             self._to_nodes)
        return nodes

    def list_sizes(self, location=None):
        params = {'Action': 'DescribeInstanceTypes'}

        resp_body = self.connection.request(self.path, params).object
        size_elements = findall(resp_body, 'InstanceTypes/InstanceType',
                                namespace=self.namespace)
        sizes = [self._to_size(each) for each in size_elements]
        return sizes

    def list_locations(self):
        params = {'Action': 'DescribeRegions'}

        resp_body = self.connection.request(self.path, params).object
        location_elements = findall(resp_body, 'Regions/Region',
                                    namespace=self.namespace)
        locations = [self._to_location(each) for each in location_elements]
        return locations

    def create_node(self, name, size, image, auth=None,
                    ex_security_group_id=None, ex_description=None,
                    ex_internet_charge_type=None,
                    ex_internet_max_bandwidth_out=None,
                    ex_internet_max_bandwidth_in=None,
                    ex_hostname=None, ex_io_optimized=None,
                    ex_system_disk=None, ex_data_disks=None,
                    ex_vswitch_id=None, ex_private_ip_address=None,
                    ex_client_token=None, **kwargs):
        """
        @inherits: :class:`NodeDriver.create_node`

        :param name: The name for this new node (required)
        :type name: ``str``

        :param image: The image to use when creating this node (required)
        :type image: `NodeImage`

        :param size: The size of the node to create (required)
        :type size: `NodeSize`

        :keyword auth: Initial authentication information for the node
                       (optional)
        :type auth: :class:`NodeAuthSSHKey` or :class:`NodeAuthPassword`

        :keyword ex_security_group_id: The id of the security group the
                                       new created node is attached to.
                                       (required)
        :type ex_security_group_id: ``str``

        :keyword ex_description: A description string for this node (optional)
        :type ex_description: ``str``

        :keyword ex_internet_charge_type: The internet charge type (optional)
        :type ex_internet_charge_type: a ``str`` of 'PayByTraffic'
                                       or 'PayByBandwidth'

        :keyword ex_internet_max_bandwidth_out: The max output bandwidth,
                                                in Mbps (optional)
                                                Required for 'PayByTraffic'
                                                internet charge type
        :type ex_internet_max_bandwidth_out: a ``int`` in range [0, 100]
                                             a ``int`` in range [1, 100] for
                                             'PayByTraffic' internet charge
                                             type

        :keyword ex_internet_max_bandwidth_in: The max input bandwidth,
                                               in Mbps (optional)
        :type ex_internet_max_bandwidth_in: a ``int`` in range [1, 200]
                                            default to 200 in server side

        :keyword ex_hostname: The hostname for the node (optional)
        :type ex_hostname: ``str``

        :keyword ex_io_optimized: Whether the node is IO optimized (optional)
        :type ex_io_optimized: ``bool``

        :keyword ex_system_disk: The system disk for the node (optional)
        :type ex_system_disk: ``dict``

        :keyword ex_data_disks: The data disks for the node (optional)
        :type ex_data_disks: a `list` of `dict`

        :keyword ex_vswitch_id: The id of vswitch for a VPC type node
                                (optional)
        :type ex_vswitch_id: ``str``

        :keyword ex_private_ip_address: The IP address in private network
                                        (optional)
        :type ex_private_ip_address: ``str``

        :keyword ex_client_token: A token generated by client to keep
                                  requests idempotency (optional)
        :type keyword ex_client_token: ``str``
        """

        params = {'Action': 'CreateInstance',
                  'RegionId': self.region,
                  'ImageId': image.id,
                  'InstanceType': size.id,
                  'InstanceName': name}

        if not ex_security_group_id:
            raise AttributeError('ex_security_group_id is mandatory')
        params['SecurityGroupId'] = ex_security_group_id

        if ex_description:
            params['Description'] = ex_description

        inet_params = self._get_internet_related_params(
            ex_internet_charge_type,
            ex_internet_max_bandwidth_in,
            ex_internet_max_bandwidth_out)
        if inet_params:
            params.update(inet_params)

        if ex_hostname:
            params['HostName'] = ex_hostname

        if auth:
            auth = self._get_and_check_auth(auth)
            try:
                key = self.ex_find_or_import_keypair_by_key_material(
                    auth.pubkey, kwargs.get('ex_keyname'))
                params['KeyName'] = key['keyName']
            except AttributeError:
                params['Password'] = auth.password

        if 'ex_keyname' in kwargs:
            params['KeyPairName'] = kwargs['ex_keyname']

        if ex_io_optimized is not None:
            optimized = ex_io_optimized
            if isinstance(optimized, bool):
                optimized = 'optimized' if optimized else 'none'
            params['IoOptimized'] = optimized

        if ex_system_disk:
            system_disk = self._get_system_disk(ex_system_disk)
            if system_disk:
                params.update(system_disk)

        if ex_data_disks:
            data_disks = self._get_data_disks(ex_data_disks)
            if data_disks:
                params.update(data_disks)

        if ex_vswitch_id:
            params['VSwitchId'] = ex_vswitch_id

        if ex_private_ip_address:
            if not ex_vswitch_id:
                raise AttributeError('must provide ex_private_ip_address  '
                                     'and ex_vswitch_id at the same time')
            else:
                params['PrivateIpAddress'] = ex_private_ip_address

        if ex_client_token:
            params['ClientToken'] = ex_client_token

        resp = self.connection.request(self.path, params=params)
        node_id = findtext(resp.object, xpath='InstanceId',
                           namespace=self.namespace)
        nodes = self.list_nodes(ex_node_ids=[node_id])
        if len(nodes) != 1:
            raise LibcloudError('could not find the new created node '
                                'with id %s. ' % node_id,
                                driver=self)
        node = nodes[0]
        self.ex_start_node(node)
        self._wait_until_state(nodes, NodeState.RUNNING)

        if 'ex_allocate_public_ip_address' in kwargs:
            self.ex_allocate_public_ip(node)
        return node

    def reboot_node(self, node, ex_force_stop=False):
        """
        Reboot the given node
<commit_msg>Wait until node is stopped before starting it<commit_after># 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.

"""
Node driver for Aliyun.
"""

try:
    import simplejson as json
except ImportError:
    import json
import time
import base64
import hashlib

from libcloud.common.aliyun import AliyunXmlResponse, SignedAliyunConnection
from libcloud.common.types import LibcloudError
from libcloud.compute.base import Node, NodeDriver, NodeImage, NodeSize, \
    StorageVolume, VolumeSnapshot, NodeLocation, KeyPair
from libcloud.compute.types import NodeState, StorageVolumeState, \
    VolumeSnapshotState
from libcloud.utils.py3 import _real_unicode as u
from libcloud.utils.py3 import ensure_string, b
from libcloud.utils.xml import findall, findattr, findtext
from libcloud.utils.publickey import get_pubkey_ssh2_fingerprint
from libcloud.utils.publickey import get_pubkey_comment

__all__ = [
    'DiskCategory',
    'InternetChargeType',
    'ECS_API_VERSION',
    'ECSDriver',
    'ECSSecurityGroup',
    'ECSZone'
]

ECS_API_VERSION = '2014-05-26'
ECS_API_ENDPOINT = 'ecs.aliyuncs.com'
DEFAULT_SIGNATURE_VERSION = '1.0'


def _parse_bool(value):
    if isinstance(value, bool):
        return value
    if u(value).lower() == 'true':
        return True
    return False


"""
Define the extra dictionary for specific resources
"""
RESOURCE_EXTRA_ATTRIBUTES_MAP = {
    'node': {
        'description': {
            'xpath': 'Description',
            'transform_func': u
        },
        'image_id': {
            'xpath': 'ImageId',
            'transform_func': u
        },
        'zone_id': {
            'xpath': 'ZoneId',
            'transform_func': u
        },
        'instance_type': {
            'xpath': 'InstanceType',
            'transform_func': u
        },
        'instance_type_family': {
            'xpath': 'InstanceTypeFamily',
            'transform_func': u
        },
        'hostname': {
            'xpath': 'HostName',
            'transform_func': u
        },
        'serial_number': {
            'xpath': 'SerialNumber',
            'transform_func': u
        },
        'internet_charge_type': {
            'xpath': 'InternetChargeType',
            'transform_func': u
        },
        'creation_time': {
            'xpath': 'CreationTime',
            'transform_func': u
        },
        'instance_network_type': {
            'xpath': 'InstanceNetworkType',
            'transform_func': u
        },
        'instance_charge_type': {
            'xpath': 'InstanceChargeType',
            'transform_func': u
        },
        'device_available': {
            'xpath': 'DeviceAvailable',
            'transform_func': u
        },
        'io_optimized': {
            'xpath': 'IoOptimized',
            'transform_func': u
        },
        'expired_time': {
            'xpath': 'ExpiredTime',
            'transform_func': u
        }
    },
    'vpc_attributes': {
        'vpc_id': {
            'xpath': 'VpcId',
            'transform_func': u
        },
        'vswitch_id': {
            'xpath': 'VSwitchId',
            'transform_func': u
        },
        'private_ip_address': {
            'xpath': 'PrivateIpAddress/IpAddress',
            'transform_func': u
        },
        'nat_ip_address': {
            'xpath': 'NatIpAddress',
            'transform_func': u
        }
    },
    'eip_address_associate': {
        'allocation_id': {
            'xpath': 'AllocationId',
            'transform_func': u
        },
        'ip_address': {
            'xpath': 'IpAddress',
            'transform_func': u
        },
        'bandwidth': {
            'xpath': 'Bandwidth',
            'transform_func': int
        },
        'internet_charge_type': {
            'xpath': 'InternetChargeType',
            'transform_func': u
        }
    },
    'operation_locks': {
        'lock_reason': {
            'xpath': 'LockReason',
            'transform_func': u
        }
    },
    'volume': {
        'region_id': {
            'xpath': 'RegionId',
            'transform_func': u
        },
        'zone_id': {
            'xpath': 'ZoneId',
            'transform_func': u
        },
        'description': {
            'xpath': 'Description',
            'transform_func': u
        },
        'type': {
            'xpath': 'Type',
            'transform_func': u
        },
        'category': {
            'xpath': 'Category',
            'transform_func': u
        },
        'image_id': {
            'xpath': 'ImageId',
            'transform_func': u
        },
        'source_snapshot_id': {
            'xpath': 'SourceSnapshotId',
            'transform_func': u
        },
        'product_code': {
            'xpath': 'ProductCode',
            'transform_func': u
        },
        'portable': {
            'xpath': 'Portable',
            'transform_func': _parse_bool
        },
        'instance_id': {
            'xpath': 'InstanceId',
            'transform_func': u
        },
        'device': {
            'xpath': 'Device',
            'transform_func': u
        },
        'delete_with_instance': {
            'xpath': 'DeleteWithInstance',
            'transform_func': _parse_bool
        },
        'enable_auto_snapshot': {
            'xpath': 'EnableAutoSnapshot',
            'transform_func': _parse_bool
        },
        'creation_time': {
            'xpath': 'CreationTime',
            'transform_func': u
        },
        'attached_time': {
            'xpath': 'AttachedTime',
            'transform_func': u
        },
        'detached_time': {
            'xpath': 'DetachedTime',
            'transform_func': u
        },
        'disk_charge_type': {
            'xpath': 'DiskChargeType',
            'transform_func': u
        }
    },
    'snapshot': {
        'snapshot_name': {
            'xpath': 'SnapshotName',
            'transform_func': u
        },
        'description': {
            'xpath': 'Description',
            'transform_func': u
        },
        'progress': {
            'xpath': 'Progress',
            'transform_func': u
        },
        'source_disk_id': {
            'xpath': 'SourceDiskId',
            'transform_func': u
        },
        'source_disk_size': {
            'xpath': 'SourceDiskSize',
            'transform_func': int
        },
        'source_disk_type': {
            'xpath': 'SourceDiskType',
            'transform_func': u
        },
        'product_code': {
            'xpath': 'ProductCode',
            'transform_func': u
        },
        'usage': {
            'xpath': 'Usage',
            'transform_func': u
        }
    },
    'image': {
        'image_version': {
            'xpath': 'ImageVersion',
            'transform_func': u
        },
        'os_type': {
            'xpath': 'OSType',
            'transform_func': u
        },
        'platform': {
            'xpath': 'Platform',
            'transform_func': u
        },
        'architecture': {
            'xpath': 'Architecture',
            'transform_func': u
        },
        'description': {
            'xpath': 'Description',
            'transform_func': u
        },
        'size': {
            'xpath': 'Size',
            'transform_func': int
        },
        'image_owner_alias': {
            'xpath': 'ImageOwnerAlias',
            'transform_func': u
        },
        'os_name': {
            'xpath': 'OSName',
            'transform_func': u
        },
        'product_code': {
            'xpath': 'ProductCode',
            'transform_func': u
        },
        'is_subscribed': {
            'xpath': 'IsSubscribed',
            'transform_func': _parse_bool
        },
        'progress': {
            'xpath': 'Progress',
            'transform_func': u
        },
        'creation_time': {
            'xpath': 'CreationTime',
            'transform_func': u
        },
        'usage': {
            'xpath': 'Usage',
            'transform_func': u
        },
        'is_copied': {
            'xpath': 'IsCopied',
            'transform_func': _parse_bool
        }
    },
    'disk_device_mapping': {
        'snapshot_id': {
            'xpath': 'SnapshotId',
            'transform_func': u
        },
        'size': {
            'xpath': 'Size',
            'transform_func': int
        },
        'device': {
            'xpath': 'Device',
            'transform_func': u
        },
        'format': {
            'xpath': 'Format',
            'transform_func': u
        },
        'import_oss_bucket': {
            'xpath': 'ImportOSSBucket',
            'transform_func': u
        },
        'import_oss_object': {
            'xpath': 'ImportOSSObject',
            'transform_func': u
        }
    }
}


class ECSConnection(SignedAliyunConnection):
    """
    Represents a single connection to the Aliyun ECS Endpoint.
    """

    api_version = ECS_API_VERSION
    host = ECS_API_ENDPOINT
    responseCls = AliyunXmlResponse
    service_name = 'ecs'


class ECSSecurityGroup(object):
    """
    Security group used to control nodes internet and intranet accessibility.
    """
    def __init__(self, id, name, description=None, driver=None, vpc_id=None,
                 creation_time=None):
        self.id = id
        self.name = name
        self.description = description
        self.driver = driver
        self.vpc_id = vpc_id
        self.creation_time = creation_time

    def __repr__(self):
        return ('<ECSSecurityGroup: id=%s, name=%s, driver=%s ...>' %
                (self.id, self.name, self.driver.name))


class ECSSecurityGroupAttribute(object):

    """
    Security group attribute.
    """
    def __init__(self, ip_protocol=None, port_range=None,
                 source_group_id=None, policy=None, nic_type=None):
        self.ip_protocol = ip_protocol
        self.port_range = port_range
        self.source_group_id = source_group_id
        self.policy = policy
        self.nic_type = nic_type

    def __repr__(self):
        return ('<ECSSecurityGroupAttribute: ip_protocol=%s ...>' %
                (self.ip_protocol))


class ECSZone(object):
    """
    ECSZone used to represent an availability zone in a region.
    """
    def __init__(self, id, name, driver=None,
                 available_resource_types=None,
                 available_instance_types=None,
                 available_disk_categories=None):
        self.id = id
        self.name = name
        self.driver = driver
        self.available_resource_types = available_resource_types
        self.available_instance_types = available_instance_types
        self.available_disk_categories = available_disk_categories

    def __repr__(self):
        return ('<ECSZone: id=%s, name=%s, driver=%s>' %
                (self.id, self.name, self.driver))


class InternetChargeType(object):
    """
    Internet connection billing types for Aliyun Nodes.
    """
    BY_BANDWIDTH = 'PayByBandwidth'
    BY_TRAFFIC = 'PayByTraffic'


class DiskCategory(object):
    """
    Enum defined disk types supported by Aliyun system and data disks.
    """
    CLOUD = 'cloud'
    CLOUD_EFFICIENCY = 'cloud_efficiency'
    CLOUD_SSD = 'cloud_ssd'
    EPHEMERAL_SSD = 'ephemeral_ssd'


class Pagination(object):
    """
    Pagination used to describe the multiple pages results.
    """
    def __init__(self, total, size, current):
        """
        Create a pagination.

        :param total: the total count of the results
        :param size: the page size of each page
        :param current: the current page number, 1-based
        """
        self.total = total
        self.size = size
        self.current = current

    def next(self):
        """
        Switch to the next page.
        :return: the new pagination or None when no more page
        :rtype: ``Pagination``
        """
        if self.total is None or (self.size * self.current >= self.total):
            return None
        self.current += 1
        return self

    def to_dict(self):
        return {'PageNumber': self.current,
                'PageSize': self.size}

    def __repr__(self):
        return ('<Pagination total=%d, size=%d, current page=%d>' %
                (self.total, self.size, self.current))


class ECSDriver(NodeDriver):
    """
    Aliyun ECS node driver.

    Used for Aliyun ECS service.

    TODO:
    Get guest OS root password
    Adjust internet bandwidth settings
    Manage security groups and rules
    """

    name = 'Aliyun ECS'
    website = 'https://www.aliyun.com/product/ecs'
    connectionCls = ECSConnection
    features = {'create_node': ['password', 'ssh_key']}
    namespace = None
    path = '/'

    internet_charge_types = InternetChargeType
    disk_categories = DiskCategory

    NODE_STATE_MAPPING = {
        'Starting': NodeState.PENDING,
        'Running': NodeState.RUNNING,
        'Stopping': NodeState.PENDING,
        'Stopped': NodeState.STOPPED
    }

    VOLUME_STATE_MAPPING = {
        'In_use': StorageVolumeState.INUSE,
        'Available': StorageVolumeState.AVAILABLE,
        'Attaching': StorageVolumeState.ATTACHING,
        'Detaching': StorageVolumeState.INUSE,
        'Creating': StorageVolumeState.CREATING,
        'ReIniting': StorageVolumeState.CREATING}

    SNAPSHOT_STATE_MAPPING = {
        'progressing': VolumeSnapshotState.CREATING,
        'accomplished': VolumeSnapshotState.AVAILABLE,
        'failed': VolumeSnapshotState.ERROR}

    def list_nodes(self, ex_node_ids=None, ex_filters=None):
        """
        List all nodes.

        @inherits: :class:`NodeDriver.create_node`

        :keyword  ex_node_ids: a list of node's ids used to filter nodes.
                               Only the nodes which's id in this list
                               will be returned.
        :type   ex_node_ids: ``list`` of ``str``
        :keyword  ex_filters: node attribute and value pairs to filter nodes.
                              Only the nodes which matchs all the pairs will
                              be returned.
                              If the filter attribute need a json array value,
                              use ``list`` object, the driver will convert it.
        :type   ex_filters: ``dict``
        """

        params = {'Action': 'DescribeInstances',
                  'RegionId': self.region}

        if ex_node_ids:
            if isinstance(ex_node_ids, list):
                params['InstanceIds'] = self._list_to_json_array(ex_node_ids)
            else:
                raise AttributeError('ex_node_ids should be a list of '
                                     'node ids.')

        if ex_filters:
            if isinstance(ex_filters, dict):
                params.update(ex_filters)
            else:
                raise AttributeError('ex_filters should be a dict of '
                                     'node attributes.')

        nodes = self._request_multiple_pages(self.path, params,
                                             self._to_nodes)
        return nodes

    def list_sizes(self, location=None):
        params = {'Action': 'DescribeInstanceTypes'}

        resp_body = self.connection.request(self.path, params).object
        size_elements = findall(resp_body, 'InstanceTypes/InstanceType',
                                namespace=self.namespace)
        sizes = [self._to_size(each) for each in size_elements]
        return sizes

    def list_locations(self):
        params = {'Action': 'DescribeRegions'}

        resp_body = self.connection.request(self.path, params).object
        location_elements = findall(resp_body, 'Regions/Region',
                                    namespace=self.namespace)
        locations = [self._to_location(each) for each in location_elements]
        return locations

    def create_node(self, name, size, image, auth=None,
                    ex_security_group_id=None, ex_description=None,
                    ex_internet_charge_type=None,
                    ex_internet_max_bandwidth_out=None,
                    ex_internet_max_bandwidth_in=None,
                    ex_hostname=None, ex_io_optimized=None,
                    ex_system_disk=None, ex_data_disks=None,
                    ex_vswitch_id=None, ex_private_ip_address=None,
                    ex_client_token=None, **kwargs):
        """
        @inherits: :class:`NodeDriver.create_node`

        :param name: The name for this new node (required)
        :type name: ``str``

        :param image: The image to use when creating this node (required)
        :type image: `NodeImage`

        :param size: The size of the node to create (required)
        :type size: `NodeSize`

        :keyword auth: Initial authentication information for the node
                       (optional)
        :type auth: :class:`NodeAuthSSHKey` or :class:`NodeAuthPassword`

        :keyword ex_security_group_id: The id of the security group the
                                       new created node is attached to.
                                       (required)
        :type ex_security_group_id: ``str``

        :keyword ex_description: A description string for this node (optional)
        :type ex_description: ``str``

        :keyword ex_internet_charge_type: The internet charge type (optional)
        :type ex_internet_charge_type: a ``str`` of 'PayByTraffic'
                                       or 'PayByBandwidth'

        :keyword ex_internet_max_bandwidth_out: The max output bandwidth,
                                                in Mbps (optional)
                                                Required for 'PayByTraffic'
                                                internet charge type
        :type ex_internet_max_bandwidth_out: a ``int`` in range [0, 100]
                                             a ``int`` in range [1, 100] for
                                             'PayByTraffic' internet charge
                                             type

        :keyword ex_internet_max_bandwidth_in: The max input bandwidth,
                                               in Mbps (optional)
        :type ex_internet_max_bandwidth_in: a ``int`` in range [1, 200]
                                            default to 200 in server side

        :keyword ex_hostname: The hostname for the node (optional)
        :type ex_hostname: ``str``

        :keyword ex_io_optimized: Whether the node is IO optimized (optional)
        :type ex_io_optimized: ``bool``

        :keyword ex_system_disk: The system disk for the node (optional)
        :type ex_system_disk: ``dict``

        :keyword ex_data_disks: The data disks for the node (optional)
        :type ex_data_disks: a `list` of `dict`

        :keyword ex_vswitch_id: The id of vswitch for a VPC type node
                                (optional)
        :type ex_vswitch_id: ``str``

        :keyword ex_private_ip_address: The IP address in private network
                                        (optional)
        :type ex_private_ip_address: ``str``

        :keyword ex_client_token: A token generated by client to keep
                                  requests idempotency (optional)
        :type keyword ex_client_token: ``str``
        """

        params = {'Action': 'CreateInstance',
                  'RegionId': self.region,
                  'ImageId': image.id,
                  'InstanceType': size.id,
                  'InstanceName': name}

        if not ex_security_group_id:
            raise AttributeError('ex_security_group_id is mandatory')
        params['SecurityGroupId'] = ex_security_group_id

        if ex_description:
            params['Description'] = ex_description

        inet_params = self._get_internet_related_params(
            ex_internet_charge_type,
            ex_internet_max_bandwidth_in,
            ex_internet_max_bandwidth_out)
        if inet_params:
            params.update(inet_params)

        if ex_hostname:
            params['HostName'] = ex_hostname

        if auth:
            auth = self._get_and_check_auth(auth)
            if getattr(auth, 'pubkey'):
                key = self.ex_find_or_import_keypair_by_key_material(
                    auth.pubkey, kwargs.get('ex_keyname'))
                params['KeyName'] = key['keyName']
            else:
                params['Password'] = auth.password

        if 'ex_keyname' in kwargs:
            params['KeyPairName'] = kwargs['ex_keyname']

        if ex_io_optimized is not None:
            optimized = ex_io_optimized
            if isinstance(optimized, bool):
                optimized = 'optimized' if optimized else 'none'
            params['IoOptimized'] = optimized

        if ex_system_disk:
            system_disk = self._get_system_disk(ex_system_disk)
            if system_disk:
                params.update(system_disk)

        if ex_data_disks:
            data_disks = self._get_data_disks(ex_data_disks)
            if data_disks:
                params.update(data_disks)

        if ex_vswitch_id:
            params['VSwitchId'] = ex_vswitch_id

        if ex_private_ip_address:
            if not ex_vswitch_id:
                raise AttributeError('must provide ex_private_ip_address  '
                                     'and ex_vswitch_id at the same time')
            else:
                params['PrivateIpAddress'] = ex_private_ip_address

        if ex_client_token:
            params['ClientToken'] = ex_client_token

        resp = self.connection.request(self.path, params=params)
        node_id = findtext(resp.object, xpath='InstanceId',
                           namespace=self.namespace)
        nodes = self.list_nodes(ex_node_ids=[node_id])
        if len(nodes) != 1:
            raise LibcloudError('could not find the new created node '
                                'with id %s. ' % node_id,
                                driver=self)
        node = nodes[0]
        self._wait_until_state([node], NodeState.STOPPED)
        self.ex_start_node(node)
        self._wait_until_state(nodes, NodeState.RUNNING)

        if 'ex_allocate_public_ip_address' in kwargs:
            self.ex_allocate_public_ip(node)
        return node

    def reboot_node(self, node, ex_force_stop=False):
        """
        Reboot the given node

{'fraction_non_alphanumeric': 0.10073913043478261, 'fraction_numerical': 0.0016521739130434783, 'mean_word_length': 1.7481331023358624, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 13, 'https://': 2, 'lorem ipsum': 0, 'www.': 4, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 66, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5693708', 'n_tokens_mistral': 12931, 'n_tokens_neox': 12767, 'n_words': 2998}