Commit 2735cb1c authored by Sigmund Augdal's avatar Sigmund Augdal

Update to use newer python-etcd

parent b432ebaf
from __future__ import absolute_import, division, print_function, unicode_literals
import logging
import etcd
V4 = "ipv4"
V6_LL = "ipv6_ll"
......@@ -16,7 +17,7 @@ def get_ipaddress_from_mac(etcd_client, mac, addrtype):
mac = mac.lower()
try:
return etcd_client.get(etcd_key(mac, addrtype)).value
except KeyError:
except etcd.EtcdKeyNotFound:
pass
......@@ -28,7 +29,7 @@ def get_mac_from_ipaddress(etcd_client, ipaddress, addrtype):
mac = entry.key.split("/")[-2]
if entry.value == ipaddress:
return mac
except KeyError:
except etcd.EtcdKeyNotFound:
return None
return None
......@@ -37,7 +38,7 @@ def add_pair(etcd_client, mac, ipaddress, addrtype):
mac = mac.lower()
try:
etcd_client.write(etcd_key(mac, addrtype), ipaddress, prevExist=False)
except KeyError: # Key exists
except etcd.EtcdKeyNotFound: # Key exists
try:
remote_addr = etcd_client.read(etcd_key(mac, addrtype), consistent=True).value
if remote_addr != ipaddress:
......@@ -45,7 +46,7 @@ def add_pair(etcd_client, mac, ipaddress, addrtype):
mac, remote_addr, ipaddress)
else:
pass # Other router did the update quicker than us
except KeyError:
except etcd.EtcdKeyNotFound:
log.warning("mac %s disappeared while adding pair (assuming other router is updating",
mac)
......@@ -59,9 +60,9 @@ def remove_pair(etcd_client, mac, ipaddress, addrtype):
new_addr = etcd_client.get(etcd_key(mac, addrtype)).value
log.warning("IP address changed while trying to delete.\n" +
"Expected address {}, got {}".format(ipaddress, new_addr))
except KeyError: # Key has been deleted by someone else
except etcd.EtcdKeyNotFound: # Key has been deleted by someone else
pass
except KeyError: # Shouldn't happen, but shouldn't cause any harm to catch
except etcd.EtcdKeyNotFound: # Shouldn't happen, but shouldn't cause any harm to catch
pass
......@@ -79,5 +80,5 @@ def set_address_pair(etcd_client, mac, ipaddress, addrtype):
def remove_mac(etcd_client, mac):
try:
etcd_client.delete(etcd_key(mac, ""), recursive=True)
except KeyError: # Already gone. Someone was quicker than us
except etcd.EtcdKeyNotFound: # Already gone. Someone was quicker than us
pass
......@@ -5,6 +5,8 @@ import logging
import uuid
import ipaddress
import etcd
RULE_KEYS = ("protocol", "source_type", "source_cidr", "source_security_group", "destination_port",
"comment")
......@@ -51,14 +53,14 @@ def get_group_name(etcd_client, group_id, library=False):
def get_group_cloud(etcd_client, group_id, library=False):
try:
return etcd_client.get(group_key(group_id, library=library) + "/cloud").value
except KeyError:
except etcd.EtcdKeyNotFound:
return None
def get_group_owner(etcd_client, group_id, library=False):
try:
return etcd_client.get(group_key(group_id, library=library) + "/owner").value
except KeyError:
except etcd.EtcdKeyNotFound:
return None
......@@ -103,7 +105,7 @@ def get_security_groups(etcd_client, cloudid=None):
if info == 'cloud':
info = 'cloudid'
groups[group_id][info] = entry.value
except KeyError:
except etcd.EtcdKeyNotFound:
pass
if cloudid:
groups = {groupid: group for groupid, group in groups.items() if group.get('cloudid', cloudid) == cloudid}
......@@ -135,7 +137,7 @@ def get_library_groups(etcd_client, cloudid=None):
if info == 'cloud':
info = 'cloudid'
groups[group_id][info] = entry.value
except KeyError:
except etcd.EtcdKeyNotFound:
pass
if cloudid:
groups = {groupid: group for groupid, group in groups.items() if group.get('cloudid', cloudid) == cloudid}
......@@ -190,7 +192,7 @@ def get_group_references(etcd_client, groupid, library=False):
if rule_target == match_rule and entry.value == groupid:
print(entry.key)
references.append((referer_group, rule_id))
except KeyError:
except etcd.EtcdKeyNotFound:
pass
return references
......@@ -210,7 +212,7 @@ def get_group_rules(etcd_client, group_id):
rule[e_key] = entry.value
if "protocol" in rule and "source_type" in rule:
rules.append(rule)
except KeyError:
except etcd.EtcdKeyNotFound:
return []
rules.sort(key=lambda x: x['id'])
return rules
......@@ -237,7 +239,7 @@ def get_group_mode(etcd_client, group_id):
group_key = security_group_key(group_id)
try:
return etcd_client.get(group_key + "/mode").value
except KeyError:
except etcd.EtcdKeyNotFound:
return "open"
......@@ -245,7 +247,7 @@ def get_locked(etcd_client, group_id, library=False):
key = group_key(group_id, library=library)
try:
return etcd_client.get(key + "/lock").value == "locked"
except KeyError:
except etcd.EtcdKeyNotFound:
return False
......@@ -305,13 +307,13 @@ def add_rule(etcd_client, group_id, params):
source_security_group = params["source_security_group"]
try:
etcd_client.get(security_group_key(source_security_group) + "/name")
except KeyError:
except etcd.EtcdKeyNotFound:
raise ValueError("Invalid source security group: " + source_security_group)
elif source_type == "library_group":
source_library_group = params["source_library_group"]
try:
etcd_client.get(library_group_key(source_library_group) + "/name")
except KeyError:
except etcd.EtcdKeyNotFound:
raise ValueError("Invalid source library group: " + source_library_group)
etcd_client.set(rule_key + "/protocol", protocol)
......@@ -340,7 +342,7 @@ def get_group_members(etcd_client, group_id):
members_key = security_group_key(group_id) + "/members/by_mac"
try:
raw_members = etcd_client.read(members_key, consistent=True).value
except KeyError:
except etcd.EtcdKeyNotFound:
return None, []
members = raw_members.split("\n")
members = [m for m in members if m]
......
......@@ -137,7 +137,7 @@ class TestGetGroupMembers(object):
assert members == ["aa:bb:cc:dd:ee:ff", "11:22:33:44:55:66"]
def test_no_key(self):
self.etcd.read = mock.MagicMock(side_effect=KeyError("Key Not Found"))
self.etcd.read = mock.MagicMock(side_effect=etcd.EtcdKeyNotFound("Key Not Found"))
raw, members = security_groups.get_group_members(self.etcd, sec_group)
assert raw is None
assert members == []
......
......@@ -6,7 +6,7 @@ from setuptools import setup, find_packages
here = os.path.abspath(os.path.dirname(__file__))
requires = [
'python-etcd==0.3.2',
'python-etcd',
'requests',
'daemonize',
'pytest',
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment