Rename URL components for native DHCP support

Also update metadata route (with VM's IP as next_hop)
when users change the IP of a VM.

Change-Id: Iab1daee0c6a0c4f45b6a057cc5c52e7a23551a5a
This commit is contained in:
Shih-Hao Li 2016-06-24 16:20:13 -07:00
parent 03eea64ef9
commit 90301b487b
3 changed files with 38 additions and 23 deletions

View File

@ -440,19 +440,6 @@ class LogicalRouterPort(AbstractRESTResource):
operation="get router link port") operation="get router link port")
class DhcpProfile(AbstractRESTResource):
@property
def uri_segment(self):
return 'dhcp/server-profiles'
def create(self, *args, **kwargs):
pass
def update(self, uuid, *args, **kwargs):
pass
class MetaDataProxy(AbstractRESTResource): class MetaDataProxy(AbstractRESTResource):
@property @property
@ -466,11 +453,24 @@ class MetaDataProxy(AbstractRESTResource):
pass pass
class DhcpProfile(AbstractRESTResource):
@property
def uri_segment(self):
return 'dhcp/server-profiles'
def create(self, *args, **kwargs):
pass
def update(self, uuid, *args, **kwargs):
pass
class LogicalDhcpServer(AbstractRESTResource): class LogicalDhcpServer(AbstractRESTResource):
@property @property
def uri_segment(self): def uri_segment(self):
return 'dhcp/services' return 'dhcp/servers'
def _construct_server(self, body, dhcp_profile_id=None, server_ip=None, def _construct_server(self, body, dhcp_profile_id=None, server_ip=None,
dns_servers=None, domain_name=None, gateway_ip=None, dns_servers=None, domain_name=None, gateway_ip=None,
@ -517,11 +517,11 @@ class LogicalDhcpServer(AbstractRESTResource):
body['lease_time'] = lease_time body['lease_time'] = lease_time
if options: if options:
body['options'] = options body['options'] = options
url = "%s/bindings" % server_uuid url = "%s/static-bindings" % server_uuid
return self._client.url_post(url, body) return self._client.url_post(url, body)
def get_binding(self, server_uuid, binding_uuid): def get_binding(self, server_uuid, binding_uuid):
url = "%s/bindings/%s" % (server_uuid, binding_uuid) url = "%s/static-bindings/%s" % (server_uuid, binding_uuid)
return self._client.url_get(url) return self._client.url_get(url)
@utils.retry_upon_exception_nsxv3( @utils.retry_upon_exception_nsxv3(
@ -530,9 +530,9 @@ class LogicalDhcpServer(AbstractRESTResource):
def update_binding(self, server_uuid, binding_uuid, **kwargs): def update_binding(self, server_uuid, binding_uuid, **kwargs):
body = self.get_binding(server_uuid, binding_uuid) body = self.get_binding(server_uuid, binding_uuid)
body.update(kwargs) body.update(kwargs)
url = "%s/bindings/%s" % (server_uuid, binding_uuid) url = "%s/static-bindings/%s" % (server_uuid, binding_uuid)
return self._client.url_put(url, body) return self._client.url_put(url, body)
def delete_binding(self, server_uuid, binding_uuid): def delete_binding(self, server_uuid, binding_uuid):
url = "%s/bindings/%s" % (server_uuid, binding_uuid) url = "%s/static-bindings/%s" % (server_uuid, binding_uuid)
return self._client.url_delete(url) return self._client.url_delete(url)

View File

@ -1459,7 +1459,8 @@ class NsxV3Plugin(agentschedulers_db.AZDhcpAgentSchedulerDbMixin,
bindings = nsx_db.get_nsx_dhcp_bindings(context.session, bindings = nsx_db.get_nsx_dhcp_bindings(context.session,
old_port['id']) old_port['id'])
if ip_change: if ip_change:
# If IP address is changed, update associated DHCP bindings. # If IP address is changed, update associated DHCP bindings,
# metadata route, and default hostname.
# Mac address (if changed) will be updated at the same time. # Mac address (if changed) will be updated at the same time.
if ([subnet_id for (subnet_id, ip) in ips_to_add] == if ([subnet_id for (subnet_id, ip) in ips_to_add] ==
[subnet_id for (subnet_id, ip) in ips_to_delete]): [subnet_id for (subnet_id, ip) in ips_to_delete]):
@ -1496,9 +1497,14 @@ class NsxV3Plugin(agentschedulers_db.AZDhcpAgentSchedulerDbMixin,
def _update_dhcp_binding_on_server(self, context, binding, mac, ip): def _update_dhcp_binding_on_server(self, context, binding, mac, ip):
try: try:
data = {'mac_address': mac, 'ip_address': ip}
if ip != binding['ip_address']:
data['host_name'] = 'host-%s' % ip.replace('.', '-')
data['options'] = {'option121': {'static_routes': [
{'network': '%s' % nsx_rpc.METADATA_DHCP_ROUTE,
'next_hop': ip}]}}
self._dhcp_server.update_binding( self._dhcp_server.update_binding(
binding['nsx_service_id'], binding['nsx_binding_id'], binding['nsx_service_id'], binding['nsx_binding_id'], **data)
mac_address=mac, ip_address=ip)
LOG.info(_LI("Updated static binding (mac: %(mac)s, ip: %(ip)s) " LOG.info(_LI("Updated static binding (mac: %(mac)s, ip: %(ip)s) "
"for port %(port)s on logical DHCP server " "for port %(port)s on logical DHCP server "
"%(server)s"), "%(server)s"),

View File

@ -1050,7 +1050,11 @@ class NsxNativeDhcpTestCase(NsxV3PluginTestCaseMixin):
new_ip = '10.0.0.4' new_ip = '10.0.0.4'
update_data = {'port': {'fixed_ips': [ update_data = {'port': {'fixed_ips': [
{'subnet_id': subnet['subnet']['id'], 'ip_address': new_ip}]}} {'subnet_id': subnet['subnet']['id'], 'ip_address': new_ip}]}}
assert_data = {'ip_address': new_ip} assert_data = {'host_name': 'host-%s' % new_ip.replace('.', '-'),
'ip_address': new_ip,
'options': {'option121': {'static_routes': [
{'network': '%s' % nsx_rpc.METADATA_DHCP_ROUTE,
'next_hop': new_ip}]}}}
self._verify_dhcp_binding(subnet, port_data, update_data, self._verify_dhcp_binding(subnet, port_data, update_data,
assert_data) assert_data)
@ -1076,7 +1080,12 @@ class NsxNativeDhcpTestCase(NsxV3PluginTestCaseMixin):
new_ip = '10.0.0.4' new_ip = '10.0.0.4'
update_data = {'port': {'mac_address': new_mac, 'fixed_ips': [ update_data = {'port': {'mac_address': new_mac, 'fixed_ips': [
{'subnet_id': subnet['subnet']['id'], 'ip_address': new_ip}]}} {'subnet_id': subnet['subnet']['id'], 'ip_address': new_ip}]}}
assert_data = {'mac_address': new_mac, 'ip_address': new_ip} assert_data = {'host_name': 'host-%s' % new_ip.replace('.', '-'),
'mac_address': new_mac,
'ip_address': new_ip,
'options': {'option121': {'static_routes': [
{'network': '%s' % nsx_rpc.METADATA_DHCP_ROUTE,
'next_hop': new_ip}]}}}
self._verify_dhcp_binding(subnet, port_data, update_data, self._verify_dhcp_binding(subnet, port_data, update_data,
assert_data) assert_data)