<s> import random <EOL> import logging <EOL> import os <EOL> import time <EOL> import copy <EOL> from contextlib import contextmanager <EOL> from cinderclient . v1 import client as cinderclient <EOL> import novaclient . v2 . client as nvclient <EOL> import neutronclient . v2_0 . client as neclient <EOL> from retrying import retry <EOL> from cosmo_tester . framework . handlers import ( <EOL> BaseHandler , <EOL> BaseCloudifyInputsConfigReader ) <EOL> from cosmo_tester . framework . util import get_actual_keypath <EOL> logging . getLogger ( '<STR_LIT>' ) . setLevel ( logging . INFO ) <EOL> logging . getLogger ( '<STR_LIT>' ) . setLevel ( logging . INFO ) <EOL> VOLUME_TERMINATION_TIMEOUT_SECS = <NUM_LIT> <EOL> class OpenstackCleanupContext ( BaseHandler . CleanupContext ) : <EOL> def __init__ ( self , context_name , env ) : <EOL> super ( OpenstackCleanupContext , self ) . __init__ ( context_name , env ) <EOL> self . before_run = self . env . handler . openstack_infra_state ( ) <EOL> def cleanup ( self ) : <EOL> """<STR_LIT>""" <EOL> super ( OpenstackCleanupContext , self ) . cleanup ( ) <EOL> resources_to_teardown = self . get_resources_to_teardown ( <EOL> self . env , resources_to_keep = self . before_run ) <EOL> if self . skip_cleanup : <EOL> self . logger . warn ( '<STR_LIT>' <EOL> . format ( self . context_name , resources_to_teardown ) ) <EOL> else : <EOL> self . _clean ( self . env , resources_to_teardown ) <EOL> @ classmethod <EOL> def clean_all ( cls , env ) : <EOL> """<STR_LIT>""" <EOL> super ( OpenstackCleanupContext , cls ) . clean_all ( env ) <EOL> resources_to_teardown = cls . get_resources_to_teardown ( env ) <EOL> cls . _clean ( env , resources_to_teardown ) <EOL> @ classmethod <EOL> def _clean ( cls , env , resources_to_teardown ) : <EOL> cls . logger . info ( '<STR_LIT>' <EOL> '<STR_LIT>' . format ( resources_to_teardown ) ) <EOL> failed_to_remove = env . handler . remove_openstack_resources ( <EOL> resources_to_teardown ) <EOL> if failed_to_remove : <EOL> trimmed_failed_to_remove = { key : value for key , value in <EOL> failed_to_remove . iteritems ( ) <EOL> if value } <EOL> if len ( trimmed_failed_to_remove ) > <NUM_LIT:0> : <EOL> msg = '<STR_LIT>' '<STR_LIT>' . format ( trimmed_failed_to_remove ) <EOL> cls . logger . error ( msg ) <EOL> raise RuntimeError ( msg ) <EOL> @ classmethod <EOL> def get_resources_to_teardown ( cls , env , resources_to_keep = None ) : <EOL> all_existing_resources = env . handler . openstack_infra_state ( ) <EOL> if resources_to_keep : <EOL> return env . handler . openstack_infra_state_delta ( <EOL> before = resources_to_keep , after = all_existing_resources ) <EOL> else : <EOL> return all_existing_resources <EOL> def update_server_id ( self , server_name ) : <EOL> nova , _ , _ = self . env . handler . openstack_clients ( ) <EOL> servers = nova . servers . list ( <EOL> search_opts = { '<STR_LIT:name>' : server_name } ) <EOL> if len ( servers ) > <NUM_LIT:1> : <EOL> raise RuntimeError ( <EOL> '<STR_LIT>' <EOL> . format ( server_name , len ( servers ) ) ) <EOL> new_server_id = servers [ <NUM_LIT:0> ] . id <EOL> old_server_id = None <EOL> servers = self . before_run [ '<STR_LIT>' ] <EOL> for server_id , name in servers . iteritems ( ) : <EOL> if server_name == name : <EOL> old_server_id = server_id <EOL> break <EOL> if old_server_id is None : <EOL> raise RuntimeError ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> . format ( server_name ) ) <EOL> servers [ new_server_id ] = servers . pop ( old_server_id ) <EOL> class CloudifyOpenstackInputsConfigReader ( BaseCloudifyInputsConfigReader ) : <EOL> def __init__ ( self , cloudify_config , manager_blueprint_path , ** kwargs ) : <EOL> super ( CloudifyOpenstackInputsConfigReader , self ) . __init__ ( <EOL> cloudify_config , manager_blueprint_path = manager_blueprint_path , <EOL> ** kwargs ) <EOL> @ property <EOL> def region ( self ) : <EOL> return self . config [ '<STR_LIT>' ] <EOL> @ property <EOL> def management_server_name ( self ) : <EOL> return self . config [ '<STR_LIT>' ] <EOL> @ property <EOL> def agent_key_path ( self ) : <EOL> return self . config [ '<STR_LIT>' ] <EOL> @ property <EOL> def management_user_name ( self ) : <EOL> return self . config [ '<STR_LIT>' ] <EOL> @ property <EOL> def management_key_path ( self ) : <EOL> return self . config [ '<STR_LIT>' ] <EOL> @ property <EOL> def agent_keypair_name ( self ) : <EOL> return self . config [ '<STR_LIT>' ] <EOL> @ property <EOL> def management_keypair_name ( self ) : <EOL> return self . config [ '<STR_LIT>' ] <EOL> @ property <EOL> def use_existing_agent_keypair ( self ) : <EOL> return self . config [ '<STR_LIT>' ] <EOL> @ property <EOL> def use_existing_manager_keypair ( self ) : <EOL> return self . config [ '<STR_LIT>' ] <EOL> @ property <EOL> def external_network_name ( self ) : <EOL> return self . config [ '<STR_LIT>' ] <EOL> @ property <EOL> def keystone_username ( self ) : <EOL> return self . config [ '<STR_LIT>' ] <EOL> @ property <EOL> def keystone_password ( self ) : <EOL> return self . config [ '<STR_LIT>' ] <EOL> @ property <EOL> def keystone_tenant_name ( self ) : <EOL> return self . config [ '<STR_LIT>' ] <EOL> @ property <EOL> def keystone_url ( self ) : <EOL> return self . config [ '<STR_LIT>' ] <EOL> @ property <EOL> def neutron_url ( self ) : <EOL> return self . config . get ( '<STR_LIT>' , None ) <EOL> @ property <EOL> def management_network_name ( self ) : <EOL> return self . config [ '<STR_LIT>' ] <EOL> @ property <EOL> def management_subnet_name ( self ) : <EOL> return self . config [ '<STR_LIT>' ] <EOL> @ property <EOL> def management_router_name ( self ) : <EOL> return self . config [ '<STR_LIT>' ] <EOL> @ property <EOL> def agents_security_group ( self ) : <EOL> return self . config [ '<STR_LIT>' ] <EOL> @ property <EOL> def management_security_group ( self ) : <EOL> return self . config [ '<STR_LIT>' ] <EOL> class OpenstackHandler ( BaseHandler ) : <EOL> CleanupContext = OpenstackCleanupContext <EOL> CloudifyConfigReader = CloudifyOpenstackInputsConfigReader <EOL> def before_bootstrap ( self ) : <EOL> super ( OpenstackHandler , self ) . before_bootstrap ( ) <EOL> with self . update_cloudify_config ( ) as patch : <EOL> suffix = '<STR_LIT>' % random . randrange ( <NUM_LIT:16> ** <NUM_LIT:6> ) <EOL> server_name_prop_path = '<STR_LIT>' <EOL> patch . append_value ( server_name_prop_path , suffix ) <EOL> def after_bootstrap ( self , provider_context ) : <EOL> super ( OpenstackHandler , self ) . after_bootstrap ( provider_context ) <EOL> resources = provider_context [ '<STR_LIT>' ] <EOL> agent_keypair = resources [ '<STR_LIT>' ] <EOL> management_keypair = resources [ '<STR_LIT>' ] <EOL> self . remove_agent_keypair = agent_keypair [ '<STR_LIT>' ] is False <EOL> self . remove_management_keypair = management_keypair [ '<STR_LIT>' ] is False <EOL> def after_teardown ( self ) : <EOL> super ( OpenstackHandler , self ) . after_teardown ( ) <EOL> if self . remove_agent_keypair : <EOL> agent_key_path = get_actual_keypath ( self . env , <EOL> self . env . agent_key_path , <EOL> raise_on_missing = False ) <EOL> if agent_key_path : <EOL> os . remove ( agent_key_path ) <EOL> if self . remove_management_keypair : <EOL> management_key_path = get_actual_keypath ( <EOL> self . env , <EOL> self . env . management_key_path , <EOL> raise_on_missing = False ) <EOL> if management_key_path : <EOL> os . remove ( management_key_path ) <EOL> def openstack_clients ( self ) : <EOL> creds = self . _client_creds ( ) <EOL> self . logger . info ( '<STR_LIT>' <EOL> '<STR_LIT>' . format ( creds ) ) <EOL> return ( nvclient . Client ( ** creds ) , <EOL> neclient . Client ( username = creds [ '<STR_LIT:username>' ] , <EOL> password = creds [ '<STR_LIT>' ] , <EOL> tenant_name = creds [ '<STR_LIT>' ] , <EOL> region_name = creds [ '<STR_LIT>' ] , <EOL> auth_url = creds [ '<STR_LIT>' ] ) , <EOL> cinderclient . Client ( ** creds ) ) <EOL> @ retry ( stop_max_attempt_number = <NUM_LIT:5> , wait_fixed = <NUM_LIT> ) <EOL> def openstack_infra_state ( self ) : <EOL> """<STR_LIT>""" <EOL> nova , neutron , cinder = self . openstack_clients ( ) <EOL> try : <EOL> prefix = self . env . resources_prefix <EOL> except ( AttributeError , KeyError ) : <EOL> prefix = '<STR_LIT>' <EOL> return { <EOL> '<STR_LIT>' : dict ( self . _networks ( neutron , prefix ) ) , <EOL> '<STR_LIT>' : dict ( self . _subnets ( neutron , prefix ) ) , <EOL> '<STR_LIT>' : dict ( self . _routers ( neutron , prefix ) ) , <EOL> '<STR_LIT>' : dict ( self . _security_groups ( neutron , prefix ) ) , <EOL> '<STR_LIT>' : dict ( self . _servers ( nova , prefix ) ) , <EOL> '<STR_LIT>' : dict ( self . _key_pairs ( nova , prefix ) ) , <EOL> '<STR_LIT>' : dict ( self . _floatingips ( neutron , prefix ) ) , <EOL> '<STR_LIT>' : dict ( self . _ports ( neutron , prefix ) ) , <EOL> '<STR_LIT>' : dict ( self . _volumes ( cinder , prefix ) ) <EOL> } <EOL> def openstack_infra_state_delta ( self , before , after ) : <EOL> after = copy . deepcopy ( after ) <EOL> return { <EOL> prop : self . _remove_keys ( after [ prop ] , before [ prop ] . keys ( ) ) <EOL> for prop in before <EOL> } <EOL> def _find_keypairs_to_delete ( self , nodes , node_instances ) : <EOL> """<STR_LIT>""" <EOL> keypairs = set ( ) <EOL> for node in nodes : <EOL> if node . get ( '<STR_LIT:type>' ) != '<STR_LIT>' : <EOL> continue <EOL> key = ( node . get ( '<STR_LIT>' ) , node [ '<STR_LIT:id>' ] ) <EOL> keypairs . add ( key ) <EOL> for node_instance in node_instances : <EOL> key = ( node_instance . get ( '<STR_LIT>' ) , <EOL> node_instance [ '<STR_LIT>' ] ) <EOL> if key not in keypairs : <EOL> continue <EOL> runtime_properties = node_instance [ '<STR_LIT>' ] <EOL> if not runtime_properties : <EOL> continue <EOL> name = runtime_properties . get ( '<STR_LIT>' ) <EOL> if name : <EOL> yield name <EOL> def _delete_keypairs_by_name ( self , keypair_names ) : <EOL> nova , neutron , cinder = self . openstack_clients ( ) <EOL> existing_keypairs = nova . keypairs . list ( ) <EOL> for name in keypair_names : <EOL> for keypair in existing_keypairs : <EOL> if keypair . name == name : <EOL> nova . keypairs . delete ( keypair ) <EOL> def remove_keypairs_from_local_env ( self , local_env ) : <EOL> """<STR_LIT>""" <EOL> nodes = local_env . storage . get_nodes ( ) <EOL> node_instances = local_env . storage . get_node_instances ( ) <EOL> names = self . _find_keypairs_to_delete ( nodes , node_instances ) <EOL> self . _delete_keypairs_by_name ( names ) <EOL> def remove_keypairs_from_manager ( self , deployment_id = None , <EOL> rest_client = None ) : <EOL> """<STR_LIT>""" <EOL> if rest_client is None : <EOL> rest_client = self . env . rest_client <EOL> nodes = rest_client . nodes . list ( deployment_id = deployment_id ) <EOL> node_instances = rest_client . node_instances . list ( <EOL> deployment_id = deployment_id ) <EOL> keypairs = self . _find_keypairs_to_delete ( nodes , node_instances ) <EOL> self . _delete_keypairs_by_name ( keypairs ) <EOL> def remove_keypair ( self , name ) : <EOL> """<STR_LIT>""" <EOL> self . _delete_keypairs_by_name ( [ name ] ) <EOL> def remove_openstack_resources ( self , resources_to_remove ) : <EOL> for _ in range ( <NUM_LIT:3> ) : <EOL> resources_to_remove = self . _remove_openstack_resources_impl ( <EOL> resources_to_remove ) <EOL> if all ( [ len ( g ) == <NUM_LIT:0> for g in resources_to_remove . values ( ) ] ) : <EOL> break <EOL> time . sleep ( <NUM_LIT:3> ) <EOL> return resources_to_remove <EOL> def _remove_openstack_resources_impl ( self , resources_to_remove ) : <EOL> nova , neutron , cinder = self . openstack_clients ( ) <EOL> servers = nova . servers . list ( ) <EOL> ports = neutron . list_ports ( ) [ '<STR_LIT>' ] <EOL> routers = neutron . list_routers ( ) [ '<STR_LIT>' ] <EOL> subnets = neutron . list_subnets ( ) [ '<STR_LIT>' ] <EOL> networks = neutron . list_networks ( ) [ '<STR_LIT>' ] <EOL> floatingips = neutron . list_floatingips ( ) [ '<STR_LIT>' ] <EOL> security_groups = neutron . list_security_groups ( ) [ '<STR_LIT>' ] <EOL> volumes = cinder . volumes . list ( ) <EOL> failed = { <EOL> '<STR_LIT>' : { } , <EOL> '<STR_LIT>' : { } , <EOL> '<STR_LIT>' : { } , <EOL> '<STR_LIT>' : { } , <EOL> '<STR_LIT>' : { } , <EOL> '<STR_LIT>' : { } , <EOL> '<STR_LIT>' : { } , <EOL> '<STR_LIT>' : { } , <EOL> '<STR_LIT>' : { } <EOL> } <EOL> volumes_to_remove = [ ] <EOL> for volume in volumes : <EOL> if volume . id in resources_to_remove [ '<STR_LIT>' ] : <EOL> volumes_to_remove . append ( volume ) <EOL> left_volumes = self . _delete_volumes ( nova , cinder , volumes_to_remove ) <EOL> for volume_id , ex in left_volumes . iteritems ( ) : <EOL> failed [ '<STR_LIT>' ] [ volume_id ] = ex <EOL> for server in servers : <EOL> if server . id in resources_to_remove [ '<STR_LIT>' ] : <EOL> with self . _handled_exception ( server . id , failed , '<STR_LIT>' ) : <EOL> nova . servers . delete ( server ) <EOL> for router in routers : <EOL> if router [ '<STR_LIT:id>' ] in resources_to_remove [ '<STR_LIT>' ] : <EOL> with self . _handled_exception ( router [ '<STR_LIT:id>' ] , failed , '<STR_LIT>' ) : <EOL> for p in neutron . list_ports ( <EOL> device_id = router [ '<STR_LIT:id>' ] ) [ '<STR_LIT>' ] : <EOL> neutron . remove_interface_router ( router [ '<STR_LIT:id>' ] , { <EOL> '<STR_LIT>' : p [ '<STR_LIT:id>' ] <EOL> } ) <EOL> neutron . delete_router ( router [ '<STR_LIT:id>' ] ) <EOL> for port in ports : <EOL> if port [ '<STR_LIT:id>' ] in resources_to_remove [ '<STR_LIT>' ] : <EOL> with self . _handled_exception ( port [ '<STR_LIT:id>' ] , failed , '<STR_LIT>' ) : <EOL> neutron . delete_port ( port [ '<STR_LIT:id>' ] ) <EOL> for subnet in subnets : <EOL> if subnet [ '<STR_LIT:id>' ] in resources_to_remove [ '<STR_LIT>' ] : <EOL> with self . _handled_exception ( subnet [ '<STR_LIT:id>' ] , failed , '<STR_LIT>' ) : <EOL> neutron . delete_subnet ( subnet [ '<STR_LIT:id>' ] ) <EOL> for network in networks : <EOL> if network [ '<STR_LIT:name>' ] == self . env . external_network_name : <EOL> continue <EOL> if network [ '<STR_LIT:id>' ] in resources_to_remove [ '<STR_LIT>' ] : <EOL> with self . _handled_exception ( network [ '<STR_LIT:id>' ] , failed , <EOL> '<STR_LIT>' ) : <EOL> neutron . delete_network ( network [ '<STR_LIT:id>' ] ) <EOL> for floatingip in floatingips : <EOL> if floatingip [ '<STR_LIT:id>' ] in resources_to_remove [ '<STR_LIT>' ] : <EOL> with self . _handled_exception ( floatingip [ '<STR_LIT:id>' ] , failed , <EOL> '<STR_LIT>' ) : <EOL> neutron . delete_floatingip ( floatingip [ '<STR_LIT:id>' ] ) <EOL> for security_group in security_groups : <EOL> if security_group [ '<STR_LIT:name>' ] == '<STR_LIT:default>' : <EOL> continue <EOL> if security_group [ '<STR_LIT:id>' ] in resources_to_remove [ '<STR_LIT>' ] : <EOL> with self . _handled_exception ( security_group [ '<STR_LIT:id>' ] , <EOL> failed , '<STR_LIT>' ) : <EOL> neutron . delete_security_group ( security_group [ '<STR_LIT:id>' ] ) <EOL> return failed <EOL> def _delete_volumes ( self , nova , cinder , existing_volumes ) : <EOL> unremovables = { } <EOL> end_time = time . time ( ) + VOLUME_TERMINATION_TIMEOUT_SECS <EOL> for volume in existing_volumes : <EOL> if volume . status in [ '<STR_LIT>' , '<STR_LIT:error>' , '<STR_LIT>' ] : <EOL> try : <EOL> self . logger . info ( '<STR_LIT>' <EOL> '<STR_LIT>' . <EOL> format ( volume . display_name , volume . id , <EOL> volume . status ) ) <EOL> for attachment in volume . attachments : <EOL> nova . volumes . delete_server_volume ( <EOL> server_id = attachment [ '<STR_LIT>' ] , <EOL> attachment_id = attachment [ '<STR_LIT:id>' ] ) <EOL> except Exception as e : <EOL> self . logger . warning ( '<STR_LIT>' <EOL> '<STR_LIT>' . <EOL> format ( volume . display_name , volume . id , <EOL> e ) ) <EOL> unremovables [ volume . id ] = e <EOL> existing_volumes . remove ( volume ) <EOL> time . sleep ( <NUM_LIT:3> ) <EOL> for volume in existing_volumes : <EOL> if volume . status in [ '<STR_LIT>' , '<STR_LIT:error>' , '<STR_LIT>' ] : <EOL> try : <EOL> self . logger . info ( '<STR_LIT>' <EOL> '<STR_LIT>' . <EOL> format ( volume . display_name , volume . id , <EOL> volume . status ) ) <EOL> cinder . volumes . delete ( volume ) <EOL> except Exception as e : <EOL> self . logger . warning ( '<STR_LIT>' <EOL> '<STR_LIT>' . <EOL> format ( volume . display_name , volume . id , <EOL> e ) ) <EOL> unremovables [ volume . id ] = e <EOL> existing_volumes . remove ( volume ) <EOL> while existing_volumes and time . time ( ) < end_time : <EOL> time . sleep ( <NUM_LIT:3> ) <EOL> for volume in existing_volumes : <EOL> volume_id = volume . id <EOL> volume_name = volume . display_name <EOL> try : <EOL> vol = cinder . volumes . get ( volume_id ) <EOL> if vol . status == '<STR_LIT>' : <EOL> self . logger . debug ( '<STR_LIT>' <EOL> '<STR_LIT>' . format ( volume_name , <EOL> volume_id ) ) <EOL> else : <EOL> self . logger . warning ( '<STR_LIT>' <EOL> '<STR_LIT>' . <EOL> format ( volume_name , volume_id , <EOL> vol . status ) ) <EOL> except Exception as e : <EOL> if hasattr ( e , '<STR_LIT:code>' ) and e . code == <NUM_LIT> : <EOL> self . logger . info ( '<STR_LIT>' . <EOL> format ( volume_name , volume_id ) ) <EOL> existing_volumes . remove ( volume ) <EOL> else : <EOL> self . logger . warning ( '<STR_LIT>' <EOL> '<STR_LIT>' . <EOL> format ( volume_name , <EOL> volume_id , e ) ) <EOL> unremovables [ volume_id ] = e <EOL> existing_volumes . remove ( volume ) <EOL> if existing_volumes : <EOL> for volume in existing_volumes : <EOL> try : <EOL> vol = cinder . volumes . get ( volume . id ) <EOL> vol_status = vol . status <EOL> except : <EOL> vol_status = '<STR_LIT>' <EOL> unremovables [ volume . id ] = '<STR_LIT>' '<STR_LIT>' '<STR_LIT>' . format ( volume . display_name , <EOL> volume . id , <EOL> vol_status ) <EOL> if unremovables : <EOL> self . logger . warning ( '<STR_LIT>' . format ( <EOL> unremovables ) ) <EOL> return unremovables <EOL> def _client_creds ( self ) : <EOL> return { <EOL> '<STR_LIT:username>' : self . env . keystone_username , <EOL> '<STR_LIT>' : self . env . keystone_password , <EOL> '<STR_LIT>' : self . env . keystone_url , <EOL> '<STR_LIT>' : self . env . keystone_tenant_name , <EOL> '<STR_LIT>' : self . env . region <EOL> } <EOL> def _networks ( self , neutron , prefix ) : <EOL> return [ ( n [ '<STR_LIT:id>' ] , n [ '<STR_LIT:name>' ] ) <EOL> for n in neutron . list_networks ( ) [ '<STR_LIT>' ] <EOL> if self . _check_prefix ( n [ '<STR_LIT:name>' ] , prefix ) ] <EOL> def _subnets ( self , neutron , prefix ) : <EOL> return [ ( n [ '<STR_LIT:id>' ] , n [ '<STR_LIT:name>' ] ) <EOL> for n in neutron . list_subnets ( ) [ '<STR_LIT>' ] <EOL> if self . _check_prefix ( n [ '<STR_LIT:name>' ] , prefix ) ] <EOL> def _routers ( self , neutron , prefix ) : <EOL> return [ ( n [ '<STR_LIT:id>' ] , n [ '<STR_LIT:name>' ] ) <EOL> for n in neutron . list_routers ( ) [ '<STR_LIT>' ] <EOL> if self . _check_prefix ( n [ '<STR_LIT:name>' ] , prefix ) ] <EOL> def _security_groups ( self , neutron , prefix ) : <EOL> return [ ( n [ '<STR_LIT:id>' ] , n [ '<STR_LIT:name>' ] ) <EOL> for n in neutron . list_security_groups ( ) [ '<STR_LIT>' ] <EOL> if self . _check_prefix ( n [ '<STR_LIT:name>' ] , prefix ) ] <EOL> def _servers ( self , nova , prefix ) : <EOL> return [ ( s . id , s . human_id ) <EOL> for s in nova . servers . list ( ) <EOL> if self . _check_prefix ( s . human_id , prefix ) ] <EOL> def _key_pairs ( self , nova , prefix ) : <EOL> return [ ( kp . id , kp . name ) <EOL> for kp in nova . keypairs . list ( ) <EOL> if self . _check_prefix ( kp . name , prefix ) ] <EOL> def _floatingips ( self , neutron , prefix ) : <EOL> return [ ( ip [ '<STR_LIT:id>' ] , ip [ '<STR_LIT>' ] ) <EOL> for ip in neutron . list_floatingips ( ) [ '<STR_LIT>' ] ] <EOL> def _ports ( self , neutron , prefix ) : <EOL> return [ ( p [ '<STR_LIT:id>' ] , p [ '<STR_LIT:name>' ] ) <EOL> for p in neutron . list_ports ( ) [ '<STR_LIT>' ] <EOL> if self . _check_prefix ( p [ '<STR_LIT:name>' ] , prefix ) ] <EOL> def _volumes ( self , cinder , prefix ) : <EOL> return [ ( v . id , v . display_name ) for v in cinder . volumes . list ( ) <EOL> if self . _check_prefix ( v . display_name , prefix ) ] <EOL> def _check_prefix ( self , name , prefix ) : <EOL> return name is None or name . startswith ( prefix ) <EOL> def _remove_keys ( self , dct , keys ) : <EOL> for key in keys : <EOL> if key in dct : <EOL> del dct [ key ] <EOL> return dct <EOL> @ contextmanager <EOL> def _handled_exception ( self , resource_id , failed , resource_group ) : <EOL> try : <EOL> yield <EOL> except BaseException , ex : <EOL> failed [ resource_group ] [ resource_id ] = ex <EOL> handler = OpenstackHandler </s>
