From d46b65f982e1f9c51ee407a6fb31f32956540123 Mon Sep 17 00:00:00 2001 From: Ildar Kamalov Date: Thu, 13 Dec 2018 14:38:00 +0300 Subject: [PATCH] Add enable/disable for DHCP server --- client/src/__locales/en.json | 3 +- client/src/actions/index.js | 9 +-- client/src/api/Api.js | 63 +++---------------- client/src/components/Settings/Dhcp/Leases.js | 2 +- client/src/components/Settings/Dhcp/index.js | 8 +-- client/src/reducers/index.js | 4 ++ config.go | 2 +- 7 files changed, 27 insertions(+), 64 deletions(-) diff --git a/client/src/__locales/en.json b/client/src/__locales/en.json index d20555fb..0148b7ea 100644 --- a/client/src/__locales/en.json +++ b/client/src/__locales/en.json @@ -138,5 +138,6 @@ "dhcp_disable": "Disable DHCP server", "dhcp_not_found": "No active DHCP servers found on the network. It is safe to enable the built-in DHCP server.", "dhcp_leases": "DHCP leases", - "dhcp_leases_not_found": "No DHCP leases found" + "dhcp_leases_not_found": "No DHCP leases found", + "dhcp_config_saved": "Saved DHCP server config" } diff --git a/client/src/actions/index.js b/client/src/actions/index.js index cd56647e..e23ccb81 100644 --- a/client/src/actions/index.js +++ b/client/src/actions/index.js @@ -546,6 +546,7 @@ export const setDhcpConfig = config => async (dispatch) => { dispatch(setDhcpConfigRequest()); try { await apiClient.setDhcpConfig(config); + dispatch(addSuccessToast('dhcp_config_saved')); dispatch(setDhcpConfigSuccess()); } catch (error) { dispatch(addErrorToast({ error })); @@ -572,17 +573,17 @@ export const toggleDhcpRequest = createAction('TOGGLE_DHCP_REQUEST'); export const toggleDhcpFailure = createAction('TOGGLE_DHCP_FAILURE'); export const toggleDhcpSuccess = createAction('TOGGLE_DHCP_SUCCESS'); -export const toggleDhcp = status => async (dispatch) => { +export const toggleDhcp = config => async (dispatch) => { dispatch(toggleDhcpRequest()); let successMessage = ''; try { - if (status) { + if (config.enabled) { successMessage = 'disabled_dhcp'; - await apiClient.disableGlobalProtection(); + await apiClient.setDhcpConfig({ ...config, enabled: false }); } else { successMessage = 'enabled_dhcp'; - await apiClient.enableGlobalProtection(); + await apiClient.setDhcpConfig({ ...config, enabled: true }); } dispatch(addSuccessToast(successMessage)); diff --git a/client/src/api/Api.js b/client/src/api/Api.js index 966b5199..f7b363e9 100644 --- a/client/src/api/Api.js +++ b/client/src/api/Api.js @@ -309,64 +309,21 @@ export default class Api { DHCP_FIND_ACTIVE = { path: 'dhcp/find_active_dhcp', method: 'GET' }; getDhcpStatus() { - // const { path, method } = this.DHCP_STATUS; - // return this.makeRequest(path, method); - - const example = { - config: { - enabled: false, - gateway_ip: '192.168.1.1', - subnet_mask: '255.255.255.0', - range_start: '192.168.1.2', - range_end: '192.168.10.50', - lease_duration: '43200', - }, - leases: [ - { - mac: '001109b3b3b8', - ip: '192.168.1.22', - hostname: 'dell', - expires: '2017-07-21T17:32:28Z', - }, - { - mac: '001109b3b3b9', - ip: '192.168.1.23', - hostname: 'dell', - expires: '2017-07-21T17:32:28Z', - }, - ], - }; - - return new Promise((resolve) => { - setTimeout(() => { - resolve(example); - }, 1000); - }); + const { path, method } = this.DHCP_STATUS; + return this.makeRequest(path, method); } setDhcpConfig(config) { - // const { path, method } = this.DHCP_SET_CONFIG; - // const parameters = config; - // return this.makeRequest(path, method, parameters); - - return new Promise((resolve) => { - setTimeout(() => { - resolve(window.alert(`Set config:\n\n${JSON.stringify(config, null, 2)}`)); - }, 1000); - }); + const { path, method } = this.DHCP_SET_CONFIG; + const parameters = { + data: config, + headers: { 'Content-Type': 'application/json' }, + }; + return this.makeRequest(path, method, parameters); } findActiveDhcp() { - // const { path, method } = this.DHCP_FIND_ACTIVE; - // return this.makeRequest(path, method); - - return new Promise((resolve) => { - setTimeout(() => { - resolve({ - gateway_ip: '127.0.0.1', - found: true, - }); - }, 10000); - }); + const { path, method } = this.DHCP_FIND_ACTIVE; + return this.makeRequest(path, method); } } diff --git a/client/src/components/Settings/Dhcp/Leases.js b/client/src/components/Settings/Dhcp/Leases.js index 89959946..562f3243 100644 --- a/client/src/components/Settings/Dhcp/Leases.js +++ b/client/src/components/Settings/Dhcp/Leases.js @@ -19,7 +19,7 @@ const columns = [{ const Leases = props => ( { - const { enabled } = this.props.dhcp.config; - const buttonText = enabled ? 'dhcp_disable' : 'dhcp_enable'; - const buttonClass = enabled ? 'btn-gray' : 'btn-success'; + const { config } = this.props.dhcp; + const buttonText = config.enabled ? 'dhcp_disable' : 'dhcp_enable'; + const buttonClass = config.enabled ? 'btn-gray' : 'btn-success'; return ( - ); diff --git a/client/src/reducers/index.js b/client/src/reducers/index.js index 0702a06a..6f0f61d4 100644 --- a/client/src/reducers/index.js +++ b/client/src/reducers/index.js @@ -289,6 +289,10 @@ const dhcp = handleActions({ }, { processing: true, processingStatus: false, + config: { + enabled: false, + }, + leases: [], }); export default combineReducers({ diff --git a/config.go b/config.go index 6f01d023..9cd54ddf 100644 --- a/config.go +++ b/config.go @@ -58,7 +58,7 @@ type dhcpState struct { // field ordering is important -- yaml fields will mirror ordering from here type dhcpConfig struct { - Enabled bool + Enabled bool `json:"enabled" yaml:"enabled"` GatewayIP string `json:"gateway_ip" yaml:"gateway_ip"` SubnetMask string `json:"subnet_mask" yaml:"subnet_mask"` RangeStart string `json:"range_start" yaml:"range_start"`