# -*- coding: utf-8 -*-
import logging
from base import BaseStep
from dbaas_dnsapi.models import HOST as DNSAPI_HOST, INSTANCE as DNSAPI_INSTANCE, FLIPPER as DNSAPI_FLIPPER
from dbaas_dnsapi.models import PlanAttr, DatabaseInfraDNSList
from dbaas_cloudstack.models import Databaseinfraattr
#from util import get_credentials_for

LOG = logging.getLogger(__name__)

class CreateDns(BaseStep):

    def do(self, workflow_dict):
        for infra_attr in workflow_dict['databaseinfraattr']:

            if infra_attr.is_write:
                dnsname = infra_attr.name
            else:
                dnsname = infra_attr.name + 'r'

            planattr = PlanAttr.objects.get(dbaas_plan=workflow_dict['databaseinfra'].plan)
            if planattr.dnsapi_database_sufix:
                sufix = '.' + planattr.dnsapi_database_sufix
            else:
                sufix = ''

            if type == DNSAPI_HOST:
                domain = planattr.dnsapi_vm_domain
            else:
                domain = planattr.dnsapi_database_domain
                name += sufix

            databaseinfradnslist = DatabaseInfraDNSList(
                databaseinfra = workflow_dict['databaseinfra'].id,
                name = dnsname,
                domain = domain,
                ip = infra_attr.ip,
                type = type)
            databaseinfradnslist.save()

            dnsname = '%s.%s' % (name, domain)


    def undo(self, workflow_dict):
        pass

@@ -76,30 +76,28 @@
 _dnsapi.
-models
+util
  import 
 HOST as 
@@ -92,146 +92,71 @@
 ort 
-HOST as DNSAPI_HOST, INSTANCE as DNSAPI_INSTANCE, FLIPPER as DNSAPI_FLIPPER%0Afrom dbaas_dnsapi.models import PlanAttr, DatabaseInfraDNSList
+add_dns_record%0Afrom dbaas_dnsapi.provider import DNSAPIProvider
 %0Afro
@@ -167,18 +167,14 @@
 aas_
-cloudstack
+dnsapi
 .mod
@@ -188,25 +188,31 @@
 ort 
-Databaseinfraattr
+HOST, FLIPPER, INSTANCE
 %0A#fr
@@ -335,32 +335,33 @@
 workflow_dict):%0A
+%0A
         for infr
@@ -465,34 +465,54 @@
   dnsname = 
-infra_attr
+workflow_dict%5B'databaseinfra'%5D
 .name%0A      
@@ -549,26 +549,46 @@
 sname = 
-infra_attr
+workflow_dict%5B'databaseinfra'%5D
 .name + 
@@ -588,16 +588,17 @@
 name + '
+-
 r'%0A%0A    
@@ -609,51 +609,48 @@
     
-planattr = PlanAttr.objects.get(dbaas_plan=
+dnsname = add_dns_record(databaseinfra= 
 work
@@ -679,14 +679,9 @@
 ra'%5D
-.plan)
+,
 %0A   
@@ -693,103 +693,135 @@
     
-if planattr.dnsapi_database_sufix:%0A                sufix = '.' + planattr.dnsapi_database_sufix
+                                         name= dnsname,%0A                                                     ip= infra_attr.ip,
 %0A   
@@ -821,38 +821,32 @@
 ip,%0A            
-else:%0A
                 
@@ -849,286 +849,691 @@
     
-sufix = ''%0A%0A            if type == DNSAPI_HOST:%0A                domain = planattr.dnsapi_vm_domain%0A            else:%0A                domain = planattr.dnsapi_database_domain%0A                name += sufix%0A%0A            databaseinfradnslist = DatabaseInfraDNSList(%0A                
+                         type= FLIPPER)%0A%0A%0A        for host_name in zip(workflow_dict%5B'hosts'%5D, workflow_dict%5B'names'%5D%5B'vms'%5D):%0A            host = host_name%5B0%5D%0A%0A            host.hostname = add_dns_record(databaseinfra= workflow_dict%5B'databaseinfra'%5D,%0A                                                             name= host_name%5B1%5D,%0A                                                             ip= host.addres,%0A                                                             type= HOST)%0A            host.save()%0A%0A        for instance_name in zip(workflow_dict%5B'instances'%5D, workflow_dict%5B'names'%5D%5B'vms'%5D):%0A            instance = instance_name%5B0%5D%0A%0A            instance.dns = add_dns_record(
 data
@@ -1541,17 +1541,16 @@
 aseinfra
- 
 = workfl
@@ -1577,13 +1577,52 @@
 ra'%5D
-.id
 ,%0A
+                                          
     
@@ -1641,18 +1641,26 @@
 name
- 
 = 
-d
+i
 ns
+tance_
 name
+%5B1%5D
 ,%0A  
@@ -1677,25 +1677,34 @@
     
-domain = domain,%0A
+                          
     
@@ -1721,26 +1721,69 @@
   ip
- 
 = in
-fra_attr.ip
+stance.addres
 ,%0A
+                                          
     
@@ -1802,15 +1802,18 @@
 type
- 
 = 
-type
+INSTANCE
 )%0A  
@@ -1826,28 +1826,16 @@
     
-databaseinfradnslist
+instance
 .sav
@@ -1839,28 +1839,25 @@
 save()%0A%0A
-    
+%0A
         
 dnsname 
@@ -1852,42 +1852,89 @@
     
-dnsname = '%25s.%25s' %25 (name, domain)
+DNSAPIProvider.create_database_dns(databaseinfra=workflow_dict%5B'databaseinfra'%5D)%0A
 %0A%0A%0A 

Add create_dns step