morpheus-cli 3.6.31 → 3.6.32
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/morpheus/api/api_client.rb +8 -0
- data/lib/morpheus/api/apps_interface.rb +15 -0
- data/lib/morpheus/api/clouds_interface.rb +15 -0
- data/lib/morpheus/api/environments_interface.rb +54 -0
- data/lib/morpheus/api/groups_interface.rb +15 -0
- data/lib/morpheus/api/instances_interface.rb +15 -0
- data/lib/morpheus/api/servers_interface.rb +14 -0
- data/lib/morpheus/api/wiki_interface.rb +54 -0
- data/lib/morpheus/cli.rb +2 -0
- data/lib/morpheus/cli/apps.rb +209 -21
- data/lib/morpheus/cli/clouds.rb +209 -1
- data/lib/morpheus/cli/environments_command.rb +406 -0
- data/lib/morpheus/cli/groups.rb +207 -0
- data/lib/morpheus/cli/hosts.rb +208 -0
- data/lib/morpheus/cli/instances.rb +350 -36
- data/lib/morpheus/cli/key_pairs.rb +3 -3
- data/lib/morpheus/cli/library_container_types_command.rb +10 -5
- data/lib/morpheus/cli/mixins/provisioning_helper.rb +28 -3
- data/lib/morpheus/cli/networks_command.rb +12 -0
- data/lib/morpheus/cli/recent_activity_command.rb +9 -3
- data/lib/morpheus/cli/remote.rb +1 -1
- data/lib/morpheus/cli/reports_command.rb +60 -11
- data/lib/morpheus/cli/tenants_command.rb +43 -46
- data/lib/morpheus/cli/version.rb +1 -1
- data/lib/morpheus/cli/wiki_command.rb +461 -0
- metadata +6 -2
data/lib/morpheus/cli/clouds.rb
CHANGED
@@ -10,7 +10,9 @@ class Morpheus::Cli::Clouds
|
|
10
10
|
include Morpheus::Cli::CliCommand
|
11
11
|
include Morpheus::Cli::InfrastructureHelper
|
12
12
|
|
13
|
-
register_subcommands :list, :count, :get, :add, :update, :remove, :
|
13
|
+
register_subcommands :list, :count, :get, :add, :update, :remove, :security_groups, :apply_security_groups, :types => :list_cloud_types
|
14
|
+
register_subcommands :wiki, :update_wiki
|
15
|
+
#register_subcommands :firewall_disable, :firewall_enable
|
14
16
|
alias_subcommand :details, :get
|
15
17
|
set_default_subcommand :list
|
16
18
|
|
@@ -660,6 +662,205 @@ class Morpheus::Cli::Clouds
|
|
660
662
|
end
|
661
663
|
end
|
662
664
|
|
665
|
+
def wiki(args)
|
666
|
+
options = {}
|
667
|
+
params = {}
|
668
|
+
open_wiki_link = false
|
669
|
+
optparse = Morpheus::Cli::OptionParser.new do |opts|
|
670
|
+
opts.banner = subcommand_usage("[cloud]")
|
671
|
+
opts.on('--view', '--view', "View wiki page in web browser.") do
|
672
|
+
open_wiki_link = true
|
673
|
+
end
|
674
|
+
build_common_options(opts, options, [:json, :dry_run, :remote])
|
675
|
+
opts.footer = "View wiki page details for a cloud." + "\n" +
|
676
|
+
"[cloud] is required. This is the name or id of a cloud."
|
677
|
+
end
|
678
|
+
optparse.parse!(args)
|
679
|
+
if args.count != 1
|
680
|
+
puts_error "#{Morpheus::Terminal.angry_prompt}wrong number of arguments. Expected 1 and received #{args.count} #{args.inspect}\n#{optparse}"
|
681
|
+
return 1
|
682
|
+
end
|
683
|
+
connect(options)
|
684
|
+
|
685
|
+
begin
|
686
|
+
cloud = find_cloud_by_name_or_id(args[0])
|
687
|
+
return 1 if cloud.nil?
|
688
|
+
|
689
|
+
|
690
|
+
@clouds_interface.setopts(options)
|
691
|
+
if options[:dry_run]
|
692
|
+
print_dry_run @clouds_interface.dry.wiki(cloud["id"], params)
|
693
|
+
return
|
694
|
+
end
|
695
|
+
json_response = @clouds_interface.wiki(cloud["id"], params)
|
696
|
+
page = json_response['page']
|
697
|
+
|
698
|
+
render_result = render_with_format(json_response, options, 'page')
|
699
|
+
return 0 if render_result
|
700
|
+
|
701
|
+
if page
|
702
|
+
|
703
|
+
# my_terminal.exec("wiki get #{page['id']}")
|
704
|
+
|
705
|
+
print_h1 "Cloud Wiki Page: #{cloud['name']}"
|
706
|
+
# print_h1 "Wiki Page Details"
|
707
|
+
print cyan
|
708
|
+
|
709
|
+
print_description_list({
|
710
|
+
"Page ID" => 'id',
|
711
|
+
"Name" => 'name',
|
712
|
+
#"Category" => 'category',
|
713
|
+
#"Ref Type" => 'refType',
|
714
|
+
#"Ref ID" => 'refId',
|
715
|
+
#"Owner" => lambda {|it| it['account'] ? it['account']['name'] : '' },
|
716
|
+
"Created" => lambda {|it| format_local_dt(it['dateCreated']) },
|
717
|
+
"Created By" => lambda {|it| it['createdBy'] ? it['createdBy']['username'] : '' },
|
718
|
+
"Updated" => lambda {|it| format_local_dt(it['lastUpdated']) },
|
719
|
+
"Updated By" => lambda {|it| it['updatedBy'] ? it['updatedBy']['username'] : '' }
|
720
|
+
}, page)
|
721
|
+
print reset,"\n"
|
722
|
+
|
723
|
+
print_h2 "Page Content"
|
724
|
+
print cyan, page['content'], reset, "\n"
|
725
|
+
|
726
|
+
else
|
727
|
+
print "\n"
|
728
|
+
print cyan, "No wiki page found.", reset, "\n"
|
729
|
+
end
|
730
|
+
print reset,"\n"
|
731
|
+
|
732
|
+
if open_wiki_link
|
733
|
+
return view_wiki([args[0]])
|
734
|
+
end
|
735
|
+
|
736
|
+
return 0
|
737
|
+
rescue RestClient::Exception => e
|
738
|
+
print_rest_exception(e, options)
|
739
|
+
exit 1
|
740
|
+
end
|
741
|
+
end
|
742
|
+
|
743
|
+
def view_wiki(args)
|
744
|
+
params = {}
|
745
|
+
options = {}
|
746
|
+
optparse = Morpheus::Cli::OptionParser.new do |opts|
|
747
|
+
opts.banner = subcommand_usage("[id]")
|
748
|
+
build_common_options(opts, options, [:dry_run, :remote])
|
749
|
+
opts.footer = "View cloud wiki page in a web browser" + "\n" +
|
750
|
+
"[cloud] is required. This is the name or id of a cloud."
|
751
|
+
end
|
752
|
+
optparse.parse!(args)
|
753
|
+
if args.count != 1
|
754
|
+
raise_command_error "wrong number of arguments, expected 1 and got (#{args.count}) #{args.join(' ')}\n#{optparse}"
|
755
|
+
end
|
756
|
+
connect(options)
|
757
|
+
begin
|
758
|
+
cloud = find_cloud_by_name_or_id(args[0])
|
759
|
+
return 1 if cloud.nil?
|
760
|
+
|
761
|
+
link = "#{@appliance_url}/login/oauth-redirect?access_token=#{@access_token}\\&redirectUri=/infrastructure/clouds/#{cloud['id']}#!wiki"
|
762
|
+
|
763
|
+
open_command = nil
|
764
|
+
if RbConfig::CONFIG['host_os'] =~ /mswin|mingw|cygwin/
|
765
|
+
open_command = "start #{link}"
|
766
|
+
elsif RbConfig::CONFIG['host_os'] =~ /darwin/
|
767
|
+
open_command = "open #{link}"
|
768
|
+
elsif RbConfig::CONFIG['host_os'] =~ /linux|bsd/
|
769
|
+
open_command = "xdg-open #{link}"
|
770
|
+
end
|
771
|
+
|
772
|
+
if options[:dry_run]
|
773
|
+
puts "system: #{open_command}"
|
774
|
+
return 0
|
775
|
+
end
|
776
|
+
|
777
|
+
system(open_command)
|
778
|
+
|
779
|
+
return 0
|
780
|
+
rescue RestClient::Exception => e
|
781
|
+
print_rest_exception(e, options)
|
782
|
+
exit 1
|
783
|
+
end
|
784
|
+
end
|
785
|
+
|
786
|
+
def update_wiki(args)
|
787
|
+
options = {}
|
788
|
+
params = {}
|
789
|
+
optparse = Morpheus::Cli::OptionParser.new do |opts|
|
790
|
+
opts.banner = subcommand_usage("[cloud] [options]")
|
791
|
+
build_option_type_options(opts, options, update_wiki_page_option_types)
|
792
|
+
opts.on('--file FILE', "File containing the wiki content. This can be used instead of --content") do |filename|
|
793
|
+
full_filename = File.expand_path(filename)
|
794
|
+
if File.exists?(full_filename)
|
795
|
+
params['content'] = File.read(full_filename)
|
796
|
+
else
|
797
|
+
print_red_alert "File not found: #{full_filename}"
|
798
|
+
return 1
|
799
|
+
end
|
800
|
+
# use the filename as the name by default.
|
801
|
+
if !params['name']
|
802
|
+
params['name'] = File.basename(full_filename)
|
803
|
+
end
|
804
|
+
end
|
805
|
+
opts.on(nil, '--clear', "Clear current page content") do |val|
|
806
|
+
params['content'] = ""
|
807
|
+
end
|
808
|
+
build_common_options(opts, options, [:options, :payload, :json, :dry_run, :remote])
|
809
|
+
end
|
810
|
+
optparse.parse!(args)
|
811
|
+
if args.count != 1
|
812
|
+
puts_error "#{Morpheus::Terminal.angry_prompt}wrong number of arguments. Expected 1 and received #{args.count} #{args.inspect}\n#{optparse}"
|
813
|
+
return 1
|
814
|
+
end
|
815
|
+
connect(options)
|
816
|
+
|
817
|
+
begin
|
818
|
+
cloud = find_cloud_by_name_or_id(args[0])
|
819
|
+
return 1 if cloud.nil?
|
820
|
+
# construct payload
|
821
|
+
passed_options = options[:options] ? options[:options].reject {|k,v| k.is_a?(Symbol) } : {}
|
822
|
+
payload = nil
|
823
|
+
if options[:payload]
|
824
|
+
payload = options[:payload]
|
825
|
+
payload.deep_merge!({'page' => passed_options}) unless passed_options.empty?
|
826
|
+
else
|
827
|
+
payload = {
|
828
|
+
'page' => {
|
829
|
+
}
|
830
|
+
}
|
831
|
+
# allow arbitrary -O options
|
832
|
+
payload.deep_merge!({'page' => passed_options}) unless passed_options.empty?
|
833
|
+
# prompt for options
|
834
|
+
#params = Morpheus::Cli::OptionTypes.prompt(update_wiki_page_option_types, options[:options], @api_client, options[:params])
|
835
|
+
#params = passed_options
|
836
|
+
params.deep_merge!(passed_options)
|
837
|
+
|
838
|
+
if params.empty?
|
839
|
+
raise_command_error "Specify at least one option to update.\n#{optparse}"
|
840
|
+
end
|
841
|
+
|
842
|
+
payload.deep_merge!({'page' => params}) unless params.empty?
|
843
|
+
end
|
844
|
+
@clouds_interface.setopts(options)
|
845
|
+
if options[:dry_run]
|
846
|
+
print_dry_run @clouds_interface.dry.update_wiki(cloud["id"], payload)
|
847
|
+
return
|
848
|
+
end
|
849
|
+
json_response = @clouds_interface.update_wiki(cloud["id"], payload)
|
850
|
+
|
851
|
+
if options[:json]
|
852
|
+
puts as_json(json_response, options)
|
853
|
+
else
|
854
|
+
print_green_success "Updated wiki page for cloud #{cloud['name']}"
|
855
|
+
wiki([cloud['id']])
|
856
|
+
end
|
857
|
+
return 0
|
858
|
+
rescue RestClient::Exception => e
|
859
|
+
print_rest_exception(e, options)
|
860
|
+
exit 1
|
861
|
+
end
|
862
|
+
end
|
863
|
+
|
663
864
|
private
|
664
865
|
|
665
866
|
def print_clouds_table(clouds, opts={})
|
@@ -742,4 +943,11 @@ class Morpheus::Cli::Clouds
|
|
742
943
|
out
|
743
944
|
end
|
744
945
|
|
946
|
+
def update_wiki_page_option_types
|
947
|
+
[
|
948
|
+
{'fieldName' => 'name', 'fieldLabel' => 'Name', 'type' => 'text', 'required' => false, 'displayOrder' => 1, 'description' => 'The name of the wiki page for this instance. Default is the instance name.'},
|
949
|
+
#{'fieldName' => 'category', 'fieldLabel' => 'Category', 'type' => 'text', 'required' => false, 'displayOrder' => 2},
|
950
|
+
{'fieldName' => 'content', 'fieldLabel' => 'Content', 'type' => 'textarea', 'required' => false, 'displayOrder' => 3, 'description' => 'The content (markdown) of the wiki page.'}
|
951
|
+
]
|
952
|
+
end
|
745
953
|
end
|
@@ -0,0 +1,406 @@
|
|
1
|
+
require 'io/console'
|
2
|
+
require 'rest_client'
|
3
|
+
require 'optparse'
|
4
|
+
require 'morpheus/cli/cli_command'
|
5
|
+
require 'morpheus/cli/option_types'
|
6
|
+
require 'json'
|
7
|
+
|
8
|
+
class Morpheus::Cli::EnvironmentsCommand
|
9
|
+
include Morpheus::Cli::CliCommand
|
10
|
+
set_command_name :environments
|
11
|
+
register_subcommands :list, :get, :add, :update, :remove, :'toggle-active'
|
12
|
+
|
13
|
+
def initialize()
|
14
|
+
# @appliance_name, @appliance_url = Morpheus::Cli::Remote.active_appliance
|
15
|
+
end
|
16
|
+
|
17
|
+
def connect(opts)
|
18
|
+
@api_client = establish_remote_appliance_connection(opts)
|
19
|
+
@environments_interface = Morpheus::APIClient.new(@access_token,nil,nil, @appliance_url).environments
|
20
|
+
end
|
21
|
+
|
22
|
+
def handle(args)
|
23
|
+
handle_subcommand(args)
|
24
|
+
end
|
25
|
+
|
26
|
+
def list(args)
|
27
|
+
options = {}
|
28
|
+
params = {}
|
29
|
+
optparse = Morpheus::Cli::OptionParser.new do |opts|
|
30
|
+
opts.banner = subcommand_usage()
|
31
|
+
build_common_options(opts, options, [:list, :query, :json, :yaml, :csv, :fields, :dry_run, :remote])
|
32
|
+
end
|
33
|
+
optparse.parse!(args)
|
34
|
+
if args.count != 0
|
35
|
+
raise_command_error "wrong number of arguments, expected 0 and got (#{args.count}) #{args}\n#{optparse}"
|
36
|
+
end
|
37
|
+
connect(options)
|
38
|
+
begin
|
39
|
+
params.merge!(parse_list_options(options))
|
40
|
+
@environments_interface.setopts(options)
|
41
|
+
if options[:dry_run]
|
42
|
+
print_dry_run @environments_interface.dry.list(params)
|
43
|
+
return 0
|
44
|
+
end
|
45
|
+
json_response = @environments_interface.list(params)
|
46
|
+
render_result = render_with_format(json_response, options, 'environments')
|
47
|
+
return 0 if render_result
|
48
|
+
environments = json_response['environments']
|
49
|
+
unless options[:quiet]
|
50
|
+
title = "Morpheus Environments"
|
51
|
+
subtitles = []
|
52
|
+
subtitles += parse_list_subtitles(options)
|
53
|
+
print_h1 title, subtitles
|
54
|
+
if environments.empty?
|
55
|
+
print yellow,"No Environments found.",reset
|
56
|
+
else
|
57
|
+
print_environments_table(environments)
|
58
|
+
print_results_pagination(json_response)
|
59
|
+
end
|
60
|
+
print reset,"\n"
|
61
|
+
end
|
62
|
+
return 0
|
63
|
+
rescue RestClient::Exception => e
|
64
|
+
print_rest_exception(e, options)
|
65
|
+
exit 1
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
def get(args)
|
70
|
+
options = {}
|
71
|
+
params = {}
|
72
|
+
optparse = Morpheus::Cli::OptionParser.new do |opts|
|
73
|
+
opts.banner = subcommand_usage("[name]")
|
74
|
+
build_common_options(opts, options, [:query, :json, :yaml, :csv, :fields, :dry_run, :remote])
|
75
|
+
end
|
76
|
+
optparse.parse!(args)
|
77
|
+
|
78
|
+
if args.count != 1
|
79
|
+
raise_command_error "wrong number of arguments, expected 1 and got (#{args.count}) #{args}\n#{optparse}"
|
80
|
+
end
|
81
|
+
|
82
|
+
connect(options)
|
83
|
+
begin
|
84
|
+
@environments_interface.setopts(options)
|
85
|
+
if options[:dry_run]
|
86
|
+
if args[0].to_s =~ /\A\d{1,}\Z/
|
87
|
+
print_dry_run @environments_interface.dry.get(args[0])
|
88
|
+
else
|
89
|
+
print_dry_run @environments_interface.dry.list({name: args[0].to_s})
|
90
|
+
end
|
91
|
+
return 0
|
92
|
+
end
|
93
|
+
environment = find_environment_by_name_or_id(args[0])
|
94
|
+
return 1 if environment.nil?
|
95
|
+
json_response = {'environment' => environment}
|
96
|
+
render_result = render_with_format(json_response, options, 'environment')
|
97
|
+
return 0 if render_result
|
98
|
+
|
99
|
+
unless options[:quiet]
|
100
|
+
print_h1 "Environment Details"
|
101
|
+
print cyan
|
102
|
+
|
103
|
+
print_description_list({
|
104
|
+
"ID" => 'id',
|
105
|
+
"Name" => 'name',
|
106
|
+
"Code" => 'code',
|
107
|
+
"Description" => 'description',
|
108
|
+
"Owner" => lambda {|it| it['account'] ? it['account']['name'] : '' },
|
109
|
+
"Visibility" => lambda {|it| it['visibility'].to_s.capitalize },
|
110
|
+
"Sort Order" => lambda {|it| it['sortOrder'] },
|
111
|
+
"Active" => lambda {|it| format_boolean(it['active']) },
|
112
|
+
#"Created" => lambda {|it| format_local_dt(it['dateCreated']) }
|
113
|
+
}, environment)
|
114
|
+
print reset,"\n"
|
115
|
+
end
|
116
|
+
return 0
|
117
|
+
rescue RestClient::Exception => e
|
118
|
+
print_rest_exception(e, options)
|
119
|
+
exit 1
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
123
|
+
def add(args)
|
124
|
+
options = {}
|
125
|
+
optparse = Morpheus::Cli::OptionParser.new do |opts|
|
126
|
+
opts.banner = subcommand_usage("[name] [options]")
|
127
|
+
build_option_type_options(opts, options, add_environment_option_types)
|
128
|
+
build_common_options(opts, options, [:payload, :options, :json, :dry_run, :remote])
|
129
|
+
end
|
130
|
+
optparse.parse!(args)
|
131
|
+
if args.count > 1
|
132
|
+
raise_command_error "wrong number of arguments, expected 0-1 and got (#{args.count}) #{args}\n#{optparse}"
|
133
|
+
end
|
134
|
+
if args[0]
|
135
|
+
options[:options] ||= {}
|
136
|
+
options[:options]['name'] ||= args[0]
|
137
|
+
end
|
138
|
+
connect(options)
|
139
|
+
begin
|
140
|
+
# construct payload
|
141
|
+
passed_options = options[:options] ? options[:options].reject {|k,v| k.is_a?(Symbol) } : {}
|
142
|
+
payload = nil
|
143
|
+
if options[:payload]
|
144
|
+
payload = options[:payload]
|
145
|
+
payload.deep_merge!({'environment' => passed_options}) unless passed_options.empty?
|
146
|
+
else
|
147
|
+
payload = {
|
148
|
+
'environment' => {
|
149
|
+
}
|
150
|
+
}
|
151
|
+
# allow arbitrary -O options
|
152
|
+
payload.deep_merge!({'environment' => passed_options}) unless passed_options.empty?
|
153
|
+
# prompt for options
|
154
|
+
params = Morpheus::Cli::OptionTypes.prompt(add_environment_option_types, options[:options], @api_client, options[:params])
|
155
|
+
payload.deep_merge!({'environment' => params}) unless params.empty?
|
156
|
+
end
|
157
|
+
|
158
|
+
@environments_interface.setopts(options)
|
159
|
+
if options[:dry_run]
|
160
|
+
print_dry_run @environments_interface.dry.create(payload)
|
161
|
+
return
|
162
|
+
end
|
163
|
+
json_response = @environments_interface.create(payload)
|
164
|
+
if options[:json]
|
165
|
+
print JSON.pretty_generate(json_response)
|
166
|
+
print "\n"
|
167
|
+
else
|
168
|
+
display_name = json_response['environment'] ? json_response['environment']['name'] : ''
|
169
|
+
print_green_success "Environment #{display_name} added"
|
170
|
+
get([json_response['environment']['id']])
|
171
|
+
end
|
172
|
+
return 0
|
173
|
+
rescue RestClient::Exception => e
|
174
|
+
print_rest_exception(e, options)
|
175
|
+
exit 1
|
176
|
+
end
|
177
|
+
end
|
178
|
+
|
179
|
+
def update(args)
|
180
|
+
options = {}
|
181
|
+
optparse = Morpheus::Cli::OptionParser.new do |opts|
|
182
|
+
opts.banner = subcommand_usage("[name] [options]")
|
183
|
+
build_option_type_options(opts, options, update_environment_option_types)
|
184
|
+
build_common_options(opts, options, [:payload, :options, :json, :dry_run, :remote])
|
185
|
+
end
|
186
|
+
optparse.parse!(args)
|
187
|
+
|
188
|
+
if args.count != 1
|
189
|
+
raise_command_error "wrong number of arguments, expected 1 and got (#{args.count}) #{args}\n#{optparse}"
|
190
|
+
end
|
191
|
+
|
192
|
+
connect(options)
|
193
|
+
begin
|
194
|
+
|
195
|
+
environment = find_environment_by_name_or_id(args[0])
|
196
|
+
return 1 if environment.nil?
|
197
|
+
|
198
|
+
# construct payload
|
199
|
+
passed_options = options[:options] ? options[:options].reject {|k,v| k.is_a?(Symbol) } : {}
|
200
|
+
payload = nil
|
201
|
+
if options[:payload]
|
202
|
+
payload = options[:payload]
|
203
|
+
payload.deep_merge!({'environment' => passed_options}) unless passed_options.empty?
|
204
|
+
else
|
205
|
+
payload = {
|
206
|
+
'environment' => {
|
207
|
+
}
|
208
|
+
}
|
209
|
+
# allow arbitrary -O options
|
210
|
+
payload.deep_merge!({'environment' => passed_options}) unless passed_options.empty?
|
211
|
+
# prompt for options
|
212
|
+
#params = Morpheus::Cli::OptionTypes.prompt(update_environment_option_types, options[:options], @api_client, options[:params])
|
213
|
+
params = passed_options
|
214
|
+
|
215
|
+
if params.empty?
|
216
|
+
raise_command_error "Specify at least one option to update.\n#{optparse}"
|
217
|
+
end
|
218
|
+
|
219
|
+
payload.deep_merge!({'environment' => params}) unless params.empty?
|
220
|
+
end
|
221
|
+
@environments_interface.setopts(options)
|
222
|
+
if options[:dry_run]
|
223
|
+
print_dry_run @environments_interface.dry.update(environment['id'], payload)
|
224
|
+
return
|
225
|
+
end
|
226
|
+
json_response = @environments_interface.update(environment['id'], payload)
|
227
|
+
if options[:json]
|
228
|
+
print JSON.pretty_generate(json_response)
|
229
|
+
print "\n"
|
230
|
+
else
|
231
|
+
display_name = json_response['environment'] ? json_response['environment']['name'] : ''
|
232
|
+
print_green_success "Environment #{display_name} updated"
|
233
|
+
get([json_response['environment']['id']])
|
234
|
+
end
|
235
|
+
return 0
|
236
|
+
rescue RestClient::Exception => e
|
237
|
+
print_rest_exception(e, options)
|
238
|
+
exit 1
|
239
|
+
end
|
240
|
+
end
|
241
|
+
|
242
|
+
def remove(args)
|
243
|
+
options = {}
|
244
|
+
optparse = Morpheus::Cli::OptionParser.new do |opts|
|
245
|
+
opts.banner = subcommand_usage("[name]")
|
246
|
+
build_common_options(opts, options, [:auto_confirm, :json, :dry_run, :remote])
|
247
|
+
end
|
248
|
+
optparse.parse!(args)
|
249
|
+
|
250
|
+
if args.count != 1
|
251
|
+
raise_command_error "wrong number of arguments, expected 1 and got (#{args.count}) #{args}\n#{optparse}"
|
252
|
+
end
|
253
|
+
|
254
|
+
connect(options)
|
255
|
+
begin
|
256
|
+
environment = find_environment_by_name_or_id(args[0])
|
257
|
+
return 1 if environment.nil?
|
258
|
+
|
259
|
+
unless options[:yes] || Morpheus::Cli::OptionTypes.confirm("Are you sure you want to delete the environment #{environment['name']}?")
|
260
|
+
return 9, "aborted command"
|
261
|
+
end
|
262
|
+
@environments_interface.setopts(options)
|
263
|
+
if options[:dry_run]
|
264
|
+
print_dry_run @environments_interface.dry.destroy(environment['id'])
|
265
|
+
return
|
266
|
+
end
|
267
|
+
json_response = @environments_interface.destroy(environment['id'])
|
268
|
+
if options[:json]
|
269
|
+
print JSON.pretty_generate(json_response)
|
270
|
+
print "\n"
|
271
|
+
else
|
272
|
+
print_green_success "Environment #{environment['name']} removed"
|
273
|
+
# list([])
|
274
|
+
end
|
275
|
+
return 0
|
276
|
+
rescue RestClient::Exception => e
|
277
|
+
print_rest_exception(e, options)
|
278
|
+
exit 1
|
279
|
+
end
|
280
|
+
end
|
281
|
+
|
282
|
+
def toggle_active(args)
|
283
|
+
params = {}
|
284
|
+
options = {}
|
285
|
+
optparse = Morpheus::Cli::OptionParser.new do |opts|
|
286
|
+
opts.banner = subcommand_usage("[name] [on|off]")
|
287
|
+
build_common_options(opts, options, [:json, :dry_run, :remote])
|
288
|
+
end
|
289
|
+
optparse.parse!(args)
|
290
|
+
|
291
|
+
if args.count < 1 || args.count > 2
|
292
|
+
raise_command_error "wrong number of arguments, expected 1 and got (#{args.count}) #{args}\n#{optparse}"
|
293
|
+
end
|
294
|
+
if args[1]
|
295
|
+
params['active'] = (args[1].to_s == 'on' || args[1].to_s == 'true')
|
296
|
+
end
|
297
|
+
connect(options)
|
298
|
+
begin
|
299
|
+
|
300
|
+
environment = find_environment_by_name_or_id(args[0])
|
301
|
+
return 1 if environment.nil?
|
302
|
+
|
303
|
+
@environments_interface.setopts(options)
|
304
|
+
if options[:dry_run]
|
305
|
+
print_dry_run @environments_interface.dry.toggle_active(environment['id'], params)
|
306
|
+
return
|
307
|
+
end
|
308
|
+
json_response = @environments_interface.toggle_active(environment['id'], params)
|
309
|
+
if options[:json]
|
310
|
+
print JSON.pretty_generate(json_response)
|
311
|
+
print "\n"
|
312
|
+
else
|
313
|
+
display_name = json_response['environment'] ? json_response['environment']['name'] : ''
|
314
|
+
print_green_success "Environment #{display_name} updated"
|
315
|
+
get([json_response['environment']['id']])
|
316
|
+
end
|
317
|
+
return 0
|
318
|
+
rescue RestClient::Exception => e
|
319
|
+
print_rest_exception(e, options)
|
320
|
+
exit 1
|
321
|
+
end
|
322
|
+
end
|
323
|
+
|
324
|
+
private
|
325
|
+
def find_environment_by_name_or_id(val)
|
326
|
+
if val.to_s =~ /\A\d{1,}\Z/
|
327
|
+
return find_environment_by_id(val)
|
328
|
+
else
|
329
|
+
return find_environment_by_name(val)
|
330
|
+
end
|
331
|
+
end
|
332
|
+
|
333
|
+
def find_environment_by_id(id)
|
334
|
+
raise "#{self.class} has not defined @environments_interface" if @environments_interface.nil?
|
335
|
+
begin
|
336
|
+
json_response = @environments_interface.get(id)
|
337
|
+
return json_response['environment']
|
338
|
+
rescue RestClient::Exception => e
|
339
|
+
if e.response && e.response.code == 404
|
340
|
+
print_red_alert "Environment not found by id #{id}"
|
341
|
+
else
|
342
|
+
raise e
|
343
|
+
end
|
344
|
+
end
|
345
|
+
end
|
346
|
+
|
347
|
+
def find_environment_by_name(name)
|
348
|
+
raise "#{self.class} has not defined @environments_interface" if @environments_interface.nil?
|
349
|
+
environments = @environments_interface.list({name: name.to_s})['environments']
|
350
|
+
if environments.empty?
|
351
|
+
print_red_alert "Environment not found by name #{name}"
|
352
|
+
return nil
|
353
|
+
elsif environments.size > 1
|
354
|
+
print_red_alert "#{environments.size} environments by name #{name}"
|
355
|
+
print_environments_table(environments, {color: red})
|
356
|
+
print reset,"\n"
|
357
|
+
return nil
|
358
|
+
else
|
359
|
+
return environments[0]
|
360
|
+
end
|
361
|
+
end
|
362
|
+
|
363
|
+
def print_environments_table(environments, opts={})
|
364
|
+
table_color = opts[:color] || cyan
|
365
|
+
rows = environments.collect do |environment|
|
366
|
+
{
|
367
|
+
id: environment['id'],
|
368
|
+
name: environment['name'],
|
369
|
+
code: environment['code'],
|
370
|
+
description: environment['description'],
|
371
|
+
active: format_boolean(environment['active'])
|
372
|
+
}
|
373
|
+
end
|
374
|
+
columns = [
|
375
|
+
:id,
|
376
|
+
:name,
|
377
|
+
:code,
|
378
|
+
:description,
|
379
|
+
:active
|
380
|
+
]
|
381
|
+
print as_pretty_table(rows, columns, opts)
|
382
|
+
print reset
|
383
|
+
end
|
384
|
+
|
385
|
+
|
386
|
+
def add_environment_option_types
|
387
|
+
[
|
388
|
+
{'fieldName' => 'name', 'fieldLabel' => 'Name', 'type' => 'text', 'required' => true, 'displayOrder' => 1},
|
389
|
+
{'fieldName' => 'code', 'fieldLabel' => 'Code', 'type' => 'text', 'required' => true, 'displayOrder' => 2},
|
390
|
+
{'fieldName' => 'description', 'fieldLabel' => 'Description', 'type' => 'text', 'required' => false, 'displayOrder' => 3},
|
391
|
+
{'fieldName' => 'visibility', 'fieldLabel' => 'Visibility', 'type' => 'select', 'selectOptions' => [{'name' => 'Private', 'value' => 'private'}, {'name' => 'Public', 'value' => 'public'}], 'defaultValue' => 'private', 'displayOrder' => 6},
|
392
|
+
{'fieldName' => 'sortOrder', 'fieldLabel' => 'Sort Order', 'type' => 'number', 'required' => false, 'defaultValue' => 0, 'displayOrder' => 5}
|
393
|
+
]
|
394
|
+
end
|
395
|
+
|
396
|
+
def update_environment_option_types
|
397
|
+
[
|
398
|
+
{'fieldName' => 'name', 'fieldLabel' => 'Name', 'type' => 'text', 'required' => false, 'displayOrder' => 1},
|
399
|
+
#{'fieldName' => 'code', 'fieldLabel' => 'Code', 'type' => 'text', 'required' => true, 'displayOrder' => 2},
|
400
|
+
{'fieldName' => 'description', 'fieldLabel' => 'Description', 'type' => 'text', 'required' => false, 'displayOrder' => 3},
|
401
|
+
{'fieldName' => 'visibility', 'fieldLabel' => 'Visibility', 'type' => 'select', 'selectOptions' => [{'name' => 'Private', 'value' => 'private'}, {'name' => 'Public', 'value' => 'public'}], 'defaultValue' => 'private', 'displayOrder' => 6},
|
402
|
+
{'fieldName' => 'sortOrder', 'fieldLabel' => 'Sort Order', 'type' => 'number', 'required' => false, 'defaultValue' => 0, 'displayOrder' => 5}
|
403
|
+
]
|
404
|
+
end
|
405
|
+
|
406
|
+
end
|