/batch_submit/pymodules/python2.7/lib/python/apache_libcloud-0.15.1-py2.7.egg/libcloud/compute/drivers/softlayer.py
Python | 474 lines | 391 code | 50 blank | 33 comment | 16 complexity | c66b58049a1717e8ef891acfafe36730 MD5 | raw file
- # 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.
- """
- Softlayer driver
- """
- import time
- from libcloud.common.base import ConnectionUserAndKey
- from libcloud.common.xmlrpc import XMLRPCResponse, XMLRPCConnection
- from libcloud.common.types import InvalidCredsError, LibcloudError
- from libcloud.compute.types import Provider, NodeState
- from libcloud.compute.base import NodeDriver, Node, NodeLocation, NodeSize, \
- NodeImage
- DEFAULT_DOMAIN = 'example.com'
- DEFAULT_CPU_SIZE = 1
- DEFAULT_RAM_SIZE = 2048
- DEFAULT_DISK_SIZE = 100
- DATACENTERS = {
- 'hou02': {'country': 'US'},
- 'sea01': {'country': 'US', 'name': 'Seattle - West Coast U.S.'},
- 'wdc01': {'country': 'US', 'name': 'Washington, DC - East Coast U.S.'},
- 'dal01': {'country': 'US'},
- 'dal02': {'country': 'US'},
- 'dal04': {'country': 'US'},
- 'dal05': {'country': 'US', 'name': 'Dallas - Central U.S.'},
- 'dal06': {'country': 'US'},
- 'dal07': {'country': 'US'},
- 'sjc01': {'country': 'US', 'name': 'San Jose - West Coast U.S.'},
- 'sng01': {'country': 'SG', 'name': 'Singapore - Southeast Asia'},
- 'ams01': {'country': 'NL', 'name': 'Amsterdam - Western Europe'},
- }
- NODE_STATE_MAP = {
- 'RUNNING': NodeState.RUNNING,
- 'HALTED': NodeState.UNKNOWN,
- 'PAUSED': NodeState.UNKNOWN,
- 'INITIATING': NodeState.PENDING
- }
- SL_BASE_TEMPLATES = [
- {
- 'name': '1 CPU, 1GB ram, 25GB',
- 'ram': 1024,
- 'disk': 25,
- 'cpus': 1,
- }, {
- 'name': '1 CPU, 1GB ram, 100GB',
- 'ram': 1024,
- 'disk': 100,
- 'cpus': 1,
- }, {
- 'name': '1 CPU, 2GB ram, 100GB',
- 'ram': 2 * 1024,
- 'disk': 100,
- 'cpus': 1,
- }, {
- 'name': '1 CPU, 4GB ram, 100GB',
- 'ram': 4 * 1024,
- 'disk': 100,
- 'cpus': 1,
- }, {
- 'name': '2 CPU, 2GB ram, 100GB',
- 'ram': 2 * 1024,
- 'disk': 100,
- 'cpus': 2,
- }, {
- 'name': '2 CPU, 4GB ram, 100GB',
- 'ram': 4 * 1024,
- 'disk': 100,
- 'cpus': 2,
- }, {
- 'name': '2 CPU, 8GB ram, 100GB',
- 'ram': 8 * 1024,
- 'disk': 100,
- 'cpus': 2,
- }, {
- 'name': '4 CPU, 4GB ram, 100GB',
- 'ram': 4 * 1024,
- 'disk': 100,
- 'cpus': 4,
- }, {
- 'name': '4 CPU, 8GB ram, 100GB',
- 'ram': 8 * 1024,
- 'disk': 100,
- 'cpus': 4,
- }, {
- 'name': '6 CPU, 4GB ram, 100GB',
- 'ram': 4 * 1024,
- 'disk': 100,
- 'cpus': 6,
- }, {
- 'name': '6 CPU, 8GB ram, 100GB',
- 'ram': 8 * 1024,
- 'disk': 100,
- 'cpus': 6,
- }, {
- 'name': '8 CPU, 8GB ram, 100GB',
- 'ram': 8 * 1024,
- 'disk': 100,
- 'cpus': 8,
- }, {
- 'name': '8 CPU, 16GB ram, 100GB',
- 'ram': 16 * 1024,
- 'disk': 100,
- 'cpus': 8,
- }]
- SL_TEMPLATES = {}
- for i, template in enumerate(SL_BASE_TEMPLATES):
- # Add local disk templates
- local = template.copy()
- local['local_disk'] = True
- SL_TEMPLATES[i] = local
- class SoftLayerException(LibcloudError):
- """
- Exception class for SoftLayer driver
- """
- pass
- class SoftLayerResponse(XMLRPCResponse):
- defaultExceptionCls = SoftLayerException
- exceptions = {
- 'SoftLayer_Account': InvalidCredsError,
- }
- class SoftLayerConnection(XMLRPCConnection, ConnectionUserAndKey):
- responseCls = SoftLayerResponse
- host = 'api.softlayer.com'
- endpoint = '/xmlrpc/v3'
- def request(self, service, method, *args, **kwargs):
- headers = {}
- headers.update(self._get_auth_headers())
- headers.update(self._get_init_params(service, kwargs.get('id')))
- headers.update(
- self._get_object_mask(service, kwargs.get('object_mask')))
- headers.update(
- self._get_object_mask(service, kwargs.get('object_mask')))
- args = ({'headers': headers}, ) + args
- endpoint = '%s/%s' % (self.endpoint, service)
- return super(SoftLayerConnection, self).request(method, *args,
- **{'endpoint':
- endpoint})
- def _get_auth_headers(self):
- return {
- 'authenticate': {
- 'username': self.user_id,
- 'apiKey': self.key
- }
- }
- def _get_init_params(self, service, id):
- if id is not None:
- return {
- '%sInitParameters' % service: {'id': id}
- }
- else:
- return {}
- def _get_object_mask(self, service, mask):
- if mask is not None:
- return {
- '%sObjectMask' % service: {'mask': mask}
- }
- else:
- return {}
- class SoftLayerNodeDriver(NodeDriver):
- """
- SoftLayer node driver
- Extra node attributes:
- - password: root password
- - hourlyRecurringFee: hourly price (if applicable)
- - recurringFee : flat rate (if applicable)
- - recurringMonths : The number of months in which the recurringFee
- will be incurred.
- """
- connectionCls = SoftLayerConnection
- name = 'SoftLayer'
- website = 'http://www.softlayer.com/'
- type = Provider.SOFTLAYER
- features = {'create_node': ['generates_password']}
- def _to_node(self, host):
- try:
- password = \
- host['operatingSystem']['passwords'][0]['password']
- except (IndexError, KeyError):
- password = None
- hourlyRecurringFee = host.get('billingItem', {}).get(
- 'hourlyRecurringFee', 0)
- recurringFee = host.get('billingItem', {}).get('recurringFee', 0)
- recurringMonths = host.get('billingItem', {}).get('recurringMonths', 0)
- createDate = host.get('createDate', None)
- # When machine is launching it gets state halted
- # we change this to pending
- state = NODE_STATE_MAP.get(host['powerState']['keyName'],
- NodeState.UNKNOWN)
- if not password and state == NodeState.UNKNOWN:
- state = NODE_STATE_MAP['INITIATING']
- public_ips = []
- private_ips = []
- if 'primaryIpAddress' in host:
- public_ips.append(host['primaryIpAddress'])
- if 'primaryBackendIpAddress' in host:
- private_ips.append(host['primaryBackendIpAddress'])
- image = host.get('operatingSystem', {}).get('softwareLicense', {}) \
- .get('softwareDescription', {}) \
- .get('longDescription', None)
- return Node(
- id=host['id'],
- name=host['fullyQualifiedDomainName'],
- state=state,
- public_ips=public_ips,
-