Skip to content

Commit

Permalink
begin work on a twisted client for etcd
Browse files Browse the repository at this point in the history
  • Loading branch information
Russell Haering committed Sep 28, 2013
0 parents commit 266a72e
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .gitignore
@@ -0,0 +1,7 @@
*.py[co]
bin/*
lib/*
include/*
local/*
src/*
build/*
29 changes: 29 additions & 0 deletions setup.py
@@ -0,0 +1,29 @@
"""
Copyright 2013 Russell Haering.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""

from setuptools import setup, find_packages

setup(
name='txetcd',
version='0.0.1',
packages=find_packages(),
install_requires=['treq >= 12.1.0'],
author='Russell Haering',
author_email='russellhaering@gmail.com',
description='A Twisted client for etcd: https://github.com/coreos/etcd',
license='Apache License (2.0)',
url='https://github.com/russellhaering/txetcd'
)
17 changes: 17 additions & 0 deletions txetcd/__init__.py
@@ -0,0 +1,17 @@
"""
Copyright 2013 Russell Haering.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""

from txetcd.client import EtcdClient
73 changes: 73 additions & 0 deletions txetcd/client.py
@@ -0,0 +1,73 @@
"""
Copyright 2013 Russell Haering.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""

import random

from treq import json_content
from treq.client import HTTPClient


class EtcdClient(object):
API_VERSION = 'v1'

def __init__(self, seeds=[('localhost', 4001)]):
self.nodes = set(seeds)
self.http_client = HTTPClient.with_config()

def _get_node(self, leader):
# TODO: discover the rest of the cluster
# TODO: cache the leader
return random.sample(self.nodes, 1)[0]

def _decode_response(self, response):
return json_content(response)

def _request(self, method, path, params=None, leader=False):
node = self._get_node(leader)
url = 'http://{host}:{port}/{version}{path}'.format(
host=node[0],
port=node[1],
version=self.API_VERSION,
path=path,
)
d = self.http_client.request(method, url, params=params)
d.addCallback(self._decode_response)
return d

def set(self, key, value, prev_value=None, ttl=None):
path = '/keys/{key}'.format(key=key)
params = {'value': value}

if ttl is not None:
params['ttl'] = ttl

if prev_value is not None:
params['prevValue'] = prev_value

return self._request('POST', path, params=params, leader=True)

def watch(self, key, index=None):
path = '/watch/{key}'.format(key=key)
params = None

if index is not None:
params['index'] = index

return self._request('GET', path)

def delete(self, key):
path = '/keys/{key}'.format(key=key)
return self._request('DELETE', path)

0 comments on commit 266a72e

Please sign in to comment.