Fixed setting the custom MTU at TF clouds by updating the ports
The MOS 22.5 cloud does not have TF version which can set the
custom MTU during the network creation. This is available only
since TF R21.4. Till that time, we can use the old way of
updating the VM ports, see [1].
Also, added the workaround for mtu field at MOS TF clouds
At the MOS TF clouds, the network payload from the API does not
containt 'mtu' attribute. Thus, we need to do the workaround. Here,
the workaround is done similarly to the CLI: it sets None for the mtu.
Also, made some PEP8 and codestile fixes.
[1] https://supportportal.juniper.net/s/article/Contrail-How-to-update-the-interface-MTU-of-an-exsiting-VMI-with-DHCP-option?language=en_US
Related-PROD: PROD-36943
Change-Id: Ieeb0dc50a29b3145b9ffaae2b7673543983fd915
diff --git a/utils/os_client.py b/utils/os_client.py
index 03462dc..9b128c5 100644
--- a/utils/os_client.py
+++ b/utils/os_client.py
@@ -408,6 +408,10 @@
"Must be an integer at 'custom_mtu' in "
"global_config.yaml.\n{}".format(mtu, e))
net = self.os_clients.network.create_network(net_body)['network']
+ # WA for TF because the network object does not have 'mtu' field by
+ # default, so this blocked running tests at TF envs with default MTU
+ if 'mtu' not in net:
+ net['mtu'] = None
logger.info("Created internal network {}".format(net_name))
return net
@@ -465,3 +469,26 @@
msg = "Could not delete a Floating IP, UUID {}. Error: {}" \
"".format(floatingip_id, e)
logger.info(msg)
+
+ def is_cloud_tf(self):
+ # Detect the TF cloud by assuming it does not have any neutron
+ # agents (404 in response)
+ try:
+ self.os_clients.network.list_agents()
+ except neutron_common.exceptions.NotFound:
+ logger.info("MOS TF cloud is detected.")
+ return True
+ return False
+
+ def update_network_port_with_custom_mtu(self, vm_uuid, custom_mtu):
+ port_uuid = self.os_clients.network.list_ports(
+ device_id=vm_uuid).get("ports")[0]["id"]
+ body = {"port": {"extra_dhcp_opts": [
+ {"opt_name": "interface-mtu", "opt_value": str(custom_mtu)}]}}
+ try:
+ self.os_clients.network.update_port(port_uuid, body)
+ except Exception as e:
+ raise Exception("Could not set custom MTU by updating the port. "
+ "See detailed error: {}".format(e))
+ logger.info("The port {} is updated with custom MTU {}."
+ "".format(port_uuid, custom_mtu))