Class: Morpheus::Cli::SecurityGroupRules
- Inherits:
-
Object
- Object
- Morpheus::Cli::SecurityGroupRules
- Includes:
- CliCommand
- Defined in:
- lib/morpheus/cli/security_group_rules.rb
Instance Attribute Summary
Attributes included from CliCommand
Instance Method Summary collapse
- #add_custom_rule(args) ⇒ Object
- #add_instance_rule(args) ⇒ Object
- #connect(opts) ⇒ Object
- #handle(args) ⇒ Object
- #list(args) ⇒ Object
- #remove(args) ⇒ Object
Methods included from CliCommand
#build_common_options, #build_option_type_options, #command_name, #default_subcommand, #establish_remote_appliance_connection, #handle_subcommand, included, #interactive?, #noninteractive, #print_usage, #subcommand_aliases, #subcommand_usage, #subcommands, #usage, #verify_access_token!
Instance Method Details
#add_custom_rule(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 |
# File 'lib/morpheus/cli/security_group_rules.rb', line 25 def add_custom_rule(args) usage = <<-EOT Usage: morpheus #{command_name} add-custom-rule SOURCE_CIDR PORT_RANGE PROTOCOL [options] \tSOURCE_CIDR: CIDR to white-list \tPORT_RANGE: Port value (i.e. 123) or port range (i.e. 1-65535) \tPROTOCOL: tcp, udp, icmp EOT = {} security_group_id = nil optparse = OptionParser.new do|opts| opts. = usage opts.on( '-s', '--secgroup SECGROUP', "Security Group ID (Use will use security as set with 'security-groups use id'" ) do |id| security_group_id = id end (opts, , [:json, :dry_run]) end optparse.parse!(args) if args.count < 3 puts optparse. exit 1 end if security_group_id.nil? && @active_security_group security_group_id = @active_security_group[@appliance_name.to_sym] end if security_group_id.nil? puts "Security Group ID must be specified with options or set using 'security-groups use id'" exit 1 end params = { :rule => { :source => args[0], :portRange => args[1], :protocol => args[2], :customRule => true } } connect() begin if [:dry_run] print_dry_run @security_group_rules_interface.dry.create(security_group_id, params) return end json_response = @security_group_rules_interface.create(security_group_id, params) if [:json] print JSON.pretty_generate(json_response) print "\n" return end list([security_group_id]) rescue RestClient::Exception => e print_rest_exception(e, ) exit 1 end end |
#add_instance_rule(args) ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 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 |
# File 'lib/morpheus/cli/security_group_rules.rb', line 84 def add_instance_rule(args) usage = <<-EOT Usage: morpheus #{command_name} add_instance_rule SOURCE_CIDR INSTANCE_TYPE_ID [options] \tSOURCE_CIDR: CIDR to white-list \tINSTANCE_TYPE_ID: ID of the Instance Type to access EOT = {} security_group_id = nil optparse = OptionParser.new do|opts| opts. = usage opts.on( '-s', '--secgroup secgroup', "Security Group ID (Use will use security as set with 'security-groups use id'" ) do |id| security_group_id = id end (opts, , [:json, :dry_run]) end optparse.parse!(args) if args.count < 2 puts optparse. exit 1 end if security_group_id.nil? && @active_security_group security_group_id = @active_security_group[@appliance_name.to_sym] end if security_group_id.nil? puts "Security Group ID must be specified with options or set using 'security-groups use id'" exit end params = { :rule => { :source => args[0], :instanceTypeId => args[1] } } connect() begin if [:dry_run] print_dry_run @security_group_rules_interface.dry.create(security_group_id, params) return end json_response = @security_group_rules_interface.create(security_group_id, params) if [:json] print JSON.pretty_generate(json_response) print "\n" return end list([security_group_id]) rescue RestClient::Exception => e print_rest_exception(e, ) exit 1 end end |
#connect(opts) ⇒ Object
15 16 17 18 19 |
# File 'lib/morpheus/cli/security_group_rules.rb', line 15 def connect(opts) @api_client = establish_remote_appliance_connection(opts) @security_group_rules_interface = Morpheus::APIClient.new(@access_token,nil,nil, @appliance_url).security_group_rules @active_security_group = ::Morpheus::Cli::SecurityGroups.load_security_group_file end |
#handle(args) ⇒ Object
21 22 23 |
# File 'lib/morpheus/cli/security_group_rules.rb', line 21 def handle(args) handle_subcommand(args) end |
#list(args) ⇒ Object
139 140 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 176 177 178 179 180 181 182 183 184 |
# File 'lib/morpheus/cli/security_group_rules.rb', line 139 def list(args) = {} security_group_id = nil optparse = OptionParser.new do|opts| opts. = subcommand_usage("[id]") (opts, , [:json, :dry_run]) end optparse.parse!(args) security_group_id = args[0] if security_group_id.nil? && @active_security_group security_group_id = @active_security_group[@appliance_name.to_sym] end if security_group_id.nil? puts "Security Group ID must be specified with options or set using 'security-groups use id'" exit 1 end connect() begin params = {} if [:dry_run] print_dry_run @security_group_rules_interface.dry.get(security_group_id, params) return end json_response = @security_group_rules_interface.get(security_group_id, params) if [:json] print JSON.pretty_generate(json_response) print "\n" return end rules = json_response['rules'] print "\n" ,cyan, bold, "Morpheus Security Group Rules for Security Group ID:#{security_group_id}\n","==================", reset, "\n\n" if rules.empty? puts yellow,"No Security Group Rules currently configured.",reset else rules = rules.sort {|x,y| x["id"] <=> y["id"] } rules.each do |rule| print cyan, "= #{rule['id']} - (CIDR:#{rule['source']}, Port Range:#{rule['portRange']}, Protocol:#{rule['protocol']}, Custom Rule:#{rule['customRule']}, Instance Type:#{rule['instanceTypeId']})\n" end end print reset,"\n" rescue RestClient::Exception => e print_rest_exception(e, ) exit 1 end end |
#remove(args) ⇒ Object
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 213 214 215 216 217 218 219 220 221 222 223 224 225 226 |
# File 'lib/morpheus/cli/security_group_rules.rb', line 186 def remove(args) = {} security_group_id = nil optparse = OptionParser.new do|opts| opts. = subcommand_usage("[id] [options]") opts.on( '-s', '--secgroup secgroup', "Security Group ID (Use will use security as set with 'security-groups use id'" ) do |id| security_group_id = id end (opts, , [:json, :dry_run]) end optparse.parse!(args) if args.count < 1 puts optparse exit 1 end if security_group_id.nil? && @active_security_group security_group_id = @active_security_group[@appliance_name.to_sym] end if security_group_id.nil? puts "Security Group ID must be specified with options or set using 'security-groups use id'" exit end connect() begin if [:dry_run] print_dry_run @security_group_rules_interface.dry.delete(security_group_id, args[0]) return end json_response = @security_group_rules_interface.delete(security_group_id, args[0]) if [:json] print JSON.pretty_generate(json_response) print "\n" return end list([security_group_id]) rescue RestClient::Exception => e print_rest_exception(e, ) exit 1 end end |