Skip to content

Instantly share code, notes, and snippets.

@hub-cap
Last active August 29, 2015 13:56
Show Gist options
  • Save hub-cap/8969562 to your computer and use it in GitHub Desktop.
Save hub-cap/8969562 to your computer and use it in GitHub Desktop.
def _gen_ports(portstr):
from_port, sep, to_port = portstr.partition('-')
if not to_port:
if not sep:
to_port = from_port
else:
# This is an invalid configuration, such as "1-",
# so the port will be set to 0, which is generally
# not going to be seen as valid.
to_port = 0
return from_port, to_port
def _create_rules(self, secgrp, protocol, ports=[]):
# hub_cap - Not sure if this error handling should be in its own method or not...
# I didnt take the time to see its usefulness in teh rest of the file.
err = inst_models.InstanceTasks.BUILDING_ERROR_SEC_GROUP
err_msg = ('Error creating security group rules. '
'Invalid port format. '
'FromPort = %(from)s, ToPort = %(to)s')
def set_error_and_raise(ports):
self.update_db(task_status=err)
msg = error_msg % {'from': ports[0], 'to': ports[1]}
raise MalformedSecurityGroupRuleError(msg)
for port_tuple in map(_gen_ports, ports):
try:
port_tuple = map(int, port_tuple)
except ValueError:
set_error_and_raise(port_tuple)
if any([port_tuple[1] > port_tuple[0],
port_tuple[0] == 0,
port_tuple[1] == 0]):
set_error_and_raise(port_tuple)
SecurityGroupRule.create_sec_group_rule(
secgrp, protocol,
port_tuple[0], port_tuple[1],
CONF.trove_security_group_rule_cidr,
self.context)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment