Module: Morpheus::Cli::BackupsHelper

Included in:
BackupJobsCommand, BackupsCommand
Defined in:
lib/morpheus/cli/mixins/backups_helper.rb

Overview

Provides common methods for backups management

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



6
7
8
# File 'lib/morpheus/cli/mixins/backups_helper.rb', line 6

def self.included(klass)
  klass.send :include, Morpheus::Cli::PrintHelper
end

Instance Method Details

#backup_job_list_keyObject



71
72
73
74
# File 'lib/morpheus/cli/mixins/backups_helper.rb', line 71

def backup_job_list_key
  # 'backupJobs'
  'jobs'
end

#backup_job_object_keyObject



66
67
68
69
# File 'lib/morpheus/cli/mixins/backups_helper.rb', line 66

def backup_job_object_key
  # 'backupJob'
  'job'
end

#backup_jobs_interfacesObject



15
16
17
18
# File 'lib/morpheus/cli/mixins/backups_helper.rb', line 15

def backup_jobs_interfaces
  raise "#{self.class} has not defined @backup_jobs_interface" if @backup_jobs_interface.nil?
  @backup_jobs_interface
end

#backup_list_keyObject



24
25
26
# File 'lib/morpheus/cli/mixins/backups_helper.rb', line 24

def backup_list_key
  'backups'
end

#backup_object_keyObject



20
21
22
# File 'lib/morpheus/cli/mixins/backups_helper.rb', line 20

def backup_object_key
  'backup'
end

#backups_interfaceObject



10
11
12
13
# File 'lib/morpheus/cli/mixins/backups_helper.rb', line 10

def backups_interface
  raise "#{self.class} has not defined @backups_interface" if @backups_interface.nil?
  @backups_interface
end

#find_backup_by_id(id) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/morpheus/cli/mixins/backups_helper.rb', line 36

def find_backup_by_id(id)
  begin
    json_response = backups_interface.get(id.to_i)
    return json_response[backup_object_key]
  rescue RestClient::Exception => e
    if e.response && e.response.code == 404
      print_red_alert "Backup not found by id '#{id}'"
    else
      raise e
    end
  end
end

#find_backup_by_name(name) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/morpheus/cli/mixins/backups_helper.rb', line 49

def find_backup_by_name(name)
  json_response = backups_interface.list({name: name.to_s})
  backups = json_response[backup_list_key]
  if backups.empty?
    print_red_alert "Backup not found by name '#{name}'"
    return nil
  elsif backups.size > 1
    print_red_alert "#{backups.size} backups found by name '#{name}'"
    puts_error as_pretty_table(backups, [:id, :name], {color:red})
    print_red_alert "Try using ID instead"
    print reset,"\n"
    return nil
  else
    return backups[0]
  end
end

#find_backup_by_name_or_id(val) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/morpheus/cli/mixins/backups_helper.rb', line 28

def find_backup_by_name_or_id(val)
  if val.to_s =~ /\A\d{1,}\Z/
    return find_backup_by_id(val)
  else
    return find_backup_by_name(val)
  end
end

#find_backup_job_by_id(id) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/morpheus/cli/mixins/backups_helper.rb', line 84

def find_backup_job_by_id(id)
  begin
    json_response = backup_jobs_interface.get(id.to_i)
    return json_response[backup_job_object_key]
  rescue RestClient::Exception => e
    if e.response && e.response.code == 404
      print_red_alert "Backup job not found by id '#{id}'"
    else
      raise e
    end
  end
end

#find_backup_job_by_name(name) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/morpheus/cli/mixins/backups_helper.rb', line 97

def find_backup_job_by_name(name)
  json_response = backup_jobs_interface.list({name: name.to_s})
  backup_jobs = json_response[backup_job_list_key]
  if backup_jobs.empty?
    print_red_alert "Backup job not found by name '#{name}'"
    return nil
  elsif backup_jobs.size > 1
    print_red_alert "#{backup_jobs.size} backup jobs found by name '#{name}'"
    puts_error as_pretty_table(backup_jobs, [:id, :name], {color:red})
    print_red_alert "Try using ID instead"
    print reset,"\n"
    return nil
  else
    return backup_jobs[0]
  end
end

#find_backup_job_by_name_or_id(val) ⇒ Object



76
77
78
79
80
81
82
# File 'lib/morpheus/cli/mixins/backups_helper.rb', line 76

def find_backup_job_by_name_or_id(val)
  if val.to_s =~ /\A\d{1,}\Z/
    return find_backup_job_by_id(val)
  else
    return find_backup_job_by_name(val)
  end
end