# -*- coding: utf-8 -*-
from util import full_stack
from dbaas_credentials.credential import Credential
from dbaas_credentials.models import CredentialType
from dbaas_zabbix import factory_for
from workflow.steps.util.base import BaseInstanceStep


class ZabbixStep(BaseInstanceStep):

    def __init__(self, instance):
        super(ZabbixStep, self).__init__(instance)

        integration = CredentialType.objects.get(type=CredentialType.ZABBIX)
        environment = self.instance.databaseinfra.environment
        credentials = Credential.get_credentials(environment, integration)

        self.zabbix_provider = factory_for(
            databaseinfra=self.instance.databaseinfra,
            credentials=credentials
        )

    def __del__(self):
        self.zabbix_provider.logout()

    def do(self):
        raise NotImplementedError

    def undo(self):
        pass


class DisableAlarms(ZabbixStep):

    def __unicode__(self):
        return "Disable Zabbix alarms..."

    def do(self):
        try:
            self.zabbix_provider.disable_alarms_to(
                self.instance.hostname.hostname
            )
            self.zabbix_provider.disable_alarms_to(self.instance.dns)
        except Exception as e:
            self.zabbix_provider.enable_alarms_to(
                self.instance.hostname.hostname
            )
            self.zabbix_provider.enable_alarms_to(self.instance.dns)

            raise EnvironmentError(
                'Could not disable Zabbix alarms: {}\n\n{}'.format(
                    e, full_stack()
                )
            )


class DestroyAlarms(ZabbixStep):

    def __unicode__(self):
        return "Destroying Zabbix alarms..."

    def do(self):
        try:
            self.zabbix_provider.delete_instance_monitors(
                self.instance.hostname.hostname
            )
            self.zabbix_provider.delete_instance_monitors(self.instance.dns)
        except Exception as e:
            raise EnvironmentError(
                'Could not destroy Zabbix alarms: {}\n\n{}'.format(
                    e, full_stack()
                )
            )


class CreateAlarms(ZabbixStep):

    def __unicode__(self):
        return "Creating Zabbix alarms..."

    def do(self):
        try:
            self.zabbix_provider.create_instance_basic_monitors(
                self.instance.hostname
            )
            self.zabbix_provider.create_instance_monitors(self.instance)
        except Exception as e:
            raise EnvironmentError(
                'Could not create Zabbix alarms: {}\n\n{}'.format(
                    e, full_stack()
                )
            )

@@ -21,36 +21,8 @@
 -*-%0A
-from util import full_stack%0A
 from
@@ -970,33 +970,16 @@
 (self):%0A
-        try:%0A    
         
@@ -1010,36 +1010,32 @@
 able_alarms_to(%0A
-    
             self
@@ -1070,33 +1070,26 @@
         
-    )%0A   
+)%0A
         
  self.za
@@ -1072,33 +1072,32 @@
       )%0A        
- 
 self.zabbix_prov
@@ -1142,394 +1142,8 @@
 ns)%0A
-        except Exception as e:%0A            self.zabbix_provider.enable_alarms_to(%0A                self.instance.hostname.hostname%0A            )%0A            self.zabbix_provider.enable_alarms_to(self.instance.dns)%0A%0A            raise EnvironmentError(%0A                'Could not disable Zabbix alarms: %7B%7D%5Cn%5Cn%7B%7D'.format(%0A                    e, full_stack()%0A                )%0A            )%0A
 %0A%0Acl
@@ -1265,33 +1265,16 @@
 (self):%0A
-        try:%0A    
         
@@ -1324,36 +1324,32 @@
 rs(%0A            
-    
 self.instance.ho
@@ -1368,34 +1368,26 @@
 ame%0A        
-   
- 
 )%0A
-    
         self
@@ -1451,211 +1451,8 @@
 ns)%0A
-        except Exception as e:%0A            raise EnvironmentError(%0A                'Could not destroy Zabbix alarms: %7B%7D%5Cn%5Cn%7B%7D'.format(%0A                    e, full_stack()%0A                )%0A            )%0A
 %0A%0Acl
@@ -1575,25 +1575,8 @@
 f):%0A
-        try:%0A    
     
@@ -1636,36 +1636,32 @@
 rs(%0A            
-    
 self.instance.ho
@@ -1667,36 +1667,32 @@
 ostname%0A        
-    
 )%0A            se
@@ -1681,28 +1681,24 @@
   )%0A        
- 
-   
 self.zabbix_
@@ -1750,206 +1750,4 @@
 ce)%0A
-        except Exception as e:%0A            raise EnvironmentError(%0A                'Could not create Zabbix alarms: %7B%7D%5Cn%5Cn%7B%7D'.format(%0A                    e, full_stack()%0A                )%0A            )%0A

Improve zabbix upgrade error control