morpheus-cli 2.10.0 → 2.10.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (80) hide show
  1. checksums.yaml +4 -4
  2. data/bin/morpheus +27 -32
  3. data/lib/morpheus/api/accounts_interface.rb +36 -47
  4. data/lib/morpheus/api/api_client.rb +141 -110
  5. data/lib/morpheus/api/app_templates_interface.rb +56 -72
  6. data/lib/morpheus/api/apps_interface.rb +111 -132
  7. data/lib/morpheus/api/auth_interface.rb +30 -0
  8. data/lib/morpheus/api/clouds_interface.rb +71 -76
  9. data/lib/morpheus/api/custom_instance_types_interface.rb +21 -46
  10. data/lib/morpheus/api/dashboard_interface.rb +10 -17
  11. data/lib/morpheus/api/deploy_interface.rb +60 -72
  12. data/lib/morpheus/api/deployments_interface.rb +53 -71
  13. data/lib/morpheus/api/groups_interface.rb +55 -45
  14. data/lib/morpheus/api/instance_types_interface.rb +19 -23
  15. data/lib/morpheus/api/instances_interface.rb +179 -177
  16. data/lib/morpheus/api/key_pairs_interface.rb +11 -17
  17. data/lib/morpheus/api/license_interface.rb +18 -23
  18. data/lib/morpheus/api/load_balancers_interface.rb +54 -69
  19. data/lib/morpheus/api/logs_interface.rb +25 -29
  20. data/lib/morpheus/api/options_interface.rb +13 -17
  21. data/lib/morpheus/api/provision_types_interface.rb +19 -22
  22. data/lib/morpheus/api/roles_interface.rb +75 -94
  23. data/lib/morpheus/api/security_group_rules_interface.rb +28 -37
  24. data/lib/morpheus/api/security_groups_interface.rb +39 -51
  25. data/lib/morpheus/api/servers_interface.rb +113 -115
  26. data/lib/morpheus/api/setup_interface.rb +31 -0
  27. data/lib/morpheus/api/task_sets_interface.rb +36 -38
  28. data/lib/morpheus/api/tasks_interface.rb +56 -69
  29. data/lib/morpheus/api/users_interface.rb +67 -76
  30. data/lib/morpheus/api/virtual_images_interface.rb +61 -61
  31. data/lib/morpheus/api/whoami_interface.rb +12 -15
  32. data/lib/morpheus/cli.rb +71 -60
  33. data/lib/morpheus/cli/accounts.rb +254 -315
  34. data/lib/morpheus/cli/alias_command.rb +219 -0
  35. data/lib/morpheus/cli/app_templates.rb +264 -272
  36. data/lib/morpheus/cli/apps.rb +608 -671
  37. data/lib/morpheus/cli/cli_command.rb +259 -21
  38. data/lib/morpheus/cli/cli_registry.rb +99 -14
  39. data/lib/morpheus/cli/clouds.rb +599 -372
  40. data/lib/morpheus/cli/config_file.rb +126 -0
  41. data/lib/morpheus/cli/credentials.rb +141 -117
  42. data/lib/morpheus/cli/dashboard_command.rb +48 -56
  43. data/lib/morpheus/cli/deployments.rb +254 -268
  44. data/lib/morpheus/cli/deploys.rb +150 -142
  45. data/lib/morpheus/cli/error_handler.rb +38 -0
  46. data/lib/morpheus/cli/groups.rb +551 -179
  47. data/lib/morpheus/cli/hosts.rb +862 -617
  48. data/lib/morpheus/cli/instance_types.rb +103 -95
  49. data/lib/morpheus/cli/instances.rb +1335 -1009
  50. data/lib/morpheus/cli/key_pairs.rb +82 -90
  51. data/lib/morpheus/cli/library.rb +498 -499
  52. data/lib/morpheus/cli/license.rb +83 -101
  53. data/lib/morpheus/cli/load_balancers.rb +314 -300
  54. data/lib/morpheus/cli/login.rb +66 -44
  55. data/lib/morpheus/cli/logout.rb +47 -46
  56. data/lib/morpheus/cli/mixins/accounts_helper.rb +69 -31
  57. data/lib/morpheus/cli/mixins/infrastructure_helper.rb +106 -0
  58. data/lib/morpheus/cli/mixins/print_helper.rb +181 -17
  59. data/lib/morpheus/cli/mixins/provisioning_helper.rb +535 -458
  60. data/lib/morpheus/cli/mixins/whoami_helper.rb +2 -2
  61. data/lib/morpheus/cli/option_parser.rb +35 -0
  62. data/lib/morpheus/cli/option_types.rb +232 -192
  63. data/lib/morpheus/cli/recent_activity_command.rb +61 -65
  64. data/lib/morpheus/cli/remote.rb +446 -199
  65. data/lib/morpheus/cli/roles.rb +884 -906
  66. data/lib/morpheus/cli/security_group_rules.rb +213 -203
  67. data/lib/morpheus/cli/security_groups.rb +237 -192
  68. data/lib/morpheus/cli/shell.rb +338 -231
  69. data/lib/morpheus/cli/tasks.rb +326 -308
  70. data/lib/morpheus/cli/users.rb +457 -462
  71. data/lib/morpheus/cli/version.rb +1 -1
  72. data/lib/morpheus/cli/version_command.rb +16 -18
  73. data/lib/morpheus/cli/virtual_images.rb +526 -345
  74. data/lib/morpheus/cli/whoami.rb +125 -111
  75. data/lib/morpheus/cli/workflows.rb +338 -185
  76. data/lib/morpheus/formatters.rb +8 -1
  77. data/lib/morpheus/logging.rb +1 -1
  78. data/lib/morpheus/rest_client.rb +17 -8
  79. metadata +9 -3
  80. data/lib/morpheus/api/custom_instance_types.rb +0 -55
@@ -5,112 +5,94 @@ require 'table_print'
5
5
  require 'morpheus/cli/cli_command'
6
6
 
7
7
  class Morpheus::Cli::License
8
- include Morpheus::Cli::CliCommand
8
+ include Morpheus::Cli::CliCommand
9
9
 
10
- def initialize()
11
- @appliance_name, @appliance_url = Morpheus::Cli::Remote.active_appliance
12
- end
10
+ register_subcommands :get, :apply
11
+ alias_subcommand :details, :get
13
12
 
14
- def connect(opts)
15
- if opts[:remote]
16
- @appliance_url = opts[:remote]
17
- @appliance_name = opts[:remote]
18
- @access_token = Morpheus::Cli::Credentials.new(@appliance_name,@appliance_url).request_credentials(opts)
19
- else
20
- @access_token = Morpheus::Cli::Credentials.new(@appliance_name,@appliance_url).request_credentials(opts)
21
- end
22
- @api_client = Morpheus::APIClient.new(@access_token,nil,nil, @appliance_url)
23
- @license_interface = Morpheus::APIClient.new(@access_token,nil,nil, @appliance_url).license
24
-
25
- if @access_token.empty?
26
- print_red_alert "Invalid Credentials. Unable to acquire access token. Please verify your credentials and try again."
27
- exit 1
28
- end
29
- end
13
+ def initialize()
14
+ # @appliance_name, @appliance_url = Morpheus::Cli::Remote.active_appliance
15
+ end
30
16
 
17
+ def connect(opts)
18
+ @api_client = establish_remote_appliance_connection(opts)
19
+ @api_client = Morpheus::APIClient.new(@access_token,nil,nil, @appliance_url)
20
+ @license_interface = Morpheus::APIClient.new(@access_token,nil,nil, @appliance_url).license
21
+ end
31
22
 
32
- def handle(args)
33
- if args.empty?
34
- puts "\nUsage: morpheus license [details, apply]\n\n"
35
- return
36
- end
23
+ def handle(args)
24
+ handle_subcommand(args)
25
+ end
37
26
 
38
- case args[0]
39
- when 'apply'
40
- apply(args[1..-1])
41
- when 'details'
42
- details(args[1..-1])
43
- else
44
- puts "\nUsage: morpheus license [details, apply]\n\n"
45
- exit 127
46
- end
47
- end
27
+ def get(args)
28
+ options = {}
29
+ optparse = OptionParser.new do|opts|
30
+ opts.banner = subcommand_usage()
31
+ build_common_options(opts, options, [:json, :dry_run, :remote])
32
+ end
33
+ optparse.parse!(args)
34
+ connect(options)
35
+ begin
36
+ if options[:dry_run]
37
+ print_dry_run @license_interface.dry.get()
38
+ return
39
+ end
40
+ license = @license_interface.get()
41
+ if options[:json]
42
+ puts JSON.pretty_generate(license)
43
+ else
44
+ if license['license'].nil?
45
+ puts "No License Currently Applied to the appliance."
46
+ exit 1
47
+ else
48
+ print "\n", cyan, "License\n=======\n"
49
+ max_memory = Filesize.from("#{license['license']['maxMemory']} B").pretty
50
+ max_storage = Filesize.from("#{license['license']['maxStorage']} B").pretty
51
+ used_memory = Filesize.from("#{license['usedMemory']} B").pretty
52
+ puts "Account: #{license['license']['accountName']}"
53
+ puts "Start Date: #{license['license']['startDate']}"
54
+ puts "End Date: #{license['license']['endDate']}"
55
+ puts "Memory: #{used_memory} / #{max_memory}"
56
+ puts "Max Storage: #{max_storage}"
57
+ end
58
+ print reset,"\n"
59
+ end
60
+ rescue RestClient::Exception => e
61
+ print_rest_exception(e, options)
62
+ return false
63
+ end
64
+ end
48
65
 
49
-
50
- def details(args)
51
- options = {}
52
- optparse = OptionParser.new do|opts|
53
- opts.banner = "Usage: morpheus license details"
54
- build_common_options(opts, options, [:json, :remote])
55
- end
56
- optparse.parse(args)
57
- connect(options)
58
- begin
59
- license = @license_interface.get()
60
-
61
- if options[:json]
62
- puts JSON.pretty_generate(license)
63
- else
64
- if license['license'].nil?
65
- puts "No License Currently Applied to the appliance."
66
- exit 1
67
- else
68
- print "\n", cyan, "License\n=======\n"
69
- max_memory = Filesize.from("#{license['license']['maxMemory']} B").pretty
70
- max_storage = Filesize.from("#{license['license']['maxStorage']} B").pretty
71
- used_memory = Filesize.from("#{license['usedMemory']} B").pretty
72
- puts "Account: #{license['license']['accountName']}"
73
- puts "Start Date: #{license['license']['startDate']}"
74
- puts "End Date: #{license['license']['endDate']}"
75
- puts "Memory: #{used_memory} / #{max_memory}"
76
- puts "Max Storage: #{max_storage}"
77
- end
78
- print reset,"\n\n"
79
- end
80
- rescue RestClient::Exception => e
81
- print_rest_exception(e, options)
82
- exit 1
83
- end
84
- end
85
-
86
- def apply(args)
87
- key = args[0]
88
- options = {}
89
- account_name = nil
90
- optparse = OptionParser.new do|opts|
91
- opts.banner = "Usage: morpheus license apply [key]"
92
- build_common_options(opts, options, [:json, :remote])
93
- end
94
- if args.count < 1
95
- puts "\n#{optparse.banner}\n\n"
96
- exit 1
97
- end
98
- optparse.parse(args)
99
-
100
- connect(options)
101
-
102
- begin
103
- license_results = @license_interface.apply(key)
104
-
105
- if options[:json]
106
- puts JSON.pretty_generate(license_results)
107
- else
108
- puts "License applied successfully!"
109
- end
110
- rescue RestClient::Exception => e
111
- print_rest_exception(e, options)
112
- exit 1
113
- end
114
- end
66
+ def apply(args)
67
+ options = {}
68
+ account_name = nil
69
+ optparse = OptionParser.new do|opts|
70
+ opts.banner = subcommand_usage("[key]")
71
+ build_common_options(opts, options, [:json, :dry_run, :remote])
72
+ end
73
+ optparse.parse!(args)
74
+ connect(options)
75
+ begin
76
+ if args[0]
77
+ key = args[0]
78
+ else
79
+ v_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldName' => 'licenseKey', 'fieldLabel' => 'License Key', 'type' => 'text', 'required' => true}], options[:options])
80
+ instance_name = v_prompt['licenseKey'] || ''
81
+ end
82
+ if options[:dry_run]
83
+ print_dry_run @license_interface.dry.apply(key)
84
+ return
85
+ end
86
+ license_results = @license_interface.apply(key)
87
+ if options[:json]
88
+ puts JSON.pretty_generate(license_results)
89
+ else
90
+ puts "License applied successfully!"
91
+ end
92
+ rescue RestClient::Exception => e
93
+ print_rest_exception(e, options)
94
+ return false
95
+ end
96
+ end
115
97
 
116
98
  end
@@ -6,331 +6,345 @@ require 'table_print'
6
6
  require 'morpheus/cli/cli_command'
7
7
 
8
8
  class Morpheus::Cli::LoadBalancers
9
- include Morpheus::Cli::CliCommand
9
+ include Morpheus::Cli::CliCommand
10
10
 
11
- def initialize()
12
- @appliance_name, @appliance_url = Morpheus::Cli::Remote.active_appliance
13
- end
11
+ register_subcommands :list, :get, :add, :update, :remove, {:types => :lb_types}
12
+ alias_subcommand :details, :get
14
13
 
15
- def connect(opts)
16
- if opts[:remote]
17
- @appliance_url = opts[:remote]
18
- @appliance_name = opts[:remote]
19
- @access_token = Morpheus::Cli::Credentials.new(@appliance_name,@appliance_url).request_credentials(opts)
20
- else
21
- @access_token = Morpheus::Cli::Credentials.new(@appliance_name,@appliance_url).request_credentials(opts)
22
- end
23
- @api_client = Morpheus::APIClient.new(@access_token,nil,nil, @appliance_url)
24
- @load_balancers_interface = Morpheus::APIClient.new(@access_token,nil,nil, @appliance_url).load_balancers
25
-
26
- if @access_token.empty?
27
- print_red_alert "Invalid Credentials. Unable to acquire access token. Please verify your credentials and try again."
28
- exit 1
29
- end
30
- end
14
+ def initialize()
15
+ # @appliance_name, @appliance_url = Morpheus::Cli::Remote.active_appliance
16
+ end
31
17
 
18
+ def connect(opts)
19
+ @api_client = establish_remote_appliance_connection(opts)
20
+ @load_balancers_interface = Morpheus::APIClient.new(@access_token,nil,nil, @appliance_url).load_balancers
21
+ end
32
22
 
33
- def handle(args)
34
- if args.empty?
35
- puts "\nUsage: morpheus load-balancers [list,add, update,remove, details, lb-types]\n\n"
36
- return
37
- end
38
23
 
39
- case args[0]
40
- when 'list'
41
- list(args[1..-1])
42
- when 'add'
43
- add(args[1..-1])
44
- when 'update'
45
- # update(args[1..-1])
46
- when 'details'
47
- details(args[1..-1])
48
- when 'remove'
49
- remove(args[1..-1])
50
- when 'lb-types'
51
- lb_types(args[1..-1])
52
- else
53
- puts "\nUsage: morpheus load-balancers [list,add, update,remove, details, lb-types]\n\n"
54
- exit 127
55
- end
56
- end
24
+ def handle(args)
25
+ handle_subcommand(args)
26
+ end
57
27
 
58
- def list(args)
59
- options = {}
60
- optparse = OptionParser.new do|opts|
61
- opts.banner = "Usage: morpheus load-balancers list [-s] [-o] [-m]"
62
- build_common_options(opts, options, [:list, :json, :remote])
63
- end
64
- optparse.parse(args)
65
- connect(options)
66
- begin
67
- params = {}
68
- [:phrase, :offset, :max, :sort, :direction].each do |k|
69
- params[k] = options[k] unless options[k].nil?
70
- end
71
- json_response = @load_balancers_interface.get(params)
72
- if options[:json]
73
- print JSON.pretty_generate(json_response)
74
- else
75
- lbs = json_response['loadBalancers']
76
- print "\n" ,cyan, bold, "Morpheus Load Balancers\n","==================", reset, "\n\n"
77
- if lbs.empty?
78
- puts yellow,"No load balancers currently configured.",reset
79
- else
80
- print cyan
81
- lb_table_data = lbs.collect do |lb|
82
- {name: lb['name'], id: lb['id'], type: lb['type']['name']}
83
- end
84
- tp lb_table_data, :id, :name, :type
85
- end
86
- print reset,"\n\n"
87
- end
88
-
89
-
90
- rescue RestClient::Exception => e
91
- print_rest_exception(e, options)
92
- exit 1
93
- end
94
- end
28
+ def list(args)
29
+ options = {}
30
+ optparse = OptionParser.new do|opts|
31
+ opts.banner = subcommand_usage()
32
+ build_common_options(opts, options, [:list, :json, :dry_run, :remote])
33
+ end
34
+ optparse.parse!(args)
35
+ connect(options)
36
+ begin
37
+ params = {}
38
+ [:phrase, :offset, :max, :sort, :direction].each do |k|
39
+ params[k] = options[k] unless options[k].nil?
40
+ end
41
+ if options[:dry_run]
42
+ print_dry_run @load_balancers_interface.dry.get(params)
43
+ return
44
+ end
45
+ json_response = @load_balancers_interface.get(params)
46
+ if options[:json]
47
+ print JSON.pretty_generate(json_response)
48
+ else
49
+ lbs = json_response['loadBalancers']
50
+ print "\n" ,cyan, bold, "Morpheus Load Balancers\n","==================", reset, "\n\n"
51
+ if lbs.empty?
52
+ puts yellow,"No load balancers currently configured.",reset
53
+ else
54
+ print cyan
55
+ lb_table_data = lbs.collect do |lb|
56
+ {name: lb['name'], id: lb['id'], type: lb['type']['name']}
57
+ end
58
+ tp lb_table_data, :id, :name, :type
59
+ end
60
+ print reset,"\n"
61
+ end
62
+ rescue RestClient::Exception => e
63
+ print_rest_exception(e, options)
64
+ exit 1
65
+ end
66
+ end
95
67
 
96
- def details(args)
97
- lb_name = args[0]
98
- options = {}
99
- optparse = OptionParser.new do|opts|
100
- opts.banner = "Usage: morpheus load-balancers details [name]"
101
- build_common_options(opts, options, [:json, :remote])
102
- end
103
- if args.count < 1
104
- puts "\n#{optparse.banner}\n\n"
105
- exit 1
106
- end
107
- optparse.parse(args)
108
- connect(options)
109
- begin
110
- lb = find_lb_by_name(lb_name)
68
+ def get(args)
69
+ options = {}
70
+ optparse = OptionParser.new do|opts|
71
+ opts.banner = subcommand_usage("[name]")
72
+ build_common_options(opts, options, [:json, :dry_run, :remote])
73
+ end
74
+ optparse.parse!(args)
75
+ if args.count < 1
76
+ puts optparse
77
+ exit 1
78
+ end
79
+ lb_name = args[0]
80
+ connect(options)
81
+ begin
82
+ if options[:dry_run]
83
+ if lb_name.to_s =~ /\A\d{1,}\Z/
84
+ print_dry_run @load_balancers_interface.dry.get(lb_name.to_i)
85
+ else
86
+ print_dry_run @load_balancers_interface.dry.get({name:lb_name})
87
+ end
88
+ return
89
+ end
90
+ lb = find_lb_by_name_or_id(lb_name)
91
+ exit 1 if lb.nil?
92
+ lb_type = load_balancer_type_for_name_or_id(lb['type']['code'])
93
+ if options[:json]
94
+ puts JSON.pretty_generate({loadBalancer: lb})
95
+ else
96
+ print "\n", cyan, "Lb #{lb['name']} - #{lb['type']['name']}\n\n"
97
+ # lb_type['optionTypes'].sort { |x,y| x['displayOrder'].to_i <=> y['displayOrder'].to_i }.each do |optionType|
98
+ # puts " #{optionType['fieldLabel']} : " + (optionType['type'] == 'password' ? "#{task['taskOptions'][optionType['fieldName']] ? '************' : ''}" : "#{task['taskOptions'][optionType['fieldName']] || optionType['defaultValue']}")
99
+ # end
100
+ print reset,"\n\n"
101
+ end
102
+ rescue RestClient::Exception => e
103
+ print_rest_exception(e, options)
104
+ exit 1
105
+ end
106
+ end
111
107
 
112
- exit 1 if lb.nil?
113
- lb_type = find_lb_type_by_name(lb['type']['name'])
114
- if options[:json]
115
- puts JSON.pretty_generate({task:task})
116
- else
117
- print "\n", cyan, "Lb #{lb['name']} - #{lb['type']['name']}\n\n"
118
- lb_type['optionTypes'].sort { |x,y| x['displayOrder'].to_i <=> y['displayOrder'].to_i }.each do |optionType|
119
- puts " #{optionType['fieldLabel']} : " + (optionType['type'] == 'password' ? "#{task['taskOptions'][optionType['fieldName']] ? '************' : ''}" : "#{task['taskOptions'][optionType['fieldName']] || optionType['defaultValue']}")
120
- end
121
- print reset,"\n\n"
122
- end
123
- rescue RestClient::Exception => e
124
- print_rest_exception(e, options)
125
- exit 1
126
- end
127
- end
108
+ def update(args)
109
+ lb_name = args[0]
110
+ options = {}
111
+ account_name = nil
112
+ optparse = OptionParser.new do|opts|
113
+ opts.banner = subcommand_usage("[name] [options]")
114
+ build_common_options(opts, options, [:options, :json, :dry_run, :remote])
115
+ end
116
+ optparse.parse!(args)
117
+ if args.count < 1
118
+ puts optparse
119
+ exit 1
120
+ end
121
+ connect(options)
122
+ begin
128
123
 
129
- def update(args)
130
- lb_name = args[0]
131
- options = {}
132
- account_name = nil
133
- optparse = OptionParser.new do|opts|
134
- opts.banner = "Usage: morpheus tasks update [task] [options]"
135
- build_common_options(opts, options, [:options, :json, :remote])
136
- end
137
- if args.count < 1
138
- puts "\n#{optparse.banner}\n\n"
139
- exit 1
140
- end
141
- optparse.parse(args)
124
+ lb = find_lb_by_name_or_id(lb_name)
125
+ exit 1 if lb.nil?
126
+ lb_type = load_balancer_type_for_name_or_id(lb['type']['code'])
142
127
 
143
- connect(options)
144
-
145
- begin
128
+ #params = Morpheus::Cli::OptionTypes.prompt(add_load_balancer_option_types, options[:options], @api_client, options[:params]) # options[:params] is mysterious
129
+ params = options[:options] || {}
146
130
 
131
+ if params.empty?
132
+ puts optparse
133
+ option_lines = update_task_option_types(lb_type).collect {|it| "\t-O #{it['fieldContext'] ? (it['fieldContext'] + '.') : ''}#{it['fieldName']}=\"value\"" }.join("\n")
134
+ puts "\nAvailable Options:\n#{option_lines}\n\n"
135
+ exit 1
136
+ end
147
137
 
148
- task = find_task_by_name_or_code_or_id(lb_name)
149
- exit 1 if task.nil?
150
- lb_type = find_lb_type_by_name(task['type']['name'])
138
+ # todo: fix this...
139
+ #puts "parsed params is : #{params.inspect}"
140
+ lb_keys = ['name']
141
+ changes_payload = (params.select {|k,v| task_keys.include?(k) })
142
+ task_payload = task
143
+ if changes_payload
144
+ task_payload.merge!(changes_payload)
145
+ end
146
+ puts params
147
+ if params['taskOptions']
148
+ task_payload['taskOptions'].merge!(params['taskOptions'])
149
+ end
150
+ payload = {task: task_payload}
151
+ if options[:dry_run]
152
+ print_dry_run @load_balancers_interface.dry.update(task['id'], payload)
153
+ return
154
+ end
155
+ response = @load_balancers_interface.update(task['id'], payload)
156
+ if options[:json]
157
+ print JSON.pretty_generate(json_response)
158
+ if !response['success']
159
+ exit 1
160
+ end
161
+ else
162
+ print "\n", cyan, "Task #{response['task']['name']} updated", reset, "\n\n"
163
+ end
164
+ rescue RestClient::Exception => e
165
+ print_rest_exception(e, options)
166
+ exit 1
167
+ end
168
+ end
151
169
 
152
- #params = Morpheus::Cli::OptionTypes.prompt(add_user_option_types, options[:options], @api_client, options[:params]) # options[:params] is mysterious
153
- params = options[:options] || {}
154
170
 
155
- if params.empty?
156
- puts "\n#{optparse.banner}\n\n"
157
- option_lines = update_task_option_types(lb_type).collect {|it| "\t-O #{it['fieldContext'] ? (it['fieldContext'] + '.') : ''}#{it['fieldName']}=\"value\"" }.join("\n")
158
- puts "\nAvailable Options:\n#{option_lines}\n\n"
159
- exit 1
160
- end
171
+ def lb_types(args)
172
+ options = {}
173
+ optparse = OptionParser.new do|opts|
174
+ opts.banner = subcommand_usage()
175
+ build_common_options(opts, options, [:json, :dry_run, :remote])
176
+ end
177
+ optparse.parse!(args)
178
+ connect(options)
179
+ begin
180
+ if options[:dry_run]
181
+ print_dry_run @load_balancers_interface.dry.load_balancer_types()
182
+ return
183
+ end
184
+ json_response = @load_balancers_interface.load_balancer_types()
185
+ if options[:json]
186
+ print JSON.pretty_generate(json_response)
187
+ else
188
+ lb_types = json_response['loadBalancerTypes']
189
+ print "\n" ,cyan, bold, "Morpheus Load Balancer Types\n","============================", reset, "\n\n"
190
+ if lb_types.nil? || lb_types.empty?
191
+ puts yellow,"No lb types currently exist on this appliance. This could be a seed issue.",reset
192
+ else
193
+ print cyan
194
+ lb_table_data = lb_types.collect do |lb_type|
195
+ {name: lb_type['name'], id: lb_type['id'], code: lb_type['code']}
196
+ end
197
+ tp lb_table_data, :id, :name, :code
198
+ end
161
199
 
162
- #puts "parsed params is : #{params.inspect}"
163
- task_keys = ['name']
164
- changes_payload = (params.select {|k,v| task_keys.include?(k) })
165
- task_payload = task
166
- if changes_payload
167
- task_payload.merge!(changes_payload)
168
- end
169
- puts params
170
- if params['taskOptions']
171
- task_payload['taskOptions'].merge!(params['taskOptions'])
172
- end
200
+ print reset,"\n"
201
+ end
202
+ rescue RestClient::Exception => e
203
+ print_rest_exception(e, options)
204
+ exit 1
205
+ end
206
+ end
173
207
 
174
- request_payload = {task: task_payload}
175
- response = @load_balancers_interface.update(task['id'], request_payload)
176
- if options[:json]
177
- print JSON.pretty_generate(json_response)
178
- if !response['success']
179
- exit 1
180
- end
181
- else
182
- print "\n", cyan, "Task #{response['task']['name']} updated", reset, "\n\n"
183
- end
184
- rescue RestClient::Exception => e
185
- print_rest_exception(e, options)
186
- exit 1
187
- end
188
- end
208
+ # JD: This is broken.. copied from tasks? should optionTypes exist?
209
+ def add(args)
210
+ lb_type_name = nil
211
+ options = {}
212
+ optparse = OptionParser.new do|opts|
213
+ opts.banner = subcommand_usage("[name] -t LB_TYPE")
214
+ opts.on( '-t', '--type LB_TYPE', "Load Balancer Type" ) do |val|
215
+ lb_type_name = val
216
+ end
217
+ build_common_options(opts, options, [:options, :json, :dry_run, :remote])
218
+ end
219
+ optparse.parse!(args)
220
+ lb_name = args[0]
221
+ if args.count < 1
222
+ puts optparse
223
+ exit 1
224
+ end
225
+ if lb_type_name.nil?
226
+ puts optparse
227
+ exit 1
228
+ end
229
+ connect(options)
230
+ begin
231
+ lb_type = load_balancer_type_for_name_or_id(lb_type_name)
232
+ if lb_type.nil?
233
+ print_red_alert "LB Type #{lb_type_name} not found!"
234
+ exit 1
235
+ end
189
236
 
237
+ payload = {loadBalancer: {name: lb_name, type: {code: lb_type['code'], id: lb_type['id']}}}
190
238
 
191
- def lb_types(args)
192
- options = {}
193
- optparse = OptionParser.new do|opts|
194
- opts.banner = "Usage: morpheus load-balancers lb-types"
195
- build_common_options(opts, options, [:json, :remote])
196
- end
197
- optparse.parse(args)
198
- connect(options)
199
- begin
200
- json_response = @load_balancers_interface.load_balancer_types()
201
- if options[:json]
202
- print JSON.pretty_generate(json_response)
203
- else
204
- lb_types = json_response['loadBalancerTypes']
205
- print "\n" ,cyan, bold, "Morpheus Load Balancer Types\n","============================", reset, "\n\n"
206
- if lb_types.nil? || lb_types.empty?
207
- puts yellow,"No lb types currently exist on this appliance. This could be a seed issue.",reset
208
- else
209
- print cyan
210
- lb_table_data = lb_types.collect do |lb_type|
211
- {name: lb_type['name'], id: lb_type['id'], code: lb_type['code']}
212
- end
213
- tp lb_table_data, :id, :name, :code
214
- end
239
+ # todo: The options available here are specific by type...
240
+ #input_options = Morpheus::Cli::OptionTypes.prompt(lb_type['optionTypes'],options[:options],@api_client, options[:params])
241
+ json_response = @load_balancers_interface.create(payload)
242
+ if options[:json]
243
+ print JSON.pretty_generate(json_response)
244
+ else
245
+ print "\n", cyan, "LB #{json_response['loadBalancer']['name']} created successfully", reset, "\n\n"
246
+ end
247
+ rescue RestClient::Exception => e
248
+ print_rest_exception(e, options)
249
+ exit 1
250
+ end
251
+ end
215
252
 
216
- print reset,"\n\n"
217
- end
218
-
219
-
220
- rescue RestClient::Exception => e
221
- print_rest_exception(e, options)
222
- exit 1
223
- end
224
- end
253
+ def remove(args)
254
+ lb_name = args[0]
255
+ options = {}
256
+ optparse = OptionParser.new do|opts|
257
+ opts.banner = subcommand_usage("[name]")
258
+ build_common_options(opts, options, [:auto_confirm, :json, :dry_run, :remote])
259
+ end
260
+ optparse.parse!(args)
261
+ if args.count < 1
262
+ puts optparse
263
+ exit 1
264
+ end
265
+ connect(options)
266
+ begin
267
+ lb = find_lb_by_name_or_id(lb_name)
268
+ exit 1 if lb.nil?
269
+ unless options[:yes] || Morpheus::Cli::OptionTypes.confirm("Are you sure you want to delete the load balancer #{lb['name']}?")
270
+ exit
271
+ end
272
+ if options[:dry_run]
273
+ print_dry_run @load_balancers_interface.dry.destroy(lb['id'])
274
+ return
275
+ end
276
+ json_response = @load_balancers_interface.destroy(lb['id'])
277
+ if options[:json]
278
+ print JSON.pretty_generate(json_response), "\n"
279
+ else
280
+ print "\n", cyan, "Load Balancer #{lb['name']} removed", reset, "\n\n"
281
+ end
282
+ rescue RestClient::Exception => e
283
+ print_rest_exception(e, options)
284
+ exit 1
285
+ end
286
+ end
225
287
 
226
- def add(args)
227
- lb_name = args[0]
228
- lb_type_name = nil
229
- options = {}
230
- optparse = OptionParser.new do|opts|
231
- opts.banner = "Usage: morpheus load-balancers add [lb] -t LB_TYPE"
232
- opts.on( '-t', '--type LB_TYPE', "Lb Type" ) do |val|
233
- lb_type_name = val
234
- end
235
- build_common_options(opts, options, [:options, :json, :remote])
236
- end
237
- if args.count < 1
238
- puts "\n#{optparse.banner}\n\n"
239
- exit 1
240
- end
241
- optparse.parse(args)
242
- connect(options)
243
288
 
244
- if lb_type_name.nil?
245
- puts "LB Type must be specified...\n#{optparse.banner}"
246
- exit 1
247
- end
289
+ private
248
290
 
249
- lb_type = find_lb_type_by_name(lb_type_name)
250
- if lb_type.nil?
251
- puts "LB Type not found!"
252
- exit 1
253
- end
254
- input_options = Morpheus::Cli::OptionTypes.prompt(lb_type['optionTypes'],options[:options],@api_client, options[:params])
255
- payload = {task: {name: lb_name, taskOptions: input_options['taskOptions'], type: {code: lb_type['code'], id: lb_type['id']}}}
256
- begin
257
- json_response = @load_balancers_interface.create(payload)
258
- if options[:json]
259
- print JSON.pretty_generate(json_response)
260
- else
261
- print "\n", cyan, "LB #{json_response['loadBalancer']['name']} created successfully", reset, "\n\n"
262
- end
263
- rescue RestClient::Exception => e
264
- print_rest_exception(e, options)
265
- exit 1
266
- end
267
- end
291
+ def find_lb_by_name_or_id(val)
292
+ if val.to_s =~ /\A\d{1,}\Z/
293
+ return find_lb_by_id(val)
294
+ else
295
+ return find_lb_by_name(val)
296
+ end
297
+ end
268
298
 
269
- def remove(args)
270
- lb_name = args[0]
271
- options = {}
272
- optparse = OptionParser.new do|opts|
273
- opts.banner = "Usage: morpheus load-balancers remove [name]"
274
- build_common_options(opts, options, [:auto_confirm, :json, :remote])
275
- end
276
- if args.count < 1
277
- puts "\n#{optparse.banner}\n\n"
278
- exit 1
279
- end
280
- optparse.parse(args)
281
- connect(options)
282
- begin
283
- lb = find_lb_by_name(lb_name)
284
- exit 1 if lb.nil?
285
- unless options[:yes] || Morpheus::Cli::OptionTypes.confirm("Are you sure you want to delete the load balancer #{lb['name']}?")
286
- exit
287
- end
288
- json_response = @load_balancers_interface.destroy(lb['id'])
289
- if options[:json]
290
- print JSON.pretty_generate(json_response)
291
- else
292
- print "\n", cyan, "Load Balancer #{lb['name']} removed", reset, "\n\n"
293
- end
294
- rescue RestClient::Exception => e
295
- print_rest_exception(e, options)
296
- exit 1
297
- end
298
- end
299
+ def find_lb_by_id(id)
300
+ begin
301
+ json_response = @load_balancers_interface.get(id.to_i)
302
+ return json_response['loadBalancer']
303
+ rescue RestClient::Exception => e
304
+ if e.response && e.response.code == 404
305
+ print_red_alert "Load Balancer not found by id #{id}"
306
+ else
307
+ raise e
308
+ end
309
+ end
310
+ end
299
311
 
312
+ def find_lb_by_name(name)
313
+ lbs = @load_balancers_interface.get({name: name.to_s})['loadBalancers']
314
+ if lbs.empty?
315
+ print_red_alert "Load Balancer not found by name #{name}"
316
+ return nil
317
+ elsif lbs.size > 1
318
+ print_red_alert "#{lbs.size} load balancers found by name #{name}"
319
+ #print_lbs_table(lbs, {color: red})
320
+ print reset,"\n\n"
321
+ return nil
322
+ else
323
+ return lbs[0]
324
+ end
325
+ end
300
326
 
301
- private
302
- def find_lb_by_name(val)
303
- raise "find_lb_by_name passed a bad name: #{val.inspect}" if val.to_s == ''
304
- results = @load_balancers_interface.get(val)
305
- result = nil
306
- if !results['loadBalancers'].nil? && !results['loadBalancers'].empty?
307
- result = results['loadBalancers'][0]
308
- elsif val.to_i.to_s == val
309
- results = @load_balancers_interface.get(val.to_i)
310
- result = results['loadBalancer']
311
- end
312
- if result.nil?
313
- print_red_alert "LB not found by '#{val}'"
314
- return nil
315
- end
316
- return result
317
- end
327
+ def get_available_load_balancer_types(refresh=false)
328
+ if !@available_load_balancer_types || refresh
329
+ @available_load_balancer_types = @load_balancers_interface.load_balancer_types['loadBalancerTypes']
330
+ end
331
+ return @available_load_balancer_types
332
+ end
318
333
 
319
- def find_lb_type_by_name(val)
320
- raise "find_lb_type_by_name passed a bad name: #{val.inspect}" if val.to_s == ''
321
- results = @load_balancers_interface.load_balancer_types(val)
322
- result = nil
323
- if !results['loadBalancerTypes'].nil? && !results['loadBalancerTypes'].empty?
324
- result = results['loadBalancerTypes'][0]
325
- elsif val.to_i.to_s == val
326
- results = @load_balancers_interface.load_balancer_types(val.to_i)
327
- result = results['loadBalancerType']
328
- end
329
- if result.nil?
330
- print_red_alert "LB Type not found by '#{val}'"
331
- return nil
332
- end
333
- return result
334
- end
334
+ def load_balancer_type_for_name_or_id(val)
335
+ if val.to_s =~ /\A\d{1,}\Z/
336
+ return load_balancer_type_for_id(val)
337
+ else
338
+ return load_balancer_type_for_name(val)
339
+ end
340
+ end
341
+
342
+ def load_balancer_type_for_id(id)
343
+ return get_available_load_balancer_types().find { |z| z['id'].to_i == id.to_i}
344
+ end
345
+
346
+ def load_balancer_type_for_name(name)
347
+ return get_available_load_balancer_types().find { |z| z['name'].downcase == name.downcase || z['code'].downcase == name.downcase}
348
+ end
335
349
 
336
350
  end