<s> from __future__ import absolute_import <EOL> import logging <EOL> from django . conf import settings <EOL> from django . utils . functional import cached_property <EOL> from django . utils . translation import ugettext_lazy as _ <EOL> import six <EOL> from novaclient import client as nova_client <EOL> from novaclient import exceptions as nova_exceptions <EOL> from novaclient . v2 . contrib import instance_action as nova_instance_action <EOL> from novaclient . v2 . contrib import list_extensions as nova_list_extensions <EOL> from novaclient . v2 import security_group_rules as nova_rules <EOL> from novaclient . v2 import security_groups as nova_security_groups <EOL> from novaclient . v2 import servers as nova_servers <EOL> from horizon import conf <EOL> from horizon import exceptions as horizon_exceptions <EOL> from horizon . utils import functions as utils <EOL> from horizon . utils . memoized import memoized <EOL> from openstack_dashboard . api import base <EOL> from openstack_dashboard . api import network_base <EOL> LOG = logging . getLogger ( __name__ ) <EOL> VERSIONS = base . APIVersionManager ( "<STR_LIT>" , preferred_version = <NUM_LIT:2> ) <EOL> VERSIONS . load_supported_version ( <NUM_LIT> , { "<STR_LIT>" : nova_client , "<STR_LIT:version>" : <NUM_LIT> } ) <EOL> VERSIONS . load_supported_version ( <NUM_LIT:2> , { "<STR_LIT>" : nova_client , "<STR_LIT:version>" : <NUM_LIT:2> } ) <EOL> INSTANCE_ACTIVE_STATE = '<STR_LIT>' <EOL> VOLUME_STATE_AVAILABLE = "<STR_LIT>" <EOL> DEFAULT_QUOTA_NAME = '<STR_LIT:default>' <EOL> class VNCConsole ( base . APIDictWrapper ) : <EOL> """<STR_LIT>""" <EOL> _attrs = [ '<STR_LIT:url>' , '<STR_LIT:type>' ] <EOL> class SPICEConsole ( base . APIDictWrapper ) : <EOL> """<STR_LIT>""" <EOL> _attrs = [ '<STR_LIT:url>' , '<STR_LIT:type>' ] <EOL> class RDPConsole ( base . APIDictWrapper ) : <EOL> """<STR_LIT>""" <EOL> _attrs = [ '<STR_LIT:url>' , '<STR_LIT:type>' ] <EOL> class SerialConsole ( base . APIDictWrapper ) : <EOL> """<STR_LIT>""" <EOL> _attrs = [ '<STR_LIT:url>' , '<STR_LIT:type>' ] <EOL> class Server ( base . APIResourceWrapper ) : <EOL> """<STR_LIT>""" <EOL> _attrs = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:id>' , '<STR_LIT:image>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT:name>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:status>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' ] <EOL> def __init__ ( self , apiresource , request ) : <EOL> super ( Server , self ) . __init__ ( apiresource ) <EOL> self . request = request <EOL> @ property <EOL> def image_name ( self ) : <EOL> import glanceclient . exc as glance_exceptions <EOL> from openstack_dashboard . api import glance <EOL> if not self . image : <EOL> return _ ( "<STR_LIT:->" ) <EOL> if hasattr ( self . image , '<STR_LIT:name>' ) : <EOL> return self . image . name <EOL> if '<STR_LIT:name>' in self . image : <EOL> return self . image [ '<STR_LIT:name>' ] <EOL> else : <EOL> try : <EOL> image = glance . image_get ( self . request , self . image [ '<STR_LIT:id>' ] ) <EOL> return image . name <EOL> except ( glance_exceptions . ClientException , <EOL> horizon_exceptions . ServiceCatalogException ) : <EOL> return _ ( "<STR_LIT:->" ) <EOL> @ property <EOL> def internal_name ( self ) : <EOL> return getattr ( self , '<STR_LIT>' , "<STR_LIT>" ) <EOL> @ property <EOL> def availability_zone ( self ) : <EOL> return getattr ( self , '<STR_LIT>' , "<STR_LIT>" ) <EOL> @ property <EOL> def host_server ( self ) : <EOL> return getattr ( self , '<STR_LIT>' , '<STR_LIT>' ) <EOL> class Hypervisor ( base . APIDictWrapper ) : <EOL> """<STR_LIT>""" <EOL> _attrs = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:id>' , <EOL> '<STR_LIT>' ] <EOL> @ property <EOL> def servers ( self ) : <EOL> servers = [ ] <EOL> try : <EOL> servers = self . _apidict . servers <EOL> except Exception : <EOL> pass <EOL> return servers <EOL> class NovaUsage ( base . APIResourceWrapper ) : <EOL> """<STR_LIT>""" <EOL> _attrs = [ '<STR_LIT:start>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ] <EOL> def get_summary ( self ) : <EOL> return { '<STR_LIT>' : self . total_active_instances , <EOL> '<STR_LIT>' : self . memory_mb , <EOL> '<STR_LIT>' : self . vcpus , <EOL> '<STR_LIT>' : self . vcpu_hours , <EOL> '<STR_LIT>' : self . local_gb , <EOL> '<STR_LIT>' : self . disk_gb_hours , <EOL> '<STR_LIT>' : self . memory_mb_hours } <EOL> @ property <EOL> def total_active_instances ( self ) : <EOL> return sum ( <NUM_LIT:1> for s in self . server_usages if s [ '<STR_LIT>' ] is None ) <EOL> @ property <EOL> def vcpus ( self ) : <EOL> return sum ( s [ '<STR_LIT>' ] for s in self . server_usages <EOL> if s [ '<STR_LIT>' ] is None ) <EOL> @ property <EOL> def vcpu_hours ( self ) : <EOL> return getattr ( self , "<STR_LIT>" , <NUM_LIT:0> ) <EOL> @ property <EOL> def local_gb ( self ) : <EOL> return sum ( s [ '<STR_LIT>' ] for s in self . server_usages <EOL> if s [ '<STR_LIT>' ] is None ) <EOL> @ property <EOL> def memory_mb ( self ) : <EOL> return sum ( s [ '<STR_LIT>' ] for s in self . server_usages <EOL> if s [ '<STR_LIT>' ] is None ) <EOL> @ property <EOL> def disk_gb_hours ( self ) : <EOL> return getattr ( self , "<STR_LIT>" , <NUM_LIT:0> ) <EOL> @ property <EOL> def memory_mb_hours ( self ) : <EOL> return getattr ( self , "<STR_LIT>" , <NUM_LIT:0> ) <EOL> class SecurityGroup ( base . APIResourceWrapper ) : <EOL> """<STR_LIT>""" <EOL> _attrs = [ '<STR_LIT:id>' , '<STR_LIT:name>' , '<STR_LIT:description>' , '<STR_LIT>' ] <EOL> @ cached_property <EOL> def rules ( self ) : <EOL> """<STR_LIT>""" <EOL> manager = nova_rules . SecurityGroupRuleManager ( None ) <EOL> rule_objs = [ nova_rules . SecurityGroupRule ( manager , rule ) <EOL> for rule in self . _apiresource . rules ] <EOL> return [ SecurityGroupRule ( rule ) for rule in rule_objs ] <EOL> def to_dict ( self ) : <EOL> return self . _apiresource . to_dict ( ) <EOL> @ six . python_2_unicode_compatible <EOL> class SecurityGroupRule ( base . APIResourceWrapper ) : <EOL> """<STR_LIT>""" <EOL> _attrs = [ '<STR_LIT:id>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> def __str__ ( self ) : <EOL> if '<STR_LIT:name>' in self . group : <EOL> vals = { '<STR_LIT>' : self . from_port , <EOL> '<STR_LIT:to>' : self . to_port , <EOL> '<STR_LIT>' : self . ip_protocol , <EOL> '<STR_LIT>' : self . group [ '<STR_LIT:name>' ] } <EOL> return ( _ ( '<STR_LIT>' ) % <EOL> vals ) <EOL> else : <EOL> vals = { '<STR_LIT>' : self . from_port , <EOL> '<STR_LIT:to>' : self . to_port , <EOL> '<STR_LIT>' : self . ip_protocol , <EOL> '<STR_LIT>' : self . ip_range [ '<STR_LIT>' ] } <EOL> return ( _ ( '<STR_LIT>' ) % <EOL> vals ) <EOL> @ property <EOL> def ethertype ( self ) : <EOL> return None <EOL> @ property <EOL> def direction ( self ) : <EOL> return '<STR_LIT>' <EOL> class SecurityGroupManager ( network_base . SecurityGroupManager ) : <EOL> backend = '<STR_LIT>' <EOL> def __init__ ( self , request ) : <EOL> self . request = request <EOL> self . client = novaclient ( request ) <EOL> def list ( self ) : <EOL> return [ SecurityGroup ( g ) for g <EOL> in self . client . security_groups . list ( ) ] <EOL> def get ( self , sg_id ) : <EOL> return SecurityGroup ( self . client . security_groups . get ( sg_id ) ) <EOL> def create ( self , name , desc ) : <EOL> return SecurityGroup ( self . client . security_groups . create ( name , desc ) ) <EOL> def update ( self , sg_id , name , desc ) : <EOL> return SecurityGroup ( self . client . security_groups . update ( sg_id , <EOL> name , desc ) ) <EOL> def delete ( self , security_group_id ) : <EOL> self . client . security_groups . delete ( security_group_id ) <EOL> def rule_create ( self , parent_group_id , <EOL> direction = None , ethertype = None , <EOL> ip_protocol = None , from_port = None , to_port = None , <EOL> cidr = None , group_id = None ) : <EOL> try : <EOL> sg = self . client . security_group_rules . create ( parent_group_id , <EOL> ip_protocol , <EOL> from_port , <EOL> to_port , <EOL> cidr , <EOL> group_id ) <EOL> except nova_exceptions . BadRequest : <EOL> raise horizon_exceptions . Conflict ( <EOL> _ ( '<STR_LIT>' ) ) <EOL> return SecurityGroupRule ( sg ) <EOL> def rule_delete ( self , security_group_rule_id ) : <EOL> self . client . security_group_rules . delete ( security_group_rule_id ) <EOL> def list_by_instance ( self , instance_id ) : <EOL> """<STR_LIT>""" <EOL> security_groups = [ ] <EOL> nclient = self . client <EOL> resp , body = nclient . client . get ( '<STR_LIT>' <EOL> % instance_id ) <EOL> if body : <EOL> sg_objs = [ <EOL> nova_security_groups . SecurityGroup ( <EOL> nclient . security_groups , sg , loaded = True ) <EOL> for sg in body . get ( '<STR_LIT>' , [ ] ) ] <EOL> security_groups = [ SecurityGroup ( sg ) for sg in sg_objs ] <EOL> return security_groups <EOL> def update_instance_security_group ( self , instance_id , <EOL> new_security_group_ids ) : <EOL> try : <EOL> all_groups = self . list ( ) <EOL> except Exception : <EOL> raise Exception ( _ ( "<STR_LIT>" ) ) <EOL> wanted_groups = set ( [ sg . name for sg in all_groups <EOL> if sg . id in new_security_group_ids ] ) <EOL> try : <EOL> current_groups = self . list_by_instance ( instance_id ) <EOL> except Exception : <EOL> raise Exception ( _ ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> % instance_id ) <EOL> current_group_names = set ( [ sg . name for sg in current_groups ] ) <EOL> groups_to_add = wanted_groups - current_group_names <EOL> groups_to_remove = current_group_names - wanted_groups <EOL> num_groups_to_modify = len ( groups_to_add | groups_to_remove ) <EOL> try : <EOL> for group in groups_to_add : <EOL> self . client . servers . add_security_group ( instance_id , group ) <EOL> num_groups_to_modify -= <NUM_LIT:1> <EOL> for group in groups_to_remove : <EOL> self . client . servers . remove_security_group ( instance_id , group ) <EOL> num_groups_to_modify -= <NUM_LIT:1> <EOL> except nova_exceptions . ClientException as err : <EOL> LOG . error ( _ ( "<STR_LIT>" <EOL> "<STR_LIT>" ) % <EOL> dict ( num_groups_to_modify = num_groups_to_modify , <EOL> err = err ) ) <EOL> raise nova_exceptions . ClientException ( <EOL> err . code , <EOL> _ ( "<STR_LIT>" ) % <EOL> num_groups_to_modify ) <EOL> return True <EOL> class FlavorExtraSpec ( object ) : <EOL> def __init__ ( self , flavor_id , key , val ) : <EOL> self . flavor_id = flavor_id <EOL> self . id = key <EOL> self . key = key <EOL> self . value = val <EOL> class FloatingIp ( base . APIResourceWrapper ) : <EOL> _attrs = [ '<STR_LIT:id>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ] <EOL> def __init__ ( self , fip ) : <EOL> fip . __setattr__ ( '<STR_LIT>' , fip . instance_id ) <EOL> fip . __setattr__ ( '<STR_LIT>' , <EOL> '<STR_LIT>' if fip . instance_id else None ) <EOL> super ( FloatingIp , self ) . __init__ ( fip ) <EOL> class FloatingIpPool ( base . APIDictWrapper ) : <EOL> def __init__ ( self , pool ) : <EOL> pool_dict = { '<STR_LIT:id>' : pool . name , <EOL> '<STR_LIT:name>' : pool . name } <EOL> super ( FloatingIpPool , self ) . __init__ ( pool_dict ) <EOL> class FloatingIpTarget ( base . APIDictWrapper ) : <EOL> def __init__ ( self , server ) : <EOL> server_dict = { '<STR_LIT:name>' : '<STR_LIT>' % ( server . name , server . id ) , <EOL> '<STR_LIT:id>' : server . id } <EOL> super ( FloatingIpTarget , self ) . __init__ ( server_dict ) <EOL> class FloatingIpManager ( network_base . FloatingIpManager ) : <EOL> def __init__ ( self , request ) : <EOL> self . request = request <EOL> self . client = novaclient ( request ) <EOL> def list_pools ( self ) : <EOL> return [ FloatingIpPool ( pool ) <EOL> for pool in self . client . floating_ip_pools . list ( ) ] <EOL> def list ( self ) : <EOL> return [ FloatingIp ( fip ) <EOL> for fip in self . client . floating_ips . list ( ) ] <EOL> def get ( self , floating_ip_id ) : <EOL> return FloatingIp ( self . client . floating_ips . get ( floating_ip_id ) ) <EOL> def allocate ( self , pool ) : <EOL> return FloatingIp ( self . client . floating_ips . create ( pool = pool ) ) <EOL> def release ( self , floating_ip_id ) : <EOL> self . client . floating_ips . delete ( floating_ip_id ) <EOL> def associate ( self , floating_ip_id , port_id ) : <EOL> server = self . client . servers . get ( port_id ) <EOL> fip = self . client . floating_ips . get ( floating_ip_id ) <EOL> self . client . servers . add_floating_ip ( server . id , fip . ip ) <EOL> def disassociate ( self , floating_ip_id ) : <EOL> fip = self . client . floating_ips . get ( floating_ip_id ) <EOL> server = self . client . servers . get ( fip . instance_id ) <EOL> self . client . servers . remove_floating_ip ( server . id , fip . ip ) <EOL> def list_targets ( self ) : <EOL> return [ FloatingIpTarget ( s ) for s in self . client . servers . list ( ) ] <EOL> def get_target_id_by_instance ( self , instance_id , target_list = None ) : <EOL> return instance_id <EOL> def list_target_id_by_instance ( self , instance_id , target_list = None ) : <EOL> return [ instance_id , ] <EOL> def is_simple_associate_supported ( self ) : <EOL> return conf . HORIZON_CONFIG [ "<STR_LIT>" ] <EOL> def is_supported ( self ) : <EOL> return True <EOL> @ memoized <EOL> def novaclient ( request ) : <EOL> insecure = getattr ( settings , '<STR_LIT>' , False ) <EOL> cacert = getattr ( settings , '<STR_LIT>' , None ) <EOL> c = nova_client . Client ( VERSIONS . get_active_version ( ) [ '<STR_LIT:version>' ] , <EOL> request . user . username , <EOL> request . user . token . id , <EOL> project_id = request . user . tenant_id , <EOL> auth_url = base . url_for ( request , '<STR_LIT>' ) , <EOL> insecure = insecure , <EOL> cacert = cacert , <EOL> http_log_debug = settings . DEBUG ) <EOL> c . client . auth_token = request . user . token . id <EOL> c . client . management_url = base . url_for ( request , '<STR_LIT>' ) <EOL> return c <EOL> def server_vnc_console ( request , instance_id , console_type = '<STR_LIT>' ) : <EOL> return VNCConsole ( novaclient ( request ) . servers . get_vnc_console ( <EOL> instance_id , console_type ) [ '<STR_LIT>' ] ) <EOL> def server_spice_console ( request , instance_id , console_type = '<STR_LIT>' ) : <EOL> return SPICEConsole ( novaclient ( request ) . servers . get_spice_console ( <EOL> instance_id , console_type ) [ '<STR_LIT>' ] ) <EOL> def server_rdp_console ( request , instance_id , console_type = '<STR_LIT>' ) : <EOL> return RDPConsole ( novaclient ( request ) . servers . get_rdp_console ( <EOL> instance_id , console_type ) [ '<STR_LIT>' ] ) <EOL> def server_serial_console ( request , instance_id , console_type = '<STR_LIT>' ) : <EOL> return SerialConsole ( novaclient ( request ) . servers . get_serial_console ( <EOL> instance_id , console_type ) [ '<STR_LIT>' ] ) <EOL> def flavor_create ( request , name , memory , vcpu , disk , flavorid = '<STR_LIT>' , <EOL> ephemeral = <NUM_LIT:0> , swap = <NUM_LIT:0> , metadata = None , is_public = True , <EOL> rxtx_factor = <NUM_LIT:1> ) : <EOL> flavor = novaclient ( request ) . flavors . create ( name , memory , vcpu , disk , <EOL> flavorid = flavorid , <EOL> ephemeral = ephemeral , <EOL> swap = swap , is_public = is_public , <EOL> rxtx_factor = rxtx_factor ) <EOL> if ( metadata ) : <EOL> flavor_extra_set ( request , flavor . id , metadata ) <EOL> return flavor <EOL> def flavor_delete ( request , flavor_id ) : <EOL> novaclient ( request ) . flavors . delete ( flavor_id ) <EOL> def flavor_get ( request , flavor_id , get_extras = False ) : <EOL> flavor = novaclient ( request ) . flavors . get ( flavor_id ) <EOL> if get_extras : <EOL> flavor . extras = flavor_get_extras ( request , flavor . id , True , flavor ) <EOL> return flavor <EOL> @ memoized <EOL> def flavor_list ( request , is_public = True , get_extras = False ) : <EOL> """<STR_LIT>""" <EOL> flavors = novaclient ( request ) . flavors . list ( is_public = is_public ) <EOL> if get_extras : <EOL> for flavor in flavors : <EOL> flavor . extras = flavor_get_extras ( request , flavor . id , True , flavor ) <EOL> return flavors <EOL> def update_pagination ( entities , page_size , marker , sort_dir , sort_key , <EOL> reversed_order ) : <EOL> has_more_data = has_prev_data = False <EOL> if len ( entities ) > page_size : <EOL> has_more_data = True <EOL> entities . pop ( ) <EOL> if marker is not None : <EOL> has_prev_data = True <EOL> elif reversed_order and marker is not None : <EOL> has_more_data = True <EOL> elif marker is not None : <EOL> has_prev_data = True <EOL> if reversed_order : <EOL> entities = sorted ( entities , key = lambda entity : <EOL> ( getattr ( entity , sort_key ) or '<STR_LIT>' ) . lower ( ) , <EOL> reverse = ( sort_dir == '<STR_LIT>' ) ) <EOL> return entities , has_more_data , has_prev_data <EOL> @ memoized <EOL> def flavor_list_paged ( request , is_public = True , get_extras = False , marker = None , <EOL> paginate = False , sort_key = "<STR_LIT:name>" , sort_dir = "<STR_LIT>" , <EOL> reversed_order = False ) : <EOL> """<STR_LIT>""" <EOL> has_more_data = False <EOL> has_prev_data = False <EOL> if paginate : <EOL> if reversed_order : <EOL> sort_dir = '<STR_LIT>' if sort_dir == '<STR_LIT>' else '<STR_LIT>' <EOL> page_size = utils . get_page_size ( request ) <EOL> flavors = novaclient ( request ) . flavors . list ( is_public = is_public , <EOL> marker = marker , <EOL> limit = page_size + <NUM_LIT:1> , <EOL> sort_key = sort_key , <EOL> sort_dir = sort_dir ) <EOL> flavors , has_more_data , has_prev_data = update_pagination ( <EOL> flavors , page_size , marker , sort_dir , sort_key , reversed_order ) <EOL> else : <EOL> flavors = novaclient ( request ) . flavors . list ( is_public = is_public ) <EOL> if get_extras : <EOL> for flavor in flavors : <EOL> flavor . extras = flavor_get_extras ( request , flavor . id , True , flavor ) <EOL> return ( flavors , has_more_data , has_prev_data ) <EOL> @ memoized <EOL> def flavor_access_list ( request , flavor = None ) : <EOL> """<STR_LIT>""" <EOL> return novaclient ( request ) . flavor_access . list ( flavor = flavor ) <EOL> def add_tenant_to_flavor ( request , flavor , tenant ) : <EOL> """<STR_LIT>""" <EOL> return novaclient ( request ) . flavor_access . add_tenant_access ( <EOL> flavor = flavor , tenant = tenant ) <EOL> def remove_tenant_from_flavor ( request , flavor , tenant ) : <EOL> """<STR_LIT>""" <EOL> return novaclient ( request ) . flavor_access . remove_tenant_access ( <EOL> flavor = flavor , tenant = tenant ) <EOL> def flavor_get_extras ( request , flavor_id , raw = False , flavor = None ) : <EOL> """<STR_LIT>""" <EOL> if flavor is None : <EOL> flavor = novaclient ( request ) . flavors . get ( flavor_id ) <EOL> extras = flavor . get_keys ( ) <EOL> if raw : <EOL> return extras <EOL> return [ FlavorExtraSpec ( flavor_id , key , value ) for <EOL> key , value in extras . items ( ) ] <EOL> def flavor_extra_delete ( request , flavor_id , keys ) : <EOL> """<STR_LIT>""" <EOL> flavor = novaclient ( request ) . flavors . get ( flavor_id ) <EOL> return flavor . unset_keys ( keys ) <EOL> def flavor_extra_set ( request , flavor_id , metadata ) : <EOL> """<STR_LIT>""" <EOL> flavor = novaclient ( request ) . flavors . get ( flavor_id ) <EOL> if ( not metadata ) : <EOL> return None <EOL> return flavor . set_keys ( metadata ) <EOL> def snapshot_create ( request , instance_id , name ) : <EOL> return novaclient ( request ) . servers . create_image ( instance_id , name ) <EOL> def keypair_create ( request , name ) : <EOL> return novaclient ( request ) . keypairs . create ( name ) <EOL> def keypair_import ( request , name , public_key ) : <EOL> return novaclient ( request ) . keypairs . create ( name , public_key ) <EOL> def keypair_delete ( request , keypair_id ) : <EOL> novaclient ( request ) . keypairs . delete ( keypair_id ) <EOL> def keypair_list ( request ) : <EOL> return novaclient ( request ) . keypairs . list ( ) <EOL> def keypair_get ( request , keypair_id ) : <EOL> return novaclient ( request ) . keypairs . get ( keypair_id ) <EOL> def server_create ( request , name , image , flavor , key_name , user_data , <EOL> security_groups , block_device_mapping = None , <EOL> block_device_mapping_v2 = None , nics = None , <EOL> availability_zone = None , instance_count = <NUM_LIT:1> , admin_pass = None , <EOL> disk_config = None , config_drive = None , meta = None , <EOL> scheduler_hints = None ) : <EOL> return Server ( novaclient ( request ) . servers . create ( <EOL> name , image , flavor , userdata = user_data , <EOL> security_groups = security_groups , <EOL> key_name = key_name , block_device_mapping = block_device_mapping , <EOL> block_device_mapping_v2 = block_device_mapping_v2 , <EOL> nics = nics , availability_zone = availability_zone , <EOL> min_count = instance_count , admin_pass = admin_pass , <EOL> disk_config = disk_config , config_drive = config_drive , <EOL> meta = meta , scheduler_hints = scheduler_hints ) , request ) <EOL> def server_delete ( request , instance ) : <EOL> novaclient ( request ) . servers . delete ( instance ) <EOL> def server_get ( request , instance_id ) : <EOL> return Server ( novaclient ( request ) . servers . get ( instance_id ) , request ) <EOL> def server_list ( request , search_opts = None , all_tenants = False ) : <EOL> page_size = utils . get_page_size ( request ) <EOL> c = novaclient ( request ) <EOL> paginate = False <EOL> if search_opts is None : <EOL> search_opts = { } <EOL> elif '<STR_LIT>' in search_opts : <EOL> paginate = search_opts . pop ( '<STR_LIT>' ) <EOL> if paginate : <EOL> search_opts [ '<STR_LIT>' ] = page_size + <NUM_LIT:1> <EOL> if all_tenants : <EOL> search_opts [ '<STR_LIT>' ] = True <EOL> else : <EOL> search_opts [ '<STR_LIT>' ] = request . user . tenant_id <EOL> servers = [ Server ( s , request ) <EOL> for s in c . servers . list ( True , search_opts ) ] <EOL> has_more_data = False <EOL> if paginate and len ( servers ) > page_size : <EOL> servers . pop ( - <NUM_LIT:1> ) <EOL> has_more_data = True <EOL> elif paginate and len ( servers ) == getattr ( settings , '<STR_LIT>' , <EOL> <NUM_LIT:1000> ) : <EOL> has_more_data = True <EOL> return ( servers , has_more_data ) <EOL> def server_console_output ( request , instance_id , tail_length = None ) : <EOL> """<STR_LIT>""" <EOL> return novaclient ( request ) . servers . get_console_output ( instance_id , <EOL> length = tail_length ) <EOL> def server_pause ( request , instance_id ) : <EOL> novaclient ( request ) . servers . pause ( instance_id ) <EOL> def server_unpause ( request , instance_id ) : <EOL> novaclient ( request ) . servers . unpause ( instance_id ) <EOL> def server_suspend ( request , instance_id ) : <EOL> novaclient ( request ) . servers . suspend ( instance_id ) <EOL> def server_resume ( request , instance_id ) : <EOL> novaclient ( request ) . servers . resume ( instance_id ) <EOL> def server_shelve ( request , instance_id ) : <EOL> novaclient ( request ) . servers . shelve ( instance_id ) <EOL> def server_unshelve ( request , instance_id ) : <EOL> novaclient ( request ) . servers . unshelve ( instance_id ) <EOL> def server_reboot ( request , instance_id , soft_reboot = False ) : <EOL> hardness = nova_servers . REBOOT_HARD <EOL> if soft_reboot : <EOL> hardness = nova_servers . REBOOT_SOFT <EOL> novaclient ( request ) . servers . reboot ( instance_id , hardness ) <EOL> def server_rebuild ( request , instance_id , image_id , password = None , <EOL> disk_config = None ) : <EOL> return novaclient ( request ) . servers . rebuild ( instance_id , image_id , <EOL> password , disk_config ) <EOL> def server_update ( request , instance_id , name ) : <EOL> return novaclient ( request ) . servers . update ( instance_id , name = name ) <EOL> def server_migrate ( request , instance_id ) : <EOL> novaclient ( request ) . servers . migrate ( instance_id ) <EOL> def server_live_migrate ( request , instance_id , host , block_migration = False , <EOL> disk_over_commit = False ) : <EOL> novaclient ( request ) . servers . live_migrate ( instance_id , host , <EOL> block_migration , <EOL> disk_over_commit ) <EOL> def server_resize ( request , instance_id , flavor , disk_config = None , ** kwargs ) : <EOL> novaclient ( request ) . servers . resize ( instance_id , flavor , <EOL> disk_config , ** kwargs ) <EOL> def server_confirm_resize ( request , instance_id ) : <EOL> novaclient ( request ) . servers . confirm_resize ( instance_id ) <EOL> def server_revert_resize ( request , instance_id ) : <EOL> novaclient ( request ) . servers . revert_resize ( instance_id ) <EOL> def server_start ( request , instance_id ) : <EOL> novaclient ( request ) . servers . start ( instance_id ) <EOL> def server_stop ( request , instance_id ) : <EOL> novaclient ( request ) . servers . stop ( instance_id ) <EOL> def server_lock ( request , instance_id ) : <EOL> novaclient ( request ) . servers . lock ( instance_id ) <EOL> def server_unlock ( request , instance_id ) : <EOL> novaclient ( request ) . servers . unlock ( instance_id ) <EOL> def server_metadata_update ( request , instance_id , metadata ) : <EOL> novaclient ( request ) . servers . set_meta ( instance_id , metadata ) <EOL> def server_metadata_delete ( request , instance_id , keys ) : <EOL> novaclient ( request ) . servers . delete_meta ( instance_id , keys ) <EOL> def tenant_quota_get ( request , tenant_id ) : <EOL> return base . QuotaSet ( novaclient ( request ) . quotas . get ( tenant_id ) ) <EOL> def tenant_quota_update ( request , tenant_id , ** kwargs ) : <EOL> novaclient ( request ) . quotas . update ( tenant_id , ** kwargs ) <EOL> def default_quota_get ( request , tenant_id ) : <EOL> return base . QuotaSet ( novaclient ( request ) . quotas . defaults ( tenant_id ) ) <EOL> def default_quota_update ( request , ** kwargs ) : <EOL> novaclient ( request ) . quota_classes . update ( DEFAULT_QUOTA_NAME , ** kwargs ) <EOL> def usage_get ( request , tenant_id , start , end ) : <EOL> return NovaUsage ( novaclient ( request ) . usage . get ( tenant_id , start , end ) ) <EOL> def usage_list ( request , start , end ) : <EOL> return [ NovaUsage ( u ) for u in <EOL> novaclient ( request ) . usage . list ( start , end , True ) ] <EOL> def virtual_interfaces_list ( request , instance_id ) : <EOL> return novaclient ( request ) . virtual_interfaces . list ( instance_id ) <EOL> def get_x509_credentials ( request ) : <EOL> return novaclient ( request ) . certs . create ( ) <EOL> def get_x509_root_certificate ( request ) : <EOL> return novaclient ( request ) . certs . get ( ) <EOL> def get_password ( request , instance_id , private_key = None ) : <EOL> return novaclient ( request ) . servers . get_password ( instance_id , private_key ) <EOL> def instance_volume_attach ( request , volume_id , instance_id , device ) : <EOL> return novaclient ( request ) . volumes . create_server_volume ( instance_id , <EOL> volume_id , <EOL> device ) <EOL> def instance_volume_detach ( request , instance_id , att_id ) : <EOL> return novaclient ( request ) . volumes . delete_server_volume ( instance_id , <EOL> att_id ) <EOL> def instance_volumes_list ( request , instance_id ) : <EOL> from openstack_dashboard . api import cinder <EOL> volumes = novaclient ( request ) . volumes . get_server_volumes ( instance_id ) <EOL> for volume in volumes : <EOL> volume_data = cinder . cinderclient ( request ) . volumes . get ( volume . id ) <EOL> volume . name = cinder . Volume ( volume_data ) . name <EOL> return volumes <EOL> def hypervisor_list ( request ) : <EOL> return novaclient ( request ) . hypervisors . list ( ) <EOL> def hypervisor_stats ( request ) : <EOL> return novaclient ( request ) . hypervisors . statistics ( ) <EOL> def hypervisor_search ( request , query , servers = True ) : <EOL> return novaclient ( request ) . hypervisors . search ( query , servers ) <EOL> def evacuate_host ( request , host , target = None , on_shared_storage = False ) : <EOL> hypervisors = novaclient ( request ) . hypervisors . search ( host , True ) <EOL> response = [ ] <EOL> err_code = None <EOL> for hypervisor in hypervisors : <EOL> hyper = Hypervisor ( hypervisor ) <EOL> for server in hyper . servers : <EOL> try : <EOL> novaclient ( request ) . servers . evacuate ( server [ '<STR_LIT>' ] , <EOL> target , <EOL> on_shared_storage ) <EOL> except nova_exceptions . ClientException as err : <EOL> err_code = err . code <EOL> msg = _ ( "<STR_LIT>" ) <EOL> msg = msg % { '<STR_LIT:name>' : server [ '<STR_LIT:name>' ] , '<STR_LIT>' : server [ '<STR_LIT>' ] } <EOL> response . append ( msg ) <EOL> if err_code : <EOL> msg = _ ( '<STR_LIT>' ) % '<STR_LIT:U+002CU+0020>' . join ( response ) <EOL> raise nova_exceptions . ClientException ( err_code , msg ) <EOL> return True <EOL> def migrate_host ( request , host , live_migrate = False , disk_over_commit = False , <EOL> block_migration = False ) : <EOL> hypervisors = novaclient ( request ) . hypervisors . search ( host , True ) <EOL> response = [ ] <EOL> err_code = None <EOL> for hyper in hypervisors : <EOL> for server in getattr ( hyper , "<STR_LIT>" , [ ] ) : <EOL> try : <EOL> if live_migrate : <EOL> instance = server_get ( request , server [ '<STR_LIT>' ] ) <EOL> if instance . status in [ "<STR_LIT>" , "<STR_LIT>" ] : <EOL> novaclient ( request ) . servers . live_migrate ( <EOL> server [ '<STR_LIT>' ] , <EOL> None , <EOL> block_migration , <EOL> disk_over_commit <EOL> ) <EOL> else : <EOL> novaclient ( request ) . servers . migrate ( server [ '<STR_LIT>' ] ) <EOL> else : <EOL> novaclient ( request ) . servers . migrate ( server [ '<STR_LIT>' ] ) <EOL> except nova_exceptions . ClientException as err : <EOL> err_code = err . code <EOL> msg = _ ( "<STR_LIT>" ) <EOL> msg = msg % { '<STR_LIT:name>' : server [ '<STR_LIT:name>' ] , '<STR_LIT>' : server [ '<STR_LIT>' ] } <EOL> response . append ( msg ) <EOL> if err_code : <EOL> msg = _ ( '<STR_LIT>' ) % '<STR_LIT:U+002CU+0020>' . join ( response ) <EOL> raise nova_exceptions . ClientException ( err_code , msg ) <EOL> return True <EOL> def tenant_absolute_limits ( request , reserved = False ) : <EOL> limits = novaclient ( request ) . limits . get ( reserved = reserved ) . absolute <EOL> limits_dict = { } <EOL> for limit in limits : <EOL> if limit . value < <NUM_LIT:0> : <EOL> if limit . name . startswith ( '<STR_LIT>' ) and limit . name . endswith ( '<STR_LIT>' ) : <EOL> limits_dict [ limit . name ] = <NUM_LIT:0> <EOL> else : <EOL> limits_dict [ limit . name ] = float ( "<STR_LIT>" ) <EOL> else : <EOL> limits_dict [ limit . name ] = limit . value <EOL> return limits_dict <EOL> def availability_zone_list ( request , detailed = False ) : <EOL> return novaclient ( request ) . availability_zones . list ( detailed = detailed ) <EOL> def service_list ( request , binary = None ) : <EOL> return novaclient ( request ) . services . list ( binary = binary ) <EOL> def service_enable ( request , host , binary ) : <EOL> return novaclient ( request ) . services . enable ( host , binary ) <EOL> def service_disable ( request , host , binary , reason = None ) : <EOL> if reason : <EOL> return novaclient ( request ) . services . disable_log_reason ( host , <EOL> binary , reason ) <EOL> else : <EOL> return novaclient ( request ) . services . disable ( host , binary ) <EOL> def aggregate_details_list ( request ) : <EOL> result = [ ] <EOL> c = novaclient ( request ) <EOL> for aggregate in c . aggregates . list ( ) : <EOL> result . append ( c . aggregates . get_details ( aggregate . id ) ) <EOL> return result <EOL> def aggregate_create ( request , name , availability_zone = None ) : <EOL> return novaclient ( request ) . aggregates . create ( name , availability_zone ) <EOL> def aggregate_delete ( request , aggregate_id ) : <EOL> return novaclient ( request ) . aggregates . delete ( aggregate_id ) <EOL> def aggregate_get ( request , aggregate_id ) : <EOL> return novaclient ( request ) . aggregates . get ( aggregate_id ) <EOL> def aggregate_update ( request , aggregate_id , values ) : <EOL> return novaclient ( request ) . aggregates . update ( aggregate_id , values ) <EOL> def aggregate_set_metadata ( request , aggregate_id , metadata ) : <EOL> return novaclient ( request ) . aggregates . set_metadata ( aggregate_id , metadata ) <EOL> def host_list ( request ) : <EOL> return novaclient ( request ) . hosts . list ( ) <EOL> def add_host_to_aggregate ( request , aggregate_id , host ) : <EOL> return novaclient ( request ) . aggregates . add_host ( aggregate_id , host ) <EOL> def remove_host_from_aggregate ( request , aggregate_id , host ) : <EOL> return novaclient ( request ) . aggregates . remove_host ( aggregate_id , host ) <EOL> def interface_attach ( request , <EOL> server , port_id = None , net_id = None , fixed_ip = None ) : <EOL> return novaclient ( request ) . servers . interface_attach ( server , <EOL> port_id , <EOL> net_id , <EOL> fixed_ip ) <EOL> def interface_detach ( request , server , port_id ) : <EOL> return novaclient ( request ) . servers . interface_detach ( server , port_id ) <EOL> @ memoized <EOL> def list_extensions ( request ) : <EOL> """<STR_LIT>""" <EOL> blacklist = set ( getattr ( settings , <EOL> '<STR_LIT>' , [ ] ) ) <EOL> return [ <EOL> extension for extension in <EOL> nova_list_extensions . ListExtManager ( novaclient ( request ) ) . show_all ( ) <EOL> if extension . name not in blacklist <EOL> ] <EOL> @ memoized <EOL> def extension_supported ( extension_name , request ) : <EOL> """<STR_LIT>""" <EOL> extensions = list_extensions ( request ) <EOL> for extension in extensions : <EOL> if extension . name == extension_name : <EOL> return True <EOL> return False <EOL> def can_set_server_password ( ) : <EOL> features = getattr ( settings , '<STR_LIT>' , { } ) <EOL> return features . get ( '<STR_LIT>' , False ) <EOL> def instance_action_list ( request , instance_id ) : <EOL> return nova_instance_action . InstanceActionManager ( <EOL> novaclient ( request ) ) . list ( instance_id ) <EOL> def can_set_mount_point ( ) : <EOL> """<STR_LIT>""" <EOL> hypervisor_features = getattr ( <EOL> settings , "<STR_LIT>" , { } ) <EOL> return hypervisor_features . get ( "<STR_LIT>" , False ) <EOL> def requires_keypair ( ) : <EOL> features = getattr ( settings , '<STR_LIT>' , { } ) <EOL> return features . get ( '<STR_LIT>' , False ) </s>
