def get_standard_package(self, server_id, is_cci=True): """ Retrieves the standard firewall package for the CCI. :param int server_id: The ID of the server to create the firewall for :param bool is_cci: True if the id provided is for a CCI, False for a server :returns: A dictionary containing the standard CCI firewall package """ mask = ('mask[primaryNetworkComponent[speed]]') if is_cci: svc = self.client['Virtual_Guest'] else: svc = self.client['Hardware_Server'] item = svc.getObject(mask=mask, id=server_id) _filter = NestedDict({}) _value = "%s%s" % (item['primaryNetworkComponent']['speed'], "Mbps Hardware Firewall") _filter['items']['description'] = query_filter(_value) kwargs = NestedDict({}) kwargs['id'] = 0 # look at package id 0 kwargs['filter'] = _filter.to_dict() return self.prod_pkg.getItems(**kwargs)
def find_free_volume(self, size, imported): """ Find a volume in the pool of the given size. :param size: size to search for. :param imported: list of imported volumes :returns: sl_vol: SoftLayer iSCSI volume representation """ _filter = NestedDict({}) if self.configuration.sl_vol_order_ceil: _filter['iscsiNetworkStorage'][ 'capacityGb'] = query_filter('>=%s' % size) else: _filter['iscsiNetworkStorage']['capacityGb'] = query_filter(size) _filter['iscsiNetworkStorage'][ 'billingItem'][ 'location'][ 'id'] = query_filter(self.location) sl_volumes = self.client['Account'].getIscsiNetworkStorage( mask='mask[id,capacityGb,' 'username,password,billingItem[id]]', filter=_filter.to_dict()) if len(sl_volumes) == 0: return None sl_volumes = sorted(sl_volumes, key=lambda x: int(x['capacityGb'])) for sl_vol in sl_volumes: if sl_vol['id'] in imported: continue return self._get_vol(sl_vol['id']) LOG.warn(_("No free volume found of size %s" % size)) return None
def get_private_images(self, guid=None, name=None, limit=None, marker=None): _filter = NestedDict() if name: _filter['privateBlockDeviceTemplateGroups']['name'] = \ query_filter(name) if marker is not None: _filter['privateBlockDeviceTemplateGroups']['globalIdentifier'] = \ query_filter('> %s' % marker) if guid: _filter['privateBlockDeviceTemplateGroups'] = { 'globalIdentifier': query_filter(guid)} params = {} params['mask'] = self.image_mask if _filter: params['filter'] = _filter.to_dict() if limit: params['limit'] = limit account = self.client['Account'] return account.getPrivateBlockDeviceTemplateGroups(**params)
def _find_item(self, size, category_code, ceil): """ Find the item_price IDs for the iSCSIs of given size :param int size: iSCSI volume size :returns: Returns a list of item price IDs matching the given volume size or first large enough size, if the `sl_vol_order_ceil` configuration value is se """ _filter = NestedDict({}) _filter[ 'items'][ 'categories'][ 'categoryCode'] = query_filter(category_code) if ceil: _filter['items'][ 'capacity'] = query_filter('>=%s' % size) else: _filter['items']['capacity'] = query_filter(size) iscsi_item_prices = self.client['Product_Package'].getItems( id=0, mask=','.join(('id', 'prices', 'capacity')), filter=_filter.to_dict()) if len(iscsi_item_prices) == 0: return None iscsi_item_prices = sorted( iscsi_item_prices, key=lambda x: float(x['capacity'])) return iscsi_item_prices[0]['prices'][0]['id']
def list_subnets(self, identifier=None, datacenter=None, version=0, subnet_type=None, **kwargs): """ Display a list of all subnets on the account. This provides a quick overview of all subnets including information about data center residence and the number of devices attached. :param string identifier: If specified, the list will only contain the subnet matching this network identifier. :param string datacenter: If specified, the list will only contain subnets in the specified data center. :param int version: Only returns subnets of this version (4 or 6). :param string subnet_type: If specified, it will only returns subnets of this type. :param dict \\*\\*kwargs: response-level options (mask, limit, etc.) """ if "mask" not in kwargs: kwargs["mask"] = DEFAULT_SUBNET_MASK _filter = NestedDict(kwargs.get("filter") or {}) if identifier: _filter["subnets"]["networkIdentifier"] = query_filter(identifier) if datacenter: _filter["subnets"]["datacenter"]["name"] = query_filter(datacenter) if version: _filter["subnets"]["version"] = query_filter(version) if subnet_type: _filter["subnets"]["subnetType"] = query_filter(subnet_type) else: # This filters out global IPs from the subnet listing. _filter["subnets"]["subnetType"] = {"operation": "!= GLOBAL_IP"} kwargs["filter"] = _filter.to_dict() return self.account.getSubnets(**kwargs)
def list_vlans(self, datacenter=None, vlan_number=None, name=None, **kwargs): """ Display a list of all VLANs on the account. This provides a quick overview of all VLANs including information about data center residence and the number of devices attached. :param string datacenter: If specified, the list will only contain VLANs in the specified data center. :param int vlan_number: If specified, the list will only contain the VLAN matching this VLAN number. :param int name: If specified, the list will only contain the VLAN matching this VLAN name. :param dict \\*\\*kwargs: response-level options (mask, limit, etc.) """ _filter = NestedDict(kwargs.get("filter") or {}) if vlan_number: _filter["networkVlans"]["vlanNumber"] = query_filter(vlan_number) if name: _filter["networkVlans"]["name"] = query_filter(name) if datacenter: _filter["networkVlans"]["primaryRouter"]["datacenter"]["name"] = query_filter(datacenter) kwargs["filter"] = _filter.to_dict() if "mask" not in kwargs: kwargs["mask"] = DEFAULT_VLAN_MASK return self.account.getNetworkVlans(**kwargs)
def list_vlans(self, datacenter=None, vlan_number=None, name=None, **kwargs): """ Display a list of all VLANs on the account. This provides a quick overview of all VLANs including information about data center residence and the number of devices attached. :param string datacenter: If specified, the list will only contain VLANs in the specified data center. :param int vlan_number: If specified, the list will only contain the VLAN matching this VLAN number. :param int name: If specified, the list will only contain the VLAN matching this VLAN name. :param dict \*\*kwargs: response-level arguments (limit, offset, etc.) """ _filter = NestedDict(kwargs.get('filter') or {}) if vlan_number: _filter['networkVlans']['vlanNumber'] = query_filter(vlan_number) if name: _filter['networkVlans']['name'] = query_filter(name) if datacenter: _filter['networkVlans']['primaryRouter']['datacenter']['name'] = \ query_filter(datacenter) kwargs['filter'] = _filter.to_dict() if 'mask' not in kwargs: kwargs['mask'] = self._get_vlan_mask() return self.account.getNetworkVlans(**kwargs)
def get_records(self, zone_id, ttl=None, data=None, host=None, type=None, **kwargs): """ List, and optionally filter, records within a zone. :param zone: the zone name in which to search. :param int ttl: optionally, time in seconds: :param data: optionally, the records data :param host: optionally, record's host :param type: optionally, the type of record: :returns: A list of dictionaries representing the matching records within the specified zone. """ _filter = NestedDict() if ttl: _filter['resourceRecords']['ttl'] = query_filter(ttl) if host: _filter['resourceRecords']['host'] = query_filter(host) if data: _filter['resourceRecords']['data'] = query_filter(data) if type: _filter['resourceRecords']['type'] = query_filter(type.lower()) results = self.service.getResourceRecords( id=zone_id, mask='id,expire,domainId,host,minimum,refresh,retry,' 'mxPriority,ttl,type,data,responsiblePerson', filter=_filter.to_dict(), ) return results
def list_keys(self, label=None): """ Lists all SSH keys on the account. :param string label: Filter list based on SSH key label :returns: A list of dictionaries with information about each key """ _filter = NestedDict({}) if label: _filter['sshKeys']['label'] = query_filter(label) return self.client['Account'].getSshKeys(filter=_filter.to_dict())
def list_global_ips(self, version=0): """ Returns a list of all global IP address records on the account. :param int version: Only returns IPs of this version (4 or 6). """ mask = ['destinationIpAddress[hardware, virtualGuest]', 'ipAddress'] mask = 'mask[%s]' % ','.join(mask) _filter = NestedDict({}) if version: v = query_filter(version) _filter['globalIpRecords']['ipAddress']['subnet']['version'] = v _filter = _filter.to_dict() return self.account.getGlobalIpRecords(filter=_filter, mask=mask)
def _order_iscsi(self, item): """ Places an order for volume. :param item: item price id to be used to order """ iscsi_order = self._build_order(item) try: self.product_order.verifyOrder(iscsi_order) LOG.debug(_("Order verified successfully")) order = self.product_order.placeOrder(iscsi_order) except SoftLayerAPIError as ex: LOG.debug(_("Cannot place order: %s" % ex)) raise exception.VolumeBackendAPIException(data=ex.message) LOG.debug(_("Order placed successfully")) billing_item_id = order['placedOrder']['items'][0]['id'] LOG.debug(_("Billing item id: %s associated" % billing_item_id)) billing_svc = self.client['Billing_Order_Item'] for retry in xrange(self.configuration.sl_vol_active_retry): billing_item = billing_svc.getBillingItem( id=billing_item_id) if billing_item and \ billing_item.get('notes'): # iscsi is available break LOG.debug("Ordered volume is not in active state, " "sleeping after %s retries" % retry) time.sleep(self.configuration.sl_vol_active_wait) if not billing_item.get('notes'): raise exception.VolumeBackendAPIException( data="Unable to retrive the " "billing item for the order placed. " "Order Id: %s" % order.get('id')) LOG.debug(_("Billing Item associated: '%s'" % billing_item)) user_name = billing_item['notes'] _filter = NestedDict({}) _filter[ 'iscsiNetworkStorage'][ 'username'] = query_filter( user_name) result = self.client['Account'].\ getIscsiNetworkStorage(mask='mask[billingItem[id]]', filter=_filter.to_dict()) sl_vol = result[0] return sl_vol
def list_subnets(self, identifier=None, datacenter=None, version=0, subnet_type=None, **kwargs): """ Display a list of all subnets on the account. This provides a quick overview of all subnets including information about data center residence and the number of devices attached. :param string identifier: If specified, the list will only contain the subnet matching this network identifier. :param string datacenter: If specified, the list will only contain subnets in the specified data center. :param int version: Only returns subnets of this version (4 or 6). :param string subnet_type: If specified, it will only returns subnets of this type. :param dict \*\*kwargs: response-level arguments (limit, offset, etc.) """ if 'mask' not in kwargs: mask = self._get_subnet_mask() kwargs['mask'] = 'mask[%s]' % ','.join(mask) _filter = NestedDict(kwargs.get('filter') or {}) if identifier: _filter['subnets']['networkIdentifier'] = query_filter(identifier) if datacenter: _filter['subnets']['datacenter']['name'] = \ query_filter(datacenter) if version: _filter['subnets']['version'] = query_filter(version) if subnet_type: _filter['subnets']['subnetType'] = query_filter(subnet_type) else: # This filters out global IPs from the subnet listing. _filter['subnets']['subnetType'] = {'operation': 'not null'} kwargs['filter'] = _filter.to_dict() results = [] # Filtering out routed global IPs here. This is being done in code # because of complications getting the object filter syntax working. for subnet in self.account.getSubnets(**kwargs): if 'GLOBAL_IP' not in subnet['subnetType']: results.append(subnet) return results
def reset_service_group(self, loadbal_id, group_id): """ Resets all the connections on the service group :param int loadbal_id: The id of the loadbal :param int group_id: The id of the service group to reset """ _filter = NestedDict({}) _filter['virtualServers']['id'] = query_filter(group_id) kwargs = NestedDict({}) kwargs['filter'] = _filter.to_dict() kwargs['mask'] = 'mask[serviceGroups]' virtual_servers = self.lb_svc.getVirtualServers(id=loadbal_id, **kwargs) actual_id = virtual_servers[0]['serviceGroups'][0]['id'] svc = self.client['Network_Application_Delivery_Controller' '_LoadBalancer_Service_Group'] return svc.kickAllConnections(id=actual_id)
def get_lb_pkgs(self): """ Retrieves the local load balancer packages. :returns: A dictionary containing the load balancer packages """ lb_filter = '*Load Balancer*' _filter = NestedDict({}) _filter['items']['description'] = query_filter(lb_filter) kwargs = NestedDict({}) kwargs['id'] = 0 # look at package id 0 kwargs['filter'] = _filter.to_dict() packages = self.prod_pkg.getItems(**kwargs) pkgs = [] for package in packages: if not package['description'].startswith('Global'): pkgs.append(package) return pkgs
def list_public_images(self, guid=None, name=None, **kwargs): """ List all public images. :param string guid: filter based on GUID :param string name: filter based on name :param dict \*\*kwargs: response-level arguments (limit, offset, etc.) """ if 'mask' not in kwargs: kwargs['mask'] = IMAGE_MASK _filter = NestedDict(kwargs.get('filter') or {}) if name: _filter['name'] = query_filter(name) if guid: _filter['globalIdentifier'] = query_filter(guid) kwargs['filter'] = _filter.to_dict() return self.vgbdtg.getPublicImages(**kwargs)
def get_dedicated_package(self, ha_enabled=False): """ Retrieves the dedicated firewall package. :param bool ha_enabled: True if HA is to be enabled on the firewall False for No HA :returns: A dictionary containing the dedicated CCI firewall package """ fwl_filter = 'Hardware Firewall (Dedicated)' ha_fwl_filter = 'Hardware Firewall (High Availability)' _filter = NestedDict({}) if ha_enabled: _filter['items']['description'] = query_filter(ha_fwl_filter) else: _filter['items']['description'] = query_filter(fwl_filter) kwargs = NestedDict({}) kwargs['id'] = 0 # look at package id 0 kwargs['filter'] = _filter.to_dict() return self.prod_pkg.getItems(**kwargs)
def edit_service(self, loadbal_id, service_id, ip_address_id=None, port=None, enabled=None, hc_type=None, weight=None): """ Edits an existing service properties :param int loadbal_id: The id of the loadbal where the service resides :param int service_id: The id of the service to edit :param string ip_address: The ip address of the service :param int port: the port of the service :param int enabled: 1 to enable the service, 0 to disable it :param int hc_type: The health check type :param int weight: the weight to give to the service """ _filter = NestedDict({}) _filter['virtualServers']['serviceGroups']['services']['id'] = \ query_filter(service_id) kwargs = NestedDict({}) kwargs['filter'] = _filter.to_dict() kwargs['mask'] = ('mask[serviceGroups[services[groupReferences,' 'healthChecks]]]') virtual_servers = self.lb_svc.getVirtualServers(id=loadbal_id, **kwargs) for service in virtual_servers[0]['serviceGroups'][0]['services']: if service['id'] == service_id: if enabled is not None: service['enabled'] = int(enabled) if port is not None: service['port'] = int(port) if weight is not None: service['groupReferences'][0]['weight'] = int(weight) if hc_type is not None: service['healthChecks'][0]['healthCheckTypeId'] = \ int(hc_type) if ip_address_id is not None: service['ipAddressId'] = ip_address_id template = {'virtualServers': virtual_servers} load_balancer = self.lb_svc.editObject(template, id=loadbal_id) return load_balancer
def get_public_images(self, guid=None, name=None, limit=None, marker=None): _filter = NestedDict() if name: _filter['name'] = query_filter(name) if guid: _filter['globalIdentifier'] = query_filter(guid) if marker is not None: _filter['globalIdentifier'] = query_filter('> %s' % marker) params = {} params['mask'] = self.image_mask if _filter: params['filter'] = _filter.to_dict() if limit: params['limit'] = limit vgbdtg = self.client['Virtual_Guest_Block_Device_Template_Group'] return vgbdtg.getPublicImages(**params)
def list_subnets(self, identifier=None, datacenter=None, version=0, subnet_type=None, **kwargs): """ Display a list of all subnets on the account. This provides a quick overview of all subnets including information about data center residence and the number of devices attached. :param string identifier: If specified, the list will only contain the subnet matching this network identifier. :param string datacenter: If specified, the list will only contain subnets in the specified data center. :param int version: Only returns subnets of this version (4 or 6). :param string subnet_type: If specified, it will only returns subnets of this type. :param dict \*\*kwargs: response-level arguments (limit, offset, etc.) """ if 'mask' not in kwargs: kwargs['mask'] = ','.join(self._get_subnet_mask()) _filter = NestedDict(kwargs.get('filter') or {}) if identifier: _filter['subnets']['networkIdentifier'] = query_filter(identifier) if datacenter: _filter['subnets']['datacenter']['name'] = \ query_filter(datacenter) if version: _filter['subnets']['version'] = query_filter(version) if subnet_type: _filter['subnets']['subnetType'] = query_filter(subnet_type) else: # This filters out global IPs from the subnet listing. _filter['subnets']['subnetType'] = {'operation': '!= GLOBAL_IP'} kwargs['filter'] = _filter.to_dict() return self.account.getSubnets(**kwargs)
def list_private_images(self, guid=None, name=None, **kwargs): """ List all private images. :param string guid: filter based on GUID :param string name: filter based on name :param dict \*\*kwargs: response-level arguments (limit, offset, etc.) """ if 'mask' not in kwargs: kwargs['mask'] = IMAGE_MASK _filter = NestedDict(kwargs.get('filter') or {}) if name: _filter['privateBlockDeviceTemplateGroups']['name'] = \ query_filter(name) if guid: _filter['privateBlockDeviceTemplateGroups']['globalIdentifier'] = \ query_filter(guid) kwargs['filter'] = _filter.to_dict() account = self.client['Account'] return account.getPrivateBlockDeviceTemplateGroups(**kwargs)
def list_global_ips(self, version=None, identifier=None, **kwargs): """ Returns a list of all global IP address records on the account. :param int version: Only returns IPs of this version (4 or 6) :param string identifier: If specified, the list will only contain the global ips matching this network identifier. """ if "mask" not in kwargs: mask = ["destinationIpAddress[hardware, virtualGuest]", "ipAddress"] kwargs["mask"] = ",".join(mask) _filter = NestedDict({}) if version: ver = query_filter(version) _filter["globalIpRecords"]["ipAddress"]["subnet"]["version"] = ver if identifier: subnet_filter = _filter["globalIpRecords"]["ipAddress"]["subnet"] subnet_filter["networkIdentifier"] = query_filter(identifier) kwargs["filter"] = _filter.to_dict() return self.account.getGlobalIpRecords(**kwargs)
def list_global_ips(self, version=None, identifier=None, **kwargs): """ Returns a list of all global IP address records on the account. :param int version: Only returns IPs of this version (4 or 6) :param string identifier: If specified, the list will only contain the global ips matching this network identifier. """ if 'mask' not in kwargs: mask = ['destinationIpAddress[hardware, virtualGuest]', 'ipAddress'] kwargs['mask'] = ','.join(mask) _filter = NestedDict({}) if version: v = query_filter(version) _filter['globalIpRecords']['ipAddress']['subnet']['version'] = v if identifier: subnet_filter = _filter['globalIpRecords']['ipAddress']['subnet'] subnet_filter['networkIdentifier'] = query_filter(identifier) kwargs['filter'] = _filter.to_dict() return self.account.getGlobalIpRecords(**kwargs)
def list_hardware(self, tags=None, cpus=None, memory=None, hostname=None, domain=None, datacenter=None, nic_speed=None, public_ip=None, private_ip=None, **kwargs): """ List all hardware (servers and bare metal computing instances). :param list tags: filter based on tags :param integer cpus: filter based on number of CPUS :param integer memory: filter based on amount of memory in gigabytes :param string hostname: filter based on hostname :param string domain: filter based on domain :param string datacenter: filter based on datacenter :param integer nic_speed: filter based on network speed (in MBPS) :param string public_ip: filter based on public ip address :param string private_ip: filter based on private ip address :param dict \*\*kwargs: response-level arguments (limit, offset, etc.) :returns: Returns a list of dictionaries representing the matching hardware. This list will contain both dedicated servers and bare metal computing instances """ if 'mask' not in kwargs: hw_items = set([ 'id', 'hostname', 'domain', 'hardwareStatusId', 'globalIdentifier', 'fullyQualifiedDomainName', 'processorPhysicalCoreAmount', 'memoryCapacity', 'primaryBackendIpAddress', 'primaryIpAddress', 'datacenter', ]) server_items = set([ 'activeTransaction[id, transactionStatus[friendlyName,name]]', ]) kwargs['mask'] = '[mask[%s],' \ ' mask(SoftLayer_Hardware_Server)[%s]]' % \ (','.join(hw_items), ','.join(server_items)) _filter = NestedDict(kwargs.get('filter') or {}) if tags: _filter['hardware']['tagReferences']['tag']['name'] = { 'operation': 'in', 'options': [{'name': 'data', 'value': tags}], } if cpus: _filter['hardware']['processorPhysicalCoreAmount'] = \ query_filter(cpus) if memory: _filter['hardware']['memoryCapacity'] = query_filter(memory) if hostname: _filter['hardware']['hostname'] = query_filter(hostname) if domain: _filter['hardware']['domain'] = query_filter(domain) if datacenter: _filter['hardware']['datacenter']['name'] = \ query_filter(datacenter) if nic_speed: _filter['hardware']['networkComponents']['maxSpeed'] = \ query_filter(nic_speed) if public_ip: _filter['hardware']['primaryIpAddress'] = \ query_filter(public_ip) if private_ip: _filter['hardware']['primaryBackendIpAddress'] = \ query_filter(private_ip) kwargs['filter'] = _filter.to_dict() return self.account.getHardware(**kwargs)
def list_instances(self, hourly=True, monthly=True, tags=None, cpus=None, memory=None, hostname=None, domain=None, local_disk=None, datacenter=None, nic_speed=None, public_ip=None, private_ip=None, **kwargs): """ Retrieve a list of all CCIs on the account. :param boolean hourly: include hourly instances :param boolean monthly: include monthly instances :param list tags: filter based on tags :param integer cpus: filter based on number of CPUS :param integer memory: filter based on amount of memory :param string hostname: filter based on hostname :param string domain: filter based on domain :param string local_disk: filter based on local_disk :param string datacenter: filter based on datacenter :param integer nic_speed: filter based on network speed (in MBPS) :param string public_ip: filter based on public ip address :param string private_ip: filter based on private ip address :param dict \*\*kwargs: response-level arguments (limit, offset, etc.) :returns: Returns a list of dictionaries representing the matching CCIs :: # Print out a list of all hourly CCIs in the DAL05 data center. # env variables # SL_USERNAME = YOUR_USERNAME # SL_API_KEY = YOUR_API_KEY import SoftLayer client = SoftLayer.Client() mgr = SoftLayer.CCIManager(client) for cci in mgr.list_instances(hourly=True, datacenter='dal05'): print cci['fullyQualifiedDomainName'], cci['primaryIpAddress'] """ if 'mask' not in kwargs: items = set([ 'id', 'globalIdentifier', 'hostname', 'domain', 'fullyQualifiedDomainName', 'primaryBackendIpAddress', 'primaryIpAddress', 'lastKnownPowerState.name', 'powerState', 'maxCpu', 'maxMemory', 'datacenter', 'activeTransaction.transactionStatus[friendlyName,name]', 'status', ]) kwargs['mask'] = "mask[%s]" % ','.join(items) call = 'getVirtualGuests' if not all([hourly, monthly]): if hourly: call = 'getHourlyVirtualGuests' elif monthly: call = 'getMonthlyVirtualGuests' _filter = NestedDict(kwargs.get('filter') or {}) if tags: _filter['virtualGuests']['tagReferences']['tag']['name'] = { 'operation': 'in', 'options': [{'name': 'data', 'value': tags}], } if cpus: _filter['virtualGuests']['maxCpu'] = query_filter(cpus) if memory: _filter['virtualGuests']['maxMemory'] = query_filter(memory) if hostname: _filter['virtualGuests']['hostname'] = query_filter(hostname) if domain: _filter['virtualGuests']['domain'] = query_filter(domain) if local_disk is not None: _filter['virtualGuests']['localDiskFlag'] = \ query_filter(bool(local_disk)) if datacenter: _filter['virtualGuests']['datacenter']['name'] = \ query_filter(datacenter) if nic_speed: _filter['virtualGuests']['networkComponents']['maxSpeed'] = \ query_filter(nic_speed) if public_ip: _filter['virtualGuests']['primaryIpAddress'] = \ query_filter(public_ip) if private_ip: _filter['virtualGuests']['primaryBackendIpAddress'] = \ query_filter(private_ip) kwargs['filter'] = _filter.to_dict() func = getattr(self.account, call) return func(**kwargs)