Methods
public class
public instance
Included modules
Public class methods
returned by EC2::Base#describe_security_groups
groupName: stage-app groupDescription: ownerId: "207436219441" ipPermissions: item: - ipRanges: item: - cidrIp: 216.19.182.83/32 - cidrIp: 24.5.71.201/32 - cidrIp: 75.157.176.202/32 - cidrIp: 84.28.52.172/32 - cidrIp: 87.212.145.201/32 - cidrIp: 96.49.129.178/32 groups: item: - groupName: default userId: "207436219441" - groupName: stage-app userId: "207436219441" fromPort: "22" toPort: "22" ipProtocol: tcp
Returns a Rudy::AWS::EC2::Group object
# File lib/rudy/aws/ec2/group.rb, line 211 211: def self.from_hash(ghash) 212: newg = Rudy::AWS::EC2::Group.new 213: newg.name = ghash['groupName'] 214: newg.description = ghash['groupDescription'] 215: newg.owner_id = ghash['ownerId'] 216: newg.addresses = {} 217: newg.groups = {} 218: 219: return newg unless ghash['ipPermissions'].is_a?(Hash) 220: 221: ghash['ipPermissions']['item'].each do |oldp| 222: newp = Rudy::AWS::EC2::Group::Rule.new 223: newp.ports = Range.new(oldp['fromPort'], oldp['toPort']) 224: newp.protocol = oldp['ipProtocol'] 225: if oldp['groups'].is_a?(Hash) 226: oldp['groups']['item'].each do |oldpg| 227: name = [oldpg['userId'], oldpg['groupName']].join(':') # account_num:name 228: newg.add_group(name, newp) 229: end 230: end 231: if oldp['ipRanges'].is_a?(Hash) 232: oldp['ipRanges']['item'].each do |olda| 233: name = "#{olda['cidrIp']}" 234: newg.add_address(name, newp) # ipaddress/mask/protocol 235: end 236: end 237: end 238: newg 239: end
Public instance methods
# File lib/rudy/aws/ec2/group.rb, line 155 155: def any? 156: groups = list || [] 157: !groups.empty? 158: end
Authorize a port/protocol for a specific IP address
# File lib/rudy/aws/ec2/group.rb, line 110 110: def authorize(name, addresses=[], ports=[], protocols=[], &each_group) 111: modify_rules(:authorize, name, addresses, ports, protocols, &each_group) 112: end
# File lib/rudy/aws/ec2/group.rb, line 121 121: def authorize_group(name, gname, owner, &each_group) 122: modify_group_rules(:authorize, name, gname, owner, &each_group) 123: end
Create a new EC2 security group Returns list of created groups
# File lib/rudy/aws/ec2/group.rb, line 93 93: def create(name, desc=nil, addresses=[], ports=[], protocols=[], &each_group) 94: desc ||= "Security Group #{name}" 95: ret = @@ec2.create_security_group(:group_name => name, :group_description => desc) 96: return false unless (ret && ret['return'] == 'true') 97: authorize(name, addresses, ports, protocols) 98: get(name, &each_group) 99: end
Delete an EC2 security group Returns true/false whether successful
# File lib/rudy/aws/ec2/group.rb, line 103 103: def destroy(name, &each_group) 104: list(name, &each_group) if each_group 105: ret = @@ec2.delete_security_group(:group_name => name) 106: (ret && ret['return'] == 'true') 107: end
Does the security group name exist?
# File lib/rudy/aws/ec2/group.rb, line 171 171: def exists?(name) 172: begin 173: g = list([name.to_s]) 174: rescue ::AWS::InvalidGroupNotFound 175: return false 176: end 177: 178: !g.empty? 179: end
- name a string
# File lib/rudy/aws/ec2/group.rb, line 161 161: def get(name) 162: (list([name]) || []).first 163: end
# File lib/rudy/aws/ec2/group.rb, line 130 130: def list(group_names=[], &each_group) 131: group_names ||= [] 132: groups = list_as_hash(group_names, &each_group) 133: groups &&= groups.values 134: groups 135: end
- group_names is a list of security group names to look for. If it’s empty, all groups
associated to the account will be returned.
Returns an Array of Rudy::AWS::EC2::Group objects
# File lib/rudy/aws/ec2/group.rb, line 141 141: def list_as_hash(group_names=[], &each_group) 142: group_names = [group_names].flatten.compact 143: glist = @@ec2.describe_security_groups(:group_name => group_names) || {} 144: return unless glist['securityGroupInfo'].is_a?(Hash) 145: groups = {} 146: glist['securityGroupInfo']['item'].each do |oldg| 147: g = Groups.from_hash(oldg) 148: groups[g.name] = g 149: end 150: groups.each_value { |g| each_group.call(g) } if each_group 151: groups = nil if groups.empty? 152: groups 153: end
Revoke a port/protocol for a specific IP address Takes the same arguments as authorize
# File lib/rudy/aws/ec2/group.rb, line 117 117: def revoke(name, addresses=[], ports=[], protocols=[], &each_group) 118: modify_rules(:revoke, name, addresses, ports, protocols, &each_group) 119: end
# File lib/rudy/aws/ec2/group.rb, line 126 126: def revoke_group(name, gname, owner, &each_group) 127: modify_group_rules(:revoke, name, gname, owner, &each_group) 128: end