__author__ = 'disaster'

import cattle
import logging
from fr.webcenter.backup.Singleton import Singleton

logger = logging
class Rancher(object):
    """
    This class provide helper for Rancher API.
    """

    __metaclass__ = Singleton

    def __init__(self, url=None, key=None, secret=None):
        """
        Init the connexion on Rancher API
        :param url: the Rancher URL to access on API
        :param key: The rancher key to access on API
        :param secret: The Rancher secret to access on API
        :type url: str
        :type key: str
        :type secret: str
        """

        logger.debug("url: %s", url)
        logger.debug("key: %s", key)
        logger.debug("secret: xxxx")

        self._client = cattle.Client(url=url, access_key=key, secret_key=secret)


    def getServices(self):
        """
        Get all services and many information associatied to them (stacks, instances and hosts)
        :return list The list of services
        """

        listServices = self._client.list('service')

        # We keep only enable services and services that have not 'backup.disable' label set to true
        targetListServices = []
        for service in listServices:
            if "imageUuid" in service['launchConfig'] and  service["state"] == "active" and ("labels" not in service['launchConfig'] or ("backup.disable" not in service['launchConfig']['labels'] or service['launchConfig']['labels']['backup.disable'] == "false")):

                logger.debug("Service %s must be dumping", service["name"])

                # We add the stack associated to it
                service['stack'] = self._client._get(service['links']['environment'])
                logger.debug("Service %s is on stack %s", service["name"], service['stack']['name'])

                # We add the instances
                service['instances'] = self._client._get(service['links']['instances'])
                for instance in service['instances']:
                    instance['host'] = self._client._get(instance['links']['hosts'])[0]

                logger.debug("Service %s have %d intances", service["name"], len(service['instances']))


                targetListServices.append(service)


        return targetListServices


    def getStacks(self):
        """
        Permit to get all stack on Rancher environment
        :return list: the list of Rancher stack
        """

        listEnvironments = self._client.list('environment')
        targetListEnvironment = []
        for environment in listEnvironments:
            logger.debug("Grab setting for stack %s", environment['name'])
            environment['settings'] = self._client.action(environment, 'exportconfig')
            targetListEnvironment.append(environment)


        logger.debug("Return: %s", targetListEnvironment)

        return targetListEnvironment



@@ -1219,16 +1219,49 @@
      if 
+service%5B%22type%22%5D == %22service%22 and 
 %22imageUu
@@ -1524,33 +1524,39 @@
   logger.debug(%22
-S
+Found s
 ervice %25s must b
@@ -1552,24 +1552,8 @@
 e %25s
- must be dumping
 %22, s
@@ -1621,16 +1621,74 @@
 d to it%0A
+                if 'environment' in service%5B'links'%5D:%0A    
         
@@ -1761,24 +1761,28 @@
 ironment'%5D)%0A
+    
             
@@ -1874,48 +1874,204 @@
 '%5D)%0A
-%0A                # We add the instances%0A
+                else:%0A                    logger.debug(%22No stack for service %25s%22, service%5B%22name%22%5D)%0A%0A%0A                # We add the instances%0A                if 'instances' in service%5B'links'%5D:%0A    
     
@@ -2166,24 +2166,28 @@
             
+    
 for instance
@@ -2232,16 +2232,20 @@
         
+ 
+   
 instance
@@ -2305,16 +2305,20 @@
 '%5D)%5B0%5D%0A%0A
+    
         
@@ -2413,16 +2413,118 @@
 ces'%5D))%0A
+                else:%0A                    logger.debug(%22No instance for service %25s%22, service%5B%22name%22%5D)%0A
 %0A%0A      

Fix issue with rancher 1.2 and higher