Skip to content

Commit

Permalink
add detach interface calling active parameter
Browse files Browse the repository at this point in the history
add api get switch db record by portid

Signed-off-by: wngzhe <[email protected]>

 Correcting the improper usage of optional parameters.
  • Loading branch information
wngzhe committed Jan 15, 2024
1 parent e054eaf commit b1a2209
Show file tree
Hide file tree
Showing 11 changed files with 109 additions and 5 deletions.
6 changes: 6 additions & 0 deletions doc/source/parameters.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
24 changes: 24 additions & 0 deletions doc/source/restapi.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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

13 changes: 13 additions & 0 deletions zvmconnector/restclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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': {
Expand Down Expand Up @@ -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,
Expand Down
10 changes: 10 additions & 0 deletions zvmsdk/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions zvmsdk/networkops.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
3 changes: 3 additions & 0 deletions zvmsdk/sdkwsgi/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,9 @@
'DELETE': vswitch.vswitch_delete,
'PUT': vswitch.vswitch_update,
}),
('/vswitch', {
'GET': vswitch.get_switch_info,
}),
)


Expand Down
23 changes: 23 additions & 0 deletions zvmsdk/sdkwsgi/handlers/vswitch.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
3 changes: 3 additions & 0 deletions zvmsdk/smtclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
8 changes: 8 additions & 0 deletions zvmsdk/tests/fvt/api_templates/test_get_switch_record.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"rs": 0,
"overallRC": 0,
"modID": null,
"rc": 0,
"errmsg": "",
"output": {['TEST0045', '4000', 'VSTEST', 'da19c4b3-905b-4844-898e-e36813be796e', None]}
}
17 changes: 14 additions & 3 deletions zvmsdk/tests/unit/sdkclientcases/test_restclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -665,16 +665,27 @@ 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
request.return_value = self.response
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)
Expand Down
4 changes: 2 additions & 2 deletions zvmsdk/tests/unit/sdkwsgi/handlers/test_guest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ''
Expand All @@ -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')
Expand Down

0 comments on commit b1a2209

Please sign in to comment.