<s> """<STR_LIT>""" <EOL> import random <EOL> from occi import backend <EOL> from occi import exceptions <EOL> from occi_os_api . extensions import os_addon <EOL> from occi_os_api . nova_glue import vm <EOL> from occi_os_api . nova_glue import security <EOL> class OsComputeBackend ( backend . MixinBackend , backend . ActionBackend ) : <EOL> """<STR_LIT>""" <EOL> def retrieve ( self , entity , extras ) : <EOL> """<STR_LIT>""" <EOL> uid = entity . attributes [ '<STR_LIT>' ] <EOL> context = extras [ '<STR_LIT>' ] <EOL> if '<STR_LIT>' in entity . attributes and entity . attributes [ <EOL> '<STR_LIT>' ] == '<STR_LIT>' : <EOL> entity . actions . append ( os_addon . OS_CREATE_IMAGE ) <EOL> entity . actions . append ( os_addon . OS_CHG_PWD ) <EOL> console = vm . get_vnc ( uid , context ) <EOL> if console : <EOL> entity . attributes [ '<STR_LIT>' ] = console [ '<STR_LIT:url>' ] <EOL> else : <EOL> entity . attributes [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> entity . attributes [ '<STR_LIT>' ] = vm . get_vm ( uid , context ) [ '<STR_LIT>' ] <EOL> def action ( self , entity , action , attributes , extras ) : <EOL> """<STR_LIT>""" <EOL> context = extras [ '<STR_LIT>' ] <EOL> uid = entity . attributes [ '<STR_LIT>' ] <EOL> if action == os_addon . OS_CHG_PWD : <EOL> if '<STR_LIT>' not in attributes : <EOL> msg = '<STR_LIT>' '<STR_LIT>' <EOL> raise AttributeError ( msg ) <EOL> new_password = attributes [ '<STR_LIT>' ] <EOL> vm . set_password_for_vm ( uid , new_password , context ) <EOL> elif action == os_addon . OS_CREATE_IMAGE : <EOL> if '<STR_LIT>' not in attributes : <EOL> raise AttributeError ( '<STR_LIT>' ) <EOL> image_name = attributes [ '<STR_LIT>' ] <EOL> vm . snapshot_vm ( uid , image_name , context ) <EOL> else : <EOL> raise AttributeError ( '<STR_LIT>' ) <EOL> class OsNetLinkBackend ( backend . MixinBackend , backend . ActionBackend ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> class SecurityGroupBackend ( backend . UserDefinedMixinBackend ) : <EOL> """<STR_LIT>""" <EOL> def init_sec_group ( self , category , extras ) : <EOL> """<STR_LIT>""" <EOL> if category . scheme == '<STR_LIT>' : <EOL> return <EOL> context = extras [ '<STR_LIT>' ] <EOL> group_name = category . term . strip ( ) <EOL> group_description = ( category . title . strip ( ) <EOL> if category . title else group_name ) <EOL> security . create_group ( group_name , group_description , context ) <EOL> def destroy ( self , category , extras ) : <EOL> """<STR_LIT>""" <EOL> context = extras [ '<STR_LIT>' ] <EOL> security_group = security . retrieve_group_by_name ( category . term , <EOL> extras [ '<STR_LIT>' ] ) <EOL> security . remove_group ( security_group , context ) <EOL> class SecurityRuleBackend ( backend . KindBackend ) : <EOL> """<STR_LIT>""" <EOL> def create ( self , entity , extras ) : <EOL> """<STR_LIT>""" <EOL> sec_mixin = get_sec_mixin ( entity ) <EOL> context = extras [ '<STR_LIT>' ] <EOL> security_group = security . retrieve_group_by_name ( sec_mixin . term , <EOL> context ) <EOL> sg_rule = make_sec_rule ( entity , security_group [ '<STR_LIT:id>' ] ) <EOL> if security_group_rule_exists ( security_group , sg_rule ) : <EOL> msg = '<STR_LIT>' % str ( security_group ) <EOL> raise AttributeError ( msg ) <EOL> rule = security . create_rule ( sec_mixin . term , security_group [ '<STR_LIT:id>' ] , <EOL> [ sg_rule ] , context ) <EOL> entity . attributes [ '<STR_LIT>' ] = str ( rule [ '<STR_LIT:id>' ] ) <EOL> def delete ( self , entity , extras ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> context = extras [ '<STR_LIT>' ] <EOL> rule = security . retrieve_rule ( entity . attributes [ '<STR_LIT>' ] , <EOL> context ) <EOL> security . remove_rule ( rule , context ) <EOL> except Exception as error : <EOL> raise exceptions . HTTPError ( <NUM_LIT> , str ( error ) ) <EOL> def make_sec_rule ( entity , sec_grp_id ) : <EOL> """<STR_LIT>""" <EOL> name = random . randrange ( <NUM_LIT:0> , <NUM_LIT> ) <EOL> sg_rule = { '<STR_LIT:id>' : name , <EOL> '<STR_LIT>' : sec_grp_id } <EOL> prot = entity . attributes [ '<STR_LIT>' ] . lower ( ) . strip ( ) <EOL> if prot in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> sg_rule [ '<STR_LIT>' ] = prot <EOL> else : <EOL> raise AttributeError ( '<STR_LIT>' + prot ) <EOL> from_p = entity . attributes [ '<STR_LIT>' ] . strip ( ) <EOL> from_p = int ( from_p ) <EOL> if ( type ( from_p ) is int ) and <NUM_LIT:0> < from_p <= <NUM_LIT> : <EOL> sg_rule [ '<STR_LIT>' ] = from_p <EOL> else : <EOL> raise AttributeError ( '<STR_LIT>' ) <EOL> to_p = entity . attributes [ '<STR_LIT>' ] . strip ( ) <EOL> to_p = int ( to_p ) <EOL> if ( type ( to_p ) is int ) and <NUM_LIT:0> < to_p <= <NUM_LIT> : <EOL> sg_rule [ '<STR_LIT>' ] = to_p <EOL> else : <EOL> raise AttributeError ( '<STR_LIT>' ) <EOL> if from_p > to_p : <EOL> raise AttributeError ( '<STR_LIT>' ) <EOL> cidr = entity . attributes [ '<STR_LIT>' ] . strip ( ) <EOL> if len ( cidr ) <= <NUM_LIT:0> : <EOL> cidr = '<STR_LIT>' <EOL> if True : <EOL> sg_rule [ '<STR_LIT>' ] = cidr <EOL> else : <EOL> raise AttributeError ( '<STR_LIT>' ) <EOL> sg_rule [ '<STR_LIT>' ] = { } <EOL> return sg_rule <EOL> def get_sec_mixin ( entity ) : <EOL> """<STR_LIT>""" <EOL> sec_mixin_present = <NUM_LIT:0> <EOL> sec_mixin = None <EOL> for mixin in entity . mixins : <EOL> if os_addon . SEC_GROUP in mixin . related : <EOL> sec_mixin = mixin <EOL> sec_mixin_present += <NUM_LIT:1> <EOL> if not sec_mixin_present : <EOL> msg = '<STR_LIT>' <EOL> raise AttributeError ( msg ) <EOL> if sec_mixin_present > <NUM_LIT:1> : <EOL> msg = '<STR_LIT>' <EOL> raise AttributeError ( msg ) <EOL> return sec_mixin <EOL> def security_group_rule_exists ( security_group , values ) : <EOL> """<STR_LIT>""" <EOL> for rule in security_group [ '<STR_LIT>' ] : <EOL> is_duplicate = True <EOL> keys = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> for key in keys : <EOL> if rule . get ( key ) != values . get ( key ) : <EOL> is_duplicate = False <EOL> break <EOL> if is_duplicate : <EOL> return True <EOL> return False </s>
