Class: Morpheus::Cli::StorageDatastores

Inherits:
Object
  • Object
show all
Includes:
CliCommand
Defined in:
lib/morpheus/cli/commands/storage_datastores.rb

Instance Attribute Summary

Attributes included from CliCommand

#no_prompt

Instance Method Summary collapse

Methods included from CliCommand

#add_query_parameter, #apply_options, #build_common_options, #build_get_options, #build_list_options, #build_option_type_options, #build_standard_add_many_options, #build_standard_add_options, #build_standard_api_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, #confirm, #confirm!, #default_refresh_interval, #default_sigdig, #default_subcommand, #establish_remote_appliance_connection, #execute_api, #execute_api_payload, #execute_api_request, #find_all, #find_all_json, #find_by_id, #find_by_name, #find_by_name_or_id, #find_record, #find_record_json, #full_command_usage, #get_interface, #get_list_key, #get_object_key, #get_subcommand_description, #handle_each_payload, #handle_subcommand, included, #interactive?, #my_help_command, #my_terminal, #my_terminal=, #parse_array, #parse_bytes_param, #parse_get_options!, #parse_id_list, #parse_labels, #parse_list_options, #parse_list_options!, #parse_list_subtitles, #parse_options, #parse_parameter_as_resource_id!, #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

#add(args) ⇒ Object



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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/morpheus/cli/commands/storage_datastores.rb', line 70

def add(args)
  options = {}
  params = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[name] [options]")
    build_common_options(opts, options, [:options, :payload, :json, :yaml, :dry_run, :quiet])
    opts.on( '-n', '--name NAME', "Name" ) do |val|
      options['name'] = val
    end
    opts.on( '-t', '--type DATASTORE_TYPE', "Datastore Type" ) do |val|
      options['datastoreType'] = val
    end
    opts.on( '-c', '--cloud DATASTORE_CLOUD', "Datastore Cloud" ) do |val|
      options['cloud'] = val
    end
    opts.footer = "Create a new Datastore.\n" +
                  "[name] is required. This is the name of the new datastore. It may also be passed as --name or inside your config."
  end
  optparse.parse!(args)
  if args.count > 1
    print_error Morpheus::Terminal.angry_prompt
    puts_error  "#{command_name} add expects 0-1 arguments and received #{args.count}: #{args}\n#{optparse}"
    return 1
  end
  # allow name as first argument
  if args[0] # && !options[:name]
    options[:name] = args[0]
  end
  connect(options)
  begin
    options[:options] ||= {}
    passed_options = (options[:options] || {}).reject {|k,v| k.is_a?(Symbol) }
    payload = {}
    if options[:payload]
      # payload is from parsed json|yaml files or arguments.
      payload = options[:payload]
      # merge -O options
      payload.deep_merge!(passed_options) unless passed_options.empty?
      # support some options on top of --payload
      [:name, :description, :environment].each do |k|
        if options.key?(k)
          payload[k.to_s] = options[k]
        end
      end
    else
      # prompt for payload
      payload = {}
      # merge -O options
      payload.deep_merge!(passed_options) unless passed_options.empty?

       # Name
      if passed_options['name']
        payload['name'] = passed_options['name']
      else
        v_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldName' => 'name', 'fieldLabel' => 'Name', 'type' => 'text', 'required' => true, 'description' => 'Enter a name for this archive bucket.'}], options, @api_client)
        payload['name'] = v_prompt['name']
      end

      #Datastore Type
      if passed_options['datastoreType']
        payload['datastoreType'] = passed_options['datastoreType']
      else
        payload['datastoreType'] = Morpheus::Cli::OptionTypes.prompt([{'fieldName' => 'datastoreType', 'fieldLabel' => 'Type', 'type' => 'select', 'required' => true, 'optionSource' => 'datastoreTypes'}], options[:options], @api_client)['datastoreType']
      end

      if passed_options['cloud']
        zone = passed_options['cloud']
      else
        zone = Morpheus::Cli::OptionTypes.prompt([{'fieldName' => 'zone', 'fieldLabel' => 'Cloud', 'type' => 'select', 'required' => true, 'optionSource' => 'cloudsForDatastores'}], options[:options], @api_client)['zone']
      end

      if zone
        payload['refType'] = 'ComputeZone'
        payload['refId'] = zone
      end
      
      option_types = load_option_types_for_datastore_type(payload['datastoreType'])

      values = Morpheus::Cli::OptionTypes.prompt(option_types, options[:options], @api_client)
      if values['domain']
        payload.merge!(values['domain']) if values['domain'].is_a?(Hash)
      end
      if values['config']
        payload['config'] = {}
        payload['config'].merge!(values['config']) if values['config'].is_a?(Hash)
      end

      @storage_datastore_interface.setopts(options)
      if options[:dry_run]
        print_dry_run @storage_datastore_interface.dry.create({'datastore' => payload})
        return
      end
      json_response = @storage_datastore_interface.create({'datastore' => payload})
      datastore = json_response['datastore']
      if options[:json]
        print JSON.pretty_generate(json_response),"\n"
      elsif !options[:quiet]
        datastore = json_response['datastore']
        print_green_success "Datastore #{datastore['name']} created"
        #get([datastore['id']])
      end
    end
  rescue RestClient::Exception => e
    print_rest_exception(e, options)
    exit 1
  end
end

#connect(opts) ⇒ Object



8
9
10
11
12
# File 'lib/morpheus/cli/commands/storage_datastores.rb', line 8

def connect(opts)
  @api_client = establish_remote_appliance_connection(opts)
  @storage_datastore_interface = @api_client.storage_datastores
  @cloud_datastores_interface = @api_client.cloud_datastores
end

#datastores_list_column_definitions(options) ⇒ Object



63
64
65
66
67
68
# File 'lib/morpheus/cli/commands/storage_datastores.rb', line 63

def datastores_list_column_definitions(options)
  {
    "ID" => 'id',
    "Name" => 'name'
  }
end

#find_datastore_by_id(id) ⇒ Object



419
420
421
422
423
424
425
426
427
428
429
430
431
# File 'lib/morpheus/cli/commands/storage_datastores.rb', line 419

def find_datastore_by_id(id)
  begin
    json_response = @storage_datastore_interface.get(id.to_i)
    return json_response['datastore']
  rescue RestClient::Exception => e
    if e.response && e.response.code == 404
      print_red_alert "Datastore not found by id #{id}"
      return nil
    else
      raise e
    end
  end
end

#find_datastore_by_name(name) ⇒ Object



433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
# File 'lib/morpheus/cli/commands/storage_datastores.rb', line 433

def find_datastore_by_name(name)
  json_response = @storage_datastore_interface.list({name: name.to_s})
  datastores = json_response['datastores']
  if datastores.empty?
    print_red_alert "Datastore not found by name #{name}"
    return nil
  elsif datastores.size > 1
    print_red_alert "#{datastores.size} datastores found by name #{name}"
    rows = datastores.collect do |it|
      {id: it['id'], name: it['name']}
    end
    print "\n"
    puts as_pretty_table(rows, [:id, :name], {color:red})
    return nil
  else
    datastore = datastores[0]
    # merge in tenants map
    if json_response['tenants'] && json_response['tenants'][datastore['id']]
      datastore['tenants'] = json_response['tenants'][datastore['id']]
    end
    return datastore
  end
end

#find_datastore_by_name_or_id(val) ⇒ Object



411
412
413
414
415
416
417
# File 'lib/morpheus/cli/commands/storage_datastores.rb', line 411

def find_datastore_by_name_or_id(val)
  if val.to_s =~ /\A\d{1,}\Z/
    return find_datastore_by_id(val)
  else
    return find_datastore_by_name(val)
  end
end

#get(args) ⇒ Object



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
206
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# File 'lib/morpheus/cli/commands/storage_datastores.rb', line 178

def get(args)
  datastore_id = nil
  options = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[datastore]")
    build_common_options(opts, options, [:json, :yaml, :csv, :fields, :dry_run, :remote])
    opts.footer = "Get details about a datastore." + "\n" +
                  "[datastore] is required. This is the name or id of a datastore."
  end
  optparse.parse!(args)
  if args.count == 1
    datastore_id = args[0]
  else
    raise_command_error "wrong number of arguments, expected 1 and got (#{args.count}) #{args.join(' ')}\n#{optparse}"
  end
  connect(options)
  begin
    @storage_datastore_interface.setopts(options)
    if options[:dry_run]
      if datastore_id.to_s =~ /\A\d{1,}\Z/
        print_dry_run @storage_datastore_interface.dry.get(datastore_id.to_i)
      else
        print_dry_run @storage_datastore_interface.dry.list({name:datastore_id})
      end
      return
    end
    datastore = find_datastore_by_name_or_id(datastore_id)
    return 1 if datastore.nil?
    json_response = {'datastore' => datastore}  # skip redundant request
    # json_response = @datastores_interface.get(datastore['id'])
    datastore = json_response['datastore']
    if options[:json]
      puts as_json(json_response, options, "datastore")
      return 0
    elsif options[:yaml]
      puts as_yaml(json_response, options, "datastore")
      return 0
    elsif options[:csv]
      puts records_as_csv([datastore], options)
      return 0
    end
    print_h1 "Datastore Details"
    print cyan
    description_cols = {
      "ID" => 'id',
      "Name" => 'name',
      "Type" => lambda {|it| it['type'].to_s.capitalize },
      "Cloud" => lambda {|it| it['zone'] ? it['zone']['name'] : '' },
      "Capacity" => lambda {|it| it['freeSpace'] ? Filesize.from("#{it['freeSpace']} B").pretty.strip : "Unknown" },
      "Online" => lambda {|it| format_boolean(it['online']) },
      "Active" => lambda {|it| format_boolean(it['active']) },
      "Visibility" => lambda {|it| it['visibility'].to_s.capitalize },
      "Tenants" => lambda {|it| it['tenants'] ? it['tenants'].collect {|it| it['name'] }.uniq.join(', ') : '' },
      # "Owner" => lambda {|it| it['owner'] ? it['owner']['name'] : '' },
    }
    print_description_list(description_cols, datastore)

    if datastore['resourcePermission'].nil?
      print "\n", "No group access found", "\n"
    else
      print_h2 "Group Access"
      rows = []
      if datastore['resourcePermission']['all']
        rows.push({"name" => 'All'})
      end
      if datastore['resourcePermission']['sites']
        datastore['resourcePermission']['sites'].each do |site|
          rows.push(site)
        end
      end
      rows = rows.collect do |site|
        # {group: site['name'], default: site['default'] ? 'Yes' : ''}
        {group: site['name']}
      end
      # columns = [:group, :default]
      columns = [:group]
      print cyan
      print as_pretty_table(rows, columns)
    end

    if datastore['tenants'].nil? || datastore['tenants'].nil?
      #print "\n", "No tenant permissions found", "\n"
    else
      print_h2 "Tenant Permissions"
      rows = []
      rows = datastore['tenants'] || []
      tenant_columns = {
        "TENANT" => 'name',
        #"DEFAULT" => lambda {|it| format_boolean(it['defaultTarget']) },
        "IMAGE TARGET" => lambda {|it| format_boolean(it['defaultStore']) }
      }
      print cyan
      print as_pretty_table(rows, tenant_columns)
    end

    print reset,"\n"
    return 0
  rescue RestClient::Exception => e
    print_rest_exception(e, options)
    return 1
  end
end

#handle(args) ⇒ Object



14
15
16
# File 'lib/morpheus/cli/commands/storage_datastores.rb', line 14

def handle(args)
  handle_subcommand(args)
end

#list(args) ⇒ Object



18
19
20
21
22
23
24
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
# File 'lib/morpheus/cli/commands/storage_datastores.rb', line 18

def list(args)
  options = {}
  params = {}  

  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage()
    build_standard_list_options(opts, options)
    opts.footer = "List Datastores."
  end
  optparse.parse!(args)
  connect(options)
  # verify_args!(args:args, optparse:optparse, count:0)
  if args.count > 0
    options[:phrase] = args.join(" ")
  end
  begin
    params.merge!(parse_list_options(options))
    
    @storage_datastore_interface.setopts(options)
    if options[:dry_run]
      print_dry_run @storage_datastore_interface.dry.list(params)
      return 0
    end

    json_response = @storage_datastore_interface.list(params)
    render_response(json_response, options, 'datastores') do
      datastores = json_response['datastores']
      title = "Storage Datastores"  
      print_h1 title
      if datastores.empty?
        print cyan,"No datastores found.",reset,"\n"
      else          
        columns = datastores_list_column_definitions(options).upcase_keys!
        print as_pretty_table(datastores, columns, options)
        print_results_pagination(json_response)
      end
      print reset,"\n"
    end
    return 0, nil
  rescue RestClient::Exception => e
    print_rest_exception(e, options)
    exit 1
  end
end

#load_option_types_for_datastore_type(datastore_type) ⇒ Object



407
408
409
# File 'lib/morpheus/cli/commands/storage_datastores.rb', line 407

def load_option_types_for_datastore_type(datastore_type)
  return @storage_datastore_interface.load_type_options(datastore_type)
end

#update(args) ⇒ Object



281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
# File 'lib/morpheus/cli/commands/storage_datastores.rb', line 281

def update(args)
  options = {}
  datastore_id = nil
  cloud_id = nil
  tenants = nil
  group_access_all = nil
  group_access_list = nil
  group_defaults_list = nil
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[datastore] [options]")
    opts.add_hidden_option('-c') # prefer args[0] for [cloud]
    opts.on('--group-access-all [on|off]', String, "Toggle Access for all groups.") do |val|
      group_access_all = val.to_s == 'on' || val.to_s == 'true' || val.to_s == ''
    end
    opts.on('--group-access LIST', Array, "Group Access, comma separated list of group IDs.") do |list|
      if list.size == 1 && list[0] == 'null' # hacky way to clear it
        group_access_list = []
      else
        group_access_list = list.collect {|it| it.to_s.strip.empty? ? nil : it.to_s.strip }.compact.uniq
      end
    end
    opts.on('--tenants LIST', Array, "Tenant Access, comma separated list of account IDs") do |list|
      if list.size == 1 && list[0] == 'null' # hacky way to clear it
        options['tenants'] = []
      else
        options['tenants'] = list.collect {|it| it.to_s.strip.empty? ? nil : it.to_s.strip }.compact.uniq
      end
    end
    opts.on('--visibility [private|public]', String, "Visibility") do |val|
      options['visibility'] = val
    end
    opts.on('--active [on|off]', String, "Can be used to disable a datastore") do |val|
      options['active'] = val.to_s == 'on' || val.to_s == 'true' || val.to_s == ''
    end
    build_common_options(opts, options, [:options, :payload, :json, :dry_run, :remote])
    opts.footer = "Update a datastore." + "\n" +
                  "[cloud] is required. This is the name or id of the cloud." + "\n" +
                  "[datastore] is required. This is the name or id of a datastore."
  end
  optparse.parse!(args)
  if args.count == 1
    datastore_id = args[0]
  else
    raise_command_error "wrong number of arguments, expected 1 and got (#{args.count}) #{args.join(' ')}\n#{optparse}"
  end
  connect(options)

  begin
    datastore = find_datastore_by_name_or_id(datastore_id)
    return 1 if datastore.nil?
    
    # merge -O options into normally parsed options
    options.deep_merge!(options[:options].reject {|k,v| k.is_a?(Symbol) }) if options[:options]

    # construct payload
    payload = nil
    if options[:payload]
      payload = options[:payload]
    else
      # prompt for datastore options
      payload = {
        'datastore' => {
        }
      }
      
      # allow arbitrary -O options
      payload['datastore'].deep_merge!(options[:options].reject {|k,v| k.is_a?(Symbol) }) if options[:options]

    
      # Group Access
      if group_access_all != nil
        payload['resourcePermissions'] ||= {}
        payload['resourcePermissions']['all'] = group_access_all
      end
      if group_access_list != nil
        payload['resourcePermissions'] ||= {}
        payload['resourcePermissions']['sites'] = group_access_list.collect do |site_id|
          site = {"id" => site_id.to_i}
          if group_defaults_list && group_defaults_list.include?(site_id)
            site["default"] = true
          end
          site
        end
      end

      # Tenants
      if options['tenants']
        payload['tenantPermissions'] = {}
        payload['tenantPermissions']['accounts'] = options['tenants']
      end

      # Active
      if options['active'] != nil
        payload['datastore']['active'] = options['active']
      end
      
      # Visibility
      if options['visibility'] != nil
        payload['datastore']['visibility'] = options['visibility']
      end

      if payload['datastore'].empty? && payload['resourcePermissions'].nil? && payload['tenantPermissions'].nil?
        raise_command_error "Specify at least one option to update.\n#{optparse}"
      end

    end
    @storage_datastore_interface.setopts(options)
    if options[:dry_run]
      print_dry_run @storage_datastore_interface.dry.update(datastore["id"], payload)
      return
    end
    json_response = @storage_datastore_interface.update(datastore["id"], payload)
    if options[:json]
      puts as_json(json_response)
    else
      datastore = json_response['data']['datastore']
      print_green_success "Updated datastore #{datastore['name']}"
      get([datastore['id']])
    end
    return 0
  rescue RestClient::Exception => e
    print_rest_exception(e, options)
    return 1
  end
end