Class: Morpheus::Cli::SecurityGroups
- Inherits:
-
Object
- Object
- Morpheus::Cli::SecurityGroups
- Includes:
- CliCommand
- Defined in:
- lib/morpheus/cli/security_groups.rb
Instance Attribute Summary
Attributes included from CliCommand
Class Method Summary collapse
- .load_security_group_file ⇒ Object
- .save_security_group(new_config) ⇒ Object
- .security_group_file_path ⇒ Object
Instance Method Summary collapse
- #add(args) ⇒ Object
- #connect(opts) ⇒ Object
- #get(args) ⇒ Object
- #handle(args) ⇒ Object
- #list(args) ⇒ Object
- #remove(args) ⇒ Object
- #unuse(args) ⇒ Object
- #use(args) ⇒ Object
Methods included from CliCommand
#build_common_options, #build_option_type_options, #command_name, #default_subcommand, #establish_remote_appliance_connection, #full_command_usage, #handle_subcommand, included, #interactive?, #my_help_command, #my_terminal, #my_terminal=, #parse_command_result, #parse_id_list, #parse_list_options, #parse_list_subtitles, #print, #print_error, #print_to_file, #puts, #puts_error, #raise_command_error, #render_with_format, #run_command_for_each_arg, #subcommand_aliases, #subcommand_usage, #subcommands, #usage, #verify_access_token!
Class Method Details
.load_security_group_file ⇒ Object
264 265 266 267 268 269 270 271 |
# File 'lib/morpheus/cli/security_groups.rb', line 264 def self.load_security_group_file remote_file = security_group_file_path if File.exist? remote_file return YAML.load_file(remote_file) else {} end end |
.save_security_group(new_config) ⇒ Object
277 278 279 280 281 282 283 284 285 |
# File 'lib/morpheus/cli/security_groups.rb', line 277 def self.save_security_group(new_config) fn = security_group_file_path if !Dir.exists?(File.dirname(fn)) FileUtils.mkdir_p(File.dirname(fn)) end File.open(fn, 'w') {|f| f.write new_config.to_yaml } #Store FileUtils.chmod(0600, fn) new_config end |
.security_group_file_path ⇒ Object
273 274 275 |
# File 'lib/morpheus/cli/security_groups.rb', line 273 def self.security_group_file_path File.join(Morpheus::Cli.home_directory,"securitygroup") end |
Instance Method Details
#add(args) ⇒ Object
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/morpheus/cli/security_groups.rb', line 141 def add(args) params = {:securityGroup => {}} = {} optparse = Morpheus::Cli::OptionParser.new do |opts| opts. = subcommand_usage("[name] [options]") opts.on( '-d', '--description Description', "Description of the security group" ) do |description| params[:securityGroup][:description] = description end (opts, , [:json, :dry_run]) end optparse.parse!(args) if args.count < 1 puts optparse exit 1 end params[:securityGroup][:name] = args[0] connect() begin @security_groups_interface.setopts() if [:dry_run] print_dry_run @security_groups_interface.dry.create(params) return end json_response = @security_groups_interface.create(params) if [:json] print JSON.pretty_generate(json_response) print "\n" return end list([]) rescue RestClient::Exception => e print_rest_exception(e, ) exit 1 end end |
#connect(opts) ⇒ Object
14 15 16 17 18 |
# File 'lib/morpheus/cli/security_groups.rb', line 14 def connect(opts) @api_client = establish_remote_appliance_connection(opts) @security_groups_interface = Morpheus::APIClient.new(@access_token,nil,nil, @appliance_url).security_groups @active_security_group = ::Morpheus::Cli::SecurityGroups.load_security_group_file end |
#get(args) ⇒ Object
100 101 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 |
# File 'lib/morpheus/cli/security_groups.rb', line 100 def get(args) = {} optparse = Morpheus::Cli::OptionParser.new do |opts| opts. = subcommand_usage("[id]") (opts, , [:json, :dry_run]) end optparse.parse!(args) if args.count < 1 puts optparse exit 1 end connect() begin @security_groups_interface.setopts() if [:dry_run] print_dry_run @security_groups_interface.dry.get({id: args[0]}) return end json_response = @security_groups_interface.get({id: args[0]}) if [:json] print JSON.pretty_generate(json_response) print "\n" return end security_group = json_response['securityGroup'] print_h1 "Morpheus Security Group" print cyan description_cols = { "ID" => 'id', "Name" => 'name', "Description" => 'description', #"Account" => lambda {|it| it['account'] ? it['account']['name'] : '' }, } print_description_list(description_cols, security_group) print reset,"\n" rescue RestClient::Exception => e print_rest_exception(e, ) exit 1 end end |
#handle(args) ⇒ Object
20 21 22 |
# File 'lib/morpheus/cli/security_groups.rb', line 20 def handle(args) handle_subcommand(args) end |
#list(args) ⇒ Object
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 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/morpheus/cli/security_groups.rb', line 25 def list(args) = {} optparse = Morpheus::Cli::OptionParser.new do |opts| opts. = subcommand_usage() (opts, , [:list, :query, :json, :yaml, :csv, :fields, :dry_run, :remote]) end optparse.parse!(args) connect() begin params = {} params.merge!(()) @security_groups_interface.setopts() if [:dry_run] print_dry_run @security_groups_interface.dry.list(params) return end json_response = @security_groups_interface.list(params) render_result = render_with_format(json_response, , 'securityGroups') return 0 if render_result title = "Morpheus Security Groups" subtitles = [] subtitles += parse_list_subtitles() print_h1 title, subtitles security_groups = json_response['securityGroups'] if security_groups.empty? print yellow,"No security groups found.",reset,"\n" else active_id = @active_security_group[@appliance_name.to_sym] # table_color = options[:color] || cyan # rows = security_groups.collect do |security_group| # { # id: security_group['id'].to_s + ((security_group['id'] == active_id.to_i) ? " (active)" : ""), # name: security_group['name'], # description: security_group['description'] # } # end # columns = [ # :id, # :name, # :description, # # :ports, # # :status, # ] columns = { "ID" => 'id', "NAME" => 'name', "DESCRIPTION" => 'description', # need more to show here } # custom pretty table columns ... if [:include_fields] columns = [:include_fields] end print as_pretty_table(security_groups, columns, ) print reset if json_response['meta'] print_results_pagination(json_response, {:label => "security group", :n_label => "security groups"}) else print_results_pagination({'meta'=>{'total'=>(json_response['securityGroupCount'] ? json_response['securityGroupCount'] : security_groups.size),'size'=>security_groups.size,'max'=>(params['max']||25),'offset'=>(params['offset']||0)}}, {:label => "security group", :n_label => "security groups"}) end # print reset end print reset,"\n" return 0 rescue RestClient::Exception => e print_rest_exception(e, ) exit 1 end end |
#remove(args) ⇒ Object
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 |
# File 'lib/morpheus/cli/security_groups.rb', line 177 def remove(args) = {} optparse = Morpheus::Cli::OptionParser.new do |opts| opts. = subcommand_usage("[id]") (opts, , [:json, :dry_run]) end optparse.parse!(args) if args.count < 1 puts optparse return end connect() begin json_response = @security_groups_interface.get({id: args[0]}) security_group = json_response['securityGroup'] if security_group.nil? puts "Security Group not found by id #{args[0]}" return end @security_groups_interface.setopts() if [:dry_run] print_dry_run @security_groups_interface.dry.delete(security_group['id']) return end json_response = @security_groups_interface.delete(security_group['id']) if [:json] print JSON.pretty_generate(json_response) print "\n" return end list([]) rescue RestClient::Exception => e print_rest_exception(e, ) exit 1 end end |
#unuse(args) ⇒ Object
260 261 262 |
# File 'lib/morpheus/cli/security_groups.rb', line 260 def unuse(args) use(args + ['--none']) end |
#use(args) ⇒ Object
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 |
# File 'lib/morpheus/cli/security_groups.rb', line 214 def use(args) = {} optparse = Morpheus::Cli::OptionParser.new do |opts| opts. = subcommand_usage("[id] [--none]") opts.on('--none','--none', "Do not use an active group.") do |json| [:unuse] = true end (opts, , []) end optparse.parse!(args) if args.length < 1 && ![:unuse] puts optparse return end connect() begin if [:unuse] if @active_security_group[@appliance_name.to_sym] @active_security_group.delete(@appliance_name.to_sym) end ::Morpheus::Cli::SecurityGroups.save_security_group(@active_security_group) unless [:quiet] print cyan puts "Switched to no active security group." print reset end print reset return # exit 0 end json_response = @security_groups_interface.get({id: args[0]}) security_group = json_response['securityGroup'] if !security_group.nil? @active_security_group[@appliance_name.to_sym] = security_group['id'] ::Morpheus::Cli::SecurityGroups.save_security_group(@active_security_group) puts cyan, "Using Security Group #{args[0]}", reset else puts red, "Security Group not found", reset end rescue RestClient::Exception => e print_rest_exception(e, ) exit 1 end end |