def setUp(self): super(SecuritygroupruleTestCase, self).setUp() self.securitygrouprule_client = securitygroups.SecuritygroupruleAPI() self.neutron = os_client.get_neutron_client() self.mox.StubOutWithMock(self.neutron, "create_security_group_rule") self.mox.StubOutWithMock(os_client, "get_neutron_client") os_client.get_neutron_client().AndReturn(self.neutron)
def network_create(self, name, cidr, gateway=None, dns_nameservers=None, ext_router=None): neutron = os_client.get_neutron_client() try: create_network_body = {"network": {"name": name}} network = neutron.create_network(create_network_body)["network"] create_subnet_body = { "subnet": { "network_id": network["id"], "ip_version": 4, "cidr": cidr} } if gateway: create_subnet_body["subnet"]["gateway_ip"] = gateway if dns_nameservers: create_subnet_body["subnet"][ "dns_nameservers"] = dns_nameservers subnet = neutron.create_subnet(create_subnet_body)["subnet"] if ext_router: add_interface_router_body = {"subnet_id": subnet["id"]} neutron.add_interface_router( ext_router, add_interface_router_body) except Exception as e: LOG.exception(e) raise exception.NetworkCreateFailed() return network["id"]
def securitygrouprule_create(self, neutron_securitygroup_id, protocol, port_range_min=None, port_range_max=None, remote_neutron_securitygroup_id=None, remote_ip_prefix=None, direction="ingress", ethertype="IPv4"): try: self.neutron = os_client.get_neutron_client() if remote_neutron_securitygroup_id: self.neutron.create_security_group_rule( {"security_group_rule": {"direction": direction, "ethertype": ethertype, "security_group_id": neutron_securitygroup_id, "protocol": protocol, "port_range_min": port_range_min or port_range_max, "port_range_max": port_range_max, "remote_group_id": remote_neutron_securitygroup_id, }}) elif remote_ip_prefix: self.neutron.create_security_group_rule( {"security_group_rule": {"direction": direction, "ethertype": ethertype, "security_group_id": neutron_securitygroup_id, "protocol": protocol, "port_range_min": port_range_min or port_range_max, "port_range_max": port_range_max, "remote_ip_prefix": remote_ip_prefix, }}) except Exception as e: LOG.exception(e) raise exception.SecuritygroupCreateFailed()
def securitygroup_delete(self, neutron_securitygroup_id): try: neutron = os_client.get_neutron_client() neutron.delete_security_group(neutron_securitygroup_id) except Exception as e: LOG.exception(e) raise exception.SecuritygroupDeleteFailed()
def network_create(self, name, cidr, gateway=None, ext_router=None, dns_nameservers=None): neutron = os_client.get_neutron_client() network_body = {"network": {"name": name}} network = neutron.create_network(network_body)["network"] try: subnet_body = { "subnet": { "network_id": network["id"], "ip_version": 4, "cidr": cidr } } if gateway: subnet_body["subnet"]["gateway_ip"] = gateway if dns_nameservers: subnet_body["subnet"]["dns_nameservers"] = dns_nameservers subnet = neutron.create_subnet(subnet_body)["subnet"] if ext_router: router_body = {"subnet_id": subnet["id"]} neutron.add_interface_router(ext_router, router_body) except Exception as e: neutron.delete_network(network['id']) raise e return dict(neutron_network_id=network["id"])
def network_list(self): neutron = os_client.get_neutron_client() networks = neutron.list_networks().get("networks") neutron_network_ids = [] for network in networks: neutron_network_ids.append(network.get("id")) return neutron_network_ids
def securitygroup_list(self): neutron = os_client.get_neutron_client() securitygroup_list = neutron.list_security_groups() neutron_securitygroup_ids = [] for securitygroup in securitygroup_list['security_groups']: neutron_securitygroup_ids.append(securitygroup['id']) return neutron_securitygroup_ids
def securitygroup_create(self, name): try: neutron = os_client.get_neutron_client() res = neutron.create_security_group({"security_group": {"name": name}}) neutron_securitygroup_id = res['security_group']['id'] except Exception as e: LOG.exception(e) raise exception.SecuritygroupCreateFailed() return neutron_securitygroup_id
def network_delete(self, neutron_network_id, ext_router=None): neutron = os_client.get_neutron_client() if ext_router: network = neutron.show_network(neutron_network_id)["network"] subnets = network["subnets"] for subnet in subnets: neutron.remove_interface_router(ext_router, {"subnet_id": subnet}) neutron.delete_network(neutron_network_id)
def network_delete(self, neutron_network_id, ext_router=None): neutron = os_client.get_neutron_client() try: if ext_router: network = neutron.show_network(neutron_network_id)["network"] subnets = network["subnets"] for subnet in subnets: neutron.remove_interface_router( ext_router, {"subnet_id": subnet}) neutron.delete_network(neutron_network_id) except Exception as e: LOG.exception(e) raise exception.NetworkDeleteFailed()
def securitygroup_create(self, name, rules): neutron = os_client.get_neutron_client() body = {"security_group": {"name": name}} securitygroup = neutron.create_security_group(body)['security_group'] neutron_securitygroup_id = securitygroup['id'] def _securitygroup_rule_create(neutron_securitygroup_id, protocol, port_range_min=None, port_range_max=None, remote_neutron_securitygroup_id=None, remote_ip_prefix=None): body = { "security_group_rule": { "direction": "ingress", "ethertype": "IPv4", "security_group_id": neutron_securitygroup_id, "protocol": protocol, "port_range_min": port_range_min or port_range_max, "port_range_max": port_range_max, } } if remote_neutron_securitygroup_id: body['security_group_rule']['remote_group_id'] =\ remote_neutron_securitygroup_id elif remote_ip_prefix: body['security_group_rule']['remote_ip_prefix'] =\ remote_ip_prefix neutron.create_security_group_rule(body) if rules: try: for rule in rules: _securitygroup_rule_create(neutron_securitygroup_id, **rule) except Exception as e: neutron.delete_security_group(neutron_securitygroup_id) raise e return dict(neutron_securitygroup_id=neutron_securitygroup_id)
def setUp(self): super(NetworkTestCase, self).setUp() self.network_client = networks.NetworkAPI() self.neutron_mock = self.mox.CreateMock(neutron_client.Client) self.mox.StubOutWithMock(os_client, "get_neutron_client") os_client.get_neutron_client().AndReturn(self.neutron_mock)
def network_show(self, neutron_network_id): neutron = os_client.get_neutron_client() return neutron.show_network(neutron_network_id)
def securitygroup_delete(self, neutron_securitygroup_id): neutron = os_client.get_neutron_client() neutron.delete_security_group(neutron_securitygroup_id)
def securitygroup_get(self, securitygroup_id): neutron = os_client.get_neutron_client() securitygroup = neutron.show_security_group(securitygroup_id) return securitygroup['security_group']['id']