-
Notifications
You must be signed in to change notification settings - Fork 24
[minor_change] Add nd_rest as a new generic ND REST API module. (DCNE-242) #109
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
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
90941c0
[minor_change] Add nd_rest as a new generic ND REST API module.
gmicol eb47277
[ignore] Update every aspect of the nd_rest module. Add PATCH method …
gmicol d618ae9
[ignore] Modify Documentation and YAML/JSON import/validation logic s…
gmicol 27399a5
[ignore] Add variable for keys to be sanitized in constants.py file.
gmicol 43106fa
[ignore] Modify Error message when failed to parse JSON payload.
gmicol 98456d7
[ignore] Replace the sanitize function using the two previous small s…
gmicol d61cdf6
[ignore] Add extra examples in EXAMPLES section and modify error mess…
gmicol 1af1af1
[ignore] Add pagination options to nd_rest module.
gmicol 2e77a8b
[ignore] Remove pagination as the URL query param pageSize is not wor…
gmicol File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,273 @@ | ||
| #!/usr/bin/python | ||
| # -*- coding: utf-8 -*- | ||
|
|
||
| # Copyright: (c) 2024, Gaspard Micol (@gmicol) <gmicol@cisco.com> | ||
| # GNU General Public License v3.0+ (see LICENSE or https://www.gnu.org/licenses/gpl-3.0.txt) | ||
|
|
||
| from __future__ import absolute_import, division, print_function | ||
|
|
||
| __metaclass__ = type | ||
|
|
||
| ANSIBLE_METADATA = {"metadata_version": "1.1", "status": ["preview"], "supported_by": "community"} | ||
|
|
||
| DOCUMENTATION = r""" | ||
| --- | ||
| module: nd_rest | ||
| short_description: Allows direct access to the Cisco Nexus Dashboard REST API | ||
| description: | ||
| - Enables the management of Cisco Nexus Dashboard (ND) through direct access to the Cisco ND REST API. | ||
| author: | ||
| - Gaspard Micol (@gmicol) | ||
| options: | ||
| method: | ||
| description: | ||
| - The HTTP method specifying the type of action to be performed in the request. | ||
| - Use C(delete) for removing objects. | ||
| - Use C(get) to retrieve or query objects. | ||
| - Use C(post) to create new objects. | ||
| - Use C(put) to update or replace existing objects entirely. | ||
| - Use C(patch) to apply partial modifications to existing objects. | ||
| type: str | ||
| choices: [ delete, get, post, put, patch, DELETE, GET, POST, PUT, PATCH ] | ||
anvitha-jain marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| default: get | ||
| aliases: [ action ] | ||
| path: | ||
| description: | ||
| - The URI being used to execute API calls. | ||
| type: str | ||
| required: true | ||
| aliases: [ uri ] | ||
| content: | ||
gmicol marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| description: | ||
| - Sets the payload of the API request directly. | ||
| type: raw | ||
| aliases: [ payload ] | ||
| file_path: | ||
| description: | ||
| - The file path containing the body of the HTTP request. | ||
| type: path | ||
| aliases: [ config_file ] | ||
| extends_documentation_fragment: | ||
gmicol marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| - cisco.nd.modules | ||
| - cisco.nd.check_mode | ||
| notes: | ||
| - All payloads to the REST interface must be in YAML (if PyYAML package is installed) or JSON format using this module. | ||
gmicol marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| - For Cisco Nexus Dashboard REST API references, visit U(https://developer.cisco.com/docs/nexus-dashboard/latest/api-reference/) | ||
| """ | ||
|
|
||
| EXAMPLES = r""" | ||
gmicol marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| - name: Create Security Domain using POST method by providing a JSON payload | ||
| cisco.nd.nd_rest: | ||
| host: nd | ||
| username: admin | ||
| password: SomeSecretPassword | ||
| path: /nexus/infra/api/aaa/v4/securitydomains | ||
| method: post | ||
| content: | ||
| { | ||
| "spec": { | ||
| "description": "Security Domain Test for nd_rest module.", | ||
| "name": "ansible_security_domain_test" | ||
| } | ||
| } | ||
|
|
||
| - name: Create Security Domain using POST method by providing a YAML payload | ||
| cisco.nd.nd_rest: | ||
| host: nd | ||
| username: admin | ||
| password: SomeSecretPassword | ||
| path: /nexus/infra/api/aaa/v4/securitydomains | ||
| method: post | ||
| content: | ||
| spec: | ||
| description: "Security Domain Test for nd_rest module." | ||
| name: "ansible_security_domain_test" | ||
|
|
||
| - name: Create Security Domain with POST method by providing a JSON file | ||
| cisco.nd.nd_rest: | ||
| host: nd | ||
| username: admin | ||
| password: SomeSecretPassword | ||
| path: /nexus/infra/api/aaa/v4/securitydomains | ||
| method: post | ||
| file_path: "path/to/json/file.json" | ||
|
|
||
| - name: Update Security Domain using PUT method by providing a JSON payload | ||
| cisco.nd.nd_rest: | ||
| host: nd | ||
| username: admin | ||
| password: SomeSecretPassword | ||
| path: /nexus/infra/api/aaa/v4/securitydomains/ansible_security_domain_test | ||
| method: put | ||
| content: | ||
| { | ||
| "spec": { | ||
| "description": "Updated Security Domain Test for nd_rest module." | ||
| } | ||
| } | ||
|
|
||
| - name: Query Security Domain using GET method | ||
gmicol marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| cisco.nd.nd_rest: | ||
| host: nd | ||
| username: admin | ||
| password: SomeSecretPassword | ||
| path: /nexus/infra/api/aaa/v4/securitydomains/ansible_security_domain_test | ||
| method: get | ||
| register: query_one | ||
|
|
||
| - name: Query all Security Domains using GET method | ||
| cisco.nd.nd_rest: | ||
| host: nd | ||
| username: admin | ||
| password: SomeSecretPassword | ||
| path: /nexus/infra/api/aaa/v4/securitydomains | ||
| method: get | ||
| register: query_all | ||
|
|
||
| - name: Query all Security Domains ordered by name using GET method | ||
| cisco.nd.nd_rest: | ||
| host: nd | ||
| username: admin | ||
| password: SomeSecretPassword | ||
| path: /nexus/infra/api/aaa/v4/securitydomains?orderBy=spec.name | ||
| method: get | ||
| register: query_all_ordered_by_name | ||
|
|
||
| - name: Remove Security Domain using DELETE method | ||
| cisco.nd.nd_rest: | ||
| host: nd | ||
| username: admin | ||
| password: SomeSecretPassword | ||
| path: /nexus/infra/api/aaa/v4/securitydomains/ansible_security_domain_test | ||
| method: delete | ||
|
|
||
| - name: Create a Fabric Policy Template on NDO using POST method by providing a JSON payload | ||
| cisco.nd.nd_rest: | ||
| host: nd | ||
| username: admin | ||
| password: SomeSecretPassword | ||
| path: /mso/api/v1/templates | ||
| method: post | ||
| content: | ||
| { | ||
| "displayName": "ansible_nd_rest_fabric_policies_template", | ||
| "fabricPolicyTemplate": {}, | ||
| "templateType": "fabricPolicy" | ||
| } | ||
| register: create_fabric_policies_template | ||
|
|
||
| - name: Update Fabric Policy Template on NDO using PATCH method by providing a JSON payload | ||
| cisco.nd.nd_rest: | ||
| host: nd | ||
| username: admin | ||
| password: SomeSecretPassword | ||
| path: "/mso/api/v1/templates/{{ create_fabric_policies_template.current.templateId }}" | ||
| method: patch | ||
| content: | ||
| [ | ||
| { | ||
| "op": "replace", | ||
| "path": "/fabricPolicyTemplate/template/domains", | ||
| "value": [ | ||
| { | ||
| "name": "ansible_nd_rest_physical_domain", | ||
| "description": "Ansible nd_rest Physical Domain test for PATCH", | ||
| "pool": "" | ||
| } | ||
| ] | ||
| } | ||
| ] | ||
| """ | ||
|
|
||
| RETURN = r""" | ||
| """ | ||
|
|
||
| import json | ||
| import os | ||
|
|
||
| # Optional, only used for YAML validation | ||
| try: | ||
| import yaml | ||
|
|
||
| HAS_YAML = True | ||
| except Exception: | ||
| HAS_YAML = False | ||
|
|
||
| from ansible.module_utils.basic import AnsibleModule | ||
| from ansible_collections.cisco.nd.plugins.module_utils.nd import NDModule, nd_argument_spec, sanitize | ||
| from ansible_collections.cisco.nd.plugins.module_utils.constants import ND_REST_KEYS_TO_SANITIZE | ||
| from ansible.module_utils._text import to_text | ||
|
|
||
|
|
||
| def main(): | ||
| argument_spec = nd_argument_spec() | ||
| argument_spec.update( | ||
| path=dict(type="str", required=True, aliases=["uri"]), | ||
| method=dict( | ||
| type="str", | ||
| default="get", | ||
| choices=["delete", "get", "post", "put", "patch", "DELETE", "GET", "POST", "PUT", "PATCH"], | ||
| aliases=["action"], | ||
| ), | ||
| content=dict(type="raw", aliases=["payload"]), | ||
| file_path=dict(type="path", aliases=["config_file"]), | ||
| ) | ||
|
|
||
| module = AnsibleModule( | ||
| argument_spec=argument_spec, | ||
| supports_check_mode=True, | ||
| ) | ||
|
|
||
| content = module.params.get("content") | ||
| path = module.params.get("path") | ||
| file_path = module.params.get("config_file") | ||
|
|
||
| nd = NDModule(module) | ||
|
|
||
| # Report missing file | ||
| if file_path and not os.path.isfile(file_path): | ||
akinross marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| module.fail_json(msg="Cannot find/access file '{0}'".format(file_path)) | ||
|
|
||
| if content and isinstance(content, str): | ||
| try: | ||
| if HAS_YAML: | ||
akinross marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| # Validate YAML/JSON string with PyYAML package | ||
| content = yaml.safe_load(content) | ||
| else: | ||
| # Validate JSON string only with json Python package | ||
| content = json.loads(content) | ||
| except Exception as e: | ||
| error_msg = ( | ||
| "Failed to parse provided YAML/JSON payload: {0}".format(to_text(e)) | ||
| if HAS_YAML | ||
| else "Failed to parse provided JSON payload: {0}. If a YAML payload was provided, the PyYAML package is required to be installed.".format( | ||
| to_text(e) | ||
| ) | ||
| ) | ||
| module.fail_json(msg=error_msg, payload=content) | ||
|
|
||
| method = nd.params.get("method").upper() | ||
gmicol marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| # Append previous state of the object | ||
| if method in ("PUT", "DELETE", "PATCH"): | ||
| nd.existing = nd.previous = sanitize(nd.query_obj(path, ignore_not_found_error=True), ND_REST_KEYS_TO_SANITIZE) | ||
| nd.result["previous"] = nd.previous | ||
|
|
||
| # Perform request | ||
| if module.check_mode: | ||
| nd.result["jsondata"] = content | ||
| else: | ||
| nd.result["jsondata"] = nd.request(path, method=method, data=content, file=file_path) | ||
| nd.existing = sanitize(nd.result["jsondata"], ND_REST_KEYS_TO_SANITIZE) | ||
|
|
||
| # Report changes for idempotency depending on methods | ||
| nd.result["status"] = nd.status | ||
| if sanitize(nd.result["jsondata"], ND_REST_KEYS_TO_SANITIZE) != nd.previous and method != "GET": | ||
| nd.result["changed"] = True | ||
|
|
||
| # Report success | ||
| nd.exit_json(**nd.result) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.