diff --git a/doc/source/parameters.yaml b/doc/source/parameters.yaml index c07da8540..d7daae92b 100644 --- a/doc/source/parameters.yaml +++ b/doc/source/parameters.yaml @@ -1511,3 +1511,9 @@ nic_tx: in: body required: true type: integer +switch: + description: | + Get Switch information based on port id. + in: body + required: true + type: string diff --git a/doc/source/restapi.rst b/doc/source/restapi.rst index e857dd041..2f8c20753 100644 --- a/doc/source/restapi.rst +++ b/doc/source/restapi.rst @@ -2103,3 +2103,27 @@ Export file from Feilong, internal use only. The response body contains the raw binary data that represents the actual file. The Content-Type header contains the application/octet-stream value. + + +Get Switch information based on port id +-------------------- + +**GET /switch + +Get Switch information based on port id. + +* Request: + +.. restapi_parameters:: parameters.yaml + + - portid: port id + +* Response code: + + HTTP status code 200 on success. + +* Response sample: + +.. literalinclude:: ../../zvmsdk/tests/fvt/api_templates/test_get_switch_record.tpl + :language: javascript + diff --git a/zvmconnector/restclient.py b/zvmconnector/restclient.py index eeb1bf586..a3f842954 100644 --- a/zvmconnector/restclient.py +++ b/zvmconnector/restclient.py @@ -666,6 +666,13 @@ def req_vswitch_create(start_index, *args, **kwargs): return url, body +def req_get_switch_info(start_index, *args, **kwargs): + url = '/vswitch' + body = {'portid': args[start_index]} + fill_kwargs_in_body(body, **kwargs) + return url, body + + def req_vswitch_delete(start_index, *args, **kwargs): url = '/vswitches/%s' body = None @@ -886,6 +893,7 @@ def req_vswitch_set_vlan_id_for_user(start_index, *args, **kwargs): 'guest_delete_network_interface': { 'method': 'DELETE', 'args_required': 3, + 'args_optional': 1, 'params_path': 1, 'request': req_guest_delete_network_interface}, 'guest_get_power_state': { @@ -1055,6 +1063,11 @@ def req_vswitch_set_vlan_id_for_user(start_index, *args, **kwargs): 'args_required': 1, 'params_path': 0, 'request': req_vswitch_create}, + 'get_switch_info': { + 'method': 'GET', + 'args_required': 1, + 'params_path': 0, + 'request': req_get_switch_info}, 'vswitch_delete': { 'method': 'DELETE', 'args_required': 1, diff --git a/zvmsdk/api.py b/zvmsdk/api.py index 6383eedea..71b91db74 100755 --- a/zvmsdk/api.py +++ b/zvmsdk/api.py @@ -1411,6 +1411,16 @@ def vswitch_revoke_user(self, vswitch_name, userid): """ self._networkops.revoke_user_from_vswitch(vswitch_name, userid) + def get_switch_info(self, portid): + """get switch information based on portid + + :param str portid: the id of the port + :returns: list includes switch db record information. + [{'userid': 'WGNZ0046', 'interface': '4000', 'switch': 'VSICIC', + 'port': '61343f8c-dc88-41b2-be74-ad81fe018856', 'comments': None}] + """ + return self._networkops.get_switch_info(portid) + @check_guest_exist(check_index=1) def vswitch_set_vlan_id_for_user(self, vswitch_name, userid, vlan_id): """Set vlan id for user when connecting to the vswitch diff --git a/zvmsdk/networkops.py b/zvmsdk/networkops.py index 812462083..4c3ce7f39 100644 --- a/zvmsdk/networkops.py +++ b/zvmsdk/networkops.py @@ -106,6 +106,9 @@ def delete_nic(self, userid, vdev, active=False): self._smtclient.delete_nic(userid, vdev, active=active) + def get_switch_info(self, portid): + return self._smtclient.get_switch_info(portid) + def network_configuration(self, userid, os_version, network_info, active=False): if self._smtclient.is_rhcos(os_version): diff --git a/zvmsdk/sdkwsgi/handler.py b/zvmsdk/sdkwsgi/handler.py index 0507e0183..c99ef5da7 100644 --- a/zvmsdk/sdkwsgi/handler.py +++ b/zvmsdk/sdkwsgi/handler.py @@ -165,6 +165,9 @@ 'DELETE': vswitch.vswitch_delete, 'PUT': vswitch.vswitch_update, }), + ('/vswitch', { + 'GET': vswitch.get_switch_info, + }), ) diff --git a/zvmsdk/sdkwsgi/handlers/vswitch.py b/zvmsdk/sdkwsgi/handlers/vswitch.py index 8a5268c21..9beb006e8 100644 --- a/zvmsdk/sdkwsgi/handlers/vswitch.py +++ b/zvmsdk/sdkwsgi/handlers/vswitch.py @@ -104,6 +104,10 @@ def update(self, name, body): name, userid, vlanid) return info + def host_get_switch_info(self, portid): + info = self.client.send_request('get_switch_info', portid) + return info + def get_action(): global _VSWITCHACTION @@ -206,3 +210,22 @@ def _vswitch_query(name): additional_handler=util.handle_not_found) req.response.content_type = 'application/json' return req.response + + +@util.SdkWsgify +@tokens.validate +def get_switch_info(req): + + def _get_switch_info(req): + action = get_action() + req_str = req.body.decode('utf-8') + json_data = json.loads(req_str) + portid = json_data.get('portid', None) + return action.host_get_switch_info(portid) + + info = _get_switch_info(req) + info_json = json.dumps(info) + req.response.body = utils.to_utf8(info_json) + req.response.content_type = 'application/json' + req.response.status = util.get_http_code_from_sdk_return(info) + return req.response diff --git a/zvmsdk/smtclient.py b/zvmsdk/smtclient.py index 81bb631a8..f8ff0276c 100644 --- a/zvmsdk/smtclient.py +++ b/zvmsdk/smtclient.py @@ -1471,6 +1471,9 @@ def _get_unpackdiskimage_cmd_rhcos(self, userid, image_name, image_file, transportfiles, image_disk_type, nic_id, fixed_ip_parameter] + def get_switch_info(self, portid): + return self._NetDbOperator.switch_select_record(None, portid, None) + def grant_user_to_vswitch(self, vswitch_name, userid): """Set vswitch to grant user.""" smt_userid = zvmutils.get_smt_userid() diff --git a/zvmsdk/tests/fvt/api_templates/test_get_switch_record.tpl b/zvmsdk/tests/fvt/api_templates/test_get_switch_record.tpl new file mode 100644 index 000000000..4265252f8 --- /dev/null +++ b/zvmsdk/tests/fvt/api_templates/test_get_switch_record.tpl @@ -0,0 +1,8 @@ +{ + "rs": 0, + "overallRC": 0, + "modID": null, + "rc": 0, + "errmsg": "", + "output": {['TEST0045', '4000', 'VSTEST', 'da19c4b3-905b-4844-898e-e36813be796e', None]} +} diff --git a/zvmsdk/tests/unit/sdkclientcases/test_restclient.py b/zvmsdk/tests/unit/sdkclientcases/test_restclient.py index 8c1a06dc5..3999a08ae 100644 --- a/zvmsdk/tests/unit/sdkclientcases/test_restclient.py +++ b/zvmsdk/tests/unit/sdkclientcases/test_restclient.py @@ -665,8 +665,7 @@ def test_guest_delete_network_interface(self, get_token, request): method = 'DELETE' url = '/guests/%s/interface' % self.fake_userid body = {'interface': {'os_version': 'rhel7.2', - 'vdev': '123', - 'active': False}} + 'vdev': '123'}} body = json.dumps(body) header = self.headers full_uri = self.base_url + url @@ -674,7 +673,19 @@ def test_guest_delete_network_interface(self, get_token, request): get_token.return_value = self._tmp_token() self.client.call("guest_delete_network_interface", self.fake_userid, - 'rhel7.2', '123', active=False) + 'rhel7.2', '123', False) + request.assert_called_with(method, full_uri, + data=body, headers=header, + verify=False) + request.reset_mock() + kwargs = {'active': True} + self.client.call("guest_delete_network_interface", self.fake_userid, + 'rhel7.2', '123', **kwargs) + body = {'interface': {'os_version': 'rhel7.2', + 'vdev': '123', + 'active': True}} + body = json.dumps(body) + request.return_value = self.response request.assert_called_with(method, full_uri, data=body, headers=header, verify=False) diff --git a/zvmsdk/tests/unit/sdkwsgi/handlers/test_guest.py b/zvmsdk/tests/unit/sdkwsgi/handlers/test_guest.py index 3eb565cb6..ea0136640 100644 --- a/zvmsdk/tests/unit/sdkwsgi/handlers/test_guest.py +++ b/zvmsdk/tests/unit/sdkwsgi/handlers/test_guest.py @@ -907,7 +907,7 @@ def test_guest_delete_network_interface(self, mock_interface, mock_userid): os_version = 'rhel6' vdev = '1000' bstr = """{"interface": {"os_version": "rhel6", - "vdev": "1000"}}""" + "vdev": "1000", "active": "True"}}""" self.req.body = bstr mock_userid.return_value = FAKE_USERID mock_interface.return_value = '' @@ -918,7 +918,7 @@ def test_guest_delete_network_interface(self, mock_interface, mock_userid): FAKE_USERID, os_version, vdev, - active=False) + active=True) @mock.patch.object(util, 'wsgi_path_item') @mock.patch('zvmconnector.connector.ZVMConnector.send_request')