Module: Morpheus::Cli::ProvisioningHelper

Included in:
AppTemplates, Apps, ArchivesCommand, ContainersCommand, Hosts, ImageBuilderCommand, 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

#api_clientObject



11
12
13
14
# File 'lib/morpheus/cli/mixins/provisioning_helper.rb', line 11

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

#find_cloud_by_id_for_provisioning(group_id, val) ⇒ Object



93
94
95
96
97
98
99
100
101
# File 'lib/morpheus/cli/mixins/provisioning_helper.rb', line 93

def find_cloud_by_id_for_provisioning(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_for_provisioning(group_id, val) ⇒ Object



103
104
105
106
107
108
109
110
111
# File 'lib/morpheus/cli/mixins/provisioning_helper.rb', line 103

def find_cloud_by_name_for_provisioning(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_by_name_or_id_for_provisioning(group_id, val) ⇒ Object



113
114
115
116
117
118
119
# File 'lib/morpheus/cli/mixins/provisioning_helper.rb', line 113

def find_cloud_by_name_or_id_for_provisioning(group_id, val)
  if val.to_s =~ /\A\d{1,}\Z/
    return find_cloud_by_id_for_provisioning(group_id, val)
  else
    return find_cloud_by_name_for_provisioning(group_id, val)
  end
end

#find_group_by_id_for_provisioning(val) ⇒ Object



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

def find_group_by_id_for_provisioning(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_for_provisioning(val) ⇒ Object



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

def find_group_by_name_for_provisioning(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_by_name_or_id_for_provisioning(val) ⇒ Object



85
86
87
88
89
90
91
# File 'lib/morpheus/cli/mixins/provisioning_helper.rb', line 85

def find_group_by_name_or_id_for_provisioning(val)
  if val.to_s =~ /\A\d{1,}\Z/
    return find_group_by_id_for_provisioning(val)
  else
    return find_group_by_name_for_provisioning(val)
  end
end

#find_instance_by_id(id) ⇒ Object



148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/morpheus/cli/mixins/provisioning_helper.rb', line 148

def find_instance_by_id(id)
  begin
    json_response = instances_interface.get(id.to_i)
    return json_response['instance']
  rescue RestClient::Exception => e
    if e.response && e.response.code == 404
      print_red_alert "Instance not found by id #{id}"
      exit 1
    else
      raise e
    end
  end
end

#find_instance_by_name(name) ⇒ Object



162
163
164
165
166
167
168
169
170
# File 'lib/morpheus/cli/mixins/provisioning_helper.rb', line 162

def find_instance_by_name(name)
  json_results = instances_interface.get({name: name.to_s})
  if json_results['instances'].empty?
    print_red_alert "Instance not found by name #{name}"
    exit 1
  end
  instance = json_results['instances'][0]
  return instance
end

#find_instance_by_name_or_id(val) ⇒ Object



140
141
142
143
144
145
146
# File 'lib/morpheus/cli/mixins/provisioning_helper.rb', line 140

def find_instance_by_name_or_id(val)
  if val.to_s =~ /\A\d{1,}\Z/
    return find_instance_by_id(val)
  else
    return find_instance_by_name(val)
  end
end

#find_instance_type_by_code(code) ⇒ Object



120
121
122
123
124
125
126
127
128
# File 'lib/morpheus/cli/mixins/provisioning_helper.rb', line 120

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}"
    # return nil
    exit 1
  end
  return results['instanceTypes'][0]
end

#find_instance_type_by_name(name) ⇒ Object



130
131
132
133
134
135
136
137
138
# File 'lib/morpheus/cli/mixins/provisioning_helper.rb', line 130

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

#get_available_clouds(group_id, refresh = false) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/morpheus/cli/mixins/provisioning_helper.rb', line 45

def get_available_clouds(group_id, refresh=false)
  if !group_id
    option_results = options_interface.options_for_source('clouds', {})
    return option_results['data'].collect {|it|
      {"id" => it["value"], "name" => it["name"], "value" => it["value"], "zoneTypeId" => it["zoneTypeId"]}
    }
  end
  group = find_group_by_id_for_provisioning(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



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

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
  #puts "get_available_groups() rtn: #{@available_groups.inspect}"
  return @available_groups
end

#instance_context_optionsObject



1035
1036
1037
# File 'lib/morpheus/cli/mixins/provisioning_helper.rb', line 1035

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

#instance_types_interfaceObject



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

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

#instances_interfaceObject



16
17
18
19
20
# File 'lib/morpheus/cli/mixins/provisioning_helper.rb', line 16

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

#options_interfaceObject



22
23
24
25
26
# File 'lib/morpheus/cli/mixins/provisioning_helper.rb', line 22

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

#prompt_evars(options = {}) ⇒ Object

Prompts user for environment variables for new instance returns array of evar objects null, name: “VAR”, value: “somevalue”



913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
# File 'lib/morpheus/cli/mixins/provisioning_helper.rb', line 913

def prompt_evars(options={})
  #puts "Configure Environment Variables:"
  no_prompt = (options[:no_prompt] || (options[:options] && options[:options][:no_prompt]))
  evars = []
  evar_index = 0
  has_another_evar = options[:options] && options[:options]["evar#{evar_index}"]
  add_another_evar = has_another_evar || (!no_prompt && Morpheus::Cli::OptionTypes.confirm("Add an environment variable?", {default: false}))
  while add_another_evar do
    field_context = "evar#{evar_index}"
    evar = {}
    evar['id'] = nil
    evar_label = evar_index == 0 ? "ENV" : "ENV [#{evar_index+1}]"
    v_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldContext' => field_context, 'fieldName' => 'name', 'type' => 'text', 'fieldLabel' => "#{evar_label} Name", 'required' => true, 'description' => 'Environment Variable Name.', 'defaultValue' => evar['name']}], options[:options])
    evar['name'] = v_prompt[field_context]['name']
    v_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldContext' => field_context, 'fieldName' => 'value', 'type' => 'text', 'fieldLabel' => "#{evar_label} Value", 'required' => true, 'description' => 'Environment Variable Value', 'defaultValue' => evar['value']}], options[:options])
    evar['value'] = v_prompt[field_context]['value']
    evars << evar
    evar_index += 1
    has_another_evar = options[:options] && options[:options]["evar#{evar_index}"]
    add_another_evar = has_another_evar || (!no_prompt && Morpheus::Cli::OptionTypes.confirm("Add another environment variable?", {default: false}))
  end

  return evars
end

#prompt_instance_load_balancer(instance, default_lb_id, options) ⇒ Object

Prompts user for load balancer settings returns Hash of parameters like “-1”, etc



968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
# File 'lib/morpheus/cli/mixins/provisioning_helper.rb', line 968

def prompt_instance_load_balancer(instance, default_lb_id, options)
  #puts "Configure Environment Variables:"
  no_prompt = (options[:no_prompt] || (options[:options] && options[:options][:no_prompt]))
  payload = {}
  api_params = {}
  if instance['id']
    api_params['instanceId'] = instance['id']
  end
  if instance['zone']
    api_params['zoneId'] = instance['zone']['id']
  elsif instance['cloud']
    api_params['zoneId'] = instance['cloud']['id']
  end
  if instance['group']
    api_params['siteId'] = instance['group']['id']
  elsif instance['site']
    api_params['siteId'] = instance['site']['id']
  end
  if instance['plan']
    api_params['planId'] = instance['plan']['id']
  end
  v_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldName' => 'loadBalancerId', 'type' => 'select', 'fieldLabel' => "Load Balancer", 'optionSource' => 'loadBalancers', 'required' => true, 'description' => 'Select Load Balancer for instance', 'defaultValue' => default_lb_id || ''}], options[:options], api_client, api_params)
  lb_id = v_prompt['loadBalancerId']
  payload['loadBalancerId'] = lb_id

  # todo: finish implmenting this

  # loadBalancerId
  # loadBalancerProxyProtocol
  # loadBalancerName
  # loadBalancerDescription
  # loadBalancerSslCert
  # loadBalancerScheme
  
  return payload
end

#prompt_metadata(options = {}) ⇒ Object

Prompts user for environment variables for new instance returns array of metadata objects null, name: “MYTAG”, value: “myvalue”



940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
# File 'lib/morpheus/cli/mixins/provisioning_helper.rb', line 940

def (options={})
  #puts "Configure Environment Variables:"
  no_prompt = (options[:no_prompt] || (options[:options] && options[:options][:no_prompt]))
   = []
   = 0
   = options[:options] && options[:options]["metadata#{}"]
   =  || (!no_prompt && Morpheus::Cli::OptionTypes.confirm("Add a metadata tag?", {default: false}))
  while  do
    field_context = "metadata#{}"
     = {}
    ['id'] = nil
     =  == 0 ? "Metadata Tag" : "Metadata Tag [#{+1}]"
    v_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldContext' => field_context, 'fieldName' => 'name', 'type' => 'text', 'fieldLabel' => "#{} Name", 'required' => true, 'description' => 'Metadata Tag Name.', 'defaultValue' => ['name']}], options[:options])
    # todo: metadata.type ?
    ['name'] = v_prompt[field_context]['name']
    v_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldContext' => field_context, 'fieldName' => 'value', 'type' => 'text', 'fieldLabel' => "#{} Value", 'required' => true, 'description' => 'Metadata Tag Value', 'defaultValue' => ['value']}], options[:options])
    ['value'] = v_prompt[field_context]['value']
     << 
     += 1
     = options[:options] && options[:options]["metadata#{}"]
     =  || (!no_prompt && Morpheus::Cli::OptionTypes.confirm("Add another metadata tag?", {default: false}))
  end

  return 
end

#prompt_network_interfaces(zone_id, provision_type_id, options = {}) ⇒ 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



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
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
# File 'lib/morpheus/cli/mixins/provisioning_helper.rb', line 805

def prompt_network_interfaces(zone_id, provision_type_id, options={})
  #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_groups = zone_network_data['networkGroups']
  if network_groups
    networks = network_groups + networks
  end
  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
  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_s
    selected_network = networks.find {|it| it["id"].to_s == network_interface['network']['id'] }

    if !selected_network
      print_red_alert "Network not found by id #{network_interface['network']['id']}!"
      exit 1
    end

    # 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
    ip_required = true
    if selected_network['id'].to_s.include?('networkGroup')
      #puts "IP Address: Using network group." if !no_prompt
      ip_required = false
    elsif selected_network['pool']
      #puts "IP Address: Using pool '#{selected_network['pool']['name']}'" if !no_prompt
      ip_required = false
    elsif selected_network['dhcpServer']
      #puts "IP Address: Using DHCP" if !no_prompt
      ip_required = false
    end
    v_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldContext' => field_context, 'fieldName' => 'ipAddress', 'type' => 'text', 'fieldLabel' => "IP Address", 'required' => ip_required, 'description' => 'Enter an IP for this network interface. x.x.x.x', 'defaultValue' => network_interface['ipAddress']}], options[:options])
    if v_prompt[field_context] && !v_prompt[field_context]['ipAddress'].to_s.empty?
      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?", {:default => false}))
    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



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

def prompt_new_instance(options={})

  # Group
  group_id = nil
  group = options[:group] ? find_group_by_name_or_id_for_provisioning(options[:group]) : nil
  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 = group_prompt['group']
  end

  # Cloud
  cloud_id = nil
  cloud = options[:cloud] ? find_cloud_by_name_or_id_for_provisioning(group_id, options[:cloud]) : nil
  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,
    # 'siteId' => siteId,
    'instance' => {
      'name' => instance_name,
      'site' => {
        'id' => group_id
      },
      'type' => instance_type_code,
      'instanceType' => {
        'code' => instance_type_code
      }
    }
  }

  # allow arbitrary -O values passed by the user (config and instance namespace only)
  if options[:options] && options[:options]['config'].is_a?(Hash)
    payload['config'] ||= {}
    payload['config'].deep_merge!(options[:options]['config'])
  end
  if options[:options] && options[:options]['instance'].is_a?(Hash)
    payload['instance'].deep_merge!(options[:options]['instance'])
  end

  # 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}
  if !layout
    print_red_alert "Layout not found by id #{layout_id}"
    exit 1
  end
  payload['instance']['layout'] = {'id' => layout['id']}

  # prompt for service plan
  service_plans_json = @instances_interface.service_plans({zoneId: cloud_id, layoutId: layout_id, siteId: group_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 }
  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)
      if !network_interfaces.empty?
        payload['networkInterfaces'] = network_interfaces
      end
    rescue RestClient::Exception => e
      print yellow,"Unable to load network options. Proceeding...",reset,"\n"
      print_rest_exception(e, options) if Morpheus::Logging.debug?
    end
  end

  # build option types
  option_type_list = []
  if !layout['optionTypes'].nil? && !layout['optionTypes'].empty?
    option_type_list += layout['optionTypes']
  end
  if !instance_type['optionTypes'].nil? && !instance_type['optionTypes'].empty?
    option_type_list += instance_type['optionTypes']
  end
  if !layout['provisionType'].nil? && !layout['provisionType']['optionTypes'].nil? && !layout['provisionType']['optionTypes'].empty?
    option_type_list += layout['provisionType']['optionTypes']
  end
  if !payload['volumes'].empty?
    option_type_list = reject_volume_option_types(option_type_list)
  end
  # remove networkId option if networks were configured above
  if !payload['networkInterfaces'].empty?
    option_type_list = reject_networking_option_types(option_type_list)
  end

  instance_config_payload = Morpheus::Cli::OptionTypes.prompt(option_type_list, options[:options], @api_client, {groupId: group_id, cloudId: cloud_id, zoneId: cloud_id, instanceTypeId: instance_type['id'], version: version_prompt['version']})
  payload.deep_merge!(instance_config_payload)

  ## Advanced Options

  # scale factor


  # prompt for environment variables
  evars = prompt_evars(options)
  if !evars.empty?
    payload['evars'] = evars
  end

  # prompt for metadata variables
   = (options)
  if !.empty?
    payload['metadata'] = 
  end

  return payload
end

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

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



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
728
729
730
731
732
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
# File 'lib/morpheus/cli/mixins/provisioning_helper.rb', line 527

def prompt_resize_volumes(current_volumes, plan_info, options={})
#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)



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
444
445
446
447
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
# File 'lib/morpheus/cli/mixins/provisioning_helper.rb', line 338

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?", {:default => false}))
  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?", {:default => false}))
      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



1018
1019
1020
1021
1022
1023
1024
1025
# File 'lib/morpheus/cli/mixins/provisioning_helper.rb', line 1018

def reject_networking_option_types(option_types)
  option_types.reject {|opt|
    ['networkId', 'networkType', 'ipAddress', 'netmask', 'gateway', 'nameservers',
     'vmwareNetworkType', 'vmwareIpAddress', 'vmwareNetmask', 'vmwareGateway', 'vmwareNameservers',
     'subnetId'
     ].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



1029
1030
1031
1032
1033
# File 'lib/morpheus/cli/mixins/provisioning_helper.rb', line 1029

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



1007
1008
1009
1010
1011
1012
1013
1014
# File 'lib/morpheus/cli/mixins/provisioning_helper.rb', line 1007

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