puppetclassify 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e3f0656715672e681d21f438e317f3b59e3e936d
4
+ data.tar.gz: e5cd2c0623b6d10ec7fc4ca5231277e0aa67a4d3
5
+ SHA512:
6
+ metadata.gz: d17542f290a25be6e75286edb3a247bc5df2be79e425732ad00f2226574e0db1e4928478730ea5813ea677c38772c7ed09be2fbc8e26d88ebf6d1c5368803e05
7
+ data.tar.gz: fa560aeab7e6db465bd90c88b36e20850308a67a4899229ba60497ec942140b4dc5616076bfdce58fb1a39dac3ac053a6dab40b30f3f152ce3f17f57d519aeed
data/README.md ADDED
@@ -0,0 +1,33 @@
1
+ # puppet classify
2
+
3
+ A ruby library to interface with the classifier service
4
+
5
+ ## How to install
6
+
7
+ ```
8
+ gem install puppetclassify
9
+ ```
10
+
11
+ ### Locally
12
+
13
+ ```
14
+ gem build puppetclassify.gemspec
15
+ gem install puppetclassify-0.1.0.gem
16
+ ```
17
+
18
+ ## How to use
19
+
20
+ ```ruby
21
+ require 'puppetclassify'
22
+ # URL of classifier as well as certificates and private key for auth
23
+ auth_info = {
24
+ "ca_certificate_path" => "/opt/puppet/share/puppet-dashboard/certs/ca_cert.pem",
25
+ "certificate_path" => "/opt/puppet/share/puppet-dashboard/certs/pe-internal-dashboard.cert.pem",
26
+ "private_key_path" => "/opt/puppet/share/puppet-dashboard/certs/pe-internal-dashboard.private_key.pem"
27
+ }
28
+
29
+ classifier_url = 'https://puppetmaster.local:4433/classifier-api'
30
+ puppetclassify = PuppetClassify.new(classifier_url, auth_info)
31
+ # Get all the groups
32
+ puppetclassify.groups.get_groups
33
+ ```
@@ -0,0 +1,84 @@
1
+ require 'uri'
2
+ require 'net/https'
3
+
4
+ class PuppetHttps
5
+ def initialize(settings)
6
+ # Settings hash:
7
+ # - ca_certificate_path
8
+ # - certificate_path
9
+ # - private_key_path
10
+ #
11
+
12
+ @settings = settings
13
+ end
14
+
15
+ def make_ssl_request(url, req)
16
+ connection = Net::HTTP.new(url.host, url.port)
17
+ # connection.set_debug_output $stderr
18
+ connection.use_ssl = true
19
+ connection.ssl_version = :TLSv1
20
+
21
+ connection.verify_mode = OpenSSL::SSL::VERIFY_PEER
22
+ ca_file = @settings['ca_certificate_path']
23
+ certpath = @settings['certificate_path']
24
+ pkey_path = @settings['private_key_path']
25
+
26
+ if File.exists?(ca_file)
27
+ connection.ca_file = ca_file
28
+ end
29
+
30
+ if File.exists?(certpath)
31
+ connection.cert = OpenSSL::X509::Certificate.new(File.read(certpath))
32
+ end
33
+
34
+ if File.exists?(pkey_path)
35
+ connection.key = OpenSSL::PKey::RSA.new(File.read(pkey_path))
36
+ end
37
+
38
+ connection.start { |http| http.request(req) }
39
+ end
40
+
41
+ def put(url, request_body=nil)
42
+ url = URI.parse(url)
43
+ req = Net::HTTP::Put.new(url.path)
44
+ req.content_type = 'application/json'
45
+
46
+ unless request_body.nil?
47
+ req.body = request_body
48
+ end
49
+
50
+ res = make_ssl_request(url, req)
51
+ end
52
+
53
+ def get(url)
54
+ url = URI.parse(url)
55
+ accept = 'application/json'
56
+ req = Net::HTTP::Get.new("#{url.path}?#{url.query}", "Accept" => accept)
57
+ res = make_ssl_request(url, req)
58
+ res
59
+ end
60
+
61
+ def post(url, request_body=nil)
62
+ url = URI.parse(url)
63
+
64
+ request = Net::HTTP::Post.new(url.request_uri)
65
+ request.content_type = 'application/json'
66
+
67
+ unless request_body.nil?
68
+ request.body = request_body
69
+ end
70
+
71
+ res = make_ssl_request(url, request)
72
+ res
73
+ end
74
+
75
+ def delete(url)
76
+ url = URI.parse(url)
77
+
78
+ request = Net::HTTP::Delete.new(url.request_uri)
79
+ request.content_type = 'application/json'
80
+
81
+ res = make_ssl_request(url, request)
82
+ res
83
+ end
84
+ end
@@ -0,0 +1,23 @@
1
+ require 'puppet_https'
2
+
3
+ class Classes
4
+ def initialize(nc_api_url, puppet_https)
5
+ @nc_api_url = nc_api_url
6
+ @puppet_https = puppet_https
7
+ end
8
+
9
+ def get_classes
10
+ class_res = @puppet_https.get("#{@nc_api_url}/v1/classes")
11
+ JSON.parse(class_res.body)
12
+ end
13
+
14
+ def get_environment_classes(environment)
15
+ class_res = @puppet_https.get("#{@nc_api_url}/v1/environments/#{environment}/classes")
16
+ JSON.parse(class_res.body)
17
+ end
18
+
19
+ def get_environment_class(environment, class_name)
20
+ class_res = @puppet_https.get("#{@nc_api_url}/v1/environments/#{environment}/classes/#{class_name}")
21
+ JSON.parse(class_res.body)
22
+ end
23
+ end
@@ -0,0 +1,31 @@
1
+ require 'puppet_https'
2
+
3
+ class Classification
4
+ def initialize(nc_api_url, puppet_https)
5
+ @nc_api_url = nc_api_url
6
+ @puppet_https = puppet_https
7
+ end
8
+
9
+ def get(name)
10
+ class_res = @puppet_https.post("#{@nc_api_url}/v1/classified/nodes/#{name}")
11
+
12
+ unless class_res.code.to_i == 200
13
+ STDERR.puts "An error occured retreiving the classification of node #{name}: HTTP #{class_res.code} #{class_res.message}"
14
+ STDERR.puts class_res.body
15
+ else
16
+ JSON.parse(class_res.body)
17
+ end
18
+ end
19
+
20
+ def explain(name)
21
+ class_res = @puppet_https.post("#{@nc_api_url}/v1/classified/nodes/#{name}/explanation")
22
+
23
+ unless class_res.code.to_i == 200
24
+ STDERR.puts "An error occured retreiving the classification explanation of node #{name}: HTTP #{class_res.code} #{class_res.message}"
25
+ STDERR.puts class_res.body
26
+ else
27
+ JSON.parse(class_res.body)
28
+ end
29
+ end
30
+
31
+ end
@@ -0,0 +1,26 @@
1
+ require 'puppet_https'
2
+
3
+ class Environments
4
+ def initialize(nc_api_url, puppet_https)
5
+ @nc_api_url = nc_api_url
6
+ @puppet_https = puppet_https
7
+ end
8
+
9
+ def get_environments
10
+ env_res = @puppet_https.get("#{@nc_api_url}/v1/environments")
11
+ JSON.parse(env_res.body)
12
+ end
13
+
14
+ def get_environment(name)
15
+ env_res = @puppet_https.get("#{@nc_api_url}/v1/environments/#{name}")
16
+ JSON.parse(env_res.body)
17
+ end
18
+
19
+ def create_environment(name)
20
+ env_res = @puppet_https.put("#{@nc_api_url}/v1/environments/#{name}")
21
+
22
+ unless env_res.code.to_i == 201
23
+ STDERR.puts "An error occured saving the environment: HTTP #{env_res.code} #{env_res.message}"
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,75 @@
1
+ require 'puppet_https'
2
+ require 'json'
3
+
4
+ class Groups
5
+ def initialize(nc_api_url, puppet_https)
6
+ @nc_api_url = nc_api_url
7
+ @puppet_https = puppet_https
8
+ end
9
+
10
+ def get_group(group_id)
11
+ # HTTP GET
12
+ group_res = @puppet_https.get("#{@nc_api_url}/v1/groups/#{group_id}")
13
+ unless group_res.code.to_i != 200
14
+ JSON.parse(group_res.body)
15
+ else
16
+ STDERR.puts "An error occured with your request: HTTP #{group_res.code} #{group_res.message}"
17
+ STDERR.puts group_res.body
18
+ end
19
+ end
20
+
21
+ def get_group_id(group_name)
22
+ groups_res = @puppet_https.get("#{@nc_api_url}/v1/groups")
23
+
24
+ groups = JSON.parse(groups_res.body)
25
+
26
+ group_info = groups.find { |group| group['name'] == group_name }
27
+
28
+ if group_info.nil?
29
+ STDERR.puts "Could not find group with the name #{group_name}"
30
+ else
31
+ group_info['id']
32
+ end
33
+ end
34
+
35
+ def get_groups
36
+ group_res = @puppet_https.get("#{@nc_api_url}/v1/groups")
37
+ JSON.parse(group_res.body)
38
+ end
39
+
40
+ def create_group(group_info)
41
+ if group_info['id']
42
+ # HTTP PUT /v1/groups/:id
43
+ res = @puppet_https.put("#{@nc_api_url}/v1/groups/#{group_info['id']}", group_info.to_json)
44
+ else
45
+ # HTTP POST /v1/groups
46
+ res = @puppet_https.post("#{@nc_api_url}/v1/groups", group_info.to_json)
47
+ end
48
+
49
+ if res.code.to_i >= 400
50
+ STDERR.puts "An error occured creating the group: HTTP #{res.code} #{res.message}"
51
+ else
52
+ unless group_info['id']
53
+ res['location'].split("/")[-1]
54
+ else
55
+ group_info['id']
56
+ end
57
+ end
58
+ end
59
+
60
+ def update_group(group_info_delta)
61
+ # HTTP POST /v1/groups/:id
62
+ group_res = @puppet_https.post("#{@nc_api_url}/v1/groups/#{group_info_delta['id']}", group_info_delta.to_json)
63
+
64
+ unless group_res.code.to_i == 200
65
+ STDERR.puts "Update Group failed: HTTP #{group_res.code} #{group_res.message}"
66
+ end
67
+ end
68
+
69
+ def delete_group(group_id)
70
+ group_res = @puppet_https.delete("#{@nc_api_url}/v1/groups/#{group_id}")
71
+ if group_res.code.to_i != 204
72
+ STDERR.puts "An error occured deleting the group: HTTP #{group_res.code} #{group_res.message}"
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,17 @@
1
+ require 'puppet_https'
2
+
3
+ class ImportHierarchy
4
+ def initialize(nc_api_url, puppet_https)
5
+ @nc_api_url = nc_api_url
6
+ @puppet_https = puppet_https
7
+ end
8
+
9
+ def import(group_hierarchy)
10
+ import_res = @puppet_https.post("#{@nc_api_url}/v1/import-hierarchy", group_hierarchy.to_json)
11
+
12
+ if import_res.code.to_i >= 400
13
+ STDERR.puts "An error has occured during import: HTTP #{import_res.code} #{import_res.message}"
14
+ STDERR.puts import_res.body
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,14 @@
1
+ require 'puppet_https'
2
+
3
+ class LastClassUpdate
4
+ def initialize(nc_api_url, puppet_https)
5
+ @nc_api_url = nc_api_url
6
+ @puppet_https = puppet_https
7
+ end
8
+
9
+ def get
10
+ class_update_res = @puppet_https.get("#{@nc_api_url}/v1/last-class-update")
11
+
12
+ JSON.parse(class_update_res.body)
13
+ end
14
+ end
@@ -0,0 +1,18 @@
1
+ require 'puppet_https'
2
+
3
+ class Nodes
4
+ def initialize(nc_api_url, puppet_https)
5
+ @puppet_https = puppet_https
6
+ @nc_api_url = nc_api_url
7
+ end
8
+
9
+ def get_nodes
10
+ node_res = @puppet_https.get("#{@nc_api_url}/v1/nodes")
11
+ JSON.parse(node_res.body)
12
+ end
13
+
14
+ def get_node(node_name)
15
+ node_res = @puppet_https.get("#{@nc_api_url}/v1/nodes/#{node_name}")
16
+ JSON.parse(node_res.body)
17
+ end
18
+ end
@@ -0,0 +1,20 @@
1
+ require 'puppet_https'
2
+ require 'json'
3
+
4
+ class Rules
5
+ def initialize(nc_api_url, puppet_https)
6
+ @nc_api_url = nc_api_url
7
+ @puppet_https = puppet_https
8
+ end
9
+
10
+ def translate(rule)
11
+ rules_res = @puppet_https.post("#{@nc_api_url}/v1/rules/translate", rule.to_json)
12
+
13
+ unless rules_res.code.to_i == 200
14
+ STDERR.puts "There was a problem with your rule: HTTP #{rules_res.code.to_i} #{rules_res.message}"
15
+ STDERR.puts rules_res.body
16
+ else
17
+ JSON.parse(rules_res.body)
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,17 @@
1
+ require 'puppet_https'
2
+
3
+ class UpdateClasses
4
+ def initialize(nc_api_url, puppet_https)
5
+ @nc_api_url = nc_api_url
6
+ @puppet_https = puppet_https
7
+ end
8
+
9
+ def update
10
+ update_res = @puppet_https.post("#{@nc_api_url}/v1/update-classes")
11
+
12
+ unless update_res.code.to_i == 201
13
+ STDERR.puts "An error has occurred during the update: HTTP #{update_res.code} #{update_res.message}"
14
+ STDERR.puts update_res.body
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,18 @@
1
+ require 'puppet_https'
2
+ require 'json'
3
+
4
+ class Validate
5
+ def initialize(nc_api_url, puppet_https)
6
+ @nc_api_url = nc_api_url
7
+ @puppet_https = puppet_https
8
+ end
9
+
10
+ def validate_group(group_info)
11
+ validate_res = @puppet_https.post("#{@nc_api_url}/v1/validate/group", group_info.to_json)
12
+
13
+ unless validate_res.code.to_i == 200
14
+ STDERR.puts "An error has occured validating the group: HTTP #{validate_res.code} #{validate_res.message}"
15
+ STDERR.puts validate_res.body
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,98 @@
1
+ require 'puppet_https'
2
+ require 'puppetclassify/groups'
3
+ require 'puppetclassify/environments'
4
+ require 'puppetclassify/classes'
5
+ require 'puppetclassify/nodes'
6
+ require 'puppetclassify/import_hierarchy'
7
+ require 'puppetclassify/update_classes'
8
+ require 'puppetclassify/validate'
9
+ require 'puppetclassify/rules'
10
+ require 'puppetclassify/last_class_update'
11
+ require 'puppetclassify/classification'
12
+
13
+ class PuppetClassify
14
+ def initialize(nc_api_url, https_settings)
15
+ @nc_api_url = nc_api_url
16
+ @puppet_https = PuppetHttps.new(https_settings)
17
+ end
18
+
19
+ def groups
20
+ if @groups
21
+ @groups
22
+ else
23
+ @groups = Groups.new(@nc_api_url, @puppet_https)
24
+ end
25
+ end
26
+
27
+ def nodes
28
+ if @nodes
29
+ @nodes
30
+ else
31
+ @nodes = Nodes.new(@nc_api_url, @puppet_https)
32
+ end
33
+ end
34
+
35
+ def environments
36
+ if @environments
37
+ @environments
38
+ else
39
+ @environments = Environments.new(@nc_api_url, @puppet_https)
40
+ end
41
+ end
42
+
43
+ def classes
44
+ if @classes
45
+ @classes
46
+ else
47
+ @classes = Classes.new(@nc_api_url, @puppet_https)
48
+ end
49
+ end
50
+
51
+ def import_hierarchy
52
+ if @import_hierarchy
53
+ @import_hierarchy
54
+ else
55
+ @import_hierarchy = ImportHierarchy.new(@nc_api_url, @puppet_https)
56
+ end
57
+ end
58
+
59
+ def update_classes
60
+ if @update_classes
61
+ @update_classes
62
+ else
63
+ @update_classes = UpdateClasses.new(@nc_api_url, @puppet_https)
64
+ end
65
+ end
66
+
67
+ def validate
68
+ if @validate
69
+ @validate
70
+ else
71
+ @validate = Validate.new(@nc_api_url, @puppet_https)
72
+ end
73
+ end
74
+
75
+ def rules
76
+ if @rules
77
+ @rules
78
+ else
79
+ @rules = Rules.new(@nc_api_url, @puppet_https)
80
+ end
81
+ end
82
+
83
+ def last_class_update
84
+ if @last_class_update
85
+ @last_class_update
86
+ else
87
+ @last_class_update = LastClassUpdate.new(@nc_api_url, @puppet_https)
88
+ end
89
+ end
90
+
91
+ def classification
92
+ if @classification
93
+ @classification
94
+ else
95
+ @classification = Classification.new(@nc_api_url, @puppet_https)
96
+ end
97
+ end
98
+ end
metadata ADDED
@@ -0,0 +1,56 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: puppetclassify
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Brian Cain
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-10-30 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: A ruby library to interface with the classifier service
14
+ email: brian.cain@puppetlabs.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - README.md
20
+ - lib/puppet_https.rb
21
+ - lib/puppetclassify.rb
22
+ - lib/puppetclassify/classes.rb
23
+ - lib/puppetclassify/classification.rb
24
+ - lib/puppetclassify/environments.rb
25
+ - lib/puppetclassify/groups.rb
26
+ - lib/puppetclassify/import_hierarchy.rb
27
+ - lib/puppetclassify/last_class_update.rb
28
+ - lib/puppetclassify/nodes.rb
29
+ - lib/puppetclassify/rules.rb
30
+ - lib/puppetclassify/update_classes.rb
31
+ - lib/puppetclassify/validate.rb
32
+ homepage: https://github.com/puppetlabs/puppet-classify
33
+ licenses:
34
+ - MIT
35
+ metadata: {}
36
+ post_install_message:
37
+ rdoc_options: []
38
+ require_paths:
39
+ - lib
40
+ required_ruby_version: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - '>='
43
+ - !ruby/object:Gem::Version
44
+ version: '0'
45
+ required_rubygems_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - '>='
48
+ - !ruby/object:Gem::Version
49
+ version: '0'
50
+ requirements: []
51
+ rubyforge_project:
52
+ rubygems_version: 2.2.2
53
+ signing_key:
54
+ specification_version: 4
55
+ summary: Puppet Classify!
56
+ test_files: []