<s> import collections <EOL> import logging <EOL> import marshmallow <EOL> from marshmallow import fields <EOL> from oslo_utils import importutils <EOL> from cloudferry . lib . utils import bases <EOL> from cloudferry . lib . utils import query <EOL> LOG = logging . getLogger ( __name__ ) <EOL> MODEL_LIST = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] <EOL> DEFAULT_MIGRATION_LIST = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] <EOL> class DictField ( fields . Field ) : <EOL> def __init__ ( self , key_field , nested_field , ** kwargs ) : <EOL> super ( DictField , self ) . __init__ ( ** kwargs ) <EOL> self . key_field = key_field <EOL> self . nested_field = nested_field <EOL> def _deserialize ( self , value , attr , data ) : <EOL> if not isinstance ( value , dict ) : <EOL> self . fail ( '<STR_LIT:type>' ) <EOL> ret = { } <EOL> for key , val in value . items ( ) : <EOL> k = self . key_field . deserialize ( key ) <EOL> v = self . nested_field . deserialize ( val ) <EOL> ret [ k ] = v <EOL> return ret <EOL> class FirstFit ( fields . Field ) : <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> many = kwargs . pop ( '<STR_LIT>' , False ) <EOL> super ( FirstFit , self ) . __init__ ( ** kwargs ) <EOL> self . many = many <EOL> self . variants = args <EOL> def _deserialize ( self , value , attr , data ) : <EOL> if self . many : <EOL> return [ self . _do_deserialize ( v ) for v in value ] <EOL> else : <EOL> return self . _do_deserialize ( value ) <EOL> def _do_deserialize ( self , value ) : <EOL> errors = [ ] <EOL> for field in self . variants : <EOL> try : <EOL> return field . deserialize ( value ) <EOL> except marshmallow . ValidationError as ex : <EOL> errors . append ( ex ) <EOL> raise marshmallow . ValidationError ( [ e . messages for e in errors ] ) <EOL> class OneOrMore ( fields . Field ) : <EOL> def __init__ ( self , base_type , ** kwargs ) : <EOL> super ( OneOrMore , self ) . __init__ ( ** kwargs ) <EOL> self . base_type = base_type <EOL> def _deserialize ( self , value , attr , data ) : <EOL> if isinstance ( value , collections . Sequence ) and not isinstance ( value , basestring ) : <EOL> return [ self . base_type . _deserialize ( v , attr , data ) <EOL> for v in value ] <EOL> else : <EOL> return [ self . base_type . _deserialize ( value , attr , data ) ] <EOL> class SshGateway ( bases . Hashable , bases . Representable , <EOL> bases . ConstructableFromDict ) : <EOL> class Schema ( marshmallow . Schema ) : <EOL> hostname = fields . String ( ) <EOL> port = fields . Integer ( missing = <NUM_LIT> ) <EOL> username = fields . String ( ) <EOL> password = fields . String ( missing = None ) <EOL> private_key = fields . String ( missing = None ) <EOL> gateway = fields . Nested ( '<STR_LIT>' , missing = None ) <EOL> connection_attempts = fields . Integer ( missing = <NUM_LIT:1> ) <EOL> attempt_failure_sleep = fields . Float ( missing = <NUM_LIT> ) <EOL> @ marshmallow . post_load <EOL> def to_ssh_gateway ( self , data ) : <EOL> return SshGateway ( data ) <EOL> class SshSettings ( bases . Hashable , bases . Representable , <EOL> bases . ConstructableFromDict ) : <EOL> class Schema ( marshmallow . Schema ) : <EOL> port = fields . Integer ( missing = <NUM_LIT> ) <EOL> username = fields . String ( required = True ) <EOL> password = fields . String ( missing = None ) <EOL> gateway = fields . Nested ( SshGateway . Schema , missing = None ) <EOL> connection_attempts = fields . Integer ( missing = <NUM_LIT:1> ) <EOL> cipher = fields . String ( missing = None ) <EOL> private_key = fields . String ( missing = None ) <EOL> timeout = fields . Integer ( missing = <NUM_LIT> ) <EOL> attempt_failure_sleep = fields . Float ( missing = <NUM_LIT> ) <EOL> @ marshmallow . post_load <EOL> def to_ssh_settings ( self , data ) : <EOL> return SshSettings ( data ) <EOL> class Scope ( bases . Hashable , bases . Representable , bases . ConstructableFromDict ) : <EOL> class Schema ( marshmallow . Schema ) : <EOL> project_name = fields . String ( missing = None ) <EOL> project_id = fields . String ( missing = None ) <EOL> domain_id = fields . String ( missing = None ) <EOL> @ marshmallow . post_load <EOL> def to_scope ( self , data ) : <EOL> return Scope ( data ) <EOL> @ marshmallow . validates_schema ( skip_on_field_errors = True ) <EOL> def check_migration_have_correct_source_and_dict ( self , data ) : <EOL> if all ( data [ k ] is None for k in self . declared_fields . keys ( ) ) : <EOL> raise marshmallow . ValidationError ( <EOL> '<STR_LIT>' . format ( <EOL> keys = self . declared_fields . keys ( ) ) ) <EOL> class Credential ( bases . Hashable , bases . Representable , <EOL> bases . ConstructableFromDict ) : <EOL> class Schema ( marshmallow . Schema ) : <EOL> auth_url = fields . Url ( ) <EOL> username = fields . String ( ) <EOL> password = fields . String ( ) <EOL> region_name = fields . String ( missing = None ) <EOL> domain_id = fields . String ( missing = None ) <EOL> https_insecure = fields . Boolean ( missing = False ) <EOL> https_cacert = fields . String ( missing = None ) <EOL> endpoint_type = fields . String ( missing = '<STR_LIT>' ) <EOL> @ marshmallow . post_load <EOL> def to_credential ( self , data ) : <EOL> return Credential ( data ) <EOL> def database_settings ( database_name ) : <EOL> class_name = database_name . capitalize ( ) + '<STR_LIT>' <EOL> global_vars = globals ( ) <EOL> if class_name in global_vars : <EOL> return global_vars [ class_name ] <EOL> class DatabaseSettings ( bases . Hashable , bases . Representable , <EOL> bases . ConstructableFromDict ) : <EOL> class Schema ( marshmallow . Schema ) : <EOL> host = fields . String ( missing = '<STR_LIT:localhost>' ) <EOL> port = fields . Integer ( missing = <NUM_LIT> ) <EOL> username = fields . String ( missing = database_name ) <EOL> password = fields . String ( ) <EOL> database = fields . String ( missing = database_name ) <EOL> @ marshmallow . post_load <EOL> def to_cloud ( self , data ) : <EOL> return DatabaseSettings ( data ) <EOL> DatabaseSettings . __name__ = class_name <EOL> global_vars [ class_name ] = DatabaseSettings <EOL> return DatabaseSettings <EOL> class OpenstackCloud ( bases . Hashable , bases . Representable , <EOL> bases . ConstructableFromDict ) : <EOL> class Schema ( marshmallow . Schema ) : <EOL> credential = fields . Nested ( Credential . Schema ) <EOL> scope = fields . Nested ( Scope . Schema ) <EOL> ssh_settings = fields . Nested ( SshSettings . Schema , load_from = '<STR_LIT>' ) <EOL> keystone_db = fields . Nested ( database_settings ( '<STR_LIT>' ) . Schema ) <EOL> nova_db = fields . Nested ( database_settings ( '<STR_LIT>' ) . Schema , <EOL> required = True ) <EOL> neutron_db = fields . Nested ( database_settings ( '<STR_LIT>' ) . Schema , <EOL> required = True ) <EOL> glance_db = fields . Nested ( database_settings ( '<STR_LIT>' ) . Schema , <EOL> required = True ) <EOL> cinder_db = fields . Nested ( database_settings ( '<STR_LIT>' ) . Schema , <EOL> required = True ) <EOL> discover = fields . List ( fields . String ( ) , missing = MODEL_LIST ) <EOL> @ marshmallow . post_load <EOL> def to_cloud ( self , data ) : <EOL> return OpenstackCloud ( data ) <EOL> def __init__ ( self , data ) : <EOL> super ( OpenstackCloud , self ) . __init__ ( data ) <EOL> self . name = None <EOL> class Migration ( bases . Hashable , bases . Representable ) : <EOL> class Schema ( marshmallow . Schema ) : <EOL> source = fields . String ( required = True ) <EOL> destination = fields . String ( required = True ) <EOL> objects = DictField ( <EOL> fields . String ( ) , <EOL> FirstFit ( <EOL> fields . String ( ) , <EOL> DictField ( <EOL> fields . String ( ) , <EOL> OneOrMore ( fields . Raw ( ) ) ) , <EOL> many = True ) , <EOL> required = True ) <EOL> migration_flow_factories = fields . List ( fields . String ( ) , missing = list ) <EOL> @ marshmallow . post_load <EOL> def to_migration ( self , data ) : <EOL> return Migration ( ** data ) <EOL> def __init__ ( self , source , destination , objects , migration_flow_factories ) : <EOL> self . source = source <EOL> self . destination = destination <EOL> self . query = query . Query ( objects ) <EOL> self . migration_flow_factories = { } <EOL> migration_flow_factories = DEFAULT_MIGRATION_LIST + migration_flow_factories <EOL> for factory_class_name in migration_flow_factories : <EOL> factory_class = importutils . import_class ( factory_class_name ) <EOL> migrated_class = factory_class . migrated_class <EOL> self . migration_flow_factories [ migrated_class ] = factory_class <EOL> class Configuration ( bases . Hashable , bases . Representable , <EOL> bases . ConstructableFromDict ) : <EOL> class Schema ( marshmallow . Schema ) : <EOL> clouds = DictField ( <EOL> fields . String ( allow_none = False ) , <EOL> fields . Nested ( OpenstackCloud . Schema , default = dict ) ) <EOL> migrations = DictField ( <EOL> fields . String ( allow_none = False ) , <EOL> fields . Nested ( Migration . Schema , default = dict ) , debug = True ) <EOL> @ marshmallow . validates_schema ( skip_on_field_errors = True ) <EOL> def check_migration_have_correct_source_and_dict ( self , data ) : <EOL> clouds = data [ '<STR_LIT>' ] <EOL> migrations = data . get ( '<STR_LIT>' , { } ) <EOL> for migration_name , migration in migrations . items ( ) : <EOL> if migration . source not in clouds : <EOL> raise marshmallow . ValidationError ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' . format ( migration_name , migration . source ) ) <EOL> if migration . destination not in clouds : <EOL> raise marshmallow . ValidationError ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' . format ( migration_name , <EOL> migration . destination ) ) <EOL> @ marshmallow . post_load <EOL> def to_configuration ( self , data ) : <EOL> for name , cloud in data [ '<STR_LIT>' ] . items ( ) : <EOL> cloud . name = name <EOL> return Configuration ( data ) <EOL> def load ( data ) : <EOL> """<STR_LIT>""" <EOL> schema = Configuration . Schema ( strict = True ) <EOL> return schema . load ( data ) . data </s>
