Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 40 additions & 19 deletions lib/puppet/functions/adminapi/adminapi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
require 'puppet/util/errors'

module Adminapi
def self.request(endpoint, payload)
def self.request(endpoint, payload, no_fail = false)
application_id = Digest::SHA1.hexdigest(ENV['SERVERADMIN_TOKEN'])
payload_json = JSON.generate(payload)
uri = URI(ENV['SERVERADMIN_BASE_URL'] + endpoint)
Expand All @@ -32,15 +32,17 @@ def self.request(endpoint, payload)
# therefore set the timeout accordingly and make sure we sign the
# requests with a new timestamp before retrying.
max_retries = 3
until max_retries <= 0 do
until max_retries <= 0
begin
res = Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.scheme == 'https', :open_timeout => 15, :read_timeout => 60) do |http|
res = Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https', open_timeout: 15,
read_timeout: 60) do |http|
timestamp = Time.now.getutc.to_i.to_s
req['X-Timestamp'] = timestamp
req['X-SecurityToken'] = OpenSSL::HMAC.hexdigest(
OpenSSL::Digest.new('sha1'),
ENV['SERVERADMIN_TOKEN'],
timestamp + ':' + payload_json)
timestamp + ':' + payload_json
)

http.request(req)
end
Expand All @@ -53,30 +55,49 @@ def self.request(endpoint, payload)
end
end

raise(Puppet::ParseError, "Can't establish connection to Serveradmin") unless max_retries > -1
raise(Puppet::ParseError, "Serveradmin returned " + res.code + " > " + res.body ) unless res.code[0] == '2'
unless max_retries > -1
return nil if no_fail

raise(Puppet::ParseError, "Can't establish connection to Serveradmin")
end

unless res.code[0] == '2'
return nil if no_fail

raise(Puppet::ParseError, 'Serveradmin returned ' + res.code + ' > ' + res.body)
end

res_json = JSON.parse(res.body)

raise(Puppet::ParseError, "Serveradmin returned no status") unless res_json['status']
raise(Puppet::ParseError, "Serveradmin returned status " + res_json['status'] + " > " + res.body) unless res_json['status'] == 'success'
unless res_json['status']
return nil if no_fail

raise(Puppet::ParseError, 'Serveradmin returned no status')
end

unless res_json['status'] == 'success'
return nil if no_fail

raise(Puppet::ParseError,
'Serveradmin returned status ' + res_json['status'] + ' > ' + res.body)
end

return res_json['result']
res_json['result']
end

def self.query(filters, restrict, order_by)
def self.query(filters, restrict, order_by, no_fail = false)
request('/dataset/query', {
'filters' => filters,
'restrict' => restrict,
'order_by' => order_by,
})
'filters' => filters,
'restrict' => restrict,
'order_by' => order_by
}, no_fail)
end

def self.commit(created, changed, deleted)
def self.commit(created, changed, deleted, no_fail = false)
request('/dataset/commit', {
'created' => created,
'changed' => changed,
'deleted' => deleted,
})
'created' => created,
'changed' => changed,
'deleted' => deleted
}, no_fail)
end
end
5 changes: 3 additions & 2 deletions lib/puppet/functions/adminapi/change_multi_attribute.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@
param 'Adminapi::Attribute_id', :attribute
param 'Array[Adminapi::Attribute_value]', :additions
param 'Array[Adminapi::Attribute_value]', :removals
optional_param 'Boolean', :no_fail
return_type 'Undef'
end

def execute(object_id, attribute, additions, removals)
def execute(object_id, attribute, additions, removals, no_fail = false)
if additions.none? and removals.none?
return nil
end
Expand All @@ -30,6 +31,6 @@ def execute(object_id, attribute, additions, removals)
'add' => additions,
'remove' => removals,
},
}], [])
}], [], no_fail)
end
end