Class: Morpheus::Cli::LibraryFormsCommand

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

Instance Attribute Summary

Attributes included from CliCommand

#no_prompt

Instance Method Summary collapse

Methods included from LibraryHelper

#api_client, #find_container_type_by_id, #find_container_type_by_name, #find_container_type_by_name_or_id, #find_instance_type_by_id, #find_instance_type_by_name, #find_instance_type_by_name_or_id, #find_option_type_by_id, #find_option_type_by_name, #find_option_type_by_name_or_id, #find_option_type_list_by_id, #find_option_type_list_by_name, #find_option_type_list_by_name_or_id, #find_spec_template_by_id, #find_spec_template_by_name, #find_spec_template_by_name_or_id, #find_spec_template_type_by_id, #find_spec_template_type_by_name_or_code, #find_spec_template_type_by_name_or_code_id, #format_container_type_technology, #format_instance_type_technology, #get_all_spec_template_types, included, #load_balance_protocols_dropdown, #print_container_types_table, #print_instance_types_table, #print_resource_specs_table, #prompt_exposed_ports, #prompt_for_container_types, #prompt_for_option_types, #prompt_for_spec_templates

Methods included from CliCommand

#add_query_parameter, #apply_options, #build_common_options, #build_get_options, #build_list_options, #build_option_type_options, #build_standard_add_many_options, #build_standard_add_options, #build_standard_api_options, #build_standard_delete_options, #build_standard_get_options, #build_standard_list_options, #build_standard_post_options, #build_standard_put_options, #build_standard_remove_options, #build_standard_update_options, #command_description, #command_name, #confirm, #confirm!, #default_refresh_interval, #default_sigdig, #default_subcommand, #establish_remote_appliance_connection, #execute_api, #execute_api_payload, #execute_api_request, #find_all, #find_all_json, #find_by_id, #find_by_name, #find_by_name_or_id, #find_record, #find_record_json, #full_command_usage, #get_interface, #get_list_key, #get_object_key, #get_subcommand_description, #handle_each_payload, #handle_subcommand, included, #interactive?, #my_help_command, #my_terminal, #my_terminal=, #parse_array, #parse_bytes_param, #parse_get_options!, #parse_id_list, #parse_labels, #parse_list_options, #parse_list_options!, #parse_list_subtitles, #parse_options, #parse_parameter_as_resource_id!, #parse_passed_options, #parse_payload, #parse_query_options, #print, #print_error, #println, #prog_name, #puts, #puts_error, #raise_args_error, #raise_command_error, #render_response, #run_command_for_each_arg, #subcommand_aliases, #subcommand_description, #subcommand_usage, #subcommands, #usage, #validate_outfile, #verify_args!, #visible_subcommands

Instance Method Details

#_get(id, options) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/morpheus/cli/commands/library_forms_command.rb', line 102

def _get(id, options)
  params = {}
  params.merge!(parse_query_options(options))
  if id.to_s !~ /\A\d{1,}\Z/
    # option_type_form = find_by_name(:option_type_form, id)
    option_type_form = find_option_type_form_by_name_or_id(id)
    return 1, "Form not found" if option_type_form.nil?
    id = option_type_form['id']
  end
  @option_type_forms_interface.setopts(options)
  if options[:dry_run]
    print_dry_run @option_type_forms_interface.dry.get(id.to_i, params)
    return
  end
  json_response = @option_type_forms_interface.get(id.to_i, params)
  render_response(json_response, options, rest_object_key) do
    option_type_form = json_response[rest_object_key]
    print_h1 "Form Details", options
    print cyan
    print_description_list({
      "ID" => 'id',
      "Name" => 'name',
      "Code" => 'code',
      "Description" => 'description',
      "Labels" => lambda {|it| format_list(it['labels']) },
    }, option_type_form, options)
    
    # show option types
    print_h2 "Form Inputs"
    #print format_option_types_table(option_type_form['options'], options, nil, true)
    print format_option_types_table(option_type_form['options'], options, 'config.customOptions')
    print reset,"\n"

    # show Field Groups
    field_groups = option_type_form['fieldGroups']
    if field_groups && field_groups.size > 0
      field_groups.each do |field_group|
        print_h2 "#{field_group['name']}"
        #print format_option_types_table(field_group['options'], options, nil, true)
        if field_group['options'] && !field_group['options'].empty?
          print format_option_types_table(field_group['options'], options, 'config.customOptions')
          print reset,"\n"
        else
          print cyan, "This field group is empty", reset, "\n\n"
        end
      end
    end
  end
end

#add(args) ⇒ Object



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
# File 'lib/morpheus/cli/commands/library_forms_command.rb', line 152

def add(args)
  options = {}
  my_option_types = nil
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[name] [options]")
    build_option_type_options(opts, options, new_option_type_form_option_types())
    # maybe use --inputs for less confusion
    opts.on('--options OPTIONS', String, "List of option type inputs to add to the form, this list can include full JSON objects or just the id to use an existing option eg. --options '[5,6,7,{\"fieldName\":\"input\",\"fieldLabel\":\"Input\"}]'") do |val|
      val = "[#{val.strip}]" unless val.strip.to_s[0] == '[' && val.strip.to_s[-1] == ']'
      begin
        options[:selected_options] = JSON.parse(val)
      rescue
        raise_command_error "Failed to parse --options value '#{val}' as JSON"
      end
    end
    build_standard_add_options(opts, options)
    opts.footer = "Create a new form."
  end
  optparse.parse!(args)
  verify_args!(args:args, optparse:optparse, max:1)
  if args.count == 1
    options[:options]['name'] = args[0]
  end
  connect(options)
  parse_payload(options, rest_object_key) do |payload|
    form_payload = prompt_new_form(options)
    #form_payload.deep_compact!.booleanize! # remove empty values and convert checkbox "on" and "off" to true and false
    payload.deep_merge!({rest_object_key => form_payload})
    # cleanup payload
    # remove transient option params used for prompting for list of inputs
    payload[rest_object_key].keys.each do |k|
      if k == "option" || k.to_s =~ /^option\d+$/ || k == "fieldGroup" || k.to_s =~ /^fieldGroup\d+$/
        payload[rest_object_key].delete(k)
      end
    end
  end
  execute_api(@option_type_forms_interface, :create, [], options, rest_object_key) do |json_response|
    form = json_response[rest_object_key]
    print_green_success "Added form #{form['name']}"
    _get(form['id'], options)
  end
end

#connect(opts) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/morpheus/cli/commands/library_forms_command.rb', line 11

def connect(opts)
  @api_client = establish_remote_appliance_connection(opts)
  @library_instance_types_interface = @api_client.library_instance_types
  @provision_types_interface = @api_client.provision_types
  @option_types_interface = @api_client.option_types
  @option_type_forms_interface = @api_client.option_type_forms
end

#get(args) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/morpheus/cli/commands/library_forms_command.rb', line 83

def get(args)
  options = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[form]")
    build_standard_get_options(opts, options)
    opts.footer = <<-EOT
Get details about a form.
[form] is required. This is the name or id of a form.
EOT
  end
  optparse.parse!(args)
  verify_args!(args:args, optparse:optparse, min:1)
  connect(options)
  id_list = parse_id_list(args)
  return run_command_for_each_arg(id_list) do |arg|
    _get(arg, options)
  end
end

#handle(args) ⇒ Object



19
20
21
# File 'lib/morpheus/cli/commands/library_forms_command.rb', line 19

def handle(args)
  handle_subcommand(args)
end

#list(args) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/morpheus/cli/commands/library_forms_command.rb', line 23

def list(args)
  options = {}
  params = {}
  optparse = Morpheus::Cli::OptionParser.new do|opts|
    opts.banner = subcommand_usage()
    opts.on('-l', '--labels LABEL', String, "Filter by labels, can match any of the values") do |val|
      add_query_parameter(params, 'labels', parse_labels(val))
    end
    opts.on('--all-labels LABEL', String, "Filter by labels, must match all of the values") do |val|
      add_query_parameter(params, 'allLabels', parse_labels(val))
    end
    build_standard_list_options(opts, options)
    opts.footer = "List option forms."
  end
  optparse.parse!(args)
  # verify_args!(args:args, optparse:optparse, count:0)
  if args.count > 0
    options[:phrase] = args.join(" ")
  end
  connect(options)
  params.merge!(parse_list_options(options))
  @option_type_forms_interface.setopts(options)
  if options[:dry_run]
    print_dry_run @option_type_forms_interface.dry.list(params)
    return
  end
  json_response = @option_type_forms_interface.list(params)
  render_response(json_response, options, rest_list_key) do
    option_type_forms = json_response[rest_list_key]
    subtitles = []
    subtitles += parse_list_subtitles(options)
    print_h1 "Morpheus Forms", subtitles, options
    if option_type_forms.empty?
      print cyan,"No forms found.",reset,"\n"
    else
      rows = option_type_forms.collect do |option_type_form|
        {
          id: option_type_form['id'],
          name: option_type_form['name'],
          code: option_type_form['code'],
          labels: option_type_form['labels'],
          description: option_type_form['description'],
        }
      end
        columns = [
        :id,
        :name,
        :code,
        {:labels => {:display_method => lambda {|it| format_list(it[:labels], '', 3) rescue '' }}},
        :description,
      ]
      print cyan
      print as_pretty_table(rows, columns, options)
      print reset
      print_results_pagination(json_response)
    end
    print reset,"\n"
  end
end

#remove(args) ⇒ Object



227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/morpheus/cli/commands/library_forms_command.rb', line 227

def remove(args)
  options = {}
  params = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[form]")
    build_standard_remove_options(opts, options)
    opts.footer = <<-EOT
Delete a form.
[form] is required. This is the name or id of a form.
EOT
  end
  optparse.parse!(args)
  verify_args!(args:args, optparse:optparse, count:1)
  connect(options)
  form = find_option_type_form_by_name_or_id(args[0])
  return 1, "Form not found" if form.nil?
  parse_options(options, params)
  confirm!("Are you sure you want to delete the form #{form['name']}?", options)
  execute_api(@option_type_forms_interface, :destroy, [form['id']], options) do |json_response|
    print_green_success "Removed form #{form['name']}"
  end
end

#update(args) ⇒ Object



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
# File 'lib/morpheus/cli/commands/library_forms_command.rb', line 195

def update(args)
  options = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[form] [options]")
    build_option_type_options(opts, options, update_option_type_form_option_types())
    build_standard_update_options(opts, options)
    opts.footer = <<-EOT
Update a form.
[form] is required. This is the name or id of a form.
EOT
  end
  optparse.parse!(args)
  verify_args!(args:args, optparse:optparse, count:1)
  connect(options)
  form = find_option_type_form_by_name_or_id(args[0])
  return 1, "Form not found" if form.nil?
  parse_payload(options, rest_object_key) do |payload|
    option_types = update_option_type_form_option_types()
    v_prompt = no_prompt(option_types, options)
    #v_prompt.deep_compact!.booleanize! # remove empty values and convert checkbox "on" and "off" to true and false
    payload.deep_merge!({rest_object_key => v_prompt})
    if payload[rest_object_key].empty? # || options[:no_prompt]
      raise_command_error "Specify at least one option to update.\n#{optparse}"
    end
  end
  execute_api(@option_type_forms_interface, :update, [form['id']], options, 'backup') do |json_response|
    form = json_response[rest_object_key]
    print_green_success "Updated form #{form['name']}"
    _get(form['id'], options.merge(params:{}))
  end
end