Class: Morpheus::Cli::SecurityGroups
- Inherits:
-
Object
- Object
- Morpheus::Cli::SecurityGroups
- Includes:
- CliCommand
- Defined in:
- lib/morpheus/cli/security_groups.rb
Class Method Summary collapse
- .load_security_group_file ⇒ Object
- .save_security_group(security_group_map) ⇒ Object
- .security_group_file_path ⇒ Object
Instance Method Summary collapse
- #add(args) ⇒ Object
- #get(args) ⇒ Object
- #handle(args) ⇒ Object
-
#initialize ⇒ SecurityGroups
constructor
A new instance of SecurityGroups.
- #list(args) ⇒ Object
- #remove(args) ⇒ Object
- #use(args) ⇒ Object
Methods included from CliCommand
#build_common_options, included
Constructor Details
#initialize ⇒ SecurityGroups
Returns a new instance of SecurityGroups.
12 13 14 15 16 17 |
# File 'lib/morpheus/cli/security_groups.rb', line 12 def initialize() @appliance_name, @appliance_url = Morpheus::Cli::Remote.active_appliance @access_token = Morpheus::Cli::Credentials.new(@appliance_name,@appliance_url).request_credentials() @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 |
Class Method Details
.load_security_group_file ⇒ Object
183 184 185 186 187 188 189 190 |
# File 'lib/morpheus/cli/security_groups.rb', line 183 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(security_group_map) ⇒ Object
201 202 203 |
# File 'lib/morpheus/cli/security_groups.rb', line 201 def self.save_security_group(security_group_map) File.open(security_group_file_path, 'w') {|f| f.write security_group_map.to_yaml } #Store end |
.security_group_file_path ⇒ Object
192 193 194 195 196 197 198 199 |
# File 'lib/morpheus/cli/security_groups.rb', line 192 def self.security_group_file_path home_dir = Dir.home morpheus_dir = File.join(home_dir,".morpheus") if !Dir.exist?(morpheus_dir) Dir.mkdir(morpheus_dir) end return File.join(morpheus_dir,"securitygroup") end |
Instance Method Details
#add(args) ⇒ Object
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/morpheus/cli/security_groups.rb', line 106 def add(args) = {} params = {:securityGroup => {:name => args[0]} } optparse = OptionParser.new do|opts| opts. = "\nUsage: morpheus security-groups add NAME [options]" opts.on( '-d', '--description Description', "Description of the security group" ) do |description| params[:securityGroup][:description] = description end (opts, , []) end optparse.parse(args) if args.count < 1 puts "\n#{optparse.}\n\n" exit 1 end begin @security_groups_interface.create(params) list([]) rescue RestClient::Exception => e print_rest_exception(e, ) exit 1 end end |
#get(args) ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/morpheus/cli/security_groups.rb', line 77 def get(args) = {} optparse = OptionParser.new do|opts| opts. = "Usage: morpheus security-groups get ID" (opts, , []) end optparse.parse(args) if args.count < 1 puts "\n#{optparse.}\n\n" exit 1 end begin json_response = @security_groups_interface.get({id: args[0]}) security_group = json_response['securityGroup'] print "\n" ,cyan, bold, "Morpheus Security Group\n","==================", reset, "\n\n" if security_group.nil? puts yellow,"Security Group not found by id #{args[0]}",reset else print cyan, "= #{security_group['id']}: #{security_group['name']} (#{security_group['description']})\n" end print reset,"\n\n" rescue RestClient::Exception => e print_rest_exception(e, ) exit 1 end end |
#handle(args) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/morpheus/cli/security_groups.rb', line 20 def handle(args) if @access_token.empty? print_red_alert "Invalid Credentials. Unable to acquire access token. Please verify your credentials and try again." return 1 end if args.empty? puts "\nUsage: morpheus security-groups [list,get,add,remove,use] [name]\n\n" return end case args[0] when 'list' list(args[1..-1]) when 'get' get(args[1..-1]) when 'add' add(args[1..-1]) when 'remove' remove(args[1..-1]) when 'use' use(args[1..-1]) else puts "\nUsage: morpheus security-groups [list,get,add,remove,use] [name]\n\n" end end |
#list(args) ⇒ Object
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 |
# File 'lib/morpheus/cli/security_groups.rb', line 46 def list(args) = {} optparse = OptionParser.new do|opts| opts. = "\nUsage: morpheus security-groups list" (opts, , []) end optparse.parse(args) begin json_response = @security_groups_interface.list() security_groups = json_response['securityGroups'] print "\n" ,cyan, bold, "Morpheus Security Groups\n","==================", reset, "\n\n" if security_groups.empty? puts yellow,"No Security Groups currently configured.",reset else security_groups.each do |security_group| if @active_security_group[@appliance_name.to_sym] = security_group['id'] print cyan, "=> #{security_group['id']}: #{security_group['name']} (#{security_group['description']})\n" else print cyan, "= #{security_group['id']}: #{security_group['name']} (#{security_group['description']})\n" end end end print reset,"\n\n" rescue RestClient::Exception => e print_rest_exception(e, ) exit 1 end end |
#remove(args) ⇒ Object
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/morpheus/cli/security_groups.rb', line 130 def remove(args) = {} optparse = OptionParser.new do|opts| opts. = "\nUsage: morpheus security-groups remove ID" (opts, , []) end optparse.parse(args) if args.count < 1 puts "\n#{optparse.}\n\n" return end 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.delete(security_group['id']) list([]) rescue RestClient::Exception => e print_rest_exception(e, ) exit 1 end end |
#use(args) ⇒ Object
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 |
# File 'lib/morpheus/cli/security_groups.rb', line 156 def use(args) = {} optparse = OptionParser.new do|opts| opts. = "Usage: morpheus security-groups use ID" (opts, , []) end optparse.parse(args) if args.length < 1 puts "\n#{optparse.}\n\n" return end begin 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 "Using Security Group #{args[0]}" else puts "Security Group not found" end rescue RestClient::Exception => e print_rest_exception(e, ) exit 1 end end |