def _cache(bank, key, fun, **kwargs): ''' Cache an Azure ARM object ''' items = cache.fetch(bank, key) if items is None: items = {} item_list = fun(**kwargs) for item in item_list: items[item.name] = object_to_dict(item) cache.store(bank, key, items) return items
def show_instance(name, resource_group=None, call=None): # pylint: disable=unused-argument ''' Show the details from the provider concerning an instance ''' if call != 'action': raise SaltCloudSystemExit( 'The show_instance action must be called with -a or --action.') global compconn # pylint: disable=global-statement,invalid-name if not compconn: compconn = get_conn() data = None if resource_group is None: for group in list_resource_groups(): try: instance = compconn.virtual_machines.get(group, name) data = object_to_dict(instance) resource_group = group except CloudError: continue # Find under which cloud service the name is listed, if any if data is None: return {} ifaces = {} if 'network_profile' not in data: data['network_profile'] = {} if 'network_interfaces' not in data['network_profile']: data['network_profile']['network_interfaces'] = [] for iface in data['network_profile']['network_interfaces']: iface_name = iface.id.split('/')[-1] iface_data = show_interface( kwargs={ 'resource_group': resource_group, 'iface_name': iface_name, 'name': name, }) ifaces[iface_name] = iface_data data['network_profile']['network_interfaces'] = ifaces data['resource_group'] = resource_group salt.utils.cloud.cache_node(salt.utils.cloud.simple_types_filter(data), __active_provider_name__, __opts__) return data
def _cache(bank, key, fun, **kwargs): ''' Cache an Azure ARM object ''' items = cache.fetch(bank, key) if items is None: items = {} try: item_list = fun(**kwargs) except CloudError as exc: log.warn( 'There was a cloud error calling {0} with kwargs {1}: {2}'. format(fun, kwargs, exc)) for item in item_list: items[item.name] = object_to_dict(item) cache.store(bank, key, items) return items
def list_nodes_full(conn=None, call=None): # pylint: disable=unused-argument ''' List VMs on this Azure account, with full information ''' if call == 'action': raise SaltCloudSystemExit( 'The list_nodes_full function must be called with -f or --function.' ) global compconn # pylint: disable=global-statement,invalid-name if not compconn: compconn = get_conn() ret = {} for group in list_resource_groups(): nodes = compconn.virtual_machines.list(group) nodeobjs = _pages_to_list(nodes) for node in nodeobjs: ret[node.name] = object_to_dict(node) ret[node.name]['id'] = node.id ret[node.name]['name'] = node.name ret[node.name]['size'] = node.hardware_profile.vm_size ret[node.name]['state'] = node.provisioning_state ret[node. name]['private_ips'] = node.network_profile.network_interfaces ret[node. name]['public_ips'] = node.network_profile.network_interfaces try: ret[node.name]['image'] = '|'.join(( ret[node.name]['storage_profile']['image_reference'] ['publisher'], ret[node.name]['storage_profile']['image_reference'] ['offer'], ret[node.name]['storage_profile']['image_reference'] ['sku'], ret[node.name]['storage_profile']['image_reference'] ['version'], )) except TypeError: ret[node.name]['image'] = ret[ node.name]['storage_profile']['os_disk']['image']['uri'] return ret
def avail_sizes(call=None): # pylint: disable=unused-argument ''' Return a list of sizes from Azure ''' if call == 'action': raise SaltCloudSystemExit( 'The avail_sizes function must be called with ' '-f or --function, or with the --list-sizes option') global compconn # pylint: disable=global-statement,invalid-name if not compconn: compconn = get_conn() ret = {} location = get_location() sizes = compconn.virtual_machine_sizes.list(location) sizeobjs = _pages_to_list(sizes) for size in sizeobjs: ret[size.name] = object_to_dict(size) return ret
def avail_locations(conn=None, call=None): # pylint: disable=unused-argument ''' List available locations for Azure ''' if call == 'action': raise SaltCloudSystemExit( 'The avail_locations function must be called with ' '-f or --function, or with the --list-locations option') global webconn # pylint: disable=global-statement,invalid-name if not webconn: webconn = get_conn(WebSiteManagementClient, WebSiteManagementClientConfiguration) ret = {} regions = webconn.global_model.get_subscription_geo_regions() for location in regions.value: # pylint: disable=no-member lowername = str(location.name).lower().replace(' ', '') ret[lowername] = object_to_dict(location) return ret
def show_interface(call=None, kwargs=None): # pylint: disable=unused-argument ''' Create a network interface ''' global netconn # pylint: disable=global-statement,invalid-name if not netconn: netconn = get_conn(NetworkManagementClient, NetworkManagementClientConfiguration) if kwargs is None: kwargs = {} if kwargs.get('resource_group') is None: kwargs['resource_group'] = config.get_cloud_config_value( 'resource_group', {}, __opts__, search_global=True) iface = netconn.network_interfaces.get( kwargs['resource_group'], kwargs.get('iface_name', kwargs.get('name'))) data = object_to_dict(iface) data['resource_group'] = kwargs['resource_group'] data['ip_configurations'] = list_ip_configurations(kwargs=data) return data
def list_ip_configurations(call=None, kwargs=None): # pylint: disable=unused-argument ''' List IP configurations ''' if 'group' not in kwargs: if 'resource_group' in kwargs: kwargs['group'] = kwargs['resource_group'] else: raise SaltCloudSystemExit( 'A resource_group must be specified as "group" or "resource_group"' ) ip_conf = {} for ip_ in kwargs.get('ip_configurations', []): ip_data = object_to_dict(ip_) ip_conf[ip_data['name']] = ip_data try: pub_ip = netconn.public_ip_addresses.get( # pylint: disable=no-member kwargs['resource_group'], ip_data['name']).ip_address ip_conf[ip_data['name']]['public_ip_address'] = pub_ip except CloudError: # There is no public IP on this interface pass return ip_conf
def make_safe(data): ''' Turn object data into something serializable ''' return salt.utils.cloud.simple_types_filter(object_to_dict(data))