:return: Returns the created port.
:rtype: :class:`heat.resources.port`
"""
- port = self.find_port_by_name_or_id(name)
- if port is not None and not stack_operation:
- LOG.warning(
- "Creating port with name %s failed, as it already exists" % name)
- raise Exception("Port with name %s already exists." % name)
- LOG.debug("Creating port with name %s" % name)
port = Port(name)
if not stack_operation:
self.ports[port.id] = port
:return: Returns the port reference if it was found or None
:rtype: :class:`heat.resources.port`
"""
+ # find by id
if name_or_id in self.ports:
return self.ports[name_or_id]
- for port in self.ports.values():
- if port.name == name_or_id or port.template_name == name_or_id:
- return port
-
+ # find by name
+ matching_ports = filter(
+ lambda port: port.name == name_or_id or port.template_name == name_or_id,
+ self.ports.values()
+ )
+ matching_ports_count = len(matching_ports)
+ if matching_ports_count == 1:
+ return matching_ports[0]
+ if matching_ports_count > 1:
+ raise RuntimeError("Ambiguous port name %s" % name_or_id)
return None
def delete_port(self, name_or_id):
url, data=createportdata, headers=headers)
self.assertEqual(createportresponse.status_code, 201)
print(createportresponse.content)
- self.assertEqual(json.loads(createportresponse.content)[
- "port"]["name"], "new_port")
+ createport = json.loads(createportresponse.content)["port"]
+ self.assertEqual(createport["name"], "new_port")
print(" ")
print('->>>>>>> test Neutron Create Port With Existing Name ->>>>>>>>>>>>>>>')
print('->>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>')
url = "http://0.0.0.0:19696/v2.0/ports"
- createportwithexistingnamedata = '{"port": {"name": "new_port", "network_id": "%s"} }' % (
- json.loads(createnetworkresponse.content)["network"]["id"])
- createportwithexistingnameresponse = requests.post(
+ network_id = json.loads(createnetworkresponse.content)["network"]["id"]
+ createportwithexistingnamedata = '{"port": {"name": "duplicate_port_name", "network_id": "%s"} }' % network_id
+ createportwithexistingnameresponse1 = requests.post(
+ url, data=createportwithexistingnamedata, headers=headers)
+ createportwithexistingnameresponse2 = requests.post(
url, data=createportwithexistingnamedata, headers=headers)
- self.assertEqual(createportwithexistingnameresponse.status_code, 500)
+ createportwithexistingname1 = json.loads(createportwithexistingnameresponse1.content)["port"]
+ createportwithexistingname2 = json.loads(createportwithexistingnameresponse2.content)["port"]
+ self.assertEqual(createportwithexistingnameresponse1.status_code, 201)
+ self.assertEqual(createportwithexistingnameresponse2.status_code, 201)
+ self.assertEqual(createportwithexistingname1["name"], "duplicate_port_name")
+ self.assertEqual(createportwithexistingname2["name"], "duplicate_port_name")
+ self.assertNotEqual(createportwithexistingname1["id"], createportwithexistingname2["id"], "Duplicate port should have different id")
print(" ")
print('->>>>>>> test Neutron Create Port Without Name ->>>>>>>>>>>>>>>')