diff --git a/helpers/dcs.py b/helpers/dcs.py new file mode 100644 index 00000000..e4a6e895 --- /dev/null +++ b/helpers/dcs.py @@ -0,0 +1,32 @@ +import sys + +from collections import namedtuple +from helpers.utils import calculate_ttl + +if sys.hexversion >= 0x03000000: + from urllib.parse import urlparse, urlunparse, parse_qsl +else: + from urlparse import urlparse, urlunparse, parse_qsl + + +class Member(namedtuple('Member', 'name,conn_url,api_url,expiration,ttl')): + + @staticmethod + def fromNode(node): + scheme, netloc, path, params, query, fragment = urlparse(node['value']) + conn_url = urlunparse((scheme, netloc, path, params, '', fragment)) + api_url = ([v for n, v in parse_qsl(query) if n == 'application_name'] or [None])[0] + expiration = node.get('expiration', None) + ttl = node.get('ttl', None) + return Member(node['key'].split('/')[-1], conn_url, api_url, expiration, ttl) + + def real_ttl(self): + return calculate_ttl(self.expiration) or -1 + + +class Cluster(namedtuple('Cluster', 'initialize,leader,last_leader_operation,members')): + + def is_unlocked(self): + return not (self.leader and self.leader.name) + + diff --git a/helpers/etcd.py b/helpers/etcd.py index 3385fbe6..390b8591 100644 --- a/helpers/etcd.py +++ b/helpers/etcd.py @@ -2,44 +2,17 @@ import logging import random import requests import socket -import sys -from collections import namedtuple from dns.exception import DNSException from dns import resolver +from helpers.dcs import Cluster, Member from helpers.errors import CurrentLeaderError, EtcdError, EtcdConnectionFailed -from helpers.utils import calculate_ttl, sleep +from helpers.utils import sleep from requests.exceptions import RequestException -if sys.hexversion >= 0x03000000: - from urllib.parse import urlparse, urlunparse, parse_qsl -else: - from urlparse import urlparse, urlunparse, parse_qsl - logger = logging.getLogger(__name__) -class Member(namedtuple('Member', 'name,conn_url,api_url,expiration,ttl')): - - @staticmethod - def fromNode(node): - scheme, netloc, path, params, query, fragment = urlparse(node['value']) - conn_url = urlunparse((scheme, netloc, path, params, '', fragment)) - api_url = ([v for n, v in parse_qsl(query) if n == 'application_name'] or [None])[0] - expiration = node.get('expiration', None) - ttl = node.get('ttl', None) - return Member(node['key'].split('/')[-1], conn_url, api_url, expiration, ttl) - - def real_ttl(self): - return calculate_ttl(self.expiration) or -1 - - -class Cluster(namedtuple('Cluster', 'initialize,leader,last_leader_operation,members')): - - def is_unlocked(self): - return not (self.leader and self.leader.name) - - class Client: API_VERSION = 'v2' diff --git a/tests/test_etcd.py b/tests/test_etcd.py index 11cfbdb5..e37486ac 100644 --- a/tests/test_etcd.py +++ b/tests/test_etcd.py @@ -8,7 +8,8 @@ import unittest from dns.exception import DNSException from helpers.errors import EtcdError, CurrentLeaderError, EtcdConnectionFailed -from helpers.etcd import Client, Cluster, Etcd, Member +from helpers.dcs import Cluster, Member +from helpers.etcd import Client, Etcd class MockResponse: