Use Neutron API to handle security group
Change-Id: Ib19a634c8527b63a5e012a826fab3b082206b340
This commit is contained in:
parent
e7804e253c
commit
4da0dbba41
104
vmtp/compute.py
104
vmtp/compute.py
@ -20,12 +20,12 @@ import time
|
|||||||
|
|
||||||
import glanceclient.exc as glance_exception
|
import glanceclient.exc as glance_exception
|
||||||
import novaclient
|
import novaclient
|
||||||
import novaclient.exceptions as exceptions
|
|
||||||
|
|
||||||
class Compute(object):
|
class Compute(object):
|
||||||
|
|
||||||
def __init__(self, nova_client, config):
|
def __init__(self, nova_client, neutron_client, config):
|
||||||
self.novaclient = nova_client
|
self.novaclient = nova_client
|
||||||
|
self.neutronclient = neutron_client
|
||||||
self.config = config
|
self.config = config
|
||||||
|
|
||||||
def find_image(self, image_name):
|
def find_image(self, image_name):
|
||||||
@ -153,7 +153,7 @@ class Compute(object):
|
|||||||
retry_count=10):
|
retry_count=10):
|
||||||
|
|
||||||
if sec_group:
|
if sec_group:
|
||||||
security_groups = [sec_group.id]
|
security_groups = [sec_group['id']]
|
||||||
else:
|
else:
|
||||||
security_groups = None
|
security_groups = None
|
||||||
# Also attach the created security group for the test
|
# Also attach the created security group for the test
|
||||||
@ -384,72 +384,60 @@ class Compute(object):
|
|||||||
# Create a new security group with appropriate rules
|
# Create a new security group with appropriate rules
|
||||||
def security_group_create(self):
|
def security_group_create(self):
|
||||||
# check first the security group exists
|
# check first the security group exists
|
||||||
# May throw exceptions.NoUniqueMatch or NotFound
|
sec_groups = self.neutronclient.list_security_groups()['security_groups']
|
||||||
try:
|
group = [x for x in sec_groups if x['name'] == self.config.security_group_name]
|
||||||
group = self.novaclient.security_groups.find(name=self.config.security_group_name)
|
if len(group) > 0:
|
||||||
return group
|
return group[0]
|
||||||
except exceptions.NotFound:
|
|
||||||
group = self.novaclient.security_groups.create(name=self.config.security_group_name,
|
body = {
|
||||||
description="PNS Security group")
|
'security_group': {
|
||||||
# Once security group try to find it iteratively
|
'name': self.config.security_group_name,
|
||||||
# (this check may no longer be necessary)
|
'description': 'PNS Security Group'
|
||||||
for _ in range(self.config.generic_retry_count):
|
}
|
||||||
group = self.novaclient.security_groups.get(group)
|
}
|
||||||
if group:
|
group = self.neutronclient.create_security_group(body)['security_group']
|
||||||
self.security_group_add_rules(group)
|
self.security_group_add_rules(group)
|
||||||
return group
|
return group
|
||||||
else:
|
|
||||||
time.sleep(1)
|
|
||||||
return None
|
|
||||||
# except exceptions.NoUniqueMatch as exc:
|
|
||||||
# raise exc
|
|
||||||
|
|
||||||
# Delete a security group
|
# Delete a security group
|
||||||
def security_group_delete(self, group):
|
def security_group_delete(self, group):
|
||||||
if group:
|
if group:
|
||||||
print "Deleting security group"
|
print "Deleting security group"
|
||||||
self.novaclient.security_groups.delete(group)
|
self.neutronclient.delete_security_group(group['id'])
|
||||||
|
|
||||||
# Add rules to the security group
|
# Add rules to the security group
|
||||||
def security_group_add_rules(self, group):
|
def security_group_add_rules(self, group):
|
||||||
# Allow ping traffic
|
body = {
|
||||||
self.novaclient.security_group_rules.create(group.id,
|
'security_group_rule': {
|
||||||
ip_protocol="icmp",
|
'direction': 'ingress', 'security_group_id': group['id'], 'remote_group_id': None
|
||||||
from_port=-1,
|
}
|
||||||
to_port=-1)
|
}
|
||||||
if self.config.ipv6_mode:
|
if self.config.ipv6_mode:
|
||||||
self.novaclient.security_group_rules.create(group.id,
|
body['security_group_rule']['ethertype'] = 'IPv6'
|
||||||
ip_protocol="icmp",
|
body['security_group_rule']['remote_ip_prefix'] = '::/0'
|
||||||
from_port=-1,
|
else:
|
||||||
to_port=-1,
|
body['security_group_rule']['ethertype'] = 'IPv4'
|
||||||
cidr="::/0")
|
body['security_group_rule']['remote_ip_prefix'] = '0.0.0.0/0'
|
||||||
|
|
||||||
|
# Allow ping traffic
|
||||||
|
body['security_group_rule']['protocol'] = 'icmp'
|
||||||
|
body['security_group_rule']['port_range_min'] = None
|
||||||
|
body['security_group_rule']['port_range_max'] = None
|
||||||
|
self.neutronclient.create_security_group_rule(body)
|
||||||
|
|
||||||
# Allow SSH traffic
|
# Allow SSH traffic
|
||||||
self.novaclient.security_group_rules.create(group.id,
|
body['security_group_rule']['protocol'] = 'tcp'
|
||||||
ip_protocol="tcp",
|
body['security_group_rule']['port_range_min'] = 22
|
||||||
from_port=22,
|
body['security_group_rule']['port_range_max'] = 22
|
||||||
to_port=22)
|
self.neutronclient.create_security_group_rule(body)
|
||||||
|
|
||||||
# Allow TCP/UDP traffic for perf tools like iperf/nuttcp
|
# Allow TCP/UDP traffic for perf tools like iperf/nuttcp
|
||||||
# 5001: Data traffic (standard iperf data port)
|
# 5001: Data traffic (standard iperf data port)
|
||||||
# 5002: Control traffic (non standard)
|
# 5002: Control traffic (non standard)
|
||||||
# note that 5000/tcp is already picked by openstack keystone
|
# note that 5000/tcp is already picked by openstack keystone
|
||||||
if not self.config.ipv6_mode:
|
body['security_group_rule']['protocol'] = 'tcp'
|
||||||
self.novaclient.security_group_rules.create(group.id,
|
body['security_group_rule']['port_range_min'] = 5001
|
||||||
ip_protocol="tcp",
|
body['security_group_rule']['port_range_max'] = 5002
|
||||||
from_port=5001,
|
self.neutronclient.create_security_group_rule(body)
|
||||||
to_port=5002)
|
body['security_group_rule']['protocol'] = 'udp'
|
||||||
self.novaclient.security_group_rules.create(group.id,
|
self.neutronclient.create_security_group_rule(body)
|
||||||
ip_protocol="udp",
|
|
||||||
from_port=5001,
|
|
||||||
to_port=5001)
|
|
||||||
else:
|
|
||||||
# IPV6 rules addition
|
|
||||||
self.novaclient.security_group_rules.create(group.id,
|
|
||||||
ip_protocol="tcp",
|
|
||||||
from_port=5001,
|
|
||||||
to_port=5002,
|
|
||||||
cidr="::/0")
|
|
||||||
self.novaclient.security_group_rules.create(group.id,
|
|
||||||
ip_protocol="udp",
|
|
||||||
from_port=5001,
|
|
||||||
to_port=5001,
|
|
||||||
cidr="::/0")
|
|
||||||
|
@ -201,7 +201,7 @@ class VmtpTest(object):
|
|||||||
nova_client = Client(**creds_nova)
|
nova_client = Client(**creds_nova)
|
||||||
neutron = neutronclient.Client(**creds)
|
neutron = neutronclient.Client(**creds)
|
||||||
|
|
||||||
self.comp = compute.Compute(nova_client, self.config)
|
self.comp = compute.Compute(nova_client, neutron, self.config)
|
||||||
|
|
||||||
# Add the appropriate public key to openstack
|
# Add the appropriate public key to openstack
|
||||||
self.comp.init_key_pair(self.config.public_key_name, self.instance_access)
|
self.comp.init_key_pair(self.config.public_key_name, self.instance_access)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user