Skip to content

Commit 9679304

Browse files
authored
Merge pull request #279 from ripienaar/278
(#278) add the choria_kv_bucket resource
2 parents e20783f + 1db20e3 commit 9679304

File tree

3 files changed

+128
-2
lines changed

3 files changed

+128
-2
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
require "puppet/resource_api/simple_provider"
2+
3+
class Puppet::Provider::ChoriaKvBucket::ChoriaKvBucket < Puppet::ResourceApi::SimpleProvider
4+
def get(context)
5+
run("list", "present")
6+
end
7+
8+
def delete(context, name)
9+
run("delete", "absent", "name" => name)
10+
end
11+
12+
def create(context, name, should)
13+
run("ensure", "present", should)
14+
end
15+
16+
def update(context, name, should)
17+
create(context, name, should)
18+
end
19+
20+
def run(action, ens, args={})
21+
args.delete(:ensure)
22+
parse_result(Puppet::Util::Execution.execute(make_cmd(action, args), :failonfail => false, :custom_environment => environment), ens)
23+
end
24+
25+
def make_cmd(action, args={})
26+
choria = Puppet::Util.which("choria")
27+
raise("cannot find choria executable") if choria == ""
28+
29+
cmd = [choria, "kv", "api", "--%s" % action]
30+
args.each {|k, v|
31+
cmd << "--%s" % k
32+
cmd << v unless v.is_a?(TrueClass)
33+
}
34+
35+
cmd.join(" ")
36+
end
37+
38+
def parse_result(result, ens)
39+
if result == ""
40+
return ""
41+
end
42+
43+
parsed = JSON.parse(result, :symbolize_names => true)
44+
45+
if parsed.is_a?(Hash)
46+
raise(parsed[:error]) if parsed[:error]
47+
48+
parsed[:ensure] = ens
49+
end
50+
51+
if parsed.is_a?(Array)
52+
parsed.each {|g| g[:ensure] = ens}
53+
end
54+
55+
parsed
56+
end
57+
58+
def environment
59+
user = Etc.getpwuid(Process.euid)
60+
61+
{
62+
"USER" => user.name,
63+
"HOME" => user.dir
64+
}
65+
end
66+
end
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
require "puppet/resource_api"
2+
3+
Puppet::ResourceApi.register_type(
4+
name: "choria_kv_bucket",
5+
desc: <<-EOS,
6+
Manage Choria Key-Value store Buckets hosted in Choria Broker
7+
8+
See https://choria.io/docs/streams/key-value/ for more information
9+
EOS
10+
11+
attributes: {
12+
name: {
13+
type: 'Pattern[/[\Aa-zA-Z0-9_-]+\Z/]',
14+
desc: "Unique name for this Bucket",
15+
behaviour: :namevar,
16+
},
17+
18+
history: {
19+
type: "Integer[1,100]",
20+
desc: "How many historic values to keep for any key in the bucket",
21+
default: 1
22+
},
23+
24+
expire: {
25+
type: "Integer",
26+
desc: "How long before a value expires from the bucket, in seconds",
27+
default: -1
28+
},
29+
30+
replicas: {
31+
type: "Integer[1,5]",
32+
desc: "In clustered environments this is how many active Replicas of the data and configuration is kept, odd number is best",
33+
},
34+
35+
max_value_size: {
36+
type: "Integer",
37+
desc: "The maximum size for any value that will be accepted by the bucket",
38+
default: -1
39+
},
40+
41+
max_bucket_size: {
42+
type: "Integer",
43+
desc: "The maximum size of all values in the bucket, including history, before writes will fail",
44+
default: -1,
45+
},
46+
47+
force: {
48+
type: "Boolean",
49+
default: false,
50+
desc: "When updating replicas the Bucket has to be removed and recreated, this can only happen if force is true",
51+
behaviour: :parameter,
52+
},
53+
54+
ensure: {
55+
type: "Enum[present, absent]",
56+
desc: "Create or remove the Bucket",
57+
default: "present"
58+
}
59+
}
60+
)

spec/classes/init_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@
146146
is_expected.to contain_class("choria::repo").with_nightly(false)
147147
is_expected.to contain_class("choria::repo").with_ensure("present")
148148
is_expected.to contain_file("/etc/apt/sources.list.d/choria-release.list")
149-
is_expected.to contain_apt__source("choria-release").with(release: "bionic")
149+
is_expected.to contain_apt__source("choria-release").with(release: "ubuntu")
150150
.with(location: "mirror://mirrorlists.choria.io/apt/release/ubuntu/bionic/mirrors.txt")
151151
end
152152
end
@@ -162,7 +162,7 @@
162162
it "should manage the main repo" do
163163
is_expected.to contain_file("/etc/apt/sources.list.d/choria-release.list")
164164
is_expected.to contain_apt__source("choria-release")
165-
.with(release: "xenial")
165+
.with(release: "ubuntu")
166166
.with(location: "https://apt.eu.choria.io/release/ubuntu/xenial")
167167
end
168168
end

0 commit comments

Comments
 (0)