Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nsxt_rest execution module and unit test #239

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/ref/modules/all.rst
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Execution Modules
saltext.vmware.modules.nsxt_policy_segment
saltext.vmware.modules.nsxt_policy_tier0
saltext.vmware.modules.nsxt_policy_tier1
saltext.vmware.modules.nsxt_rest
saltext.vmware.modules.nsxt_transport_node
saltext.vmware.modules.nsxt_transport_node_profiles
saltext.vmware.modules.nsxt_transport_zone
Expand Down
6 changes: 6 additions & 0 deletions docs/ref/modules/saltext.vmware.modules.nsxt_rest.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

saltext.vmware.modules.nsxt_rest
================================

.. automodule:: saltext.vmware.modules.nsxt_rest
:members:
85 changes: 85 additions & 0 deletions src/saltext/vmware/modules/nsxt_rest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
"""
Salt execution Module to interact with NSX-T REST APIs

"""
import logging

from saltext.vmware.utils import nsxt_request

log = logging.getLogger(__name__)

__virtual_name__ = "nsxt_rest"


def call_api(
url,
username,
password,
method="get",
cert_common_name=None,
verify_ssl=True,
cert=None,
data=None,
params=None,
):
"""
Used to execute NSX-T REST API calls. In case of error, function returns a dictionary with respective error.
Sample error returned : {'error': 'Error message'}

CLI Example:

.. code-block:: bash

salt vm_minion nsxt_rest.call_api method=get url=https://nsxt-instance/ ...

url
Url or api endpoint to access

username
Username to connect to NSX-T manager

password
Password to connect to NSX-T manager


method
Http request method. Default is 'get'. Accepted values: get, post, put, patch, delete..

verify_ssl
Option to enable/disable SSL verification. Enabled by default.
If set to False, the certificate validation is skipped.

cert
Path to the SSL client certificate file to connect to NSX-T manager.
The certificate can be retrieved from browser.

cert_common_name
(Optional) By default, the hostname parameter and the common name in certificate is compared for host name
verification. If the client certificate common name and hostname do not match (in case of self-signed
certificates), specify the certificate common name as part of this parameter. This value is then used to
compare against

data
(Optional) Data to be sent to the specified url. Request body

params
(Optional) Query parameters for the request. Usually a dictionary with key-value pairs

"""
log.info("{} call for url: {}".format(method.upper(), url))
log.info("data: {}\n params: {}".format(data, params))
response = nsxt_request.call_api(
url=url,
username=username,
password=password,
verify_ssl=verify_ssl,
cert=cert,
cert_common_name=cert_common_name,
method=method,
data=data,
params=params,
)
if response:
return response
else:
return "{} call is success".format(method.upper())
45 changes: 45 additions & 0 deletions tests/unit/modules/test_nsxt_rest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
"""
Tests for nsxt_rest module
"""
import logging
from unittest.mock import patch

from saltext.vmware.modules import nsxt_rest
from saltext.vmware.utils import nsxt_request

log = logging.getLogger(__name__)


@patch.object(nsxt_request, "call_api")
def test_call_post_api_should_return_response(mock_call_api):
response = {"results": {"key": "value"}}
mock_call_api.return_value = response

assert (
nsxt_rest.call_api(
url="https://nsxt-vmware.local/api/to/call",
method="post",
username="user",
password="pass",
data={},
verify_ssl=False,
)
== response
)


@patch.object(nsxt_request, "call_api")
def test_call_delete_api_with_return_none(mock_call_api):
mock_call_api.return_value = None
method = "delete"

assert (
nsxt_rest.call_api(
url="https://nsxt-vmware.local/api/to/call",
method=method,
username="user",
password="pass",
verify_ssl=False,
)
== "{} call is success".format(method.upper())
)