Module: Morpheus::Cli::ProvisioningHelper

Included in:
Apps, Hosts, Instances
Defined in:
lib/morpheus/cli/mixins/provisioning_helper.rb

Overview

Provides common methods for provisioning instances

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



7
8
9
# File 'lib/morpheus/cli/mixins/provisioning_helper.rb', line 7

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

Instance Method Details

#find_cloud_by_id(group_id, val) ⇒ Object



67
68
69
70
71
72
73
74
75
# File 'lib/morpheus/cli/mixins/provisioning_helper.rb', line 67

def find_cloud_by_id(group_id, val)
  clouds = get_available_clouds(group_id)
  cloud = clouds.find {|it| it["id"].to_s == val.to_s }
  if !cloud
    print_red_alert "Cloud not found by id #{val}"
    exit 1
  end
  return cloud
end

#find_cloud_by_name(group_id, val) ⇒ Object



77
78
79
80
81
82
83
84
85
# File 'lib/morpheus/cli/mixins/provisioning_helper.rb', line 77

def find_cloud_by_name(group_id, val)
  clouds = get_available_clouds(group_id)
  cloud = clouds.find {|it| it["name"].to_s.downcase == val.to_s.downcase }
  if !cloud
    print_red_alert "Cloud not found by name #{val}"
    exit 1
  end
  return cloud
end

#find_cloud_from_options(group_id, options) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
# File 'lib/morpheus/cli/mixins/provisioning_helper.rb', line 87

def find_cloud_from_options(group_id, options)
  cloud = nil
  if options[:cloud]
    cloud = options[:cloud]
  elsif options[:cloud_name]
    cloud = find_cloud_by_name(group_id, options[:cloud_name])
  elsif options[:cloud_id]
    cloud = find_cloud_by_id(group_id, options[:cloud_id])
  end
  return cloud
end

#find_group_by_id(val) ⇒ Object



35
36
37
38
39
40
41
42
43
# File 'lib/morpheus/cli/mixins/provisioning_helper.rb', line 35

def find_group_by_id(val)
  groups = get_available_groups()
  group = groups.find {|it| it["id"].to_s == val.to_s }
  if !group
    print_red_alert "Group not found by id #{val}"
    exit 1
  end
  return group
end

#find_group_by_name(val) ⇒ Object



45
46
47
48
49
50
51
52
53
# File 'lib/morpheus/cli/mixins/provisioning_helper.rb', line 45

def find_group_by_name(val)
  groups = get_available_groups()
  group = groups.find {|it| it["name"].to_s.downcase == val.to_s.downcase }
  if !group
    print_red_alert "Group not found by name #{val}"
    exit 1
  end
  return group
end

#find_group_from_options(options) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/morpheus/cli/mixins/provisioning_helper.rb', line 55

def find_group_from_options(options)
  group = nil
  if options[:group]
    group = options[:group]
  elsif options[:group_name]
    group = find_group_by_name(options[:group_name])
  elsif options[:group_id]
    group = find_group_by_id(options[:group_id])
  end
  return group
end

#find_instance_type_by_code(code) ⇒ Object



99
100
101
102
103
104
105
106
# File 'lib/morpheus/cli/mixins/provisioning_helper.rb', line 99

def find_instance_type_by_code(code)
  results = @instance_types_interface.get({code: code})
  if results['instanceTypes'].empty?
    print_red_alert "Instance Type not found by code #{code}"
    exit 1
  end
  return results['instanceTypes'][0]
end

#get_available_clouds(group_id, refresh = false) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/morpheus/cli/mixins/provisioning_helper.rb', line 21

def get_available_clouds(group_id, refresh=false)
  group = find_group_by_id(group_id)
  if !group
    return []
  end
  if !group["clouds"] || refresh
    option_results = @options_interface.options_for_source('clouds', {groupId: group_id})
    group["clouds"] = option_results['data'].collect {|it|
      {"id" => it["value"], "name" => it["name"], "value" => it["value"], "zoneTypeId" => it["zoneTypeId"]}
    }
  end
  return group["clouds"]
end

#get_available_groups(refresh = false) ⇒ Object



11
12
13
14
15
16
17
18
19
# File 'lib/morpheus/cli/mixins/provisioning_helper.rb', line 11

def get_available_groups(refresh=false)
  if !@available_groups || refresh
    option_results = @options_interface.options_for_source('groups',{})
    @available_groups = option_results['data'].collect {|it|
      {"id" => it["value"], "name" => it["name"], "value" => it["value"]}
    }
  end
  return @available_groups
end

#instance_context_options ⇒ Object



861
862
863
# File 'lib/morpheus/cli/mixins/provisioning_helper.rb', line 861

def instance_context_options
  [{'name' => 'Dev', 'value' => 'dev'}, {'name' => 'Test', 'value' => 'qa'}, {'name' => 'Staging', 'value' => 'staging'}, {'name' => 'Production', 'value' => 'production'}]
end

#prompt_network_interfaces(zone_id, provision_type_id, options = {}, api_client = nil) ⇒ Object

This recreates the behavior of multi_networks.js This is used by both instances add and hosts add returns array of networkInterfaces based on provision type and cloud settings



733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
# File 'lib/morpheus/cli/mixins/provisioning_helper.rb', line 733

def prompt_network_interfaces(zone_id, provision_type_id, options={}, api_client=nil)
  #puts "Configure Networks:"
  no_prompt = (options[:no_prompt] || (options[:options] && options[:options][:no_prompt]))
  
  network_interfaces = []

  zone_network_options_json = api_client.options.options_for_source('zoneNetworkOptions', {zoneId: zone_id, provisionTypeId: provision_type_id})
  # puts "zoneNetworkOptions JSON"
  # puts JSON.pretty_generate(zone_network_options_json)    
  zone_network_data = zone_network_options_json['data'] || {}
  networks = zone_network_data['networks']
  network_interface_types = (zone_network_data['networkTypes'] || []).sort { |x,y| x['displayOrder'] <=> y['displayOrder'] }
  enable_network_type_selection = (zone_network_data['enableNetworkTypeSelection'] == 'on' || zone_network_data['enableNetworkTypeSelection'] == true)
  has_networks = zone_network_data["hasNetworks"] == true
  max_networks = zone_network_data["maxNetworks"] ? zone_network_data["maxNetworks"].to_i : nil

  # skip unless provision type supports networks
  if !has_networks
    return nil
  end

  # no networks available, shouldn't happen
  if networks.empty?
    return network_interfaces
  end

  network_options = []
  networks.each do |opt|
    if !opt.nil?
      network_options << {'name' => opt['name'], 'value' => opt['id']}
    end
  end

  network_interface_type_options = []
  network_interface_types.each do |opt|
    if !opt.nil?
      network_interface_type_options << {'name' => opt['name'], 'value' => opt['id']}
    end
  end


  interface_index = 1
  
  # has_another_interface = options[:options] && options[:options]["networkInterface"]
  # add_another_interface = has_another_interface || (!no_prompt)
  add_another_interface = true
  
  while add_another_interface do
    
    # if !no_prompt
    #   if interface_index == 1
    #     puts "Configure Network Interface"
    #   else
    #     puts "Configure Network Interface #{interface_index}"
    #   end
    # end

    field_context = interface_index == 1 ? "networkInterface" : "networkInterface#{interface_index}"
    
    network_interface = {}

    # choose network
    v_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldContext' => field_context, 'fieldName' => 'networkId', 'type' => 'select', 'fieldLabel' => "Network", 'selectOptions' => network_options, 'required' => true, 'skipSingleOption' => false, 'description' => 'Choose a network for this interface.', 'defaultValue' => network_interface['networkId']}], options[:options])
    network_interface['network'] = {}
    network_interface['network']['id'] = v_prompt[field_context]['networkId'].to_i

    selected_network = networks.find {|it| it["id"] == network_interface['network']['id'] }

    # choose network interface type
    if enable_network_type_selection && !network_interface_type_options.empty?
      v_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldContext' => field_context, 'fieldName' => 'networkInterfaceTypeId', 'type' => 'select', 'fieldLabel' => "Network Interface Type", 'selectOptions' => network_interface_type_options, 'required' => true, 'skipSingleOption' => true, 'description' => 'Choose a network interface type.', 'defaultValue' => network_interface['networkInterfaceTypeId']}], options[:options])
      network_interface['networkInterfaceTypeId'] = v_prompt[field_context]['networkInterfaceTypeId'].to_i
    end

    # choose IP unless network has a pool configured
    if selected_network['pool']
      puts "IP Address: Using pool '#{selected_network['pool']['name']}'" if !no_prompt
    elsif selected_network['dhcpServer']
      puts "IP Address: Using DHCP" if !no_prompt
    else
      v_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldContext' => field_context, 'fieldName' => 'ipAddress', 'type' => 'text', 'fieldLabel' => "IP Address", 'required' => true, 'description' => 'Enter an IP for this network interface. x.x.x.x', 'defaultValue' => network_interface['ipAddress']}], options[:options])
      network_interface['ipAddress'] = v_prompt[field_context]['ipAddress']
    end
    
    network_interfaces << network_interface
    
    interface_index += 1
    has_another_interface = options[:options] && options[:options]["networkInterface#{interface_index}"]
    add_another_interface = has_another_interface || (!no_prompt && Morpheus::Cli::OptionTypes.confirm("Add another network interface?"))
    if max_networks && network_interfaces.size >= max_networks
      add_another_interface = false
    end

  end

  return network_interfaces

end

#prompt_new_instance(options = {}) ⇒ Object

prompts user for all the configuartion options for a particular instance returns payload of data for a new instance



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
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
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
# File 'lib/morpheus/cli/mixins/provisioning_helper.rb', line 110

def prompt_new_instance(options={})

  # Group
  group_id = nil
  group = find_group_from_options(options)
  if group
    group_id = group["id"]
  else
    # print_red_alert "Group not found or specified!"
    # exit 1
    group_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldName' => 'group', 'type' => 'select', 'fieldLabel' => 'Group', 'selectOptions' => get_available_groups(), 'required' => true, 'description' => 'Select Group.'}],options[:options],@api_client,{})
    group_id = cloud_prompt['group']
  end
  

  # Cloud
  cloud_id = nil
  cloud = find_cloud_from_options(group_id, options)
  if cloud
    cloud_id = cloud["id"]
  else
    # print_red_alert "Cloud not specified!"
    # exit 1
    cloud_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldName' => 'cloud', 'type' => 'select', 'fieldLabel' => 'Cloud', 'selectOptions' => get_available_clouds(group_id), 'required' => true, 'description' => 'Select Cloud.'}],options[:options],@api_client,{groupId: group_id})
    cloud_id = cloud_prompt['cloud']
  end
  
  # Instance Type
  instance_type_code = nil
  if options[:instance_type_code]
    instance_type_code = options[:instance_type_code]
  else
    instance_type_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldName' => 'type', 'type' => 'select', 'fieldLabel' => 'Type', 'optionSource' => 'instanceTypes', 'required' => true, 'description' => 'Select Instance Type.'}],options[:options],@api_client,{groupId: group_id})
    instance_type_code = instance_type_prompt['type']
  end
  instance_type = find_instance_type_by_code(instance_type_code)

  # Instance Name

  instance_name = nil
  if options[:instance_name]
    instance_name = options[:instance_name]
  else
    name_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldName' => 'name', 'fieldLabel' => 'Instance Name', 'type' => 'text', 'required' => options[:name_required]}], options[:options])
    instance_name = name_prompt['name'] || ''
  end

  payload = {
    :zoneId => cloud_id,
    :instance => {
      :name => instance_name,
      :site => {
        :id => group_id
      },
      :instanceType => {
        :code => instance_type_code
      }
    }
  }

  # Description
  v_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldName' => 'description', 'fieldLabel' => 'Description', 'type' => 'text', 'required' => false}], options[:options])
  payload[:instance][:description] = v_prompt['description'] if !v_prompt['description'].empty?

  # Environment
  v_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldName' => 'instanceContext', 'fieldLabel' => 'Environment', 'type' => 'select', 'required' => false, 'selectOptions' => instance_context_options()}], options[:options])
  payload[:instance][:instanceContext] = v_prompt['instanceContext'] if !v_prompt['instanceContext'].empty?

  # Tags
  v_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldName' => 'tags', 'fieldLabel' => 'Tags', 'type' => 'text', 'required' => false}], options[:options])
  payload[:instance][:tags] = v_prompt['tags'].split(',').collect {|it| it.to_s.strip }.compact.uniq if !v_prompt['tags'].empty?
  

  # Version and Layout

  version_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldName' => 'version', 'type' => 'select', 'fieldLabel' => 'Version', 'optionSource' => 'instanceVersions', 'required' => true, 'skipSingleOption' => true, 'description' => 'Select which version of the instance type to be provisioned.'}],options[:options],@api_client,{groupId: group_id, cloudId: cloud_id, instanceTypeId: instance_type['id']})
  layout_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldName' => 'layout', 'type' => 'select', 'fieldLabel' => 'Layout', 'optionSource' => 'layoutsForCloud', 'required' => true, 'description' => 'Select which configuration of the instance type to be provisioned.'}],options[:options],@api_client,{groupId: group_id, cloudId: cloud_id, instanceTypeId: instance_type['id'], version: version_prompt['version']})
  layout_id = layout_prompt['layout']
  layout = instance_type['instanceTypeLayouts'].find{ |lt| lt['id'] == layout_id.to_i}
  payload[:instance][:layout] = {id: layout['id']}

  # prompt for service plan
  service_plans_json = @instances_interface.service_plans({zoneId: cloud_id, layoutId: layout_id})
  service_plans = service_plans_json["plans"]
  service_plans_dropdown = service_plans.collect {|sp| {'name' => sp["name"], 'value' => sp["id"]} } # already sorted
  plan_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldName' => 'servicePlan', 'type' => 'select', 'fieldLabel' => 'Plan', 'selectOptions' => service_plans_dropdown, 'required' => true, 'description' => 'Choose the appropriately sized plan for this instance'}],options[:options])
  service_plan = service_plans.find {|sp| sp["id"] == plan_prompt['servicePlan'].to_i }
  # todo: pick one of these three, let's go with the last one...
  payload[:servicePlan] = service_plan["id"] # pre-2.10 appliances
  payload[:instance][:plan] = {id: service_plan["id"]}

  # prompt for volumes
  volumes = prompt_volumes(service_plan, options, @api_client, {})
  if !volumes.empty?
    payload[:volumes] = volumes
  end

  if layout["provisionType"] && layout["provisionType"]["id"] && layout["provisionType"]["hasNetworks"]
    # prompt for network interfaces (if supported)
    begin
      network_interfaces = prompt_network_interfaces(cloud_id, layout["provisionType"]["id"], options, @api_client)
      if !network_interfaces.empty?
        payload[:networkInterfaces] = network_interfaces
      end
    rescue RestClient::Exception => e
      print_yellow_warning "Unable to load network options. Proceeding..."
      print_rest_exception(e, options) if Morpheus::Logging.print_stacktrace?
    end
  end
  
  
  type_payload = {}
  if !layout['optionTypes'].nil? && !layout['optionTypes'].empty?
    type_payload = Morpheus::Cli::OptionTypes.prompt(layout['optionTypes'],options[:options],@api_client,{groupId: group_id, cloudId: cloud_id, zoneId: cloud_id, instanceTypeId: instance_type['id'], version: version_prompt['version']})
  elsif !instance_type['optionTypes'].nil? && !instance_type['optionTypes'].empty?
    type_payload = Morpheus::Cli::OptionTypes.prompt(instance_type['optionTypes'],options[:options],@api_client,{groupId: group_id, cloudId: cloud_id, zoneId: cloud_id, instanceTypeId: instance_type['id'], version: version_prompt['version']})
  end
  if !type_payload['config'].nil?
    payload.merge!(type_payload['config'])
  end

  provision_payload = {}
  if !layout['provisionType'].nil? && !layout['provisionType']['optionTypes'].nil? && !layout['provisionType']['optionTypes'].empty?
    instance_type_option_types = layout['provisionType']['optionTypes']
    # remove volume options if volumes were configured
    if !payload[:volumes].empty?
      instance_type_option_types = reject_volume_option_types(instance_type_option_types)
    end
    # remove networkId option if networks were configured above
    if !payload[:networkInterfaces].empty?
      instance_type_option_types = reject_networking_option_types(instance_type_option_types)
    end
    provision_payload = Morpheus::Cli::OptionTypes.prompt(instance_type_option_types,options[:options],@api_client,{groupId: group_id, cloudId: cloud_id, zoneId: cloud_id, instanceTypeId: instance_type['id'], version: version_prompt['version']})
  end

  payload[:config] = provision_payload['config'] || {}
  if provision_payload['server']
    payload[:server] = provision_payload['server'] || {}
  end

  return payload
end

#prompt_resize_volumes(current_volumes, plan_info, options = {}, api_client = nil, api_params = {}) ⇒ Object

This recreates the behavior of multi_disk.js returns array of volumes based on service plan options (plan_info)



448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
# File 'lib/morpheus/cli/mixins/provisioning_helper.rb', line 448

def prompt_resize_volumes(current_volumes, plan_info, options={}, api_client=nil, api_params={})
  #puts "Configure Volumes:"
  no_prompt = (options[:no_prompt] || (options[:options] && options[:options][:no_prompt]))

  current_root_volume = current_volumes[0]

  volumes = []

  plan_size = nil
  if plan_info['maxStorage']
    plan_size = plan_info['maxStorage'].to_i / (1024 * 1024 * 1024)
  end

  root_storage_types = []
  if plan_info['rootStorageTypes']
    plan_info['rootStorageTypes'].each do |opt|
      if !opt.nil?
        root_storage_types << {'name' => opt['name'], 'value' => opt['id']}
      end
    end
  end

  storage_types = []
  if plan_info['storageTypes']
    plan_info['storageTypes'].each do |opt|
      if !opt.nil?
        storage_types << {'name' => opt['name'], 'value' => opt['id']}
      end
    end
  end

  datastore_options = []
  if plan_info['supportsAutoDatastore']
    if plan_info['autoOptions']
      plan_info['autoOptions'].each do |opt|
        if !opt.nil?
          datastore_options << {'name' => opt['name'], 'value' => opt['id']}
        end
      end
    end
  end
  if plan_info['datastores']
    plan_info['datastores'].each do |k, v|
      v.each do |opt|
        if !opt.nil?
          datastore_options << {'name' => "#{k}: #{opt['name']}", 'value' => opt['id']}
        end
      end
    end
  end

  #puts "Configure Root Volume"

  field_context = "rootVolume"

  if root_storage_types.empty?
    # this means there's no configuration, just send a single root volume to the server
    storage_type_id = nil
    storage_type = nil
  else
    #v_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldContext' => field_context, 'fieldName' => 'storageType', 'type' => 'select', 'fieldLabel' => 'Root Storage Type', 'selectOptions' => root_storage_types, 'required' => true, 'skipSingleOption' => true, 'description' => 'Choose a storage type.'}], options[:options])
    #storage_type_id = v_prompt[field_context]['storageType']
    storage_type_id = current_root_volume['type'] || current_root_volume['storageType']
    storage_type = plan_info['storageTypes'].find {|i| i['id'] == storage_type_id.to_i }
  end

  # sometimes the user chooses sizeId from a list of size options (AccountPrice) and other times it is free form
  root_custom_size_options = []
  if plan_info['rootCustomSizeOptions'] && plan_info['rootCustomSizeOptions'][storage_type_id.to_s]
    plan_info['rootCustomSizeOptions'][storage_type_id.to_s].each do |opt|
      if !opt.nil?
        root_custom_size_options << {'name' => opt['value'], 'value' => opt['key']}
      end
    end
  end

  volume = {
    'id' => current_root_volume['id'], 
    'rootVolume' => true,
    'name' => current_root_volume['name'],
    'size' => current_root_volume['size'] > plan_size ? current_root_volume['size'] : plan_size,
    'sizeId' => nil,
    'storageType' => storage_type_id,
    'datastoreId' => current_root_volume['datastoreId']
  }

  if plan_info['rootDiskCustomizable'] && storage_type && storage_type['customLabel']
    v_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldContext' => field_context, 'fieldName' => 'name', 'type' => 'text', 'fieldLabel' => 'Root Volume Label', 'required' => true, 'description' => 'Enter a volume label.', 'defaultValue' => volume['name']}], options[:options])
    volume['name'] = v_prompt[field_context]['name']
  end
  if plan_info['rootDiskCustomizable'] && storage_type && storage_type['customSize']
    if root_custom_size_options.empty?
      v_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldContext' => field_context, 'fieldName' => 'size', 'type' => 'number', 'fieldLabel' => 'Root Volume Size (GB)', 'required' => true, 'description' => 'Enter a volume size (GB).', 'defaultValue' => volume['size']}], options[:options])
      volume['size'] = v_prompt[field_context]['size']
      volume['sizeId'] = nil #volume.delete('sizeId')
    else
      v_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldContext' => field_context, 'fieldName' => 'sizeId', 'type' => 'select', 'fieldLabel' => 'Root Volume Size', 'selectOptions' => root_custom_size_options, 'required' => true, 'description' => 'Choose a volume size.'}], options[:options])
      volume['sizeId'] = v_prompt[field_context]['sizeId']
      volume['size'] = nil #volume.delete('size')
    end
  else
    # might need different logic here ? =o
    volume['size'] = plan_size 
    volume['sizeId'] = nil #volume.delete('sizeId')
  end
  # if !datastore_options.empty?
  #   v_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldContext' => field_context, 'fieldName' => 'datastoreId', 'type' => 'select', 'fieldLabel' => 'Root Datastore', 'selectOptions' => datastore_options, 'required' => true, 'description' => 'Choose a datastore.'}], options[:options])
  #   volume['datastoreId'] = v_prompt[field_context]['datastoreId']
  # end

  volumes << volume

  # modify or delete existing data volumes
  (1..(current_volumes.size-1)).each do |volume_index|
    current_volume = current_volumes[volume_index]
    
    if current_volume

      field_context = "dataVolume#{volume_index}"

      if no_prompt
        volume_action = 'keep'
      else
        action_options = [{'name' => 'Modify', 'value' => 'modify'}, {'name' => 'Keep', 'value' => 'keep'}, {'name' => 'Delete', 'value' => 'delete'}]
        v_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldContext' => field_context, 'fieldName' => 'action', 'type' => 'select', 'fieldLabel' => "Modify/Keep/Delete volume '#{current_volume['name']}'", 'selectOptions' => action_options, 'required' => true, 'description' => 'Modify, Keep or Delete existing data volume?'}], options[:options])
        volume_action = v_prompt[field_context]['action']
      end

      if volume_action == 'delete'
        # deleted volume is just excluded from post params
        next
      elsif volume_action == 'keep'
        volume = {
          'id' => current_volume['id'].to_i, 
          'rootVolume' => false,
          'name' => current_volume['name'],
          'size' => current_volume['size'] > plan_size ? current_volume['size'] : plan_size,
          'sizeId' => nil,
          'storageType' => (current_volume['type'] || current_volume['storageType']),
          'datastoreId' => current_volume['datastoreId']
        }
        volumes << volume
      else
      
        # v_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldContext' => field_context, 'fieldName' => 'storageType', 'type' => 'select', 'fieldLabel' => "Disk #{volume_index} Storage Type", 'selectOptions' => storage_types, 'required' => true, 'skipSingleOption' => true, 'description' => 'Choose a storage type.'}], options[:options])
        # storage_type_id = v_prompt[field_context]['storageType']
        storage_type_id = current_volume['type'] || current_volume['storageType']
        storage_type = plan_info['storageTypes'].find {|i| i['id'] == storage_type_id.to_i }
        
        # sometimes the user chooses sizeId from a list of size options (AccountPrice) and other times it is free form
        custom_size_options = []
        if plan_info['customSizeOptions'] && plan_info['customSizeOptions'][storage_type_id.to_s]
          plan_info['customSizeOptions'][storage_type_id.to_s].each do |opt|
            if !opt.nil?
              custom_size_options << {'name' => opt['value'], 'value' => opt['key']}
            end
          end
        end

        volume = {
          'id' => current_volume['id'].to_i, 
          'rootVolume' => false,
          'name' => current_volume['name'],
          'size' => current_volume['size'] > plan_size ? current_volume['size'] : plan_size,
          'sizeId' => nil,
          'storageType' => (current_volume['type'] || current_volume['storageType']),
          'datastoreId' => current_volume['datastoreId']
        }

        if plan_info['customizeVolume'] && storage_type['customLabel']
          v_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldContext' => field_context, 'fieldName' => 'name', 'type' => 'text', 'fieldLabel' => "Disk #{volume_index} Volume Label", 'required' => true, 'description' => 'Enter a volume label.', 'defaultValue' => volume['name']}], options[:options])
          volume['name'] = v_prompt[field_context]['name']
        end
        if plan_info['customizeVolume'] && storage_type['customSize']
          if custom_size_options.empty?
            v_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldContext' => field_context, 'fieldName' => 'size', 'type' => 'number', 'fieldLabel' => "Disk #{volume_index} Volume Size (GB)", 'required' => true, 'description' => 'Enter a volume size (GB).', 'defaultValue' => volume['size']}], options[:options])
            volume['size'] = v_prompt[field_context]['size']
            volume['sizeId'] = nil #volume.delete('sizeId')
          else
            v_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldContext' => field_context, 'fieldName' => 'sizeId', 'type' => 'select', 'fieldLabel' => "Disk #{volume_index} Volume Size", 'selectOptions' => custom_size_options, 'required' => true, 'description' => 'Choose a volume size.'}], options[:options])
            volume['sizeId'] = v_prompt[field_context]['sizeId']
            volume['size'] = nil #volume.delete('size')
          end
        else
          # might need different logic here ? =o
          volume['size'] = plan_size 
          volume['sizeId'] = nil #volume.delete('sizeId')
        end
        # if !datastore_options.empty?
        #   v_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldContext' => field_context, 'fieldName' => 'datastoreId', 'type' => 'select', 'fieldLabel' => "Disk #{volume_index} Datastore", 'selectOptions' => datastore_options, 'required' => true, 'description' => 'Choose a datastore.'}], options[:options])
        #   volume['datastoreId'] = v_prompt[field_context]['datastoreId']
        # end

        volumes << volume

      end

    end
  end


  if plan_info['addVolumes']
    volume_index = current_volumes.size
    
    has_another_volume = options[:options] && options[:options]["dataVolume#{volume_index}"]
    add_another_volume = has_another_volume || (!no_prompt && Morpheus::Cli::OptionTypes.confirm("Add data volume?"))
    
    while add_another_volume do
      
      #puts "Configure Data #{volume_index} Volume"

      field_context = "dataVolume#{volume_index}"

      v_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldContext' => field_context, 'fieldName' => 'storageType', 'type' => 'select', 'fieldLabel' => "Disk #{volume_index} Storage Type", 'selectOptions' => storage_types, 'required' => true, 'skipSingleOption' => true, 'description' => 'Choose a storage type.'}], options[:options])
      storage_type_id = v_prompt[field_context]['storageType']
      storage_type = plan_info['storageTypes'].find {|i| i['id'] == storage_type_id.to_i }
      

      # sometimes the user chooses sizeId from a list of size options (AccountPrice) and other times it is free form
      custom_size_options = []
      if plan_info['customSizeOptions'] && plan_info['customSizeOptions'][storage_type_id.to_s]
        plan_info['customSizeOptions'][storage_type_id.to_s].each do |opt|
          if !opt.nil?
            custom_size_options << {'name' => opt['value'], 'value' => opt['key']}
          end
        end
      end

      volume_label = (volume_index == 1 ? 'data' : "data #{volume_index}")
      volume = {
        'id' => -1, 
        'rootVolume' => false,
        'name' => volume_label,
        'size' => plan_size,
        'sizeId' => nil,
        'storageType' => storage_type_id,
        'datastoreId' => nil
      }

      if plan_info['customizeVolume'] && storage_type['customLabel']
        v_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldContext' => field_context, 'fieldName' => 'name', 'type' => 'text', 'fieldLabel' => "Disk #{volume_index} Volume Label", 'required' => true, 'description' => 'Enter a volume label.', 'defaultValue' => volume_label}], options[:options])
        volume['name'] = v_prompt[field_context]['name']
      end
      if plan_info['customizeVolume'] && storage_type['customSize']
        if custom_size_options.empty?
          v_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldContext' => field_context, 'fieldName' => 'size', 'type' => 'number', 'fieldLabel' => "Disk #{volume_index} Volume Size (GB)", 'required' => true, 'description' => 'Enter a volume size (GB).', 'defaultValue' => plan_size}], options[:options])
          volume['size'] = v_prompt[field_context]['size']
          volume['sizeId'] = nil #volume.delete('sizeId')
        else
          v_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldContext' => field_context, 'fieldName' => 'sizeId', 'type' => 'select', 'fieldLabel' => "Disk #{volume_index} Volume Size", 'selectOptions' => custom_size_options, 'required' => true, 'description' => 'Choose a volume size.'}], options[:options])
          volume['sizeId'] = v_prompt[field_context]['sizeId']
          volume['size'] = nil #volume.delete('size')
        end
      else
        # might need different logic here ? =o
        volume['size'] = plan_size 
        volume['sizeId'] = nil #volume.delete('sizeId')
      end
      if !datastore_options.empty?
        v_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldContext' => field_context, 'fieldName' => 'datastoreId', 'type' => 'select', 'fieldLabel' => "Disk #{volume_index} Datastore", 'selectOptions' => datastore_options, 'required' => true, 'description' => 'Choose a datastore.'}], options[:options])
        volume['datastoreId'] = v_prompt[field_context]['datastoreId']
      end

      volumes << volume

      # todo: should maxDisk check consider the root volume too?
      if plan_info['maxDisk'] && volume_index >= plan_info['maxDisk']
        add_another_volume = false
      else
        volume_index += 1
        has_another_volume = options[:options] && options[:options]["dataVolume#{volume_index}"]
        add_another_volume = has_another_volume || (!no_prompt && Morpheus::Cli::OptionTypes.confirm("Add another data volume?"))
      end

    end

  end

  return volumes
end

#prompt_volumes(plan_info, options = {}, api_client = nil, api_params = {}) ⇒ Object

This recreates the behavior of multi_disk.js returns array of volumes based on service plan options (plan_info)



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
280
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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
# File 'lib/morpheus/cli/mixins/provisioning_helper.rb', line 255

def prompt_volumes(plan_info, options={}, api_client=nil, api_params={})
  #puts "Configure Volumes:"
  no_prompt = (options[:no_prompt] || (options[:options] && options[:options][:no_prompt]))

  volumes = []

  plan_size = nil
  if plan_info['maxStorage']
    plan_size = plan_info['maxStorage'].to_i / (1024 * 1024 * 1024)
  end

  root_storage_types = []
  if plan_info['rootStorageTypes']
    plan_info['rootStorageTypes'].each do |opt|
      if !opt.nil?
        root_storage_types << {'name' => opt['name'], 'value' => opt['id']}
      end
    end
  end

  storage_types = []
  if plan_info['storageTypes']
    plan_info['storageTypes'].each do |opt|
      if !opt.nil?
        storage_types << {'name' => opt['name'], 'value' => opt['id']}
      end
    end
  end

  datastore_options = []
  if plan_info['supportsAutoDatastore']
    if plan_info['autoOptions']
      plan_info['autoOptions'].each do |opt|
        if !opt.nil?
          datastore_options << {'name' => opt['name'], 'value' => opt['id']}
        end
      end
    end
  end
  if plan_info['datastores']
    plan_info['datastores'].each do |k, v|
      v.each do |opt|
        if !opt.nil?
          datastore_options << {'name' => "#{k}: #{opt['name']}", 'value' => opt['id']}
        end
      end
    end
  end

  #puts "Configure Root Volume"

  field_context = "rootVolume"

  if root_storage_types.empty?
    # this means there's no configuration, just send a single root volume to the server
    storage_type_id = nil
    storage_type = nil
  else
    v_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldContext' => field_context, 'fieldName' => 'storageType', 'type' => 'select', 'fieldLabel' => 'Root Storage Type', 'selectOptions' => root_storage_types, 'required' => true, 'skipSingleOption' => true, 'description' => 'Choose a storage type.'}], options[:options])
    storage_type_id = v_prompt[field_context]['storageType']
    storage_type = plan_info['storageTypes'].find {|i| i['id'] == storage_type_id.to_i }
  end

  # sometimes the user chooses sizeId from a list of size options (AccountPrice) and other times it is free form
  root_custom_size_options = []
  if plan_info['rootCustomSizeOptions'] && plan_info['rootCustomSizeOptions'][storage_type_id.to_s]
    plan_info['rootCustomSizeOptions'][storage_type_id.to_s].each do |opt|
      if !opt.nil?
        root_custom_size_options << {'name' => opt['value'], 'value' => opt['key']}
      end
    end
  end

  volume_label = 'root'
  volume = {
    'id' => -1, 
    'rootVolume' => true,
    'name' => volume_label,
    'size' => plan_size,
    'sizeId' => nil,
    'storageType' => storage_type_id,
    'datastoreId' => nil
  }

  if plan_info['rootDiskCustomizable'] && storage_type && storage_type['customLabel']
    v_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldContext' => field_context, 'fieldName' => 'name', 'type' => 'text', 'fieldLabel' => 'Root Volume Label', 'required' => true, 'description' => 'Enter a volume label.', 'defaultValue' => volume_label}], options[:options])
    volume['name'] = v_prompt[field_context]['name']
  end
  if plan_info['rootDiskCustomizable'] && storage_type && storage_type['customSize']
    if root_custom_size_options.empty?
      v_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldContext' => field_context, 'fieldName' => 'size', 'type' => 'number', 'fieldLabel' => 'Root Volume Size (GB)', 'required' => true, 'description' => 'Enter a volume size (GB).', 'defaultValue' => plan_size}], options[:options])
      volume['size'] = v_prompt[field_context]['size']
      volume['sizeId'] = nil #volume.delete('sizeId')
    else
      v_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldContext' => field_context, 'fieldName' => 'sizeId', 'type' => 'select', 'fieldLabel' => 'Root Volume Size', 'selectOptions' => root_custom_size_options, 'required' => true, 'description' => 'Choose a volume size.'}], options[:options])
      volume['sizeId'] = v_prompt[field_context]['sizeId']
      volume['size'] = nil #volume.delete('size')
    end
  else
    # might need different logic here ? =o
    volume['size'] = plan_size 
    volume['sizeId'] = nil #volume.delete('sizeId')
  end
  if !datastore_options.empty?
    v_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldContext' => field_context, 'fieldName' => 'datastoreId', 'type' => 'select', 'fieldLabel' => 'Root Datastore', 'selectOptions' => datastore_options, 'required' => true, 'description' => 'Choose a datastore.'}], options[:options])
    volume['datastoreId'] = v_prompt[field_context]['datastoreId']
  end

  volumes << volume

  if plan_info['addVolumes']
    volume_index = 1
    
    has_another_volume = options[:options] && options[:options]["dataVolume#{volume_index}"]
    add_another_volume = has_another_volume || (!no_prompt && Morpheus::Cli::OptionTypes.confirm("Add data volume?"))
    
    while add_another_volume do
      
      #puts "Configure Data #{volume_index} Volume"

      field_context = "dataVolume#{volume_index}"

      v_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldContext' => field_context, 'fieldName' => 'storageType', 'type' => 'select', 'fieldLabel' => "Disk #{volume_index} Storage Type", 'selectOptions' => storage_types, 'required' => true, 'skipSingleOption' => true, 'description' => 'Choose a storage type.'}], options[:options])
      storage_type_id = v_prompt[field_context]['storageType']
      storage_type = plan_info['storageTypes'].find {|i| i['id'] == storage_type_id.to_i }
      

      # sometimes the user chooses sizeId from a list of size options (AccountPrice) and other times it is free form
      custom_size_options = []
      if plan_info['customSizeOptions'] && plan_info['customSizeOptions'][storage_type_id.to_s]
        plan_info['customSizeOptions'][storage_type_id.to_s].each do |opt|
          if !opt.nil?
            custom_size_options << {'name' => opt['value'], 'value' => opt['key']}
          end
        end
      end

      volume_label = (volume_index == 1 ? 'data' : "data #{volume_index}")
      volume = {
        'id' => -1, 
        'rootVolume' => false,
        'name' => volume_label,
        'size' => plan_size,
        'sizeId' => nil,
        'storageType' => storage_type_id,
        'datastoreId' => nil
      }

      if plan_info['customizeVolume'] && storage_type['customLabel']
        v_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldContext' => field_context, 'fieldName' => 'name', 'type' => 'text', 'fieldLabel' => "Disk #{volume_index} Volume Label", 'required' => true, 'description' => 'Enter a volume label.', 'defaultValue' => volume_label}], options[:options])
        volume['name'] = v_prompt[field_context]['name']
      end
      if plan_info['customizeVolume'] && storage_type['customSize']
        if custom_size_options.empty?
          v_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldContext' => field_context, 'fieldName' => 'size', 'type' => 'number', 'fieldLabel' => "Disk #{volume_index} Volume Size (GB)", 'required' => true, 'description' => 'Enter a volume size (GB).', 'defaultValue' => plan_size}], options[:options])
          volume['size'] = v_prompt[field_context]['size']
          volume['sizeId'] = nil #volume.delete('sizeId')
        else
          v_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldContext' => field_context, 'fieldName' => 'sizeId', 'type' => 'select', 'fieldLabel' => "Disk #{volume_index} Volume Size", 'selectOptions' => custom_size_options, 'required' => true, 'description' => 'Choose a volume size.'}], options[:options])
          volume['sizeId'] = v_prompt[field_context]['sizeId']
          volume['size'] = nil #volume.delete('size')
        end
      else
        # might need different logic here ? =o
        volume['size'] = plan_size 
        volume['sizeId'] = nil #volume.delete('sizeId')
      end
      if !datastore_options.empty?
        v_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldContext' => field_context, 'fieldName' => 'datastoreId', 'type' => 'select', 'fieldLabel' => "Disk #{volume_index} Datastore", 'selectOptions' => datastore_options, 'required' => true, 'description' => 'Choose a datastore.'}], options[:options])
        volume['datastoreId'] = v_prompt[field_context]['datastoreId']
      end

      volumes << volume

      # todo: should maxDisk check consider the root volume too?
      if plan_info['maxDisk'] && volume_index >= plan_info['maxDisk']
        add_another_volume = false
      else
        volume_index += 1
        has_another_volume = options[:options] && options[:options]["dataVolume#{volume_index}"]
        add_another_volume = has_another_volume || (!no_prompt && Morpheus::Cli::OptionTypes.confirm("Add another data volume?"))
      end

    end

  end

  return volumes
end

#reject_networking_option_types(option_types) ⇒ Object

reject old networking option types these will eventually get removed from the associated optionTypes



845
846
847
848
849
850
851
# File 'lib/morpheus/cli/mixins/provisioning_helper.rb', line 845

def reject_networking_option_types(option_types)
  option_types.reject {|opt| 
      ['networkId', 'networkType', 'ipAddress', 'netmask', 'gateway', 'nameservers',
        'vmwareNetworkType', 'vmwareIpAddress', 'vmwareNetmask', 'vmwareGateway', 'vmwareNameservers'
      ].include?(opt['fieldName'])
    }
end

#reject_service_plan_option_types(option_types) ⇒ Object

reject old option types that now come from the selected service plan these will eventually get removed from the associated optionTypes



855
856
857
858
859
# File 'lib/morpheus/cli/mixins/provisioning_helper.rb', line 855

def reject_service_plan_option_types(option_types)
  option_types.reject {|opt| 
      ['cpuCount', 'memorySize', 'memory'].include?(opt['fieldName'])
    }
end

#reject_volume_option_types(option_types) ⇒ Object

reject old volume option types these will eventually get removed from the associated optionTypes



834
835
836
837
838
839
840
841
# File 'lib/morpheus/cli/mixins/provisioning_helper.rb', line 834

def reject_volume_option_types(option_types)
  option_types.reject {|opt| 
      ['osDiskSize', 'osDiskType',
        'diskSize', 'diskType',
        'datastoreId', 'storagePodId'
      ].include?(opt['fieldName'])
    }
end