Merge branch 'master' of git://github.com/rackspace/python-novaclient

Conflicts:
	novaclient/v1_1/shell.py
This commit is contained in:
Gaurav Gupta 2011-10-16 15:27:25 -07:00
commit 32510bdee9
5 changed files with 212 additions and 4 deletions

@ -71,6 +71,7 @@ You'll find complete documentation on the shell by running
Positional arguments:
<subcommand>
add-fixed-ip Add a new fixed IP address to a servers network.
add-floating-ip Add a floating IP address to a server.
backup Backup a server.
backup-schedule Show or edit the backup schedule for a server.
backup-schedule-delete
@ -79,6 +80,9 @@ You'll find complete documentation on the shell by running
delete Immediately shut down and delete a server.
flavor-list Print a list of available 'flavors' (sizes of
servers).
floating-ip-create Allocate a floating IP to the current tenant.
floating-ip-delete De-allocate a floating IP from the current tenant.
floating-ip-list List allocated floating IPs for the current tenant.
help Display help about this program or one of its
subcommands.
image-create Create a new image by taking a snapshot of a running
@ -97,6 +101,7 @@ You'll find complete documentation on the shell by running
reboot Reboot a server.
rebuild Shutdown, re-image, and re-boot a server.
remove-fixed-ip Remove an IP address from a server.
remove-floating-ip Remove a floating IP address from a server.
rename Rename a server.
rescue Rescue a server.
resize Resize a server.
@ -104,6 +109,17 @@ You'll find complete documentation on the shell by running
resize-revert Revert a previous resize (and return to the previous
VM).
root-password Change the root password for a server.
secgroup-add-group-rule
Add a source group rule to a security group.
secgroup-add-rule Add a rule to a security group.
secgroup-create Create a new security group.
secgroup-delete Delete a security group.
secgroup-delete-group-rule
Delete a source group rule from a security group.
secgroup-delete-rule
Delete a rule from a security group.
secgroup-list List security groups for the curent tenant.
secgroup-list-rules List rules for a security group.
show Show details about the given server.
unrescue Unrescue a server.
volume-attach Attach a volume to a server.

@ -256,7 +256,7 @@ class HTTPClient(httplib2.Http):
"password": self.apikey}}}
if self.projectid:
body['auth']['tenantId'] = self.projectid
body['auth']['tenantName'] = self.projectid
token_url = urlparse.urljoin(url, "tokens")
resp, body = self.request(token_url, "POST", body=body)

@ -40,7 +40,7 @@ class ServiceCatalog(object):
endpoints = service['endpoints']
for endpoint in endpoints:
if filter_value == None or endpoint[attr] == filter_value:
if not filter_value or endpoint[attr] == filter_value:
return endpoint['publicURL']
raise novaclient.exceptions.EndpointNotFound()

@ -792,3 +792,195 @@ def do_volume_detach(cs, args):
"""Detach a volume from a server."""
cs.volumes.delete_server_volume(_find_server(cs, args.server).id,
args.attachment_id)
def _print_floating_ip_list(floating_ips):
utils.print_list(floating_ips, ['Ip', 'Instance Id', 'Fixed Ip'])
@utils.arg('server', metavar='<server>', help='Name or ID of server.')
@utils.arg('address', metavar='<address>', help='IP Address.')
def do_add_floating_ip(cs, args):
"""Add a floating IP address to a server."""
server = _find_server(cs, args.server)
server.add_floating_ip(args.address)
@utils.arg('server', metavar='<server>', help='Name or ID of server.')
@utils.arg('address', metavar='<address>', help='IP Address.')
def do_remove_floating_ip(cs, args):
"""Remove a floating IP address from a server."""
server = _find_server(cs, args.server)
server.remove_floating_ip(args.address)
def do_floating_ip_create(cs, args):
"""Allocate a floating IP for the current tenant."""
_print_floating_ip_list([cs.floating_ips.create()])
@utils.arg('address', metavar='<address>', help='IP of Floating Ip.')
def do_floating_ip_delete(cs, args):
"""De-allocate a floating IP."""
floating_ips = cs.floating_ips.list()
for floating_ip in floating_ips:
if floating_ip.ip == args.address:
return cs.floating_ips.delete(floating_ip.id)
raise exceptions.CommandError("Floating ip %s not found.", args.address)
def do_floating_ip_list(cs, args):
"""List floating ips for this tenant."""
_print_floating_ip_list(cs.floating_ips.list())
def _print_secgroup_rules(rules):
class FormattedRule:
def __init__(self, obj):
items = (obj if isinstance(obj, dict) else obj._info).items()
for k, v in items:
if k == 'ip_range':
v = v.get('cidr')
elif k == 'group':
k = 'source_group'
v = v.get('name')
if v == None:
v = ''
setattr(self, k, v)
rules = [FormattedRule(rule) for rule in rules]
utils.print_list(rules, ['IP Protocol', 'From Port', 'To Port',
'IP Range', 'Source Group'])
def _print_secgroups(secgroups):
utils.print_list(secgroups, ['Name', 'Description'])
def _get_secgroup(cs, secgroup):
for s in cs.security_groups.list():
if secgroup == s.name:
return s
raise exceptions.CommandError("Secgroup %s not found" % secgroup)
@utils.arg('secgroup', metavar='<secgroup>', help='ID of security group.')
@utils.arg('ip_proto', metavar='<ip_proto>', help='ip_proto (icmp, tcp, udp).')
@utils.arg('from_port', metavar='<from_port>', help='Port at start of range.')
@utils.arg('to_port', metavar='<to_port>', help='Port at end of range.')
@utils.arg('cidr', metavar='<cidr>', help='CIDR for address range.')
def do_secgroup_add_rule(cs, args):
"""Add a rule to a security group."""
secgroup = _get_secgroup(cs, args.secgroup)
rule = cs.security_group_rules.create(secgroup.id,
args.ip_proto,
args.from_port,
args.to_port,
args.cidr)
_print_secgroup_rules([rule])
@utils.arg('secgroup', metavar='<secgroup>', help='ID of security group.')
@utils.arg('ip_proto', metavar='<ip_proto>', help='ip_proto (icmp, tcp, udp).')
@utils.arg('from_port', metavar='<from_port>', help='Port at start of range.')
@utils.arg('to_port', metavar='<to_port>', help='Port at end of range.')
@utils.arg('cidr', metavar='<cidr>', help='CIDR for address range.')
def do_secgroup_delete_rule(cs, args):
"""Delete a rule from a security group."""
secgroup = _get_secgroup(cs, args.secgroup)
for rule in secgroup.rules:
if (rule['ip_protocol'] == args.ip_proto and
rule['from_port'] == int(args.from_port) and
rule['to_port'] == int(args.to_port) and
rule['ip_range']['cidr'] == args.cidr):
return cs.security_group_rules.delete(rule['id'])
raise exceptions.CommandError("Rule not found")
@utils.arg('name', metavar='<name>', help='Name of security group.')
@utils.arg('description', metavar='<description>',
help='Description of security group.')
def do_secgroup_create(cs, args):
"""Create a security group."""
_print_secgroups([cs.security_groups.create(args.name, args.description)])
@utils.arg('secgroup', metavar='<secgroup>', help='Name of security group.')
def do_secgroup_delete(cs, args):
"""Delete a security group."""
cs.security_groups.delete(_get_secgroup(cs, args.secgroup))
def do_secgroup_list(cs, args):
"""List security groups for the curent tenant."""
_print_secgroups(cs.security_groups.list())
@utils.arg('secgroup', metavar='<secgroup>', help='Name of security group.')
def do_secgroup_list_rules(cs, args):
"""List rules for a security group."""
secgroup = _get_secgroup(cs, args.secgroup)
_print_secgroup_rules(secgroup.rules)
@utils.arg('secgroup', metavar='<secgroup>', help='ID of security group.')
@utils.arg('source_group', metavar='<source_group>',
help='ID of source group.')
@utils.arg('--ip_proto', metavar='<ip_proto>',
help='ip_proto (icmp, tcp, udp).')
@utils.arg('--from_port', metavar='<from_port>',
help='Port at start of range.')
@utils.arg('--to_port', metavar='<to_port>', help='Port at end of range.')
def do_secgroup_add_group_rule(cs, args):
"""Add a source group rule to a security group."""
secgroup = _get_secgroup(cs, args.secgroup)
source_group = _get_secgroup(cs, args.source_group)
params = {}
params['group_id'] = source_group.id
if args.ip_proto or args.from_port or args.to_port:
if not (args.ip_proto and args.from_port and args.to_port):
raise exceptions.CommandError("ip_proto, from_port, and to_port"
" must be specified together")
params['ip_protocol'] = args.ip_proto
params['from_port'] = args.from_port
params['to_port'] = args.to_port
rule = cs.security_group_rules.create(secgroup.id, **params)
_print_secgroup_rules([rule])
@utils.arg('secgroup', metavar='<secgroup>', help='ID of security group.')
@utils.arg('source_group', metavar='<source_group>',
help='ID of source group.')
@utils.arg('--ip_proto', metavar='<ip_proto>',
help='ip_proto (icmp, tcp, udp).')
@utils.arg('--from_port', metavar='<from_port>',
help='Port at start of range.')
@utils.arg('--to_port', metavar='<to_port>', help='Port at end of range.')
def do_secgroup_delete_group_rule(cs, args):
"""Delete a source group rule from a security group."""
secgroup = _get_secgroup(cs, args.secgroup)
source_group = _get_secgroup(cs, args.source_group)
params = {}
params['group_name'] = source_group.name
if args.ip_proto or args.from_port or args.to_port:
if not (args.ip_proto and args.from_port and args.to_port):
raise exceptions.CommandError("ip_proto, from_port, and to_port"
" must be specified together")
params['ip_protocol'] = args.ip_proto
params['from_port'] = int(args.from_port)
params['to_port'] = int(args.to_port)
for rule in secgroup.rules:
if (rule.get('ip_protocol') == params.get('ip_protocol') and
rule.get('from_port') == params.get('from_port') and
rule.get('to_port') == params.get('to_port') and
rule.get('group', {}).get('name') ==\
params.get('group_name')):
return cs.security_group_rules.delete(rule['id'])
raise exceptions.CommandError("Rule not found")

@ -45,7 +45,7 @@ class AuthenticateAgainstKeystoneTests(utils.TestCase):
body = {'auth': {
'passwordCredentials': {'username': cs.client.user,
'password': cs.client.apikey},
'tenantId': cs.client.projectid, }}
'tenantName': cs.client.projectid, }}
token_url = urlparse.urljoin(cs.client.auth_url, "tokens")
mock_request.assert_called_with(token_url, "POST",
@ -117,7 +117,7 @@ class AuthenticateAgainstKeystoneTests(utils.TestCase):
body = {'auth': {
'passwordCredentials': {'username': cs.client.user,
'password': cs.client.apikey},
'tenantId': cs.client.projectid,}}
'tenantName': cs.client.projectid,}}
token_url = urlparse.urljoin(cs.client.auth_url, "tokens")
mock_request.assert_called_with(token_url, "POST",