Class: Groups
- Inherits:
-
Object
- Object
- Groups
- Defined in:
- lib/puppetclassify/groups.rb
Instance Method Summary collapse
- #create_group(group_info) ⇒ Object
- #delete_group(group_id) ⇒ Object
- #get_group(group_id) ⇒ Object
- #get_group_id(group_name) ⇒ Object
- #get_groups ⇒ Object
-
#initialize(nc_api_url, puppet_https) ⇒ Groups
constructor
A new instance of Groups.
- #pin_nodes(group_id, node_hash) ⇒ Object
- #unpin_nodes(group_id, node_hash) ⇒ Object
- #update_group(group_info_delta) ⇒ Object
Constructor Details
#initialize(nc_api_url, puppet_https) ⇒ Groups
Returns a new instance of Groups.
5 6 7 8 |
# File 'lib/puppetclassify/groups.rb', line 5 def initialize(nc_api_url, puppet_https) @nc_api_url = nc_api_url @puppet_https = puppet_https end |
Instance Method Details
#create_group(group_info) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/puppetclassify/groups.rb', line 45 def create_group(group_info) if group_info['id'] # HTTP PUT /v1/groups/:id res = @puppet_https.put("#{@nc_api_url}/v1/groups/#{group_info['id']}", group_info.to_json) else # HTTP POST /v1/groups res = @puppet_https.post("#{@nc_api_url}/v1/groups", group_info.to_json) end if res.code.to_i >= 400 STDERR.puts "An error occured creating the group: HTTP #{res.code} #{res.}" STDERR.puts res.body else unless group_info['id'] res['location'].split("/")[-1] else group_info['id'] end end end |
#delete_group(group_id) ⇒ Object
76 77 78 79 80 81 82 |
# File 'lib/puppetclassify/groups.rb', line 76 def delete_group(group_id) group_res = @puppet_https.delete("#{@nc_api_url}/v1/groups/#{group_id}") if group_res.code.to_i != 204 STDERR.puts "An error occured deleting the group: HTTP #{group_res.code} #{group_res.}" STDERR.puts group_res.body end end |
#get_group(group_id) ⇒ Object
10 11 12 13 14 15 16 17 18 19 |
# File 'lib/puppetclassify/groups.rb', line 10 def get_group(group_id) # HTTP GET group_res = @puppet_https.get("#{@nc_api_url}/v1/groups/#{group_id}") unless group_res.code.to_i != 200 JSON.parse(group_res.body) else STDERR.puts "An error occured with your request: HTTP #{group_res.code} #{group_res.}" STDERR.puts group_res.body end end |
#get_group_id(group_name) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/puppetclassify/groups.rb', line 21 def get_group_id(group_name) groups_res = @puppet_https.get("#{@nc_api_url}/v1/groups") unless groups_res.code.to_i != 200 groups = JSON.parse(groups_res.body) else STDERR.puts "An error occured with your request: HTTP #{groups_res.code} #{groups_res.}" STDERR.puts groups_res.body end group_info = groups.find { |group| group['name'] == group_name } if group_info.nil? STDERR.puts "Could not find group with the name #{group_name}" else group_info['id'] end end |
#get_groups ⇒ Object
40 41 42 43 |
# File 'lib/puppetclassify/groups.rb', line 40 def get_groups group_res = @puppet_https.get("#{@nc_api_url}/v1/groups") JSON.parse(group_res.body) end |
#pin_nodes(group_id, node_hash) ⇒ Object
84 85 86 87 88 89 90 91 92 93 |
# File 'lib/puppetclassify/groups.rb', line 84 def pin_nodes(group_id, node_hash) request_body = {} request_body["nodes"] = node_hash # expects node_hash to be array, i.e. ["foo", "bar", "baz"] group_response = @puppet_https.post("#{@nc_api_url}/v1/groups/#{group_id}/pin", request_body.to_json) unless group_response.code.to_i == 204 STDERR.puts "An error occured pinning nodes the group: HTTP #{group_response.code} #{group_response.}" STDERR.puts group_response.body end end |
#unpin_nodes(group_id, node_hash) ⇒ Object
95 96 97 98 99 100 101 102 103 104 |
# File 'lib/puppetclassify/groups.rb', line 95 def unpin_nodes(group_id, node_hash) request_body = {} request_body["nodes"] = node_hash # expects node_hash to be array, i.e. ["foo", "bar", "baz"] group_response = @puppet_https.post("#{@nc_api_url}/v1/groups/#{group_id}/unpin", request_body.to_json) unless group_response.code.to_i == 204 STDERR.puts "An error occured unpinning nodes the group: HTTP #{group_response.code} #{group_response.}" STDERR.puts group_response.body end end |
#update_group(group_info_delta) ⇒ Object
66 67 68 69 70 71 72 73 74 |
# File 'lib/puppetclassify/groups.rb', line 66 def update_group(group_info_delta) # HTTP POST /v1/groups/:id group_res = @puppet_https.post("#{@nc_api_url}/v1/groups/#{group_info_delta['id']}", group_info_delta.to_json) unless group_res.code.to_i == 200 STDERR.puts "Update Group failed: HTTP #{group_res.code} #{group_res.}" STDERR.puts group_res.body end end |