"""Create ELBs for Spinnaker Pipelines."""
import json
import logging

import requests

from ..utils import check_task, get_subnets, get_template, get_vpc_id


class SpinnakerELB:
    """Create ELBs for Spinnaker."""
    log = logging.getLogger(__name__)

    def __init__(self, args=None):
        self.args = args

        self.health_path = ''
        self.health_port = ''
        self.health_proto = ''
        self.set_health()

        self.gate_url = "http://gate-api.build.example.com:8084"
        self.header = {'Content-Type': 'application/json', 'Accept': '*/*'}

    def set_health(self):
        """Set Health Check path, port, and protocol."""
        target = self.args.health_target
        self.health_proto, health_port_path = target.split(':')
        self.health_port, *health_path = health_port_path.split('/')

        if not health_path:
            self.health_path = '/healthcheck'
        else:
            self.health_path = '/{0}'.format('/'.join(health_path))

        self.log.info('Health Check\n\tprotocol: %s\n\tport: %s\n\tpath: %s',
                      self.health_proto, self.health_port, self.health_path)

        return True

    def make_elb_json(self):
        """Render the JSON template with arguments.

        Returns:
            str: Rendered ELB template.
        """
        raw_subnets = get_subnets(target='elb')
        region_subnets = {self.args.region:
                          raw_subnets[self.args.env][self.args.region]}

        env = self.args.env
        region = self.args.region

        elb_facing = 'true' if self.args.subnet_type == 'internal' else 'false'

        kwargs = {
            'app_name': self.args.app,
            'env': env,
            'isInternal': elb_facing,
            'vpc_id': get_vpc_id(env, region),
            'health_protocol': self.health_proto,
            'health_path': self.health_path,
            'health_port': self.health_port,
            'health_timeout': self.args.health_timeout,
            'health_interval': self.args.health_interval,
            'unhealthy_threshold': self.args.unhealthy_threshold,
            'healthy_threshold': self.args.healthy_threshold,
            # FIXME: Use json.dumps(args.security_groups) to format for template
            'security_groups': self.args.security_groups,
            'int_listener_protocol': self.args.int_listener_protocol,
            'int_listener_port': self.args.int_listener_port,
            'ext_listener_port': self.args.ext_listener_port,
            'ext_listener_protocol': self.args.ext_listener_protocol,
            'subnet_type': self.args.subnet_type,
            'region': region,
            'hc_string': self.args.health_target,
            'availability_zones': json.dumps(region_subnets),
            'region_zones': json.dumps(region_subnets[region]),
        }

        rendered_template = get_template(
            template_file='elb_data_template.json',
            **kwargs)
        return rendered_template

    def create_elb(self):
        """Create/Update ELB.

        Args:
            json_data: elb json payload.
            app: application name related to this ELB.

        Returns:
            task id to track the elb creation status.
        """
        app = self.args.app
        json_data = self.make_elb_json()

        url = self.gate_url + '/applications/%s/tasks' % app
        response = requests.post(url, data=json_data, headers=self.header)

        assert response.ok, 'Error creating {0} ELB: {1}'.format(app,
                                                                 response.text)

        taskid = response.json()
        assert check_task(taskid, app)

@@ -36,16 +36,35 @@
 nes.%22%22%22%0A
+import collections%0A
 import j
@@ -333,126 +333,8 @@
 gs%0A%0A
-        self.health_path = ''%0A        self.health_port = ''%0A        self.health_proto = ''%0A        self.set_health()%0A%0A
     
@@ -480,18 +480,20 @@
    def s
-et
+play
 _health(
@@ -552,19 +552,312 @@
 rotocol.
-%22%22%22
+%0A%0A        Returns:%0A            HealthCheck: A **collections.namedtuple** class with *path*, *port*,%0A            and *proto* attributes.%0A        %22%22%22%0A        HealthCheck = collections.namedtuple('HealthCheck', %5B'path', 'port',%0A                                                             'proto'%5D)%0A
 %0A       
@@ -894,36 +894,24 @@
 get%0A        
-self.health_
 proto, healt
@@ -950,28 +950,16 @@
         
-self.health_
 port, *h
@@ -1036,36 +1036,24 @@
             
-self.health_
 path = '/hea
@@ -1088,28 +1088,16 @@
         
-self.health_
 path = '
@@ -1145,151 +1145,74 @@
     
-self.log.info('
+health = 
 Health
- 
 Check
-%5Cn%5Ctprotocol: %25s%5Cn%5Ctport: %25s%5Cn%5Ctpath: %25s',%0A                      self.health_proto, self.health_port, self.health_pa
+(path, port, proto)%0A        self.log.info(heal
 th)%0A
@@ -1231,12 +1231,14 @@
 urn 
-True
+health
 %0A%0A  
@@ -1695,16 +1695,54 @@
 false'%0A%0A
+        health = self.splay_health()%0A%0A
         
@@ -1931,28 +1931,23 @@
 tocol': 
-self.
 health
-_
+.
 proto,%0A 
@@ -1972,28 +1972,23 @@
 _path': 
-self.
 health
-_
+.
 path,%0A  
@@ -2016,20 +2016,15 @@
 t': 
-self.
 health
-_
+.
 port

Use namedtuple for Health Check splay