From 3362214f6bfe43074b385406c295feb28561ac32 Mon Sep 17 00:00:00 2001 From: swapnil91 Date: Wed, 18 Dec 2024 12:24:51 +0530 Subject: [PATCH] DG-28970 | Add backup_requests method (#73) * DG-28970 | Add backup_requests method * DG-28970 | Enhance test coverage * DG-28970 | Refactor backup requests and add assertions * DG-28970 | Refactor backup requests and add assertions * DG-28970 | Refactor backup process in system tests * DG-28970 | Extract assert_backup_result method * DG-28970 | Fixed pipeline error * DG-28970 | Fixed pipeline error * DG-28970 | Resolved conflicts for specific files --------- Co-authored-by: Swapnil Srivastava --- lib/sfrest/backup.rb | 47 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/lib/sfrest/backup.rb b/lib/sfrest/backup.rb index 5f36155..bbab600 100644 --- a/lib/sfrest/backup.rb +++ b/lib/sfrest/backup.rb @@ -37,6 +37,53 @@ def create_backup(site_id, datum = nil) @conn.post(current_path, datum.to_json) end + def execute_backup(options = {}) + api = options[:api] + rconn = options[:rconn] + acsf = options[:acsf] + site_nid = options[:site_nid] + name = options[:name] + components = options[:components] + user = options[:user] + + case api + when 'rest' + execute_rest_backup(rconn, site_nid, name, components) + when 'drush' + execute_drush_backup(acsf, site_nid, name, components, user) + else + raise "Unsupported API: #{api}" + end + end + + def execute_rest_backup(rconn, site_nid, name, components) + payload = { + 'label' => name, + 'components' => components + }.to_json + rconn.post "/api/v1/sites/#{site_nid}/backup", payload + end + + def execute_drush_backup(acsf, site_nid, name, components, user) + drush_components = components.is_a?(Array) ? components.join(',') : components + drush_cmd = "sf-backup #{site_nid} \"#{name}\" --components=\"#{drush_components}\" --user=#{user} --format=json" + drush_cmd_update = acsf.drush drush_cmd + result = acsf.factory_ssh.exec!(drush_cmd_update).strip + JSON.parse(result) + end + + def parse_response(response) + raise "Unexpected response type: #{response.class}" unless response.is_a?(Hash) + + [response['task_id'], response['message']] + end + + def parse_components(components_json) + JSON.parse(components_json) + rescue JSON::ParserError + components_json # Keep the original string if it's not valid JSON + end + # Gets a url to download a backup # @param [Integer] site_id Node id of site # @param [Integer] backup_id Id of backup to delete