diff --git a/nutanix/ncp/plugins/module_utils/__int__.py b/nutanix/ncp/plugins/module_utils/__init__.py similarity index 100% rename from nutanix/ncp/plugins/module_utils/__int__.py rename to nutanix/ncp/plugins/module_utils/__init__.py diff --git a/nutanix/ncp/plugins/module_utils/base_module.py b/nutanix/ncp/plugins/module_utils/base_module.py index 8cd34e3a6..cbf2fd7d6 100644 --- a/nutanix/ncp/plugins/module_utils/base_module.py +++ b/nutanix/ncp/plugins/module_utils/base_module.py @@ -1,5 +1,7 @@ # Copyright: 2021, Ansible Project # Simplified BSD License (see licenses/simplified_bsd.txt or https://opensource.org/licenses/BSD-2-Clause ) +from __future__ import (absolute_import, division, print_function) +__metaclass__ = type from ansible.module_utils.basic import AnsibleModule diff --git a/nutanix/ncp/plugins/module_utils/entity.py b/nutanix/ncp/plugins/module_utils/entity.py index 55d316f52..f907b1e99 100644 --- a/nutanix/ncp/plugins/module_utils/entity.py +++ b/nutanix/ncp/plugins/module_utils/entity.py @@ -1,5 +1,7 @@ # Copyright: 2021, Ansible Project # Simplified BSD License (see licenses/simplified_bsd.txt or https://opensource.org/licenses/BSD-2-Clause ) +from __future__ import (absolute_import, division, print_function) +__metaclass__ = type from base64 import b64encode import time import uuid @@ -53,15 +55,18 @@ def parse_data(self): def check_response(self): if self.response.get("task_uuid") and self.wait: - task = self.validate_request(self.module, self.response.get("task_uuid"), self.netloc, self.wait_timeout) + task = self.validate_request(self.module, self.response.get( + "task_uuid"), self.netloc, self.wait_timeout) self.result["task_information"] = task if not self.response.get("status"): if self.response.get("api_response_list"): - self.response["status"] = self.response.get("api_response_list")[0].get("api_response").get("status") + self.response["status"] = self.response.get("api_response_list")[ + 0].get("api_response").get("status") elif "entities" in self.response: if self.response["entities"]: - self.response["status"] = self.response.get("entities")[0].get("status") + self.response["status"] = self.response.get("entities")[ + 0].get("status") else: self.response["status"] = {"state": "complete"} @@ -69,14 +74,16 @@ def check_response(self): state = self.response.get("status").get("state") if "pending" in state.lower() or "running" in state.lower(): task = self.validate_request(self.module, - self.response.get("status").get("execution_context").get("task_uuid"), + self.response.get("status").get( + "execution_context").get("task_uuid"), self.netloc, self.wait_timeout) self.response["status"]["state"] = task.get("status") self.result["task_information"] = task self.result["changed"] = True - status = self.response.get("state") or self.response.get("status").get("state") + status = self.response.get( + "state") or self.response.get("status").get("state") if status and status.lower() != "succeeded" or self.action == "list": self.result["changed"] = False if status.lower() != "complete": @@ -93,7 +100,8 @@ def update(self): self.url += "/" + str(uuid.UUID(item_uuid)) else: self.url += "/" + str(uuid.UUID(item_uuid)) + "/file" - response = self.send_request(self.module, "get", self.url, self.data, self.username, self.password) + response = self.send_request( + self.module, "get", self.url, self.data, self.username, self.password) if response.get("state") and response.get("state").lower() == "error": self.result["changed"] = False @@ -115,7 +123,7 @@ def list(self): def send_request(module, method, req_url, req_data, username, password, timeout=30): try: credentials = bytes(username + ":" + password, encoding="ascii") - except: + except BaseException: credentials = bytes(username + ":" + password).encode("ascii") encoded_credentials = b64encode(credentials).decode("ascii") @@ -129,7 +137,8 @@ def send_request(module, method, req_url, req_data, username, password, timeout= resp, info = fetch_url(module=module, url=req_url, headers=headers, method=method, data=module.jsonify(payload), timeout=timeout) if not 300 > info['status'] > 199: - module.fail_json(msg="Fail: %s" % ("Status: " + str(info['msg']) + ", Message: " + str(info.get('body')))) + module.fail_json(msg="Fail: %s" % ( + "Status: " + str(info['msg']) + ", Message: " + str(info.get('body')))) body = resp.read() if resp else info.get("body") try: @@ -146,7 +155,8 @@ def validate_request(self, module, task_uuid, netloc, wait_timeout): task_uuid = str(uuid.UUID(task_uuid)) url = self.generate_url_from_operations("tasks", netloc, [task_uuid]) while retries > 0 or not succeeded: - response = self.send_request(module, "get", url, None, self.username, self.password) + response = self.send_request( + module, "get", url, None, self.username, self.password) if response.get("status"): status = response.get("status") if "running" not in status.lower() and "queued" not in status.lower(): @@ -182,9 +192,10 @@ def get_action(self): if self.action in self.methods_of_actions.keys(): self.action = self.methods_of_actions[self.action] elif self.action == 'present': - self.action = 'update' if self.data['metadata'].get('uuid') else 'create' + self.action = 'update' if self.data['metadata'].get( + 'uuid') else 'create' else: - raise ValueError("Wrong action: "+ self.action) + raise ValueError("Wrong action: " + self.action) def get_spec(self): import yaml @@ -221,7 +232,8 @@ def clean_spec(self, spec): list_key = obj.pop('list_key') sub_spec_key = list_key.split('__')[-1] if list_key: - value = self.get_attr_spec(sub_spec_key, getattr(self, list_key, None)) + value = self.get_attr_spec( + sub_spec_key, getattr(self, list_key, None)) if value: spec[key] = value else: @@ -238,7 +250,8 @@ def build(self): self.parse_data() - self.url = self.generate_url_from_operations(self.module_name, self.netloc, self.operations) + self.url = self.generate_url_from_operations( + self.module_name, self.netloc, self.operations) self.get_action() @@ -272,7 +285,8 @@ def run_module(self, module): self.credentials = self.auth.get("credentials") if not self.url: - self.url = str(self.auth.get("ip_address")) + ":" + str(self.auth.get("port")) + self.url = str(self.auth.get("ip_address")) + \ + ":" + str(self.auth.get("port")) self.netloc = self.url self.module_name = module._name diff --git a/nutanix/ncp/plugins/module_utils/prism/__int__.py b/nutanix/ncp/plugins/module_utils/prism/__init__.py similarity index 100% rename from nutanix/ncp/plugins/module_utils/prism/__int__.py rename to nutanix/ncp/plugins/module_utils/prism/__init__.py diff --git a/nutanix/ncp/plugins/module_utils/prism/images.py b/nutanix/ncp/plugins/module_utils/prism/images.py index 583ed2b24..095fc8542 100644 --- a/nutanix/ncp/plugins/module_utils/prism/images.py +++ b/nutanix/ncp/plugins/module_utils/prism/images.py @@ -1,6 +1,7 @@ # This file is part of Ansible # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) - +from __future__ import (absolute_import, division, print_function) +__metaclass__ = type from .prism import Prism diff --git a/nutanix/ncp/plugins/module_utils/prism/subnets.py b/nutanix/ncp/plugins/module_utils/prism/subnets.py index 075886058..e9a39d763 100644 --- a/nutanix/ncp/plugins/module_utils/prism/subnets.py +++ b/nutanix/ncp/plugins/module_utils/prism/subnets.py @@ -1,9 +1,9 @@ # This file is part of Ansible # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) - +from __future__ import (absolute_import, division, print_function) +__metaclass__ = type from .prism import Prism class Subnet(Prism): kind = 'subnet' - diff --git a/nutanix/ncp/plugins/module_utils/prism/vms.py b/nutanix/ncp/plugins/module_utils/prism/vms.py index 1d8a65c56..7c01bbb34 100644 --- a/nutanix/ncp/plugins/module_utils/prism/vms.py +++ b/nutanix/ncp/plugins/module_utils/prism/vms.py @@ -1,5 +1,7 @@ # This file is part of Ansible # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) +from __future__ import (absolute_import, division, print_function) +__metaclass__ = type from copy import deepcopy from .prism import Prism @@ -25,7 +27,8 @@ def _get_api_spec(self, param_spec, **kwargs): pass def get_entity_by_name(self, name='', kind=''): - url = self.generate_url_from_operations(kind, netloc=self.url, ops=['list']) + url = self.generate_url_from_operations( + kind, netloc=self.url, ops=['list']) data = { 'filter': 'name==%s' % name, 'length': 1 @@ -47,15 +50,16 @@ def get_entity_by_name(self, name='', kind=''): class VMSpec(): - @staticmethod def get_default_spec(self): raise NotImplementedError( - "Get Default Spec helper not implemented for {}".format(self.entity_type) + "Get Default Spec helper not implemented for {0}".format( + self.entity_type) ) def _get_api_spec(self, param_spec, **kwargs): raise NotImplementedError( - "Get Api Spec helper not implemented for {}".format(self.entity_type) + "Get Api Spec helper not implemented for {0}".format( + self.entity_type) ) def remove_null_references(self, spec, parent_spec=None, spec_key=None): @@ -129,7 +133,8 @@ def _get_api_spec(self, param_spec, **kwargs): for disk_param in param_spec: disk_final = self.get_default_spec() if disk_param.get("clone_image"): - disk_final["data_source_reference"] = self.__get_image_ref(disk_param["clone_image"], **kwargs) + disk_final["data_source_reference"] = self.__get_image_ref( + disk_param["clone_image"], **kwargs) disk_final["device_properties"]["device_type"] = disk_param["type"] disk_final["device_properties"]["disk_address"]["adapter_type"] = disk_param["bus"] @@ -219,7 +224,8 @@ def _get_api_spec(self, param_spec, **kwargs): "uuid": v } elif k == "subnet_name" and not nic_param.get("subnet_uuid"): - nic_final["subnet_reference"] = self.__get_subnet_ref(v, **kwargs) + nic_final["subnet_reference"] = self.__get_subnet_ref( + v, **kwargs) elif k == "ip_endpoint_list" and bool(v): nic_final[k] = [{"ip": v[0]}] diff --git a/nutanix/ncp/plugins/modules/__int__.py b/nutanix/ncp/plugins/modules/__init__.py similarity index 100% rename from nutanix/ncp/plugins/modules/__int__.py rename to nutanix/ncp/plugins/modules/__init__.py diff --git a/nutanix/ncp/plugins/modules/nutanix_images.py b/nutanix/ncp/plugins/modules/nutanix_images.py index 3e37a9c9f..225477671 100644 --- a/nutanix/ncp/plugins/modules/nutanix_images.py +++ b/nutanix/ncp/plugins/modules/nutanix_images.py @@ -3,6 +3,8 @@ # Copyright: (c) 2021 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) from __future__ import (absolute_import, division, print_function) +from ..module_utils.prism.images import Image +from ..module_utils.base_module import BaseModule __metaclass__ = type @@ -114,40 +116,44 @@ ''' RETURN = r''' - -# CREATE /images -responses: -- default: Internal Error -- 202: Request Accepted - -# UPDATE /images/{uuid} -responses: -- default: Internal Error -- 404: Invalid UUID provided -- 202: Request Accepted - -# LIST /images/list -responses: -- default: Internal Error -- 200: Success - -# DELETE /images/{uuid} -responses: -- default: Internal Error -- 404: Invalid UUID provided -- 202: Request Accepted +CREATE: + description: CREATE /images Response for nutanix imagese + returned: (for CREATE /images operation) + type: str + sample: + - default Internal Error + - 202 Request Accepted +UPDATE: + description: UPDATE /images/{uuid} Response for nutanix images + returned: (for UPDATE /images operation) + type: str + sample: + - default Internal Error + - 404 Invalid UUID provided + - 202 Request Accepted +LIST: + description: LIST /images/list Response for nutanix imagese + returned: (for LIST /images operation) + type: str + sample: + - default Internal Error + - 200 Success +DELETE: + description: DELETE /images/{uuid} Response for nutanix images + returned: (for DELETE /images operation) + type: str + sample: + - default Internal Error + - 404 Invalid UUID provided + - 202 Request Accepted ''' -from ..module_utils.base_module import BaseModule -from ..module_utils.prism.images import Image - def run_module(): module = BaseModule() Image(module) - def main(): run_module() diff --git a/nutanix/ncp/plugins/modules/nutanix_subnets.py b/nutanix/ncp/plugins/modules/nutanix_subnets.py index 75f8585e1..2809a2f72 100644 --- a/nutanix/ncp/plugins/modules/nutanix_subnets.py +++ b/nutanix/ncp/plugins/modules/nutanix_subnets.py @@ -3,6 +3,8 @@ # Copyright: (c) 2021 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) from __future__ import (absolute_import, division, print_function) +from ..module_utils.prism.subnets import Subnet +from ..module_utils.base_module import BaseModule __metaclass__ = type @@ -98,33 +100,38 @@ ''' RETURN = r''' - -# CREATE /subnets -responses: -- default: Internal Error -- 202: Request Accepted - -# UPDATE /subnets/{uuid} -responses: -- default: Internal Error -- 404: Invalid UUID provided -- 202: Request Accepted - -# LIST /subnets/list -responses: -- default: Internal Error -- 200: Success - -# DELETE /subnets/{uuid} -responses: -- default: Internal Error -- 404: Invalid UUID provided -- 202: Request Accepted +CREATE: + description: CREATE /subnets Response for nutanix subnets + returned: (for CREATE /subnets operation) + type: str + sample: + - default Internal Error + - 202 Request Accepted +UPDATE: + description: UPDATE /subnets/{uuid} Response for nutanix subnets + returned: (for UPDATE /subnets operation) + type: str + sample: + - default Internal Error + - 404 Invalid UUID provided + - 202 Request Accepted +LIST: + description: LIST /subnets/list Response for nutanix subnets + returned: (for LIST /subnets operation) + type: str + sample: + - default Internal Error + - 200 Success +DELETE: + description: DELETE /subnets/{uuid} Response for nutanix subnets + returned: (for DELETE /subnets operation) + type: str + sample: + - default Internal Error + - 404 Invalid UUID provided + - 202 Request Accepted ''' -from ..module_utils.base_module import BaseModule -from ..module_utils.prism.subnets import Subnet - def run_module(): module = BaseModule() diff --git a/nutanix/ncp/plugins/modules/nutanix_vms.py b/nutanix/ncp/plugins/modules/nutanix_vms.py index 9494137e8..51af7ce69 100755 --- a/nutanix/ncp/plugins/modules/nutanix_vms.py +++ b/nutanix/ncp/plugins/modules/nutanix_vms.py @@ -3,6 +3,8 @@ # Copyright: (c) 2021 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) from __future__ import (absolute_import, division, print_function) +from ..module_utils.prism.vms import VM +from ..module_utils.base_module import BaseModule __metaclass__ = type @@ -48,33 +50,36 @@ RETURN = r''' ''' -from ..module_utils.base_module import BaseModule -from ..module_utils.prism.vms import VM - def run_module(): BaseModule.argument_spec.update(dict( spec__name=dict(type='str', required=False, aliases=['name']), - spec__description=dict(type='str', required=False, aliases=['desc', 'description']), + spec__description=dict(type='str', required=False, aliases=[ + 'desc', 'description']), metadata__uuid=dict(type='str', aliases=['uuid'], required=False), - spec__resources__num_sockets=dict(type='int', default=1, aliases=['core_count', 'vcpus']), + spec__resources__num_sockets=dict( + type='int', default=1, aliases=['core_count', 'vcpus']), spec__resources__num_threads_per_core=dict(type='int', # default=1,#will not provide aliases=['threads_per_core']), - spec__resources__num_vcpus_per_socket=dict(type='int', default=1, + spec__resources__num_vcpus_per_socket=dict(type='int', default=1, aliases=['num_vcpus_per_socket', 'cores_per_vcpu']), cluster=dict(type='dict', default={}, options=dict( - spec__cluster_reference__uuid=dict(type='str', aliases=['cluster_uuid', 'uuid'], required=False), - spec__cluster_reference__name=dict(type='str', aliases=['cluster_name', 'name'], required=False), + spec__cluster_reference__uuid=dict( + type='str', aliases=['cluster_uuid', 'uuid'], required=False), + spec__cluster_reference__name=dict( + type='str', aliases=['cluster_name', 'name'], required=False), spec__cluster_reference__kind=dict(type='str', aliases=['cluster_kind'], required=False, default='cluster'), ), - ), + ), spec__resources__nic_list=dict(type='list', aliases=['networks'], elements='dict', options=dict( uuid=dict(type='str', aliases=['nic_uuid']), subnet_uuid=dict(type='str'), subnet_name=dict(type='str'), subnet_kind=dict(type='str', default='subnet'), - is_connected=dict(type='bool', aliases=['connected'], default=False), - ip_endpoint_list=dict(type='list', aliases=['private_ip'], default=[]), + is_connected=dict(type='bool', aliases=[ + 'connected'], default=False), + ip_endpoint_list=dict(type='list', aliases=[ + 'private_ip'], default=[]), nic_type=dict(type='str', default='NORMAL_NIC'), ), default=[]), @@ -82,13 +87,14 @@ def run_module(): type=dict(type='str'), size_gb=dict(type='int'), bus=dict(type='str'), - storage_config=dict(type=dict,aliases=['storage_container'], options=dict( + storage_config=dict(type=dict, aliases=['storage_container'], options=dict( storage_container_name=dict(type='str'), - storage_container_uuid=dict(type='str',aliases=['uuid']) + storage_container_uuid=dict(type='str', aliases=['uuid']) )) ), default=[]), - spec__resources__hardware_clock_timezone=dict(type='str', default='UTC', aliases=['timezone']), + spec__resources__hardware_clock_timezone=dict( + type='str', default='UTC', aliases=['timezone']), spec__resources__boot_config__boot_type=dict(type='str', default='LEGACY', aliases=['boot_type']), @@ -100,10 +106,12 @@ def run_module(): ], aliases=['boot_device_order_list']), spec__resources__memory_overcommit_enabled=dict( type='bool', default=False, aliases=['memory_overcommit_enabled']), - spec__resources__memory_size_mib=dict(type='int', default=1, aliases=['memory_size_mib', 'memory_gb']), + spec__resources__memory_size_mib=dict(type='int', default=1, aliases=[ + 'memory_size_mib', 'memory_gb']), metadata__categories_mapping=dict(type='dict', aliases=['categories']), - metadata__use_categories_mapping=dict(type='bool', aliases=['use_categories_mapping'], default=False) - )) + metadata__use_categories_mapping=dict( + type='bool', aliases=['use_categories_mapping'], default=False) + )) module = BaseModule() if module.params.get('spec__resources__memory_size_mib'): diff --git a/nutanix/ncp/tests/integration/targets/nutanix_vms/tasks/main.yml b/nutanix/ncp/tests/integration/targets/nutanix_vms/tasks/main.yml index 1f1513ec1..7b7dedaf2 100644 --- a/nutanix/ncp/tests/integration/targets/nutanix_vms/tasks/main.yml +++ b/nutanix/ncp/tests/integration/targets/nutanix_vms/tasks/main.yml @@ -1,4 +1,3 @@ - block: - - import_tasks: 'create.yml' - - import_tasks: 'delete.yml' - + - import_tasks: "create.yml" + - import_tasks: "delete.yml" diff --git a/nutanix/ncp/tests/unit/plugins/module_utils/test_entity.py b/nutanix/ncp/tests/unit/plugins/module_utils/test_entity.py index e387dbdef..4870451ce 100644 --- a/nutanix/ncp/tests/unit/plugins/module_utils/test_entity.py +++ b/nutanix/ncp/tests/unit/plugins/module_utils/test_entity.py @@ -1,3 +1,5 @@ +from __future__ import (absolute_import, division, print_function) +__metaclass__ = type from ansible.module_utils import basic from ansible_collections.nutanix.ncp.tests.unit.plugins.modules.utils import AnsibleExitJson, AnsibleFailJson, ModuleTestCase, set_module_args from ansible.module_utils.six.moves.urllib.parse import urlparse