# -*- coding: utf-8 -*-
import logging
from base import BaseStep
from dbaas_dnsapi.util import add_dns_record
from dbaas_dnsapi.provider import DNSAPIProvider
from dbaas_dnsapi.models import HOST, FLIPPER, INSTANCE
#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 = workflow_dict['databaseinfra'].name
            else:
                dnsname = workflow_dict['databaseinfra'].name + '-r'

            dnsname = add_dns_record(databaseinfra= workflow_dict['databaseinfra'],
                                                     name= dnsname,
                                                     ip= infra_attr.ip,
                                                     type= FLIPPER)


        for host_name in zip(workflow_dict['hosts'], workflow_dict['names']['vms']):
            host = host_name[0]

            host.hostname = add_dns_record(databaseinfra= workflow_dict['databaseinfra'],
                                                             name= host_name[1],
                                                             ip= host.addres,
                                                             type= HOST)
            host.save()

        for instance_name in zip(workflow_dict['instances'], workflow_dict['names']['vms']):
            instance = instance_name[0]

            instance.dns = add_dns_record(databaseinfra= workflow_dict['databaseinfra'],
                                                          name= instance_name[1],
                                                          ip= instance.addres,
                                                          type= INSTANCE)
            instance.save()


        DNSAPIProvider.create_database_dns(databaseinfra=workflow_dict['databaseinfra'])



    def undo(self, workflow_dict):
        pass

@@ -76,24 +76,25 @@
 _dnsapi.util
+s
  import add_
@@ -209,16 +209,69 @@
 NSTANCE%0A
+from dbaas_dnsapi.models import DatabaseInfraDNSList%0A
 #from ut
@@ -376,34 +376,233 @@
 def 
-do(self, workflow_dict):%0A%0A
+__unicode__(self):%0A        return %22Creating DNS%22%0A%0A    def do(self, workflow_dict):%0A        try:%0A%0A            if 'databaseinfraattr' in workflow_dict:%0A%0A                LOG.info(%22Creating dns for databaseinfraattr...%22)%0A        
     
@@ -660,16 +660,24 @@
 ttr'%5D:%0A%0A
+        
         
@@ -712,32 +712,40 @@
                 
+        
 dnsname = workfl
@@ -786,16 +786,24 @@
         
+        
 else:%0A  
@@ -808,32 +808,40 @@
                 
+ 
+       
 dnsname = workfl
@@ -878,16 +878,24 @@
 + '-r'%0A%0A
+        
         
@@ -1019,24 +1019,32 @@
             
+  
+      
 name= dnsnam
@@ -1046,16 +1046,24 @@
 nsname,%0A
+        
         
@@ -1175,24 +1175,32 @@
             
+        
 type= FLIPPE
@@ -1202,18 +1202,154 @@
 LIPPER)%0A
-%0A%0A
+                    infra_attr.dns = dnsname%0A                    infra_attr.save()%0A%0A            LOG.info(%22Creating dns for hosts...%22)%0A    
         
@@ -1433,24 +1433,28 @@
             
+    
 host = host_
@@ -1454,32 +1454,36 @@
 = host_name%5B0%5D%0A%0A
+    
             host
@@ -1613,24 +1613,28 @@
             
+    
 name= host_n
@@ -1698,24 +1698,28 @@
             
+ 
+   
 ip= host.add
@@ -1717,26 +1717,31 @@
  host.addres
+s
 ,%0A
+    
             
@@ -1809,24 +1809,28 @@
             
+    
 host.save()%0A
@@ -1822,32 +1822,90 @@
    host.save()%0A%0A
+            LOG.info(%22Creating dns for instances...%22)%0A    
         for inst
@@ -1985,32 +1985,36 @@
 %5D):%0A            
+    
 instance = insta
@@ -2018,32 +2018,36 @@
 stance_name%5B0%5D%0A%0A
+    
             inst
@@ -2173,24 +2173,28 @@
             
+    
 name= instan
@@ -2197,32 +2197,36 @@
 stance_name%5B1%5D,%0A
+    
                 
@@ -2286,18 +2286,23 @@
 e.addres
+s
 ,%0A
+    
         
@@ -2371,32 +2371,36 @@
 CE)%0A            
+    
 instance.save()%0A
@@ -2401,16 +2401,71 @@
 ave()%0A%0A%0A
+            LOG.info(%22Calling dnsapi provider...%22)%0A    
         
@@ -2550,44 +2550,579 @@
 %5D)%0A%0A
-%0A%0A    def undo(self, workflow_dict):
+            return True%0A%0A        except Exception,e :%0A            print e%0A            return False%0A%0A%0A%0A    def undo(self, workflow_dict):%0A        try:%0A            DNSAPIProvider.remove_database_dns(environment = workflow_dict%5B'environment'%5D,%0A                                                                   databaseinfraid = workflow_dict%5B'databaseinfra'%5D.id)%0A%0A%0A            DatabaseInfraDNSList.objects.filter(databaseinfra= workflow_dict%5B'databaseinfra'%5D.id).delete()%0A%0A            return True%0A%0A        except Exception,e :%0A            print e%0A            return False%0A%0A
 %0A   

Add an if clause to test if there is a 'databaseinfra' key inside workflow_dict