from workflow.workflow import stop_workflow, start_workflow
from physical.models import DatabaseInfra
from dbaas_credentials.models import CredentialType
from util import get_credentials_for
import logging
from util import build_dict, slugify
import re

LOG = logging.getLogger(__name__)

UNKNOWN = 0
MYSQL = 1
MONGODB = 2
REDIS = 3

def make_infra(plan, environment, name, team, project, description, task=None):
    if not plan.provider == plan.CLOUDSTACK:
        dbinfra = DatabaseInfra.best_for(plan= plan, environment= environment, name= name)

        if dbinfra:
            return build_dict(databaseinfra= dbinfra, created=True)

        return build_dict(databaseinfra=None, created= False)

    workflow_dict = build_dict(name= slugify(name),
                               plan= plan,
                               environment= environment,
                               steps= get_engine_steps(engine= str(plan.engine_type)),
                               qt= get_vm_qt(plan= plan, ),
                               MYSQL = MYSQL,
                               MONGODB = MONGODB,
                               REDIS = REDIS,
                               enginecod = get_engine(engine= str(plan.engine_type)),
                               dbtype = str(plan.engine_type),
                               team= team,
                               project= project,
                               description= description,
                               )

    start_workflow(workflow_dict= workflow_dict, task=task)
    return workflow_dict



def destroy_infra(databaseinfra, task=None):
    if not databaseinfra.plan.provider == databaseinfra.plan.CLOUDSTACK:
        return True

    try:
      database = databaseinfra.databases.all()[0]
      LOG.debug('Database found! {}'.format(database))
    except IndexError:
      LOG.info("Database not found...")

    instances = []
    hosts = []

    for instance in databaseinfra.instances.all():
        instances.append(instance)
        hosts.append(instance.hostname)

    workflow_dict = build_dict(plan= databaseinfra.plan,
                               environment= databaseinfra.environment,
                               steps= get_engine_steps(engine= str(databaseinfra.plan.engine_type)),
                               qt= get_vm_qt(plan= databaseinfra.plan),
                               hosts= hosts,
                               instances= instances,
                               databaseinfra= databaseinfra,
                               MYSQL = MYSQL,
                               MONGODB = MONGODB,
                               REDIS = REDIS,
                               enginecod = get_engine(engine= str(databaseinfra.plan.engine_type)),
                               database=database
                               )

    if stop_workflow(workflow_dict= workflow_dict, task=task):
        return workflow_dict
    else:
        return False


def get_engine(engine):
    engine = engine.lower()

    if re.match(r'^mongo.*', engine):
        return MONGODB
    elif re.match(r'^mysql.*', engine):
        return MYSQL
    elif re.match(r'^redis.*', engine):
        return REDIS
    else:
        return UNKNOWN



def get_engine_steps(engine):

    enginecod = get_engine(engine)

    if enginecod == MONGODB:
        from workflow.settings import DEPLOY_MONGO
        steps = DEPLOY_MONGO
    elif enginecod == MYSQL:
        from workflow.settings import DEPLOY_MYSQL
        steps = DEPLOY_MYSQL
    elif enginecod == REDIS:
        from workflow.settings import DEPLOY_REDIS
        steps = DEPLOY_REDIS
    else:
        from workflow.settings import DEPLOY_UNKNOWN
        steps = DEPLOY_UNKNOWN

    return steps

def get_vm_qt(plan):
    if plan.is_ha:
        if get_engine(engine= str(plan.engine_type)) == MONGODB:
            qt = 3
        else:
            qt = 2
    else:
        qt = 1

    return qt


def get_engine_credentials(engine, environment):
    engine = engine.lower()

    if re.match(r'^mongo.*', engine):
        credential_type = CredentialType.MONGODB
    elif re.match(r'^mysql.*', engine):
        credential_type = CredentialType.MYSQL
    elif re.match(r'^redis.*', engine):
        credential_type = CredentialType.MYSQL

    return get_credentials_for(
                environment=environment, credential_type=credential_type)

def resize_database(database, cloudstackpack, task=None):

    from dbaas_cloudstack.models import CloudStackPack
    original_cloudstackpack = CloudStackPack.objects.get(offering__serviceofferingid = database.offering_id,
                                                         offering__region__environment = database.environment,
                                                         engine_type__name = database.engine_type)
    workflow_dict = build_dict(database= database,
                               cloudstackpack= cloudstackpack,
                               original_cloudstackpack = original_cloudstackpack,
                               environment= database.environment,
                               steps= get_engine_resize_steps(engine= str(database.plan.engine_type)),
                               enginecod = get_engine(engine= str(database.plan.engine_type))
                               )

    start_workflow(workflow_dict= workflow_dict, task=task)

    return workflow_dict

def get_engine_resize_steps(engine):

    enginecod = get_engine(engine)

    if enginecod == MONGODB:
        from workflow.settings import RESIZE_MONGO
        steps = RESIZE_MONGO
    elif enginecod == MYSQL:
        from workflow.settings import RESIZE_MYSQL
        steps = RESIZE_MYSQL
    elif enginecod == REDIS:
        from workflow.settings import RESIZE_REDIS
        steps = RESIZE_REDIS
    else:
        from workflow.settings import RESIZE_UNKNOWN
        steps = RESIZE_UNKNOWN

    return steps

@@ -95,16 +95,52 @@
 seInfra%0A
+from logical.models import Database%0A
 from dba
@@ -183,16 +183,16 @@
 ialType%0A
-
 from uti
@@ -600,16 +600,240 @@
 binfra:%0A
+            database = Database.provision(databaseinfra=dbinfra, name=name)%0A            database.team = team%0A            database.description = description%0A            database.project = project%0A            database.save()%0A%0A
         
@@ -877,16 +877,36 @@
 dbinfra,
+ database= database,
  created
@@ -906,16 +906,17 @@
 created=
+ 
 True)%0A%0A 
@@ -1886,101 +1886,8 @@
 ne):
-%0A    if not databaseinfra.plan.provider == databaseinfra.plan.CLOUDSTACK:%0A        return True
 %0A%0A  
@@ -2021,16 +2021,16 @@
 xError:%0A
-
       LO
@@ -2062,16 +2062,136 @@
 d...%22)%0A%0A
+    if not databaseinfra.plan.provider == databaseinfra.plan.CLOUDSTACK:%0A        database.delete()%0A        return True%0A%0A
     inst

Add support for pre prov dbs