dm-puppetdb-adapter 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/README.md +53 -0
- data/dm-puppetdb-adapter.gemspec +1 -0
- data/lib/dm-puppetdb-adapter/adapter.rb +30 -27
- metadata +18 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 445d518825690ce87e8ea8bbdf46bdfaa2947cd0
|
4
|
+
data.tar.gz: c8d0acc1b381d1de67e43029f9177fc2e0031ba4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 959e86b132178136230087361e65008341df7500d6afd6ff82c775aee4caf20a777256c344d3d64e5aef97882fbc9de7229902738bdb7cc3caf4d98f1b16c3dc
|
7
|
+
data.tar.gz: 06bfa287b76ed9037112528da330ac049647ec2be1d8d03368672cc701ff34d9c4277d13eb7f4364518a0a5ace19120645db40036db58fe7e846ec0e7b9f8c76
|
data/.gitignore
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
/*.gem
|
data/README.md
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
# DataMapper PuppetDB Adapter
|
2
|
+
|
3
|
+
## Install
|
4
|
+
|
5
|
+
Requirements:
|
6
|
+
|
7
|
+
* json
|
8
|
+
* puppet 3.x
|
9
|
+
* dm-types
|
10
|
+
|
11
|
+
gem install dm-puppetdb-adapter
|
12
|
+
|
13
|
+
## Usage
|
14
|
+
|
15
|
+
It will use the puppet configuration of the user it runs under, so make sure it has a SSL certificate that can connect to your PuppetDB instace.
|
16
|
+
|
17
|
+
To setup the adapter:
|
18
|
+
|
19
|
+
DataMapper.setup(:default, {
|
20
|
+
:adapter => 'puppetdb',
|
21
|
+
:host => 'puppetdb.example.com',
|
22
|
+
:port => 8081,
|
23
|
+
:ssl => true,
|
24
|
+
})
|
25
|
+
|
26
|
+
Or if you use it under `dm-rails` add this to `config/database.yml`:
|
27
|
+
|
28
|
+
defaults: &defaults
|
29
|
+
adapter: puppetdb
|
30
|
+
host: 'puppetdb.example.com'
|
31
|
+
port: 8081
|
32
|
+
ssl: true
|
33
|
+
|
34
|
+
development:
|
35
|
+
<<: *defaults
|
36
|
+
|
37
|
+
test:
|
38
|
+
<<: *defaults
|
39
|
+
|
40
|
+
production:
|
41
|
+
<<: *defaults
|
42
|
+
|
43
|
+
To include all predefined PuppetDB models add this:
|
44
|
+
|
45
|
+
require "dm-puppetdb-adapter/models"
|
46
|
+
|
47
|
+
Queries like `Node.all(:name.like => '%.example.com')` etc should then work. See the models and DataMapper documentation for more info on query syntax.
|
48
|
+
|
49
|
+
## Notes
|
50
|
+
|
51
|
+
* It handles subqueries by doing multiple queries to PuppetDB, so it doesn't have optimal performance for them.
|
52
|
+
* There's no select method implemented on the adapter for doing manual queries yet.
|
53
|
+
* PuppetDB can only search for some fields server side, so the other fields are filtered client side, but all of them work filtering against. Same goes for limits and offsets.
|
data/dm-puppetdb-adapter.gemspec
CHANGED
@@ -11,11 +11,8 @@ module DataMapper
|
|
11
11
|
attr_accessor :http, :host, :port, :ssl
|
12
12
|
|
13
13
|
def read(query)
|
14
|
-
puts query.inspect
|
15
14
|
model = query.model
|
16
15
|
|
17
|
-
pdbquery = build_query(query)
|
18
|
-
puts pdbquery.inspect
|
19
16
|
query.filter_records(puppetdb_get(model.storage_name(model.repository_name), build_query(query)))
|
20
17
|
end
|
21
18
|
|
@@ -37,61 +34,67 @@ module DataMapper
|
|
37
34
|
# contruct a puppetdb query from a datamapper query
|
38
35
|
def build_query(query)
|
39
36
|
conditions = query.conditions
|
40
|
-
if conditions.
|
37
|
+
if conditions.nil?
|
41
38
|
nil
|
42
|
-
elsif conditions.one?
|
43
|
-
puppetdb_condition(conditions.first)
|
44
39
|
else
|
45
|
-
|
46
|
-
puppetdb_condition c
|
47
|
-
end.compact ]
|
48
|
-
# In case we couldn't build any query from the contained
|
49
|
-
# conditions return nil instead of a empty and
|
50
|
-
return q.last if q.count == 2
|
51
|
-
return nil if q.count < 2
|
52
|
-
q
|
40
|
+
puppetdb_condition(conditions, query.model)
|
53
41
|
end
|
54
42
|
end
|
55
43
|
|
56
44
|
##
|
57
45
|
# return puppetdb syntax for a condition
|
58
|
-
def puppetdb_condition(c)
|
46
|
+
def puppetdb_condition(c, model)
|
59
47
|
case c
|
60
48
|
when DataMapper::Query::Conditions::NullOperation
|
61
49
|
nil
|
62
50
|
when DataMapper::Query::Conditions::AbstractOperation
|
63
|
-
q = [c.class.slug.to_s, *c.operands.map { |o| puppetdb_condition o }.compact]
|
51
|
+
q = [c.class.slug.to_s, *c.operands.map { |o| puppetdb_condition o, model }.compact]
|
64
52
|
# In case we couldn't build any query from the contained
|
65
53
|
# conditions return nil instead of a empty and
|
54
|
+
return q.last if q.count == 2
|
66
55
|
return nil if q.count < 2
|
67
|
-
q
|
56
|
+
return q
|
57
|
+
end
|
58
|
+
|
59
|
+
# Determine the property we should match on
|
60
|
+
if c.subject.kind_of? DataMapper::Property
|
61
|
+
property = c.subject
|
62
|
+
elsif c.subject.kind_of? DataMapper::Associations::Relationship
|
63
|
+
property = c.subject.parent_key.first
|
64
|
+
else
|
65
|
+
puts "Unhandled subject #{c.subject.inspect}"
|
66
|
+
raise RuntimeError, "Unhandled subject #{c.subject.inspect}"
|
68
67
|
end
|
69
68
|
|
70
69
|
# We can only do comparison on certain fields
|
71
70
|
# on the server side
|
72
|
-
if
|
73
|
-
return nil unless
|
71
|
+
if property.model.respond_to? :server_fields
|
72
|
+
return nil unless property.model.server_fields.include? property.name
|
74
73
|
end
|
75
74
|
|
76
75
|
case c
|
77
76
|
when DataMapper::Query::Conditions::EqualToComparison
|
78
|
-
['=',
|
77
|
+
['=', property.field, format_value(c.value)]
|
79
78
|
when DataMapper::Query::Conditions::RegexpComparison
|
80
|
-
['~',
|
79
|
+
['~', property.field, format_value(c.value)]
|
81
80
|
when DataMapper::Query::Conditions::LessThanComparison
|
82
|
-
['<',
|
81
|
+
['<', property.field, format_value(c.value)]
|
83
82
|
when DataMapper::Query::Conditions::GreaterThanComparison
|
84
|
-
['>',
|
83
|
+
['>', property.field, format_value(c.value)]
|
85
84
|
# The following comparison operators aren't supported by PuppetDB
|
86
85
|
# So we emulate them
|
87
86
|
when DataMapper::Query::Conditions::LikeComparison
|
88
|
-
['~',
|
87
|
+
['~', property.field, c.value.gsub('%', '.*')]
|
89
88
|
when DataMapper::Query::Conditions::LessThanOrEqualToComparison
|
90
|
-
['or', ['=',
|
89
|
+
['or', ['=', property.field, format_value(c.value)], ['<', property.field, format_value(c.value)]]
|
91
90
|
when DataMapper::Query::Conditions::GreaterThanOrEqualToComparison
|
92
|
-
['or', ['=',
|
91
|
+
['or', ['=', property.field, format_value(c.value)], ['>', property.field, format_value(c.value)]]
|
93
92
|
when DataMapper::Query::Conditions::InclusionComparison
|
94
|
-
|
93
|
+
if c.value.kind_of? Range
|
94
|
+
['or', ['=', property.field, format_value(c.value.first)], ['>', property.field, format_value(c.value.first)], ['<', property.field, format_value(c.value.last)], ['=', property.field, format_value(c.value.last)]]
|
95
|
+
else
|
96
|
+
['or', *c.value.collect { |v| ['=', property.field, v.value]} ]
|
97
|
+
end
|
95
98
|
end
|
96
99
|
end
|
97
100
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dm-puppetdb-adapter
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Erik Dalén
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-
|
11
|
+
date: 2013-07-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: json
|
@@ -52,6 +52,20 @@ dependencies:
|
|
52
52
|
- - '>='
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: dm-core
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - '>='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
55
69
|
- !ruby/object:Gem::Dependency
|
56
70
|
name: rake
|
57
71
|
requirement: !ruby/object:Gem::Requirement
|
@@ -74,8 +88,10 @@ extensions: []
|
|
74
88
|
extra_rdoc_files:
|
75
89
|
- LICENSE
|
76
90
|
files:
|
91
|
+
- .gitignore
|
77
92
|
- Gemfile
|
78
93
|
- LICENSE
|
94
|
+
- README.md
|
79
95
|
- dm-puppetdb-adapter.gemspec
|
80
96
|
- lib/dm-puppetdb-adapter.rb
|
81
97
|
- lib/dm-puppetdb-adapter/adapter.rb
|