Class: Groups

Inherits:
Object
  • Object
show all
Defined in:
lib/puppetclassify/groups.rb

Instance Method Summary collapse

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



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/puppetclassify/groups.rb', line 40

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.message}"
  else
    unless group_info['id']
      res['location'].split("/")[-1]
    else
      group_info['id']
    end
  end
end

#delete_group(group_id) ⇒ Object



69
70
71
72
73
74
# File 'lib/puppetclassify/groups.rb', line 69

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.message}"
  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.message}"
    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
# File 'lib/puppetclassify/groups.rb', line 21

def get_group_id(group_name)
  groups_res = @puppet_https.get("#{@nc_api_url}/v1/groups")

  groups = JSON.parse(groups_res.body)

  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_groupsObject



35
36
37
38
# File 'lib/puppetclassify/groups.rb', line 35

def get_groups
  group_res = @puppet_https.get("#{@nc_api_url}/v1/groups")
  JSON.parse(group_res.body)
end

#update_group(group_info_delta) ⇒ Object



60
61
62
63
64
65
66
67
# File 'lib/puppetclassify/groups.rb', line 60

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.message}"
  end
end