From 9afa12fa0be4c0d9f25261a7ba1237752cd57ab0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=BCrkan=20G=C3=BCr?= Date: Fri, 4 Jul 2025 10:53:55 +0200 Subject: [PATCH] Add optional no_fail switch to commit --- lib/puppet/functions/adminapi/adminapi.rb | 59 +++++++++++++------ .../adminapi/change_multi_attribute.rb | 5 +- 2 files changed, 43 insertions(+), 21 deletions(-) diff --git a/lib/puppet/functions/adminapi/adminapi.rb b/lib/puppet/functions/adminapi/adminapi.rb index d59e5a3..ae31925 100644 --- a/lib/puppet/functions/adminapi/adminapi.rb +++ b/lib/puppet/functions/adminapi/adminapi.rb @@ -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) @@ -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 @@ -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 diff --git a/lib/puppet/functions/adminapi/change_multi_attribute.rb b/lib/puppet/functions/adminapi/change_multi_attribute.rb index 9cc62f6..1b96984 100644 --- a/lib/puppet/functions/adminapi/change_multi_attribute.rb +++ b/lib/puppet/functions/adminapi/change_multi_attribute.rb @@ -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 @@ -30,6 +31,6 @@ def execute(object_id, attribute, additions, removals) 'add' => additions, 'remove' => removals, }, - }], []) + }], [], no_fail) end end