<s> import logging <EOL> from dbaas_credentials . models import CredentialType <EOL> from util import get_credentials_for <EOL> from util import full_stack <EOL> from dbaas_cloudstack . models import HostAttr <EOL> from dbaas_cloudstack . models import DatabaseInfraAttr <EOL> from dbaas_cloudstack . provider import CloudStackProvider <EOL> from ... util . base import BaseStep <EOL> from ... . exceptions . error_codes import DBAAS_0010 <EOL> LOG = logging . getLogger ( __name__ ) <EOL> class CreateSecondaryIp ( BaseStep ) : <EOL> def __unicode__ ( self ) : <EOL> return "<STR_LIT>" <EOL> def do ( self , workflow_dict ) : <EOL> try : <EOL> if '<STR_LIT>' not in workflow_dict : <EOL> return False <EOL> if len ( workflow_dict [ '<STR_LIT>' ] ) == <NUM_LIT:1> : <EOL> return True <EOL> cs_credentials = get_credentials_for ( <EOL> environment = workflow_dict [ '<STR_LIT>' ] , <EOL> credential_type = CredentialType . CLOUDSTACK ) <EOL> LOG . info ( "<STR_LIT>" ) <EOL> networkapi_credentials = get_credentials_for ( <EOL> environment = workflow_dict [ '<STR_LIT>' ] , <EOL> credential_type = CredentialType . NETWORKAPI ) <EOL> cs_provider = CloudStackProvider ( credentials = cs_credentials , <EOL> networkapi_credentials = networkapi_credentials ) <EOL> if not cs_provider : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> return False <EOL> workflow_dict [ '<STR_LIT>' ] = [ ] <EOL> networkapi_equipment_id = cs_provider . register_networkapi_equipment ( <EOL> equipment_name = workflow_dict [ '<STR_LIT>' ] [ '<STR_LIT>' ] ) <EOL> if not networkapi_equipment_id : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> return False <EOL> workflow_dict [ '<STR_LIT>' ] = networkapi_equipment_id <EOL> for host in workflow_dict [ '<STR_LIT>' ] : <EOL> LOG . info ( "<STR_LIT>" ) <EOL> host_attr = HostAttr . objects . get ( host = host ) <EOL> reserved_ip = cs_provider . reserve_ip ( <EOL> project_id = cs_credentials . project , <EOL> vm_id = host_attr . vm_id ) <EOL> if not reserved_ip : <EOL> return False <EOL> total = DatabaseInfraAttr . objects . filter ( <EOL> databaseinfra = workflow_dict [ '<STR_LIT>' ] ) . count ( ) <EOL> databaseinfraattr = DatabaseInfraAttr ( ) <EOL> databaseinfraattr . ip = reserved_ip [ '<STR_LIT>' ] <EOL> if total == <NUM_LIT:0> : <EOL> databaseinfraattr . is_write = True <EOL> LOG . info ( "<STR_LIT>" ) <EOL> databaseinfra = workflow_dict [ '<STR_LIT>' ] <EOL> databaseinfra . endpoint = databaseinfraattr . ip + "<STR_LIT>" % <NUM_LIT> <EOL> databaseinfra . save ( ) <EOL> workflow_dict [ '<STR_LIT>' ] = databaseinfra <EOL> ip_desc = '<STR_LIT>' <EOL> else : <EOL> databaseinfraattr . is_write = False <EOL> ip_desc = '<STR_LIT>' <EOL> networkapi_ip_id = cs_provider . register_networkapi_ip ( equipment_id = networkapi_equipment_id , <EOL> ip = reserved_ip [ <EOL> '<STR_LIT>' ] , <EOL> ip_desc = ip_desc ) <EOL> databaseinfraattr . cs_ip_id = reserved_ip [ '<STR_LIT>' ] <EOL> databaseinfraattr . networkapi_equipment_id = networkapi_equipment_id <EOL> databaseinfraattr . networkapi_ip_id = networkapi_ip_id <EOL> databaseinfraattr . databaseinfra = workflow_dict [ <EOL> '<STR_LIT>' ] <EOL> databaseinfraattr . save ( ) <EOL> workflow_dict [ '<STR_LIT>' ] . append ( databaseinfraattr ) <EOL> return True <EOL> except Exception : <EOL> traceback = full_stack ( ) <EOL> workflow_dict [ '<STR_LIT>' ] [ '<STR_LIT>' ] . append ( DBAAS_0010 ) <EOL> workflow_dict [ '<STR_LIT>' ] [ '<STR_LIT>' ] . append ( traceback ) <EOL> return False <EOL> def undo ( self , workflow_dict ) : <EOL> LOG . info ( "<STR_LIT>" ) <EOL> try : <EOL> if '<STR_LIT>' not in workflow_dict and '<STR_LIT>' not in workflow_dict : <EOL> LOG . info ( <EOL> "<STR_LIT>" ) <EOL> return False <EOL> if len ( workflow_dict [ '<STR_LIT>' ] ) == <NUM_LIT:1> : <EOL> return True <EOL> databaseinfraattr = DatabaseInfraAttr . objects . filter ( <EOL> databaseinfra = workflow_dict [ '<STR_LIT>' ] ) <EOL> cs_credentials = get_credentials_for ( <EOL> environment = workflow_dict [ '<STR_LIT>' ] , <EOL> credential_type = CredentialType . CLOUDSTACK ) <EOL> networkapi_credentials = get_credentials_for ( <EOL> environment = workflow_dict [ '<STR_LIT>' ] , <EOL> credential_type = CredentialType . NETWORKAPI ) <EOL> cs_provider = CloudStackProvider ( credentials = cs_credentials , <EOL> networkapi_credentials = networkapi_credentials ) <EOL> networkapi_equipment_id = workflow_dict . get ( <EOL> '<STR_LIT>' ) <EOL> for infra_attr in databaseinfraattr : <EOL> networkapi_equipment_id = infra_attr . networkapi_equipment_id <EOL> networkapi_ip_id = infra_attr . networkapi_ip_id <EOL> if networkapi_ip_id : <EOL> LOG . info ( "<STR_LIT>" % <EOL> networkapi_ip_id ) <EOL> if not cs_provider . remove_networkapi_ip ( equipment_id = networkapi_equipment_id , <EOL> ip_id = networkapi_ip_id ) : <EOL> return False <EOL> LOG . info ( "<STR_LIT>" % infra_attr . cs_ip_id ) <EOL> if not cs_provider . remove_secondary_ips ( infra_attr . cs_ip_id ) : <EOL> return False <EOL> LOG . info ( "<STR_LIT>" ) <EOL> infra_attr . delete ( ) <EOL> LOG . info ( "<STR_LIT>" ) <EOL> if networkapi_equipment_id : <EOL> cs_provider . remove_networkapi_equipment ( <EOL> equipment_id = networkapi_equipment_id ) <EOL> return True <EOL> except Exception : <EOL> traceback = full_stack ( ) <EOL> workflow_dict [ '<STR_LIT>' ] [ '<STR_LIT>' ] . append ( DBAAS_0010 ) <EOL> workflow_dict [ '<STR_LIT>' ] [ '<STR_LIT>' ] . append ( traceback ) <EOL> return False </s>
