From afa670bb5e4de924bdaae4531f04aafe34e362b2 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Marek=20Vavru=C5=A1a?= <marek.vavrusa@nic.cz>
Date: Thu, 7 May 2015 10:36:30 +0200
Subject: [PATCH] modules/ketcd: Etcd module

---
 modules/ketcd/ketcd.lua | 49 +++++++++++++++++++++++++++++++----------
 1 file changed, 37 insertions(+), 12 deletions(-)

diff --git a/modules/ketcd/ketcd.lua b/modules/ketcd/ketcd.lua
index d4ee5053d..fed767632 100644
--- a/modules/ketcd/ketcd.lua
+++ b/modules/ketcd/ketcd.lua
@@ -1,24 +1,49 @@
 --- @module ketcd
 local ketcd = {}
-local Etcd = require('etcd.luasocket')
 
-function ketcd.init(module)
-	print('wip')
+-- @function update subtree configuration
+local function update_subtree(tree)
+	if not tree then return end
+	for i,k in pairs(tree) do
+		if k.dir then
+			update_subtree(k.nodes)
+		else
+			local key,opt = k.key:gmatch('([^/]+)/([^/]+)$')()
+			eval_cmd(key..'='..'{'..opt..'='..k.value..'}')
+		end
+	end
 end
 
-function ketcd.deinit(module)
-	print('wip')
+-- @function reload whole configuration
+function ketcd.reload()
+	local res, err = ketcd.cli:readdir('/', true)
+	if err then
+		error(err)
+	end
+	update_subtree(res.body.node.nodes)	
+end
+
+function ketcd.init(module)
+	ketcd.Etcd = require('etcd.luasocket')
+	ketcd.cli = nil
+	ketcd.ev = nil
+	ketcd.defaults = { prefix = '/kresolved' }
 end
 
-function ketcd.config(module, conf)
-	local cli, err = Etcd.new({
-		peer = conf,
-	});	
-	ketcd._cli = cli
+function ketcd.deinit(module)
+	if ketcd.ev then event.cancel(ketcd.ev) end
 end
 
-function ketcd.layers(module)
-	return {}
+function ketcd.config(conf)
+	local options = ketcd.defaults
+	if type(conf) == 'table' then
+		for k,v in pairs(conf) do options[k] = v end
+	end
+	local cli, err = ketcd.Etcd.new(options)
+	if err then
+		error(err) 
+	end
+	ketcd.cli = cli
 end
 
 return ketcd
-- 
GitLab