2222 get_openstack_id ,
2323 add_list_to_runtime_properties ,
2424 is_external_relationship ,
25- is_external_relationship_not_conditionally_created
25+ is_external_relationship_not_conditionally_created ,
26+ get_single_connected_node_by_openstack_type ,
27+ OPENSTACK_ID_PROPERTY
2628)
2729from openstack_plugin_common .floatingip import (
2830 use_external_floatingip ,
2931 set_floatingip_runtime_properties ,
3032 delete_floatingip ,
3133 floatingip_creation_validation
3234)
35+ from network import NETWORK_OPENSTACK_TYPE
3336
3437FLOATINGIP_OPENSTACK_TYPE = 'floatingip'
38+ FLOATING_NETWORK_ERROR_PREFIX = \
39+ 'Network name must be specified by either a floating_network_name, a ' \
40+ 'floating_network_id, or a relationship to a Network node template '
41+ FLOATING_NETWORK_ERROR_SUFFIX = \
42+ '(provided: network from relationships={}, floatingip={})'
43+ FLOATING_NETWORK_ERROR_MSG = FLOATING_NETWORK_ERROR_PREFIX + \
44+ FLOATING_NETWORK_ERROR_SUFFIX
3545
3646
3747@operation
@@ -47,19 +57,46 @@ def create(neutron_client, args, **kwargs):
4757 }
4858 floatingip .update (ctx .node .properties [FLOATINGIP_OPENSTACK_TYPE ], ** args )
4959
50- # Sugar: floating_network_name -> (resolve) -> floating_network_id
51- if 'floating_network_name' in floatingip :
60+ # Do we have a relationship with a network?
61+
62+ connected_network = \
63+ get_single_connected_node_by_openstack_type (
64+ ctx , NETWORK_OPENSTACK_TYPE , True )
65+
66+ if connected_network :
67+ network_from_rel = connected_network .runtime_properties [
68+ OPENSTACK_ID_PROPERTY ]
69+ else :
70+ network_from_rel = None
71+
72+ # TODO: Should we check whether this is really an "external" network?
73+
74+ network_name_provided = 'floating_network_name' in floatingip
75+ network_id_provided = 'floating_network_id' in floatingip
76+ provided = [network_name_provided ,
77+ network_id_provided ,
78+ network_from_rel is not None ].count (True )
79+
80+ # At most one is expected.
81+
82+ if provided > 1 :
83+ raise NonRecoverableError (FLOATING_NETWORK_ERROR_MSG .format (
84+ network_from_rel , floatingip ))
85+
86+ if network_from_rel :
87+ floatingip ['floating_network_id' ] = network_from_rel
88+ elif network_name_provided :
5289 floatingip ['floating_network_id' ] = neutron_client .cosmo_get_named (
5390 'network' , floatingip ['floating_network_name' ])['id' ]
5491 del floatingip ['floating_network_name' ]
55- elif 'floating_network_id' not in floatingip :
92+ elif not network_id_provided :
5693 provider_context = provider (ctx )
5794 ext_network = provider_context .ext_network
5895 if ext_network :
5996 floatingip ['floating_network_id' ] = ext_network ['id' ]
6097 else :
61- raise NonRecoverableError (
62- 'Missing floating network id, name or external network' )
98+ raise NonRecoverableError (FLOATING_NETWORK_ERROR_MSG . format (
99+ None , None ) )
63100
64101 fip = neutron_client .create_floatingip (
65102 {FLOATINGIP_OPENSTACK_TYPE : floatingip })[FLOATINGIP_OPENSTACK_TYPE ]
0 commit comments