def ensure_vlan_bridge(session, vif, cluster=None, create_vlan=True):
 vlan_num = vif['network'].get_meta('vlan')
 bridge = vif['network']['bridge']
 vlan_interface = CONF.vmware.vlan_interface
 network_ref = network_util.get_network_with_the_name(session, bridge, cluster)
 if (network_ref and (network_ref['type'] == 'DistributedVirtualPortgroup')):
  return network_ref
 if (not network_ref):
  vswitch_associated = _get_associated_vswitch_for_interface(session, vlan_interface, cluster)
  network_util.create_port_group(session, bridge, vswitch_associated, (vlan_num if create_vlan else 0), cluster)
  network_ref = network_util.get_network_with_the_name(session, bridge, cluster)
 elif create_vlan:
  vswitch_associated = _get_associated_vswitch_for_interface(session, vlan_interface, cluster)
  _get_pg_info = network_util.get_vlanid_and_vswitch_for_portgroup
  (pg_vlanid, pg_vswitch) = _get_pg_info(session, bridge, cluster)
  if (pg_vswitch != vswitch_associated):
   raise exception.InvalidVLANPortGroup(bridge=bridge, expected=vswitch_associated, actual=pg_vswitch)
  if (pg_vlanid != vlan_num):
   raise exception.InvalidVLANTag(bridge=bridge, tag=vlan_num, pgroup=pg_vlanid)
 return network_ref

def ensure_vlan_bridge ( session , vif , cluster = None , create_vlan = True ) : vlan_num = vif [ 'network' ] . get_meta ( 'vlan' ) bridge = vif [ 'network' ] [ 'bridge' ] vlan_interface = CONF . vmware . vlan_interface network_ref = network_util . get_network_with_the_name ( session , bridge , cluster ) if ( network_ref and ( network_ref [ 'type' ] == 'DistributedVirtualPortgroup' ) ) : return network_ref if ( not network_ref ) : vswitch_associated = _get_associated_vswitch_for_interface ( session , vlan_interface , cluster ) network_util . create_port_group ( session , bridge , vswitch_associated , ( vlan_num if create_vlan else 0 ) , cluster ) network_ref = network_util . get_network_with_the_name ( session , bridge , cluster ) elif create_vlan : vswitch_associated = _get_associated_vswitch_for_interface ( session , vlan_interface , cluster ) _get_pg_info = network_util . get_vlanid_and_vswitch_for_portgroup ( pg_vlanid , pg_vswitch ) = _get_pg_info ( session , bridge , cluster ) if ( pg_vswitch != vswitch_associated ) : raise exception . InvalidVLANPortGroup ( bridge = bridge , expected = vswitch_associated , actual = pg_vswitch ) if ( pg_vlanid != vlan_num ) : raise exception . InvalidVLANTag ( bridge = bridge , tag = vlan_num , pgroup = pg_vlanid ) return network_ref
create a vlan and bridge unless they already exist .