Class: Morpheus::Cli::BackupJobsCommand

Inherits:
Object
  • Object
show all
Includes:
BackupsHelper, CliCommand
Defined in:
lib/morpheus/cli/backup_jobs_command.rb

Instance Attribute Summary

Attributes included from CliCommand

#no_prompt

Instance Method Summary collapse

Methods included from BackupsHelper

#backup_job_list_key, #backup_job_object_key, #backup_jobs_interfaces, #backup_list_key, #backup_object_key, #backups_interface, #find_backup_by_id, #find_backup_by_name, #find_backup_by_name_or_id, #find_backup_job_by_id, #find_backup_job_by_name, #find_backup_job_by_name_or_id, included

Methods included from CliCommand

#apply_options, #build_common_options, #build_option_type_options, #build_standard_add_options, #build_standard_delete_options, #build_standard_get_options, #build_standard_list_options, #build_standard_post_options, #build_standard_put_options, #build_standard_remove_options, #build_standard_update_options, #command_description, #command_name, #default_refresh_interval, #default_sigdig, #default_subcommand, #establish_remote_appliance_connection, #full_command_usage, #get_subcommand_description, #handle_subcommand, included, #interactive?, #my_help_command, #my_terminal, #my_terminal=, #parse_bytes_param, #parse_id_list, #parse_list_options, #parse_list_subtitles, #parse_passed_options, #parse_payload, #parse_query_options, #print, #print_error, #println, #prog_name, #puts, #puts_error, #raise_args_error, #raise_command_error, #render_response, #run_command_for_each_arg, #subcommand_aliases, #subcommand_description, #subcommand_usage, #subcommands, #usage, #validate_outfile, #verify_args!, #visible_subcommands

Instance Method Details

#_get(id, params, options) ⇒ Object



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

def _get(id, params, options)
  @backup_jobs_interface.setopts(options)
  if options[:dry_run]
    print_dry_run @backup_jobs_interface.dry.get(id, params)
    return
  end
  json_response = @backup_jobs_interface.get(id, params)
  backup_job = json_response['job']
  render_response(json_response, options, 'job') do
    print_h1 "Backup Job Details", [], options
    print cyan
    print_description_list(backup_job_column_definitions, backup_job)
    print reset,"\n"
  end
  return 0, nil
end

#add(args) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/morpheus/cli/backup_jobs_command.rb', line 114

def add(args)
  options = {}
  params = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[name] [options]")
    build_option_type_options(opts, options, add_backup_job_option_types)
    build_option_type_options(opts, options, add_backup_job_advanced_option_types)
    build_standard_add_options(opts, options)
    opts.footer = <<-EOT
Create a new backup job
EOT
  end
  optparse.parse!(args)
  verify_args!(args:args, optparse:optparse, min:0, max:1)
  options[:options]['name'] = args[0] if args[0]
  connect(options)
  payload = {}
  if options[:payload]
    payload = options[:payload]
    payload.deep_merge!({'job' => parse_passed_options(options)})
  else
    payload.deep_merge!({'job' => parse_passed_options(options)})
    v_prompt = Morpheus::Cli::OptionTypes.prompt(add_backup_job_option_types(), options[:options], @api_client, options[:params])
    params.deep_merge!(v_prompt)
    advanced_config = Morpheus::Cli::OptionTypes.no_prompt(add_backup_job_advanced_option_types, options[:options], @api_client, options[:params])
    advanced_config.deep_compact!
    params.deep_merge!(advanced_config)
    payload['job'].deep_merge!(params)
  end
  @backup_jobs_interface.setopts(options)
  if options[:dry_run]
    print_dry_run @backup_jobs_interface.dry.create(payload)
    return 0, nil
  end
  json_response = @backup_jobs_interface.create(payload)
  backup_job = json_response['job']
  render_response(json_response, options, 'job') do
    print_green_success "Added backup job #{backup_job['name']}"
    return _get(backup_job["id"], {}, options)
  end
  return 0, nil
end

#connect(opts) ⇒ Object



15
16
17
18
19
# File 'lib/morpheus/cli/backup_jobs_command.rb', line 15

def connect(opts)
  @api_client = establish_remote_appliance_connection(opts)
  @backups_interface = @api_client.backups
  @backup_jobs_interface = @api_client.backup_jobs
end

#get(args) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/morpheus/cli/backup_jobs_command.rb', line 65

def get(args)
  params = {}
  options = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[job]")
    build_standard_get_options(opts, options)
    opts.footer = <<-EOT
Get details about a specific backup job.
[job] is required. This is the id or name a backup job.
EOT
  end
  optparse.parse!(args)
  verify_args!(args:args, optparse:optparse, min:1)
  connect(options)
  id_list = parse_id_list(args)
  id_list = id_list.collect do |id|
    if id.to_s =~ /\A\d{1,}\Z/
      id
    else
      backup_job = find_backup_job_by_name(id)
      if backup_job
        backup_job['id']
      else
        return 1, "backup job not found for name '#{id}'"
      end
    end
  end
  return run_command_for_each_arg(id_list) do |arg|
    _get(arg, params, options)
  end
end

#handle(args) ⇒ Object



21
22
23
# File 'lib/morpheus/cli/backup_jobs_command.rb', line 21

def handle(args)
  handle_subcommand(args)
end

#list(args) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/morpheus/cli/backup_jobs_command.rb', line 25

def list(args)
  options = {}
  params = {}
  ref_ids = []
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[search]")
    build_standard_list_options(opts, options)
    opts.footer = "List backup jobs."
  end
  optparse.parse!(args)
  connect(options)
  # verify_args!(args:args, optparse:optparse, count:0)
  if args.count > 0
    options[:phrase] = args.join(" ")
  end
  params.merge!(parse_list_options(options))
  @backup_jobs_interface.setopts(options)
  if options[:dry_run]
    print_dry_run @backup_jobs_interface.dry.list(params)
    return
  end
  json_response = @backup_jobs_interface.list(params)
  backup_jobs = json_response['jobs']
  render_response(json_response, options, 'jobs') do
    print_h1 "Morpheus Backup Jobs", parse_list_subtitles(options), options
    if backup_jobs.empty?
      print yellow,"No backup jobs found.",reset,"\n"
    else
      print as_pretty_table(backup_jobs, backup_job_column_definitions.upcase_keys!, options)
      print_results_pagination(json_response)
    end
    print reset,"\n"
  end
  if backup_jobs.empty?
    return 1, "no backup jobs found"
  else
    return 0, nil
  end
end

#remove(args) ⇒ Object



207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# File 'lib/morpheus/cli/backup_jobs_command.rb', line 207

def remove(args)
  options = {}
  params = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[job] [options]")
    build_standard_remove_options(opts, options)
    opts.footer = <<-EOT
Delete a backup job.
[job] is required. This is the name or id of a backup job.
EOT
  end
  optparse.parse!(args)
  verify_args!(args:args, optparse:optparse, count:1)
  connect(options)
  backup_job = find_backup_job_by_name_or_id(args[0])
  return 1 if backup_job.nil?
  @backup_jobs_interface.setopts(options)
  if options[:dry_run]
    print_dry_run @backup_jobs_interface.dry.destroy(backup_job['id'], params)
    return
  end
  unless options[:yes] || Morpheus::Cli::OptionTypes.confirm("Are you sure you want to delete the backup #{backup['name']}?")
    return 9, "aborted command"
  end
  json_response = @backup_jobs_interface.destroy(backup_job['id'], params)
  render_response(json_response, options) do
    print_green_success "Removed backup job #{backup_job['name']}"
  end
  return 0, nil
end

#update(args) ⇒ Object



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/morpheus/cli/backup_jobs_command.rb', line 157

def update(args)
  options = {}
  params = {}
  payload = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[job] [options]")
    build_option_type_options(opts, options, update_backup_job_option_types)
    build_option_type_options(opts, options, update_backup_job_advanced_option_types)
    build_standard_update_options(opts, options)
    opts.footer = <<-EOT
Update a backup job.
[job] is required. This is the name or id of a backup job.
EOT
  end
  optparse.parse!(args)
  verify_args!(args:args, optparse:optparse, count:1)
  connect(options)
  backup_job = find_backup_job_by_name_or_id(args[0])
  return 1 if backup_job.nil?
  payload = {}
  if options[:payload]
    payload = options[:payload]
    payload.deep_merge!({'job' => parse_passed_options(options)})
  else
    payload.deep_merge!({'job' => parse_passed_options(options)})
    v_prompt = Morpheus::Cli::OptionTypes.no_prompt(update_backup_job_option_types, options[:options], @api_client, options[:params])
    v_prompt.deep_compact!
    params.deep_merge!(v_prompt)
    advanced_config = Morpheus::Cli::OptionTypes.no_prompt(update_backup_job_advanced_option_types, options[:options], @api_client, options[:params])
    advanced_config.deep_compact!
    params.deep_merge!(advanced_config)
    payload.deep_merge!({'job' => params})
    if payload['job'].empty? # || options[:no_prompt]
      raise_command_error "Specify at least one option to update.\n#{optparse}"
    end
  end
  @backup_jobs_interface.setopts(options)
  if options[:dry_run]
    print_dry_run @backup_jobs_interface.dry.update(backup_job['id'], payload)
    return
  end
  json_response = @backup_jobs_interface.update(backup_job['id'], payload)
  backup_job = json_response['job']
  render_response(json_response, options, 'job') do
    print_green_success "Updated backup job #{backup_job['name']}"
    return _get(backup_job["id"], {}, options)
  end
  return 0, nil
end